Return X coordinate of a fft plot where 80% of the total area under the curve is achieved

1 Ansicht (letzte 30 Tage)
How to get X coordinate of a fft plot which covers 80% of the area of the total area covered by the whole plot.

Akzeptierte Antwort

Star Strider
Star Strider am 13 Apr. 2019
Try this:
t = linspace(0, 10, 150); % Create Data
y = sin(2*pi*t*3) .* cos(2*pi*t*2) + 0.01*randn(size(t)); % Create Data
L = numel(t);
Ts = mean(diff(t)); % Sampling Interval
Fs = 1/Ts; % Sampling Frequency
Fn = Fs/2; % Nyquist Frequency
FTy = fft(y)/L; % Fourier Transform
Fv = linspace(0, 1, fix(L/2)+1)*Fn; % Frequency Vector
Iv = 1:numel(Fv); % Index Vector
areaFrac = 0.8; % Desired Fraction Of Total Area
areaTot = trapz(Fv, abs(FTy(Iv))); % Total Area
areaCum = cumtrapz(Fv, abs(FTy(Iv))); % Cumulative Integral
Fv80 = interp1(areaCum, Fv, areaTot*areaFrac, 'spline') % Frequency Corresponding To 80% Total Area
FTy80 = interp1(Fv, abs(FTy(Iv))*2, Fv80, 'spline') % Value of Fourier Transform At ‘Fv80’
figure
plot(Fv, abs(FTy(Iv))*2, '-b')
hold on
plot(Fv, areaCum, '-k')
plot([0 max(Fv)], [1 1]*areaTot, '--k')
plot([0 max(Fv)], [1 1]*areaFrac*areaTot, ':k')
plot([1 1]*Fv80, [0 FTy80], '-r')
plot(Fv80, areaTot*areaFrac, 'r+')
hold off
legend('Fourier Transform', 'Cumulative Area', 'Maximum Area', '80% Maximum Area', 'Fourier Transform at 80% Maximum Area')
Experiment to get the result you want.
  5 Kommentare
Samyak Mohapatra
Samyak Mohapatra am 13 Apr. 2019
I think I am getting the right answer with my latest tweak. Thank you.
Star Strider
Star Strider am 13 Apr. 2019
As always, my pleasure.
I have no idea what you tweaked or what your signals are. The code appears to be correct.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Produkte


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by