How to integrate area under peak?
22 views (last 30 days)
Show older comments
For my events, I have data points for y-axis. As a sample, I have attached a text file containing datapoints for one of my events. As I have shown in figure, I want to integrate area of each region displayed in various colors. I can find start and end boundary for a region using findchangepts function. However, it provides start and end points few points away from the exact location. Could you please help me with this? Also, if someone can suggest any other function to find start and end boundary, would be a great help.
I have tried, trapz(y); but didn't work the way I want.
4 Comments
ASC
on 18 Nov 2021
I think this solution is very close to what I am trying to achieve. Maybe I am way off.
I have a set of data (D, see attached) The data is a repeating set of peaks (see plot.png). There are 40 of each peak. I want to integrate each peak and evaluate the variation. In actuality I am only interested in the first two (the two largest), but they are different enough in size that it should be easy to ignore the third.
Using this code provided by Star Strider I set the threshold for the index to idx = D >= 3. This should exclude the third peak.
When I look at the resulting areas (see hist.png):
edges = 0:10:1500;
H = histogram(segment_area, edges);
sum(H.Values)
A few things jump out.
- In my data, 40 small peaks + 40 large peaks is 80 peaks total. The code finds 158 peaks. Where are the extra peaks coming from?
- Looking at peak.png the area of the small peak (30 wide x 3 high) is about 30. The area of the large peak (150 wide x 3 high) is about 450. This explains (?) two of the clusters on the histogram. Whys does the cluster near 30 on the histogram have 79 counts (not 40)? The cluster on the histogram near 350 has 40 counts. What is the additional cluster near 1100?
Thanks for your help.
Accepted Answer
Star Strider
on 3 Sep 2017
Try this:
D = load('data for peak area.txt');
idx = D >= 0.28;
chg = [find(diff(idx ~= 0)); numel(D)];
chg(1) = 1;
for k1 = 1:numel(chg)-1
segment_area(k1) = trapz(chg(k1:k1+1), D(chg(k1:k1+1)));
Q1(:,k1) = chg([k1 k1+1]);
Q2(:,k1) = D(chg([k1 k1+1]));
end
figure(1)
plot(D)
hold on
plot(Q1, Q2, 'pg')
hold off
grid
area_text = regexp(sprintf('%.2f\n', segment_area), '\n', 'split');
text(mean(Q1), ones(size(segment_area))*0.12, area_text(1:end-1), 'HorizontalAlignment','center')
6 Comments
More Answers (1)
Image Analyst
on 3 Sep 2017
Edited: Image Analyst
on 3 Sep 2017
Why not just sum?
integratedSignal = sum(signal(index1:index2));
Note that you can't always say that summing or trapezoidal integration is always the best way. Which is best depends on your situation and interpretation of your data.
3 Comments
Image Analyst
on 3 Sep 2017
Similar but not exactly.
Summing is like getting the area of bars in a bar chart, while trapz is like drawing straight lines from the top center of each bar to the adjacent bars and getting the area underneath these slanted lines.
If you're doing something like integrating counts, like the counts represent the number of people/customers serviced at a fast food restaurant as a function of hour of the day, then summing would be more appropriate.
If your data meant something else - can't think of a real works situation so just assume it's an analtyical/mathematical formula that you've digitized, like a quadratic or something - then trapz might be more appropriate.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!