Calculate area under graph between two points

조회 수: 5 (최근 30일)
Suzan Wulms
Suzan Wulms 2019년 5월 28일
답변: darova 2019년 5월 28일
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?

답변 (3개)

dpb
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...

Star Strider
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 —
Calculate area under graph between two points - 2019 05 28.png

darova
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);

Community Treasure Hunt

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

Start Hunting!

Translated by