# How to count the number of peaks from a graph with a certain threshold?

27 views (last 30 days)
Duc Khanh on 7 Jun 2022
Commented: Image Analyst on 7 Jun 2022
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.
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?

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);
.
Star Strider on 7 Jun 2022
As always, my pleasure!

Jan on 7 Jun 2022
findpeaks will solve the problem:
[PKS,LOCS] = findpeaks(Y, 'MinPeakHeight', yourThreshold)
Maybe you use Y and X depending on what a "1000x2 plot" is.
Duc Khanh on 7 Jun 2022
My X Axis is time and the Y Axis is Voltage, I want to count the Voltage Peaks along the given time. I used this code and found the total number of peaks and labeled them on the graph.
clc
T=may(:,1);
V=may(:,2);
figure (1)
plot(T,V);
findpeaks(V(:,1));
title('Voltage Peaks');
findpeaks(V,'MinPeakHeight',3.9);
threshold = 3.99;
sum (V>threshold)

Duc Khanh on 7 Jun 2022
Here is my code, I labeled all my desired peaks and counted the total number of peaks. However, now I want to list all of the values, can you have any advice?
clc
T=may(:,1);
V=may(:,2);
figure (1)
plot(T,V);
findpeaks(V(:,1));
title('Voltage Peaks');
findpeaks(V,'MinPeakHeight',3.9);
threshold = 3.99;
sum (V>threshold)
##### 2 CommentsShowHide 1 older comment
Duc Khanh on 7 Jun 2022
Actually, I do not know anything about coding, can you write those lines for me?

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);
##### 2 CommentsShowHide 1 older comment
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)
numPeaks = 2
[peakValues, indexesOfPeakValues] = findpeaks(signal);
numPeaksFP = length(indexesOfPeakValues)
numPeaksFP = 3
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?