Calculate area under graph between two points
1 次查看(过去 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!