# How to use fft in Matlab for a white noise?

Hello. I must say, in Matlab I am a newbie, please do not swear.
I have an audio file recorded in it is white noise with a frequency of from 100-10000 Hz,
Required to construct the spectrum of this audio file
googled what it takes f-tsiya fft in Matlab (https://www.mathworks.com/help/matlab/ref/fft.html), tried to build something
L = 1000; % Length of signal
Y = fft(y);
P2 = abs(Y/L);
P1 = P2(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1);
f = Fs*(0:(L/2))/L;
plot(f,P1)
title('Single-Sided Amplitude Spectrum of X(t)')
xlabel('f (Hz)')
ylabel('|P1(f)|')

It is not clear that here P1?
And maybe there is some ready code for my problem?
March 19th 20 at 09:21
March 19th 20 at 09:23
Solution
File 1000 counts (value of L)?
If not, then L is equal to the dimension of y after reading from a file.

I do not understand why this is done
`P1(2:end-1) = 2*P1(2:end-1);`
Don't need it, IMHO.

P1 is the first half of the modules of the fft result, what is usually meant by "spectrum" - frequency response.
P1(2:end-1) = 2*P1(2:end-1); - removed.
And the picture on the chart similar to the spectrum.
and can help with the transition from P(f) to dB? (bottom graph from the application signal analyzer)
- Jaida45 commented on March 19th 20 at 09:26
@Jaida45, and the sampling frequency (Fs) what is? - aliza_Kreiger commented on March 19th 20 at 09:29
@aliza_Kreiger, 44100 - Jaida45 commented on March 19th 20 at 09:32
Do the fft on the first 44100 samples (i.e. 1 second) and L = Fs. - aliza_Kreiger commented on March 19th 20 at 09:35
In DB go simply: P1 = 20 * log10(P1)
Normalized version: P1 = 20 * log10(P1/ P1(1)) - aliza_Kreiger commented on March 19th 20 at 09:38
The maximum frequency after fft() is equal to the Nyquist frequency: Fs/2, i.e. 44100/2=22050 - that's you and 2x10^4 Hz
Try a normalized version. - aliza_Kreiger commented on March 19th 20 at 09:41
@aliza_Kreiger, I apologize for my stupidity. But can tell I took the option without rationing, moved to dB. values compared with values in signal analyzer. They are kind of different, although very similar, there are no sharp changes

it features signal analyzer? how would smooths the spectrum - Jaida45 commented on March 19th 20 at 09:44
May smooths.
You can also iron - accumulate absы 1 second. After accumulating ration for a number of savings. - aliza_Kreiger commented on March 19th 20 at 09:47
@aliza_Kreiger, can you tell me how it looks in your code? don't understand - Jaida45 commented on March 19th 20 at 09:50
Something like this:
``````N = 10; % number of accumulation
L = Fs;
P1 = zeros(L/2);
for i = 0:N-1
Y = fft(y(L*i + 1 : L*(i+1)));
P1 = P1 + abs(Y(1:L/2)/L);
end
P1 = P1 / N;``````

code is not checked.
In P1, the accumulated modules.
Add your ending - aliza_Kreiger commented on March 19th 20 at 09:53
@aliza_Kreiger, all is well. only here can you tell me, when L is equal to Fs I lost the signal in the interval from 100-5000hz approximately. what is the reason? the picture above is. - Jaida45 commented on March 19th 20 at 09:56
@Jaida45, There is present a noise signal that can be seen on the chart. Seems to work the high pass filter.
In which version have you attended signal in the lower frequencies? - aliza_Kreiger commented on March 19th 20 at 09:59
in the first comments, I L "Reports 2646000, I adjusted" did, and I had my signal. and then after you wrote: "Do fft on the first 44100 samples (i.e. 1 second) and L = Fs.", for the second screenshot. all screenshots in this thread. before and after - Jaida45 commented on March 19th 20 at 10:02
@Jaida45, There are a few points:
1. interpretation of the result of FFT depends on the sampling frequency (Fs) and FFT size (L). The easiest way to interpret the result when L = Fs - in this case, each sample of the FFT result is adequate chatot in Hertz. When L != Fs, you need the index of the frequency multiplied by a factor Fs/L to obtain the frequency in Hz. So your initial version of the chart you brought in some fractions of a Hertz (44100/2646000~0.017).
2.Probably in your file at some point present lower frequency. With the accumulation of I set N=10, i.e., the accumulation for 10 seconds. The graph is drawn only for the first 10 seconds of the signal.
You have a file 60 seconds of the recording. Try to do 60 seconds of accumulation (N=60).
Or you can make the same plot (with 10 seconds of savings), but the signal processing is not, for example, from the middle. - aliza_Kreiger commented on March 19th 20 at 10:05
@Jaida45, yeah, sory, you are there were taken by using signal analyzer. Who knows what he does inside the signal. I'm not familiar with this tool. - aliza_Kreiger commented on March 19th 20 at 10:08
@aliza_Kreiger,

here, the first is when I have L = number of array elements, and second, when L = Fs. The audio file I just recorded with a frequency of 100-10 000gts. The first figure is true, but the second disappears. - Jaida45 commented on March 19th 20 at 10:11
@Jaida45, and Let's just code that you received both schedule. - aliza_Kreiger commented on March 19th 20 at 10:14
clear, clc, close;
L = 44100; % Length of signal
Y = fft(y);
P2 = abs(Y/L);
P1 = P2(1:L/2+1);
P4 = 20*log10(P1/P1(1));
f = Fs*(0:(L/2))/L;
plot(f,P4)
title(")
xlabel('f (Hz)')
ylabel('P')

the cycle that you suggested, I did not yet to do. maybe I have to code error. L = 2646000 or or 44100 - Jaida45 commented on March 19th 20 at 10:17
@Jaida45, Well here it is clear what the problem is.
See:
1.you do fft on entire array in 2646000 elements at a sampling rate of 44100.
2.in the result of FFT of the receive array is not in Hertz, but in fractions of a Hertz (44100/2646000~0.017), i.e. for example 1 element of the array has a frequency of 1*0.017 Hz, the second element 2*0.017 Hz (actually from the index Alemana necessary to subtract 1 and then multiply by a factor). Element 44100 is the frequency of 750 Hz.
3.Of the total result of the FFT you take only 22050 samples, and it is 22050*0,017=375 Hz.

Ie, your first graph from 0 to 375 Hz, and not to 22 KHz, as you mistakenly think. So your interpretation of this graph is not true. Additionally misleads the scale x value, which is taken from here: f = Fs*(0:(L/2))/L; by the Way, this formula just scored the same factor: Fs/L and multiplied by the element's index.

If you do FFT on entire array, L have to assign a value 2646000, then it will be something more meaningful.

On account of the theory, about the ratio Fs/L can be read from Richard Lyons, "Digital signal processing" - section 3.5. Well, there is about everything else, all sensible owner. - aliza_Kreiger commented on March 19th 20 at 10:20
@aliza_Kreiger, it turns out that my real signal, it is still the first graph? where L = 2646000 = number of elements in the array. When is f = Fs*(0:(L/2))/L; correct? - Jaida45 commented on March 19th 20 at 10:23
@Jaida45Correct. But usually do not do FFT on a continuous sample time because a set of frequency components may change over time, i.e. picture on the overall FFT will not meet a more short sample of the signal. So I still recommend moving on to FFT size Fs (L=Fs).
Combed the option of accumulation:
``````clear, clc, close;
y = y(:, 1);
L = Fs; % Length of signal
N = 10; % number of accumulation
P1 = zeros([L/2, 1]);
for i = 0:N-1
Y = fft(y(L*i + 1 : L*(i+1)));
P1 = P1 + abs(Y(1:L/2));
end
P1 = P1 / (N * L);
P4 = 20*log10(P1/P1(1));
f = (0:(L/2)-1)* (Fs / L);
plot(f,P4)
title(")
xlabel('f (Hz)')
ylabel('P')``````
- aliza_Kreiger commented on March 19th 20 at 10:26
@aliza_Kreiger, thank you very much.
And last question, can I use y = decimate(x,r) and than it threatens? the reason it wants to use is not the grip of the memory MK for the source array y to the MC to check the library fft - Jaida45 commented on March 19th 20 at 10:29
You can also use resample() for this purpose.
On account of the danger depends on what you will do with this signal.
Note that when reducing the sampling rate, cut the upper frequencies.
I.e. in the signal with a sampling frequency of 44100 Hz, contains frequencies up to 22050 Hz. The slaughter of the signal, for example, RR = 10 KHz - left frequency up to 5 KHz.

Library FFT in MK on si? Usually on the lower level limit FFT size equal to a power of two. It makes sense to make the resulting black hole is equal to a power of two. If the array size is not equal to a power of two, then it is necessary to do standard DFT (not FFT), and it is much longer. In Matlab it's all hidden from the user, but on the lower level and need to resolve by hand.
Although BH = power of two this is not a mandatory requirement. To a size equal to a power of two can bring such as finishing off the missing value zeros, or taking the number of signal samples is less than BH, but equally two. In both cases you need to remember about the ratio (Fs/L) in the derivation of frequencies. Fs - sampling frequency, L is the FFT size. - aliza_Kreiger commented on March 19th 20 at 10:32
@aliza_Kreiger, c resample() is not very clear how to use it. but in your code added decimate.
spoiler
clear, clc, close;
y = decimate(y,10);
y = y(:, 1);
L = 4410; % Length of signal
N = 10; % number of accumulation
P1 = zeros([L/2, 1]);
for i = 0:N-1
Y = fft(y(L*i + 1 : L*(i+1)));
P1 = P1 + abs(Y(1:L/2));
end
P1 = P1 / (N * L);
P4 = 20*log10(P1/P1(1));
f = (0:(L/2)-1)* (L / L);
plot(f,P4)
title(")
xlabel('f (Hz)')
ylabel('P')

manually asked L, because the frequency of Fs has decreased 10 times, and I changed the formula Fs/L. T. in memory he 4410, and 44100.
got this picture
if understand this correctly, here signal just stabbed 10 times.

Library FFT in MK on si Yes. planned if no degree 2-Ki to finish off the zeros. - Jaida45 commented on March 19th 20 at 10:35
@Jaida45, Like the truth. - aliza_Kreiger commented on March 19th 20 at 10:38
@aliza_Kreiger, and there are also options to reduce the number of reports, but to keep the signal frequency which it is? - Jaida45 commented on March 19th 20 at 10:41
@Jaida45That would keep the signal up to 10 KHz you need a minimum sampling rate of 20 KHz (the Nyquist frequency). Therefore, to preserve your signal, you can reduce CHD only in two. - aliza_Kreiger commented on March 19th 20 at 10:44

Find more questions by tags MATLAB