Get frequencies out of data with an FFT

53 views (last 30 days)
Greetings,
I have a 2800 row vector of data I am trying to find the fundamental frequencies of using an FFT. I found some code and have modified it to use my own data. To check I am adding a 60Hz signal to it and have tried to find it in the frequency plot but have not succeded at all. The 2800 row vector is mostly noise and could be created by using randn(). When I run this the 60Hz signal is not evident at all and I cannot figure out why. I am hoping someone here can point out any faults on my part. Here is my code:
t = 0:.001:2.799;
x = 2+sin(2*pi*60*t);
X = x';
A = zeros(2800,1);
for ii = 1:2800
A(ii) = (X(ii) + M(ii))';
end
Y = fft(A,2800);
Pyy = Y.*conj(Y)/2800;
f = 1000/2800*(0:1399);
plot(f,Pyy(1:1400))
title('Power spectral density')
xlabel('Frequency (Hz)')
the 1400 is just a number I chose. The example used a number roughly half the sample size. M is the data vector (2800,1) Any suggestions?
Thanks as always

Accepted Answer

Wayne King
Wayne King on 6 Feb 2012
t = 0:.001:1-0.001;
Fs = 1e3;
x = 2+sin(2*pi*60*t);
x = detrend(x,0);
xdft = fft(x);
freq = 0:Fs/length(x):Fs/2;
xdft = xdft(1:length(x)/2+1);
plot(freq,abs(xdft));
[~,I] = max(abs(xdft));
fprintf('Maximum occurs at %d Hz.\n',freq(I));
If you have the Signal Processing Toolbox, you can use spectrum.periodogram
psdest = psd(spectrum.periodogram,x,'Fs',Fs,'NFFT',length(x));
[~,I] = max(psdest.Data);
fprintf('Maximum occurs at %d Hz.\n',psdest.Frequencies(I));
If there are multiple peaks, you can use findpeaks() in SPT, or just look at the plot
plot(psdest)

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!