# time domain data into FFT using Matlab

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');

• 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 6:48
• it.mathworks.com/help/matlab/ref/fft.html – Rhei Jan 26 '15 at 6: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 1:31
• What do you mean when you say "data within sampling frequency..."? – Rhei Jan 27 '15 at 5:51
• the plotted value is in bandwidth range. – nur ima Jan 28 '15 at 6:45

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

• Y = fft(y,NFFT)/N; % the division by N is to scale the amplitude, what is y?how to get y value? – nur ima Jan 27 '15 at 1:25
• Sorry, $y$ is your $signalx$ – Rhei Jan 27 '15 at 5:40

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.

• 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 1:32
• see fft example Note: you can ignore the sum of sinusoidal part and noise part. Those are just for clearly understand in freq spectrum. – pakornosky Jan 27 '15 at 5:14