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

123 views (last 30 days)
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
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?

Sign in to comment.

Accepted Answer

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

Sign in to comment.

More Answers (3)

Jan
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.
  1 Comment
Duc Khanh
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
may=xlsread("tek 92.xlsx",'Sheet1','A1:B1000');
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)

Sign in to comment.


Duc Khanh
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
may=xlsread("tek 92.xlsx",'Sheet1','A1:B1000');
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)

Image Analyst
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
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?
feng du
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!