This function quickly finds local peaks or valleys (local extrema) in a noisy vector using a user defined magnitude threshold to determine if each peak is significantly larger (or smaller) than the data around it. The problem with the strictly derivative based peak finding algorithms is that if the signal is noisy many spurious peaks are found. However, more complex methods often take much longer for large data sets, require a large amount of user interaction, and still give highly variable results. This function attempts to use the alternating nature of the derivatives along with the user defined threshold to identify local maxima or minima in a vector quickly and robustly. The function is able to correctly identify the major peaks on a 1.5 million data point noisy sum of sinusoids in under a second as is shown in the example in the code comments.
Please don't hesitate to comment or contact me if you have suggestions about improvements that could be made to this function.
@Frank I believe the function works as intended with the current indexing methodology. By using the same index (ii) for traversing both the valleys and the peaks, the function has to come down at least sel between each peak and two peaks can't be right next to each other. That said, please email me if you have an example of where you feel it does not work and I'd be happy to take a look.
Works (almost) good.
However it does not detect all peaks, I think there is a bug. The index in lines 186ff for finding the valley should be different from ii, e.g. jj:
jj = ii+1; % Move onto the valley
% Come down at least sel from peak
if ~foundPeak && tempMag > sel + x(jj)
foundPeak = true; % We have found a peak
leftMin = x(jj);
peakLoc(cInd) = tempLoc; % Add peak to index
peakMag(cInd) = tempMag;
cInd = cInd+1;
elseif x(jj) < leftMin % New left minima
leftMin = x(jj);
Dear MATLAB users community,
I am a matlab newbie,I have x and y as vectors, I plotted them in MATLAB and now I need to detect the peaks and also find the corresponding peak locations. How can I use this code in my case?
Thanks in advance.
Hello,i also have a problem with this code when i input the data under 1200 sampling frequency it doing well can detect all the peak points,but when i tried with other data under different sampling frequency sometimes it only can give me like half points some are missing,i don't know how to attach the image file,but can give me any help?
Joanne: That kind of error-message typically arises when Matlab does not know where the command resides (or when you misspelled it). Make sure that any m-file you wish to run are either in the current directory, and/or in your Matlab-path. If you have a separate folder for non-builtin m-files (such as peakfinder) that you wish to use, you can add that folder to the Matlab-path with the 'addpath' command, and put those m-files there.
the code works well but some times it happens that peaks which are not required I mean after initializing a certain value for 'sel' the peaks which are not supposed to be detected (peaks created by noise) and sometimes they are not detected
very quick and supportive response from the author. The output issue as listed above is due to my misunderstanding of the varargout. The other issue may also results from the Matlab setting rather than the code. Thank you, Nate.
very useful code. However, it cannot output the PeakMag, but only the PeakLoc when I tried it recently. and after I used it several times with a vector, the acceptable number of the element seems to be locked. When used with other vectors having different numbers of elements,the extra elements cannot be shown in the plot and the output results. I hope someone can kindly explain this...
Hi, I find this routine very good and useful. However, for my problem I would need the start-end information about each peak so I could select just the peaks and work with them, How can I get this information?
Hi. I need to find peaks in a waveform and calculate the inter-pulse interval and the pulse rate from the position of the peaks. My data are sounds recorded at 24000Hz and saved in .wav files. This code seems to be doing exactly what I want but since I’m a matlab newbie I’m not quite sure how to input my data. Do you have any suggestions?
I hope this is the right place to post this message, otherwise I apologise.
Thanks for catching that Tim. You were exactly right, the redundancy is an artifact from when I preallocated the matrix for speed. However, I essentially replaced this statement with the leftMin variable which is why you got the same results. The end effect is that the second part of that conditional can simply be eliminated.
Thanks again for your help and an updated version with the addition of a user defined threshold should be available shortly.
I think I found a mistake in the code. Or at least something strange:
if foundPeak && (x(ii) > peakMag(end) || leftMin < peakMag(end)-thresh)
The second part, x(ii) > peakMag(end), is rather strange, since peakMag(end) will ALWAYS be equal to zero, since peakMag was preallocated with zeros. I guess this is a remainder from the time before you preallocated peakMag for speed.
I replaced the mistake with the following:
if foundPeak && (x(ii) > peakMag(cInd-1) || leftMin < peakMag(cInd-1)-thresh)
The strange thing is that the results luckily stay exactly the same, which is good but strange at the same time. There is probably some redundancy in the code.
In any case, your method was more or less what I was looking for. Just need to make some small adaptations to it for my purpose, of course leaving all credits to you. Thanks!
Fixed example and error checking code. Thanks to Jiro Doke for catching my mistakes.
01 Dec 2009
Fixed problems with repeated initial values, repeated final values, and other directional issues. Thanks to Andres for his help finding and debugging these problems.
08 Dec 2009
Added support for monotone increase/decreasing functions and empty inputs. Thanks again to Andres for the debugging help.
08 Oct 2010
Updated the error checking on the threshold level.
14 Jun 2011
Removed redundancy (thanks Tim) and added thresholding option (thanks Femi).
02 Dec 2013
Added an optional boolean to exclude the endpoints since, as pointed out by Peter Cavanagh, most definitions of local extrema do not include them. By default the endpoints are included to maintain backward compatibility.
13 Aug 2014
Fix bug related to a missing equality comparison (thanks Nina Merkle!) and fix inconsistency when the endpoints are not treated as extrema (thanks Solomon Grant!).