How to count the number of peaks from a graph with a certain threshold?
123 views (last 30 days)
Show older comments
I drew a 1000x2 plot from an excel file, now I want to mark and count the number of positive peaks which are higher than a certain threshold (for example, in this image is 4). Please let me know an easy way to do that because I am not good in coding. Thank you.
1 Comment
Jan
on 7 Jun 2022
Prefer to post your code than a vague description of what it is. Then it is much easier to post a matching answer.
What is a "1000x2 plot"? Are these 2 signals or is one column the x and the other the y coordinate?
Accepted Answer
Star Strider
on 7 Jun 2022
The findpeaks function only works on vectors, so assuming that the first column of the Excel file is the independent variable and the second is the dependent variable, use findpeaks on column 2 and use the ‘locs’ output index into column 1 to get the associated independent variable value:
[pks,locs] = findpeaks(A(:,2), 'MinPeakHeight',4);
yval = pks;
xval = A(locs,1);
.
6 Comments
More Answers (3)
Image Analyst
on 7 Jun 2022
findpeaks, as the others are suggesting, may or may not be what you want. It will find peaks above a threshold but there may be some quirks with your data that cause you to fiddle around with various complicated options with it. For example let's say you have a double-peaked hump above the threshold. Is that one peak or two? There is just one stretch of data consistently above the threshold but there are two tiny peaks on the top of it. If youi just want to find out how many stretches are above the threshold you can use bwlabel if you have the Image Processing Toolbox.
[~, numPeaks] = bwlabel(yourSignal > someThresholdValue);
3 Comments
Image Analyst
on 7 Jun 2022
I was not suggesting findpeaks - the others were. And you've accepted an answer so I guess you got everything solved now so I won't answer other than to say a stretch is how many elements are above the threshold. For example there are two runs above the threshold in the plot below.
signal = [0 0 5 10 8 15 0 13 0];
plot(signal, 'b.-', 'LineWidth', 2, 'MarkerSize', 30);
threshold = 7;
yline(threshold, 'Color', 'r', 'LineWidth', 2); % Draw red line at threshold
[~, numPeaks] = bwlabel(signal > threshold)
[peakValues, indexesOfPeakValues] = findpeaks(signal);
numPeaksFP = length(indexesOfPeakValues)
So how many peaks are above the threshold? bwlabel will tell you there are 2 while findpeaks will tell you there are 3 unless you give it additional parameters. How many do you say there are?
feng du
on 15 Feb 2024
you are right, I counted over a few million action potentials using findpeaks, and the correct number using bwlabel.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!