index previous next

Peak Finding and Measurement, Version 2, April 2009

A fast, customizable Matlab routine for locating and measuring the peaks in noisy time-series data sets. It detects peaks by looking for downward zero-crossings in the smoothed first derivative. Determines the position, height, and width of each peak by least-squares curve-fitting. (This is useful primarily for signals that have several data points in each peak, not for spikes that have only one or two points).  It can find and measure 1000 peaks in a 1,000,000 point signal in 8 seconds.

The routine is available in three different versions: (1) the basic findpeaks function (findpeaks.m); (2) an interactive keypress-operated function (ipeak.m) for adjusting the peak detection criteria in real-time to optimize for any particular peak type; and (3) a script using mouse-controlled sliders for interactive control. These are all detailed below. Click here to download the ZIP file "Peakfinder.zip" that includes supporting functions, several self-contained demos to show how it works, and two interactive versions. You can also download it from the Matlab File Exchange.

The findpeaks function (findpeaks.m)

function P=findpeaks(x,y,SlopeThreshold,AmpThreshold,SmoothWidth,FitWidth)
This is the basic function to locate and measure the positive peaks in a noisy data sets. It detects peaks by looking for downward zero-crossings in the smoothed first derivative that exceed SlopeThreshold and peak amplitudes that exceed AmpThreshold, and determines the position, height, and approximate width of each peak by least-squares curve-fitting the top part of the peak. Returns a list (in matrix P) containing the peak number and the estimated position, height, and width of each peak. The data are passed in the vectors x and y (x = independent variable, y = dependent variable). The other four parameters are user-adjustable:

Example:

 >> x=[0:.1:50];y=sin(x);findpeaks(x,y,0,0.002,7,7)
ans =
            1       1.5711       1.0002       2.3318
            2       7.8543       1.0001       2.3344
            3       14.137       1.0001       2.3363
            4       20.421       1.0001       2.3375
            5       26.704       1.0001        2.338
            6       32.987       1.0002       2.3288
            7        39.27       1.0002        2.332
            8       45.553       1.0001       2.3345

Note: The accuracy of the measurements of  peak position, height, and width by the findpeaks function depends on the shape of the peaks, the extent of peak overlap, and signal-to-noise ratio.  The width measurements particularly are strongly influenced by peak overlap, noise, and the choice of FitWidth. Isolated peaks of Gaussian shape are measured most accurately.  See "ipeakdemo" (below) for an accuracy trial. For the most accurate measurements of highly overlapping peaks of various shapes, use the Interactive Peak Fitter (ipf.m).

DemoFindPeak (DemoFindPeak.m)

Click to view enlarged figure
Click to view enlarged figure
A simple demonstration script using the findpeaks function on noisy synthetic data. Numbers the peaks and prints out the P matrix in the Matlab command window: 
    Peak #   Position   Height    Width
P =
1.0000 10.0235 0.1814 1.7506
2.0000 29.9964 0.6412 1.9931
3.0000 50.0205 0.4315 2.1433
4.0000 70.0120 1.3935 2.0202
5.0000 79.9567 0.2580 1.8267
6.0000 100.0086 1.0862 2.0306
......

The iPeak Function (ipeak.m)

Click to view enlarged figure

ipeak4

ipeak
P=ipeak(DataMatrix)
Keyboard-operated interactive peak finder written as a single self-contained m-file (Matlab function), for data in the matrix "DataMatrix", with x values in row 1 and y values in row 2. Simple keypress commands pan and zoom through the signal and allow the interactive adjustment of the four arguments of the findpeaks function described above.  This function also has piecewise linear baseline subtraction, as described below for "FindPeakSliders".
T. C. O'Haver (toh@umd.edu), Version 1.1, April 25, 2009.  
Click here to view or download. ipeak.m.
 
There are three ways to call the function, with 1,2, or 6 arguments, which differ only in the way the initial values of the four peak detection parameters are calculated:

EXAMPLE 1:   (one argument)
x=[0:.1:100];y=(x.*sin(x)).^2;
ipeak([x' y']);

  Displays DataMatrix [x' y'] with starting values for peak detection parameters AmpT, SlopeT, SmoothW, FitW that are estimated by the function.  
 
EXAMPLE 2:   (two arguments)
x=[0:.1:100];y=5+5.*cos(x)+randn(size(x));
ipeak([x' y'],10);

  The optional second argument, which is an estimate of the maximum number of peaks that would fit into the data record (10 in this example), is used by the function to compute better starting values of the 4 peak detection parameters.
 
EXAMPLE 3: (six arguments)
 ipeak([x' y'],0,0,.0001,33,33);
  As above, but specifies the exact initial values of AmpT, SlopeT, SmoothW, and FitW (in the last 4 arguments).
 
