How to integrate area under peak?

42 次查看(过去 30 天)
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 个评论
Image Analyst
Image Analyst 2017-9-3
Then why not just use my solution below for all 4 regions???
ASC
ASC 2021-11-18
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.

请先登录,再进行评论。

采纳的回答

Star Strider
Star Strider 2017-9-3
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 个评论
ishita agrawal
ishita agrawal 2017-9-4
okay. I get it now. Thanks a lot for helping me. :)

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2017-9-3
编辑:Image Analyst 2017-9-3
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 个评论
Image Analyst
Image Analyst 2017-9-3
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.
ishita agrawal
ishita agrawal 2017-9-3
Thank you for more clear view. My data is nanopore electrical signals. So, for me trapz is more useful.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Numerical Integration and Differentiation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by