0
$\begingroup$

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');
$\endgroup$
  • $\begingroup$ 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 $\endgroup$ – Rhei Jan 26 '15 at 6:48
  • $\begingroup$ it.mathworks.com/help/matlab/ref/fft.html $\endgroup$ – Rhei Jan 26 '15 at 6:50
  • $\begingroup$ I just want to plot the data within sampling frequency,fs=512Hz. How to do the coding in Matlab? $\endgroup$ – nur ima Jan 27 '15 at 1:31
  • $\begingroup$ What do you mean when you say "data within sampling frequency..."? $\endgroup$ – Rhei Jan 27 '15 at 5:51
  • $\begingroup$ the plotted value is in bandwidth range. $\endgroup$ – nur ima Jan 28 '15 at 6:45
2
$\begingroup$

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’)
$\endgroup$
  • $\begingroup$ Y = fft(y,NFFT)/N; % the division by N is to scale the amplitude, what is y?how to get y value? $\endgroup$ – nur ima Jan 27 '15 at 1:25
  • $\begingroup$ Sorry, $y$ is your $signalx$ $\endgroup$ – Rhei Jan 27 '15 at 5:40
0
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ I just want to plot the data within sampling frequency,fs=512Hz. How to do the coding in Matlab? $\endgroup$ – nur ima Jan 27 '15 at 1:32
  • $\begingroup$ see fft example Note: you can ignore the sum of sinusoidal part and noise part. Those are just for clearly understand in freq spectrum. $\endgroup$ – pakornosky Jan 27 '15 at 5:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.