I am getting incorrect FFT values
10 views (last 30 days)
Show older comments
Hello,
The values (S) are oscillating around 50 but when i did FFT, I only got at 1.86. I don't know what is wrong with my code. Can someone help me?
%FFT
T=0.01; %cuz the time interval is 0,01. You can see in "t"
Fs=1/T;
t=readtable('t.txt');
S=table2array(readtable('S.txt'));
L=numel(S);
%plot(t,S)
grid on
grid minor
f=Fs*(0:(L/2))/L;
Y=fft(S-mean(S));
P2 = abs(Y/L);
P1 = P2(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1);
[pks,locs] = findpeaks(P1, 'MinPeakProminence' ,0.05)
plot(f,P1)
title('Amplitude Spectrums of Aggregat frequency')
xlabel('f (Hz)')
ylabel('Amplitude')
%xlim([0 150])
for k = 1:numel(locs)
xline(f(locs(k)), '-r' , sprintf( 'f = %.2f' ,f(locs(k))))
end
0 Comments
Accepted Answer
Star Strider
on 7 Feb 2023
The 1.86 value is correct. Look at the time domain plot —
%FFT
T=0.01; %cuz the time interval is 0,01. You can see in "t"
Fs=1/T;
t=readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1287695/t.txt');
S=table2array(readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1287700/S.txt'));
DT = datetime(t.Var1)
tv = second(DT) - second(DT(1));
T = mean(diff(tv))
L=numel(S)
[pks,locs] = findpeaks(S, 'MinPeakProminence',0.10);
ApproxFreq = 1/mean(diff(tv(locs)))
figure
plot(tv,S)
hold on
plot(tv(locs), pks, '^r')
hold off
grid on
grid minor
The frequency calculated from the time differences of the peaks is slightly different, however quite similar.
.
5 Comments
Star Strider
on 8 Feb 2023
You can always ask questions, and I will do my best to answer them.
The Fourier transform is based on matching the frequencies in the signal with frequencies calculated in the transform. The more closely they match, the higher the coefficient returned by the Fourier transform. (The integral transform involved in calculating the Fourier series is more complicated than that, however that will do for the purposes of this discussion.) It has nothing to do with zero-crossings, and everything to do with the oscillations in the signal, without regard to the signal base line.
The frequency approximation technique I used (deriving the frequency from the distance between the peaks, or the period of the signal), is a simple (and imprecise) calculation intended to demonstrate that the frequency estimated by the fft was essentially the same as the frequency calculated by taking the mean value of the peak periods, nothing else. It also has nothing to do with zero-crossings, although I could have done something similar to that by using the mean of the signal and then calculating the crossings of that value.
There are methods to estimate the frequency and amplitude of the various signal components that do not directly involve the Fourier transform, however they are more complicated. Most of them (that I am familiar with) involve doing a linear or non-linear regression of the signal, and estimating the parameters of that model. The Fourier transform is simpler, more efficient, and more precise.
More Answers (0)
See Also
Categories
Find more on Spectral Estimation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!