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 commentaires

KALYAN ACHARJYA
KALYAN ACHARJYA le 3 Sep 2017
Clarify: do you want to calculate the 4 colors area (individually)?
ishita agrawal
ishita agrawal le 3 Sep 2017
yes
Image Analyst
Image Analyst le 3 Sep 2017
Then why not just use my solution below for all 4 regions???
ASC
ASC le 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?
Thanks for your help.

Connectez-vous pour commenter.

 Réponse acceptée

Star Strider
Star Strider le 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 commentaires

ishita agrawal
ishita agrawal le 3 Sep 2017
Thank you for your response. It worked.
Star Strider
Star Strider le 3 Sep 2017
As always, my pleasure.
ishita agrawal
ishita agrawal le 4 Sep 2017
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.
ishita agrawal
ishita agrawal le 4 Sep 2017
okay. I get it now. Thanks a lot for helping me. :)
Star Strider
Star Strider le 4 Sep 2017
My pleasure!

Connectez-vous pour commenter.

Plus de réponses (1)

Image Analyst
Image Analyst le 3 Sep 2017
Modifié(e) : Image Analyst le 3 Sep 2017

0 votes

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 commentaires

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

Connectez-vous pour commenter.

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by