How to plot FFT(Fast fourier transform) in MATLAB of given data?

432 views (last 30 days)
Hello Everybody,
We are having a text file containing 2048 x and y values. we just want to plot fft of this data so we followed the following code. Please check!!!! is it right? Because we are not getting our desired output. And suggest me how to plot fft of this data.
code --
I = load('data1.asc');
for i = 1:2048
y = I(:,2);
end
plot(y)
Fs = 40000;
T = 1/Fs;
L = 2000;
NFFT = 2^nextpow2(L);
Y = abs(fft(y,NFFT))/L;
f = Fs/2*linspace(0,1,NFFT/2+1);
figure, plot(f,2*abs(Y(1:NFFT/2+1)))
axis([0 40000 0 40])

Accepted Answer

Wayne King
Wayne King on 19 Jul 2013
Edited: Wayne King on 19 Jul 2013
Your axis command is not correct. The frequency axis should only run to 20000 because that is the Nyquist frequency and I'm not sure why you picked the Y-limits on your axis command -- those may not be appropriate either.
I'll just create a signal using your sampling frequency and data length and show you
Fs = 40000;
t = 0:1/Fs:(2e3*1/Fs)-1/Fs;
y = cos(2*pi*5000*t)+randn(size(t));
T = 1/Fs;
L = 2000;
NFFT = 2^nextpow2(L);
Y = abs(fft(y,NFFT))/L;
f = Fs/2*linspace(0,1,NFFT/2+1);
plot(f,2*abs(Y(1:NFFT/2+1)))
Also, I understand you've taken this example from the documentation, but there may be no need at all to zero pad and/or do some of the other operations you have included above. But the code you have should give you an idea of the distribution of energy by frequency.
  1 Comment
Samuel Gray
Samuel Gray on 18 Dec 2020
Edited: Samuel Gray on 19 Dec 2020
Technically the Nyquist frequency is half the sampling frequency but I find that it works better to use 8x Fs, in terms of graphing time & frequency data. At or very near the Nyquist frequency a plot of the time data will just show the average time value of the waveform for any frequency components near Nyquist. To really see it in the plot you have to oversample by a factor of at least 4 or even 8 or filter the time-domain data.
And of course it may not necessary to use zero-padding.
It's used both for improved resoluton and reduced FFT time becasue the FFT algorithm is faster if the sample length is a power of 2.
Try the "noisy signal" example that is in the fft reference help (live script) as an example.
The FFT frequency (x in the plot) should be half the length of the time signal.and the returned FFT should be cut in half, when plotting f against FFT(y), due to the Nyquist criterion. Both the time signal (by zero-padding) and the FFT window size should be a power of 2 for maximum performance.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!