5.0

5.0 | 3 ratings Rate this file 155 downloads (last 30 days) File Size: 3.25 KB File ID: #25500

PeakFinder

by Nate Yoder

 

06 Oct 2009 (Updated 08 Dec 2009)

Code covered by the BSD License  

Quickly finds local maxima (peaks) or minima (valleys) in a noisy signal.

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

Download Now | Watch this File

File Information
Description

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.

MATLAB release MATLAB 7.8 (R2009a)
Other requirements None
Zip File Content  
Other Files license.txt,
peakfinder.m
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (3)
16 Nov 2009 Morvan  
30 Nov 2009 Andres

I was happily using peakfinder until I had severe trouble with a dataset beginning with repeated values.
I've boiled it down to a short example:

t = [49 49 54 49 -57 46 -96 -10 39 0];
startI1 = 1;
[Loc1, Peak1] = peakfinder(t(startI1:end),20);
startI2 = 2;
[Loc2, Peak2] = peakfinder(t(startI2:end),20);

figure
plot(t,'b.-')
hold on
plot(Loc1-1+startI1,Peak1,'ro')
plot(Loc2-1+startI2,Peak2,'go')

Problems:
1) The first call returns a wrong result (Loc1 = 2, Peak1 = 49).
2) The second call returns all three peaks, but the last one appears twice (Loc2 = [2 5 8 8].', Peak2 = [54 46 39 39].').

I hope these issues can be fixed as peakfinder works promisingly fast.
A minor suggestion: if the input is a row vector the output should be a row vector, too.

Regards
Andres

02 Dec 2009 Andres

The bugs were fixed quickly. Thanks Nate!

Please login to add a comment or rating.
Updates
06 Nov 2009

Updated code to make it slightly faster.

06 Nov 2009

Updated algorithm to make it slightly faster.

11 Nov 2009

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.

Tag Activity for this File
Tag Applied By Date/Time
signal processing Nate Yoder 06 Oct 2009 10:53:35
maxima Nate Yoder 06 Oct 2009 10:53:36
minima Nate Yoder 06 Oct 2009 10:53:36
peaks Nate Yoder 06 Oct 2009 10:53:36
valleys Nate Yoder 06 Oct 2009 10:53:36
troughs Nate Yoder 06 Oct 2009 10:53:36
local Nate Yoder 06 Oct 2009 10:53:36
extrema Nate Yoder 06 Oct 2009 10:53:36
physics Nate Yoder 06 Oct 2009 10:53:36
chemistry Nate Yoder 06 Oct 2009 10:53:36
local max Nate Yoder 05 Nov 2009 08:55:19
local maxima Nate Yoder 05 Nov 2009 08:55:19
peak finder Nate Yoder 10 Nov 2009 15:46:32
potw Shari Freedman 13 Nov 2009 09:30:39
chemistry Pär 17 Dec 2009 08:01:24
extrema Pär 17 Dec 2009 08:01:26
local max Pär 17 Dec 2009 08:01:30
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com