0

I do like to generate the FFT from time domain history data (experimental lab) using MATLAB. I am not so sure if my coding is right? Can someone verify the coding.

My code is as follow:

 clc;    
 signalx = Ch1;   
 N=6678;   
 t=Time;

 %Plot FFT Vehicle signal  
 zx = fft(signalx);  
 plot(abs(zx));  
 title('FFT Vehicle');  
 xlabel('Frequency [Hertz]')  
 ylabel('Magnitude');
jojeck
  • 11,107
  • 6
  • 38
  • 74
nur ima
  • 1
  • 2
  • To plot the fft result you should first create the vector of frequencies and then plot(frequency,abs(fft_out)) if you want to plot the full spectrum. Have a look at the matlab documentation, there is a nice example there – Rhei Jan 26 '15 at 06:48
  • http://it.mathworks.com/help/matlab/ref/fft.html – Rhei Jan 26 '15 at 06:50
  • I just want to plot the data within sampling frequency,fs=512Hz. How to do the coding in Matlab? – nur ima Jan 27 '15 at 01:31
  • What do you mean when you say "data within sampling frequency..."? – Rhei Jan 27 '15 at 05:51
  • the plotted value is in bandwidth range. – nur ima Jan 28 '15 at 06:45
  • The plotted frequencies as I defined them in the answer represent the half spectrum, so just substitute $Fs$ with your value $512$ – Rhei Jan 28 '15 at 06:48
  • Ok. Noted.Its really helpful.Thank you so much. – nur ima Jan 30 '15 at 00:29
  • Did I answer your question? If my answer was helpful to you please accept it. Thank you. – Rhei Nov 04 '15 at 09:22

2 Answers2

1

Just to expand what I wrote in the comment. This is how you should work with the FFT in Matlab.

Compute the FFT of your time signal:

NFFT = 2^nextpow2(N);
Y = fft(y,NFFT)/N; % the division by N is to scale the amplitude

Then compute the sampling frequency: (EDIT skip this 2 lines of code if you already know the value of $Fs$)

dt = t(2)-t(1);
Fs = 1/dt;

This works if your data are sampled at constant sampling frequency. If it is not the case you need to resample them (it is very easy to do if you convert your data into timeseries and then use the resample function for timeseries. Look here for more details: What is an algorithm to re-sample from a variable rate to a fixed rate?)

Finally, plot the result:

f_plot = Fs/2*linspace(0,1,NFFT/2+1); % vector of frequencies for the plot
figure()
plot(f_plot,2*abs(Y(1:NFFT/2+1))) % multiplicated by 2 to recover the energy related
% to the negative frequencies since in this way only half spectrum is plotted
xlabel(’Frequency’)
Rhei
  • 413
  • 3
  • 10
  • 22
0

In matlab,

FFT_OUTPUT = fft(time-domain data);

That's all you need if you just want the output. (not mention the speed of your code)


As in your code, It will work! , but has many unnecessary points.

pakornosky
  • 80
  • 8