How can I find the peak location from the FFT output?
21 views (last 30 days)
Show older comments
Hi,
Please, how can I find the peak location from the FFT output? I have attached my m file
0 Comments
Accepted Answer
Star Strider
on 25 Oct 2023
Missing data file.
However this is straightforwazrd —
Fs = 1000;
L = 10;
t = linspace(0, L*Fs, L*Fs+1)/Fs;
% [1 2 3]'*t
% return
s = sum(sin([1 100:100:400]'*t*2*pi),1).';
t = t(:);
figure
plot(t, s)
grid
Fn = Fs/2;
L = numel(t);
NFFT = 2^nextpow2(L);
FTs = fft(s.*hann(L), NFFT)/L;
Fv = linspace(0, 1, NFFT/2+1)*Fn;
Iv = 1:numel(Fv);
[pks,locs] = findpeaks(abs(FTs(Iv))*2, 'MinPeakProminence',0.25)
Results = table(Fv(locs).', pks, 'VariableNames',{'Frequency','Peak Magnitude'})
figure
plot(Fv, abs(FTs(Iv))*2)
grid
ylim([min(ylim) 1.5*max(ylim)])
text(Results{:,1}, Results{:,2}, compose('\\leftarrow Freq = %.1f\n Mag = %.3f',Results{:,[1 2]}), 'Rotation',45)
That approach should also work for your data.
.
8 Comments
Star Strider
on 26 Oct 2023
Edited: Star Strider
on 26 Oct 2023
As always, my pleasure!
Sure!
It looks like you allready solved that.
Here, I changed ‘FFT1’ to ‘FFT2’ to return two-sided Fourier transforms —
F = openfig('plot2.fig');
T1 = readtable('Fourier_data101.xlsx')
t1 = T1{:,1};
s1 = T1{:,2};
T2 = readtable('FFT_spatial.xlsx')
t2 = T2{:,1};
s2 = T2{:,2};
Fs2 = 1/mean(diff(t2))
[s2r,t2r] = resample(s2, t2, Fs2); % Resample To Constant Sampling Frequency
figure
plot(t2, s2, 'DisplayName','Original Signal')
hold on
plot(t2r, s2r, 'DisplayNAme','Resampled Signal')
hold off
grid
xlabel('Time')
ylabel('Amplitude')
title('FFT\_spatial — Time Domain Plot')
legend('Location','best')
[FTs1,Fv1] = FFT2(s1,t1); % Fourier_data101
[pks1,locs1] = findpeaks(abs(FTs1)*2, 'MinPeakProminence',0.005)
Results1 = table(Fv1(locs1).', pks1, 'VariableNames',{'Frequency','Peak Magnitude'})
figure
plot(Fv1, abs(FTs1)*2)
grid
ylim([min(ylim) 1.5*max(ylim)])
xlabel('Frequency')
ylabel('Magnitude')
title('Fourier\_data101')
xlim(xlim/3)
text(Results1{:,1}, Results1{:,2}, compose('\\leftarrow Freq = %.2f\n Mag = %.3f',Results1{:,[1 2]}), 'Rotation',45)
[FTs2,Fv2] = FFT2(s2r,t2r); % FFT_spatial
[pks2,locs2] = findpeaks(abs(FTs2)*2, 'MinPeakProminence',0.01)
Results2 = table(Fv2(locs2).', pks2, 'VariableNames',{'Frequency','Peak Magnitude'})
figure
plot(Fv2, abs(FTs2)*2)
grid
ylim([min(ylim) 1.5*max(ylim)])
xlabel('Frequency')
ylabel('Magnitude')
title('FFT\_spatial')
xlim(xlim/12)
text(Results2{:,1}, Results2{:,2}, compose('\\leftarrow Freq = %.2f\n Mag = %.3f',Results2{:,[1 2]}), 'Rotation',45)
function [FTs2,Fv] = FFT2(s,t)
s = s(:);
t = t(:);
L = numel(t);
Fs = 1/mean(diff(t));
Fn = Fs/2;
NFFT = 2^nextpow2(L);
FTs = fftshift(fft((s - mean(s)).*hann(L), NFFT)/sum(hann(L)));
Fv = linspace(-Fs/2, Fs/2-Fs/length(FTs), length(FTs));
Iv = 1:numel(Fv);
FTs2 = FTs(Iv);
end
Make approopriate changes to the rest of the code to get the result you want. (I do not recoognise the spectrum in ‘plot2.fig’ so I guess it was not a signal already uploaded here.)
EDIT — (26 Oct 2023 at 11:12)
Resmapling is necessary because the fft function requires evenly-sampled (or reasonably close to evenly-sampled) data. (The nufft function does not, however I prefer to use fft with resampled data, since resampling also permits other signal processing techniques such as filtering, that are otherwise impossible.) Looking at ‘t2’, the mean of the differences (‘diff(t)’) is 2.3364e-8 and the standard deviation of the differences is 1.6398e-8. It should be several orders-of-magnitude less than the mean. The sampling time differences range from 2.1069e-12 to 7.0182e-8. That range is simply too much for fft (or other signal processing functions, all of which require regular sampling) to deal with. Without resampling, the results would be inaccurate. Resampling to a common sampling frequency (chosen here as the inverse of the mean of the differences (using the median would also work), makes the subsequent analyses reliable. The time-domain plots would look substantially the same when plotted (added above).
.
More Answers (0)
See Also
Categories
Find more on Spectral Measurements 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!