Calculate area under graph between two points
1 view (last 30 days)
Show older comments
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 Comments
Answers (3)
dpb
on 28 May 2019
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 Comments
Star Strider
on 28 May 2019
Edited: Star Strider
on 28 May 2019
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](https://www.mathworks.com/matlabcentral/answers/uploaded_files/221536/Calculate%20area%20under%20graph%20between%20two%20points%20-%202019%2005%2028.png)
0 Comments
darova
on 28 May 2019
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 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!