Keyboard Controls:
 Pan left/right:       Coarse pan: < and >
                       Fine pan: left/right arrows
 Zoom in/out:          Coarse zoom: / and "  
                       Fine zoom: up/down arrows
 Adjust AmpThreshold:  A,Z
 Adjust SlopeThreshold:S,X
 Adjust SmoothWidth:   D,C
 Adjust FitWidth:      F,V
 Baseline:             B, then click baseline at 8 points
 Print peak table:     P  Prints Peak#,Position,Height,Width
 Print key commands:   K  Prints this list
 Print parameters      Q (Prints AmpT, SlopeT, SmoothW, FitW)
 Print report          R  Prints Peak table and parameters 
 Peak labels ON/OFF:   L (
Peak #, Position, Height, Width)
>> ipeakdemo
>> Peak #    Position      Height       Width
PercentErrors =
   1    -0.035481     0.042445     -0.61178
   2     -0.13457     -0.93707       -18.01
   3     0.012393     -0.75061      0.78906
   4      0.01442     -0.35048      0.19783
   5     0.072319       1.6627        4.204
   6    -0.069226      0.70238       8.6487
   7     0.027877     -0.27144       2.7331
   8    -0.061387      -1.4948      0.49849
   9   -0.0017726      0.25882      0.48333
   10   -0.0047634     -0.53386       1.2068
   11  -0.00094297    0.0099868      -3.2264
   12     0.033587       -5.272      -1.6698
   13   0.00067791      0.65194       0.9434
   14    -0.015964     -0.31073        2.658
AveragePercentError =
  7.5      0.03467      0.94638       3.2772
ipeakdemo

This is a demo script for iPeak that demonstrates the operation of iPeak and its typical measurement errors.  It generates a test signal consisting of several Gaussian peaks, adds random noise, runs iPeak, then calculates the errors between the actual and the measured peak positions, heights, and widths.  Each time you run it you get a different set of peaks. (You can easily change the noise level or the peak heights, positions, and widths by editing the script in the indicated places). Click to view or download this script.

In the example output on the left, you can see that the percent error of peak position measurements is excellent (less than 0.1%), that of peak height is also pretty good (about 1%) , but the error of  width measurement is higher (about 3% here).  For the most accurate measurements of highly overlapping peaks of various shapes, use the Interactive Peak Fitter (ipf.m).  

FindPeakSliders

Click to view enlarged figure
Before baseline correction

Click to view enlarged figure

After baseline correction

Interactive findpeaks script using mouse-controlled sliders for interactive control. Requires Matlab 6.5. This can be used to determine what values of the parameters give the most reliable peak detection. Load your data set into the vectors x and y (x = independent variable, y = dependent variable), then run this m-file and adjust the sliders to change the four arguments of the peakfind function. The four sliders correspond to the arguments of the findpeaks function described above: SlopeThreshold (SlopeT), AmpThreshold (AmpT), SmoothWidth (Smooth), and FitWidth (Fit). The range of these sliders is easily changed in lines 65 - 70 to suit a wide range of data types. The BG button is used for baseline (background) subtraction: click once on the BG button, then click on the baseline at five points starting to the left of the lowest x-value and ending to the right of the higest x-value. The background will be subtracted. You can repeat as needed. (You can also change the number of baseline points by changing BaselinePoints in the function BG.m). Peak number and the estimated position, height, and width of each peak is returned in the matrix P:
>> P
P =
1.0000 45.0095 0.1328 4.0102
2.0000 90.5301 0.3655 4.7127
3.0000 94.9673 1.4075 4.0967
......

DemoFindPeakSliders

Click to view enlarged figure
Click to view enlarged figure
A self-contained interactive demonstration of FindPeakSliders applied to noisy synthetic data set consisting of a random number of narrow peaks superimposed on a gently curved background. Requires Matlab 6.5. Use the sliders to explore the effect of the variables SlopeThreshold (SlopeT), AmpThreshold (AmpT), SmoothWidth (Smooth), and FitWidth (Fit), and the baseline correct (BG) button. Peak number and the estimated position, height, and width of each peak is returned in the matrix P.

DemoFindPeakSliders2


Click to view enlarged figure
A self-contained interactive demonstration of FindPeakSliders applied to a data set containing four simple peaks with increasing peak height and peak width. Use this to understand the difference between the variables SlopeThreshold (SlopeT), which discriminates on the basis of peak width, and AmpThreshold (AmpT), which discriminates on the basis of peak amplitude. Peak number and the estimated position, height, and width of each peak is returned in the matrix P:
>> P
P =
1.0000 49.9096 1.9895 20.1960
2.0000 126.1034 3.0585 42.3508
3.0000 201.3947 4.0752 63.3354
4.0000 299.9242 4.9925 82.2000


FindPeakSlidersG

Click to view enlarged figure
Click to view enlarged figure
Click to view enlarged figure
Click to view enlarged figure
Interactive findpeaks script with peak-zoom feature, for pre-defined data in x,y. Requires Matlab 6.5. Load a typical data set into the vectors x,y, then run this m-file and adjust the sliders to determine what values of the parameters give the most reliable peak detection. Peak number and position, height, and width of each peak is returned in the matrix P. This version has an additional slider, Peak, which zooms in on any one of the detected peaks and shows the fitted top of the peak (FitWidth) as a blue curve. To see all the peaks, set the Peaks slider to zero (all the way down).
Note: If the slider ranges are not appropriate for your data, change them in lines 63-69.

DemoFindPeakSlidersG

A self-contained interactive demonstration of FindPeakSlidersG. Requires Matlab 6.5.

ZIP file containing all of the above peakfind functions and demos.

Video Demonstration of FindPeakSliders

This is a 26-second, 1.4 MByte video (PeakFind2.wmv) of FindPeakSliders (version 1.1), showing the effect of varying the four parameters SlopeThreshold (SlopeT), AmpThreshold (AmpT), SmoothWidth, and FitWidth (labeled "PeakWidth" in this video).

Tom O'Haver
Professor Emeritus
Department of Chemistry and Biochemistry
The University of Maryland at College Park
toh@umd.edu
http://www.wam.umd.edu/~toh


index previous next

Number of unique visits since May 17, 2008:

counter
create hit