1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Peak detection using MATLAB (signal processing)

  1. Mar 6, 2010 #1
    I'm doing signal processing using MATLAB.

    But facing problem when doing Peak Detection.

    This is the signal that I used for the processing:

    This is what i wish to do:
    a) the horizontal line is the threshold point and
    b) the circle is the peak detection

    Can anyone help me in this?
    1) set a threshold point for the signal
    2) detect the peak
  2. jcsd
  3. Mar 6, 2010 #2


    User Avatar
    Homework Helper


    If x is your signal, you get logical indices for which x is above the threshold simply by
    x > threshold;
    Eg, x = [1 3 1 4],
    x > 2 returns [0 1 0 1], (and x(x > 2) returns [3 4]).

    What you should consider is that there might be a couple of successive samples above the threshold. If this is the case, I'd probably do something along the lines of x > threshold, diff, and find the positive values (if for some reason the maximum value above the threshold is important, you'd have to max as well).

    I think this came out a bit convoluted, but I tried avoiding completely solving the problem for you.
  4. Mar 6, 2010 #3
    I'm actually cant really understand what you means.
    Would you mind to explain more to me?
  5. Mar 6, 2010 #4


    User Avatar
    Homework Helper

    Well, if x is your ECG, (or whatever signal),
    you can threshold with
    x > threshold;
    Now, this is a vector with zeros and ones.
    At ones, the signal is above the threshold.
    Do e.g.
    ind = x > threshold;
    plot(1:length(x), x, find(ind), x(ind), 'o')

    This will probably be rather close to the desired output, but if there are successive values of x above the threshold, this won't come out right. You would have to determine the maximum of these successive values. I'd use the diff-command to differentiate as that ought to be quite simple to implement, but you may have other preferences.
  6. Mar 6, 2010 #5
    Thanks for help.
    what is the 'successive values' means?
  7. Mar 6, 2010 #6


    User Avatar
    Homework Helper

    By successive I meant that two values of the signal in a row exceed the threshold value.

    x = [1 1 1 1 3 4 1 1 1 1], if the threshold is 2, the algorithm is going to tell you there are two peaks, where in reality there is only one.
  8. Mar 6, 2010 #7
    oh I get it already.
    Thanks for your help. Thanks so much.
  9. Aug 14, 2010 #8
    i need matlab program of peakdetect program pl help me
  10. Mar 9, 2011 #9
    If I have successive values, how should I go about doing that?
  11. May 18, 2011 #10
    hey i also need some help regarding ecg signal..i need to make an algorithm in matlab to find values of r-peaks...so that if i want to have a value of any r-peak i can get it from this algorithm like pan-tomkins algorithm in matlab.can anyone help for it?
  12. Aug 11, 2011 #11
  13. Apr 23, 2012 #12
    Hi, i'm also facing same problem, i'm doing a heart signal peak detection project and i attached the plot result , can u tell me how i solve this ?


    Attached Files:

  14. Apr 23, 2012 #13
    Given the above example: x = [1 1 1 1 3 4 1 1 1 1]

    peakfinder (http://www.mathworks.com/matlabcentral/fileexchange/25500) gives you the following:

    [peak loc] = findpeaks(x)

    peak = 4 <-- the amplitude of the peak is 4
    loc = 6 <-- the location of the peak is 6

    there are also lots of other useful functions included in the package.
  15. Apr 24, 2012 #14
    Can tell me how to proceed this code? I'm really new for this ,thanks
  16. Apr 24, 2012 #15
    Setting up peakfinder
    1. download the peakfinder package from the link above. extract/unzip the folder and put it in a folder under your matlab path
    2. in matlab, add the peakfinder folder to path (file -> set path, click on set path, select the peakfinder folder by browsing, click on save, then click on close)

    Using peak finder
    assuming your signal is already in the matlab workspace called x, to find the peaks using peakfinder, type in the following in the matlab command window:

    [peaks, locs] = findpeaks(x)

    this would result in two vectors:

    peaks = [# # # # # ... ] <-- indicates all the peaks that are found in x
    locs = [# # # # # ...] <-- indicates all the locations/positions of the found peaks

    to examine the peaks found using this setting, you can type in the following:

    hold on;

    more functions/setting can be consulted through typing 'help peakfinder' in the matlab command window. hope this helps.
  17. Apr 24, 2012 #16
    This is very helpful ,thank you so much ^^
  18. Apr 29, 2012 #17
    I'm not sure if i should leave this message here,but i'm doing it, here's my problem,like u said i tried with the code ,it was very nice and very useful,it doing well when i input the data under 1200sampling frequency,but when i tried other data under different sampling frequency the result was not satisfactory, here i attached something related to my issue

    Attached Files:

Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook