matlab area calculation under fitted curve

조회 수: 3 (최근 30일)
Ancalagon8
Ancalagon8 2020년 9월 16일
댓글: Rik 2020년 10월 15일
I have a signal and want to calculate the area under the produced fft with step of 5Hz. So far i created an fft, normalized and smoothed it. The problem is that i want to make a loop with decimal step, because the signals' length is 1070 so 1 Hz is 1070/45= 23,77 and step (5Hz) = 118.58
So far my code is:
[x1Data, y1Data] = prepareCurveData(f1, p1norm);
% Set up fittype and options.
ft1 = fittype( 'smoothingspline');
opts1 = fitoptions( 'Method', 'SmoothingSpline');
opts1.SmoothingParam = 0.95;
% Fit model to data.
[fitresult1, gof1] = fit(x1Data, y1Data, ft1, opts1);
% Plot fit with data.
figure
h1 = plot(fitresult1, x1Data, y1Data,'b' );
h = get(gca, 'Children');
Any ideas how to proceed with the loop?
  댓글 수: 1
Rik
Rik 2020년 9월 25일
Comment posted as answer (and erroneously flagged by the spam filter) by Sirius8:
Noone? Come on guys a little help!

댓글을 달려면 로그인하십시오.

채택된 답변

Rik
Rik 2020년 9월 25일
편집: Rik 2020년 9월 28일
You can use trapz for a numerical approximation of an integration. By selecting your bounds you can use this to calculate the area under a curve.
Edit:
Using the code you posted and adding the loop I'm getting something similar to what you describe. If you don't want the legend entries, you should use explicit handles in your call to legend.
S=load('test1.mat');
[x1,y1,y11]=deal(S.x1,S.y1,S.y11);
figure(1),clf(1)
subplot(2,1,1)
plot(x1,y1)
grid on
hold all
plot(x1,y11,'m')
grid on
hold all
legend ('normalized fft','smoothed')
subplot(2,1,2)
plot(x1,y11,'m')
grid on
hold all
legend ('smoothed')
cmap=colormap('prism');n_col=0;
freq_range=5;
for x_start=0:freq_range:max(x1)
x_end=x_start+freq_range;
L=x1>=x_start & x1<=x_end;
if sum(L)<=1,continue,end%skip if there is only 1 point
X=[x_start x1(L) x_end];
Y=[0 y11(L) 0];
A=trapz(x1(L),y11(L));
n_col=n_col+1;C=cmap(n_col,:);
patch(X,Y,C,'DisplayName',sprintf('A(%d-%dHz)=%.2f',x_start,x_end,A))
end
  댓글 수: 38
Ancalagon8
Ancalagon8 2020년 10월 15일
Thank you again!
Why you think that the legend is not correct?
Rik
Rik 2020년 10월 15일
Because A is a vector once you put it in sprintf to create the legend entry. You should also change the frequency part from '%d' to something like '%.1f'.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by