How to integrate area under peak?

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

Clarify: do you want to calculate the 4 colors area (individually)?
Then why not just use my solution below for all 4 regions???
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.
  1. 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?
  2. 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.

Sign in to comment.

 Accepted Answer

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

Thank you for your response. It worked.
As always, my pleasure.
I want to ask one more thing, how did you find start and end boundary of a region? findchangepts function gives a bit shifted start and end points as you can see in attached figure 1. Also sometimes it miss very sharp changes(refer figure 2). Could you please provide a solution for that also. I have many signals like this.
I just ‘thresholded’ your signal, since the transitions in image you posted seemed to correspond to the amplitude of your signal crossing about 0.28.
The relevant lines of my code that find those are:
idx = D >= 0.28;
chg = [find(diff(idx ~= 0)); numel(D)];
chg(1) = 1;
The first of these three lines creates a ‘logical vector’ that is 1 above that value and 0 below it (the ‘idx’ vector). The second line finds the transitions between 0 and 1 and converts them into indices (the ‘chg’ vector). The third line does a minor correction, since the beginning of the first segment begins at the beginning of your signal, rather than at the first crossing of 0.28.
okay. I get it now. Thanks a lot for helping me. :)
My pleasure!

Sign in to comment.

More Answers (1)

Image Analyst
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

Isn't it like using trapz?
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.
Thank you for more clear view. My data is nanopore electrical signals. So, for me trapz is more useful.

Sign in to comment.

Asked:

on 3 Sep 2017

Commented:

ASC
on 18 Nov 2021

Community Treasure Hunt

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

Start Hunting!