FFT of a simple harmonic data

5 views (last 30 days)
Rehan Rehan
Rehan Rehan on 4 Oct 2016
Commented: Star Strider on 5 Oct 2016
Hi,
I have the two data vectors, Time and Displacement. When I plot them as plot(Time,displ), I can see the harmonic behavior but when I do the FFT using following code, I get strange plot and do not get frequency peak. I shall be grateful for the help. Here Fs=180 and L=180, the two being the sampling frequency and length of signal respectively. Both have dimensions 180x1. I just used the following code available from Matlab help for simple sine function.
Thanks...
Fs=180;
L=180;
Y=fft(displ);
f = Fs*(0:(L/2))/L;
P2 = abs(Y/L);
P1 = P2(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1);
plot(f,P1)
title('Single-Sided Amplitude Spectrum of S(t)')
xlabel('f (Hz)')
ylabel('|P1(f)|')

Accepted Answer

Star Strider
Star Strider on 4 Oct 2016
It is difficult for me to follow your code. I always use (and recommend) the code from the R2015a documentation for fft (link). Especially note the code between the first (top) two plot figures.
  3 Comments
Star Strider
Star Strider on 4 Oct 2016
I can’t open the .zip files for some reason.
Save the contents as a .mat file and upload that instead.
Star Strider
Star Strider on 5 Oct 2016
In order to calculate a frequency vector for the plot, you need to use the time data as well:
fidi1 = fopen('Rehan Rehan time.dat','rt');
tc = textscan(fidi1, '%f');
t = tc{:};
fclose(fidi1);
fidi2 = fopen('Rehan Rehan displ.dat','rt');
displc = textscan(fidi2, '%f');
fclose(fidi2);
displ = displc{:};
L = length(displ); % Data Length
Ts = mean(diff(t)); % Sampling Time Interval
Fs = 1/Ts; % Sampling Frequency
Fn = Fs/2; % Nyquist Frequency
FTdisp = fft(displ)/L;
Fv = linspace(0, 1, fix(L/2)+1)*Fn;
Iv = 1:length(Fv);
figure(1)
semilogy(Fv, abs(FTdisp(Iv)))
grid
xlabel('Frequency')
ylabel('Amplitude')

Sign in to comment.

More Answers (1)

David Goodmanson
David Goodmanson on 4 Oct 2016
Edited: David Goodmanson on 4 Oct 2016
Your signal has a large offset of -2, with some small oscillations about that value. Therefore your plot has a large peak at zero frequency (the DC offset), along with some much smaller frequency components that are too hard to see. If you do a 'semilogy' plot of P1, or replace the zero frequency component of P1 by NaN and plot that result, or subtract the DC offset from your 'displ' data before the fft, frequency peaks will appear.
  1 Comment
Rehan Rehan
Rehan Rehan on 5 Oct 2016
Edited: Rehan Rehan on 5 Oct 2016
Thanks a lot. Now it does show the peak but the peak is at wrong Freq. The data suggests the peak around 0.3 more or less but the peak is at 6. Still need a little help.

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!