I have a time history data and I try to use this data to get a spectrum by using FFT function in MATLAB. could you please assist me to figure out how could I use this function correctly?
Doing an FFT of the entire data block is probably not what you want to do. It may be better to aply the FFT to smaller data segments and combine the results. Some things to consider: Should the segments be long or short and how much should they overlap (frequency resolution and variance vs bias trade-off)? What type of window function should be applied (main-lobe width vs side-lobe height)? What frequencies are you expecting in your data? The FFT is just a numerical tool for frequency estimation, or the construction of a periodogram. Applying the FFT in different ways will give you different results/estimates, each provides a different perspective. I think MATLAB has a periodogram function but it may be a part of the DSP toolbox, I'm not sure. Either way, I expect you will develop a deeper understanding of your problem, if you write your own function that applies the FFT in the most appropriate way.
so, with this code you can compute the signal power.
t = 0:1/fs:T-1/fs;
m = length(x); % Window length
n = pow2(nextpow2(m)); % Transform length
y = fft(x,n); % DFT
f = (0:n-1)*(fs/n); % Frequency range
power = y.*conj(y)/n; % Power of the DFT
The reason why the length m is transformed as pow2(nextpow2(m)) is pretty technical and it's related to the way how FFT works. All other details come from mathematical definitions. In attachment you can find a MATLAB function using above code for finding for instance the main frequency component (very useful in financial application!).
So, for instance, from this signal x
Fs = 1000; % Sampling frequency
T = 1/Fs; % Sample time
L = 1000; % Length of signal
t = (0:L-1)*T; % Time vector
% Sum of a 50 Hz sinusoid and a 120 Hz sinusoid
x = 0.7*sin(2*pi*50*t) + sin(2*pi*120*t);
y = x + 2*randn(size(t)); % Sinusoids plus noise
plot(Fs*t(1:50),y(1:50))
title('Signal Corrupted with Zero-Mean Random Noise')
xlabel('time (milliseconds)')
if you use such a function, you find out that the main component is ~120Hz, as it should be.
fm = findMainFrequencyComponent (Fs,T,x,1)
But if you modify in the above signal the mail component, i.e.
thanks a lot for your clarification... to be honest I am a new user of MATLAB program and I have another question ..... how could I get the power spectrum for the signal by matlab which tool could I use for that?
Sure, you can use the MATLAB function periodogram that works in a pretty similar way as above.
Here you have a link how to compute the power spectral density estimates using FFT and periodogram. As you can see the estimates are pretty similar (~e-14).
In the code example provided, the signal frequencies are an integer multiple of the frequency bin spacing (Fs/L). This is the ideal (best) case. So a single FFT will represent these frequencies perfectly (in the absence of noise). These signals are orthogonal over the selected discretized time window. If the signal frequencies fall mid-way between bins (i.e. worst case) e.g. at 50.5 Hz and 120.5 Hz, then the effects I was talking about come into play. The signal power will 'spill' into adjacent bins, due to the width of the main-lobe; and it will also 'bleed' into bins far away, due to the high side-lobe level of the rectangular window. Using a tapered window function will reduce the power in bins far away (i.e. lower the side-lobes) which will make weak signals easier to see, but it will also make strong closely spaced signals harder to resolve (due to a broader main-lobe).
In addition to Gino Tesei answer, I prefer to use pwelch function to estimate PSD of any signal since it's a modified periodogram approach averaged over small segments (called window). Also you can customize your choice of using the required window function (hamming, gaussian, etc.) the length of window (the no. of FFT points) and the no. of overlapped points. If you don't want to specify any of these values, you can use '[ ]' to use the default values. .