Calculate area under graph between two points
5 ビュー (過去 30 日間)
古いコメントを表示
Hello,
We want to calculate the area between two points where the graph is zero like in the graph below. So we want the area under every single peak separately. We think we have to use trapz for it, but how do we use this one right? Or do we need to use something else?
Also we want to know which area is the biggest. Is it possible to get this value out of matlab?
0 件のコメント
回答 (3 件)
dpb
2019 年 5 月 28 日
Start with
doc findpeaks
the optional output peak widths function with proper WidthReference level should give you all the info you need.
Does require Signal Processing Toolbox...
0 件のコメント
Star Strider
2019 年 5 月 28 日
編集済み: Star Strider
2019 年 5 月 28 日
Try this:
x = linspace(0, 5*pi); % Create Data
y = sin(x); % Create Data
y = y .* (y>0); % Create Data
zci = @(v) find(v(:).*circshift(v(:), [-1 0]) <= 0); % Returns Approximate Zero-Crossing Indices Of Argument Vector
idx1 = zci(y); % Indices: y=0
idx2 = find(diff([0; idx1])>1); % Find Discontinuities
yint = cumtrapz(x, y); % Integrate
yints = diff([0 yint(idx1(idx2))]); % Peak Areas
[pkv,pki] = maxk(y, numel(yints)); % Used In ‘text’ Call
figure
plot(x, y)
text(x(pki), pkv/2, sprintfc('Area = %.2f', yints), 'HorizontalAlignment','center','VerticalAlignment','middle', 'Rotation',90)
EDIT —
Added plot figure —
0 件のコメント
darova
2019 年 5 月 28 日
Simple solution:
k = 1;
thresh = 0;
s = zeros(1,100); % how many peaks?
for i = 1:length(y)-1
% if a new peaks starts:
if (y(i) <= thresh) && (y(i+1) > thresh)
k = k + 1;
end
h = x(i+1) - x(i); % width of a current rectangle
y1 = (y(i) + y(i+1))/2; % average height
s(k) = s(k) + h*y1;
end
s = s(1:k);
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!