How can i count peaks above a threshold?

42 views (last 30 days)
I have ECG data which i have processed to show all the values above a threshold. Is there a way to count these peaks? Also how would i count the peaks without counting every point above threshold within each peak.
Code i have used:
>> plot(time, A2);
>> threshold = ((max(A2) - mean(A2)) / 2);
>> plot([0 max(time)], threshold*[1 1],'--');
>> subplot(2,1,2);
>> output = A2 >= threshold;
>> plot(time, output);
where time and A2 are matrices of the ECG data. The code gives me a new graph with the values above threshold and values below all zero.
Thanks for the answers so far. I used peaks = (nnz(diff(output > 0))/2 which gave me the correct number. I'm also doing an instantaneous HR from the ECG data so i need to know the location of the peaks. For this i tried >> [Rpeak,Rlocation] = findpeaks(output); which gives me the peaks and locations on the x(time) axis. This seems to provide the answer to my first question aswell. Only started using Matlab this semester at uni, is there any traditional coding styles or approaches i should be using?

Accepted Answer

Jan
Jan on 26 Sep 2013
It depends on what a peak is in your case. If you ask for the number of all intervals in which the signal is above a limit, this would be a run-length encoding of A2>Threshold. If you mean some kind of peaks, set everything below the limit, e.g. A = max(A, Threshold) and apply some of the many functions for finding peaks. To find them, ask your favorite search engine for "Matlab find peaks".

More Answers (2)

ES
ES on 26 Sep 2013
Edited: ES on 26 Sep 2013
To count all points above threshold,
sum(A2>Threshold)
will do.
And as far as "Also how would i count the peaks without counting every point above threshold within each peak." is concerned, you will have to add some logic to check if it is a local maxima or not. something like, sum(A2>Threshold) only if A2[loop]>A2[loop-1] and A2[loop]>A2[loop+1].

Image Analyst
Image Analyst on 26 Sep 2013
If you have the Image Processing Toolbox, the count is simply
[L, numberOfPeaks] = bwlabel(A2>threshold);
This counts the number of "stretches" or "runs" of your signal above the threshold . Of course if one stretch above the threshold had 5 really tiny "peaks" high up on top of the main big peak, it would count it as one peak, not 5, since all 5 are "stuck together" atop the main big peak and are all above the threshold. Nonetheless, based on your code, I think my code is what you're looking for.

Community Treasure Hunt

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

Start Hunting!