MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

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

Asked by Payal Arora on 19 Jul 2013

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 --

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])

## Products

No products are associated with this question.

Answer by Wayne King on 19 Jul 2013
Edited by 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.