# How to integrate area under peak?

50 views (last 30 days)
ishita agrawal on 3 Sep 2017
Commented: ASC on 18 Nov 2021
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.
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.
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?

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')
Star Strider on 4 Sep 2017
My pleasure!

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.
ishita agrawal on 3 Sep 2017
Thank you for more clear view. My data is nanopore electrical signals. So, for me trapz is more useful.