File Exchange

image thumbnail


version 7.7.1 (272 KB) by

Keypress operated interactive peak detector function.



View License

iPeak is a keyboard-operated Interactive Peak Finder for time series
  data. Expected input forms:
  ipeak(y); % Data in single y vector')
  ipeak(x,y); % Data in separate x and y vectors')
  ipeak(DataMatrix); % Data in two columns of DataMatrix')
  ipeak(x,y,10), ipeak([x;y],10) or ipeak(y,10), specifying peak density, PeakD
  ipeak(DataMatrix,PeakD,AmpT,SlopeT,SmoothW,FitW) specifying peak density, AmpT, SlopeT, SmoothW, FitW')
  ipeak(DataMatrix,PeakD,AmpT,SlopeT,SmoothW,FitW,xcenter,xrange) Adding pan and zoom settings ')
  ipeak(DataMatrix,PeakD,AmpT,SlopeT,SmoothW,FitW,xcenter,xrange,Autozero) Adding Autozeo as 9th argument')
  ipeak(DataMatrix,PeakD,AmpT,SlopeT,SmoothW,FitW,xcenter,xrange,MaxError,positions,names) Adding peak ID')
EXAMPLE 1: One input argument; data in single vector
>> y=cos(.1:.1:100);ipeak(y)
EXAMPLE 2: One input argument; data in two columns of a matrix
>> x=[0:.01:5]';y=x.*sin(x.^2).^2;M=[x y];ipeak(M);
EXAMPLE 3: Two input arguments; data in separate x and y vectors
>> x=[0:.1:100];y=(x.*sin(x)).^2;ipeak(x,y);
EXAMPLE 4: Additional input argument (after the data) to control peak
>> x=[0:.1:100];y=5+5.*cos(x)+randn(size(x));ipeak(x,y,10);
or >> ipeak([x;y],10); or >> ipeak(humps(0:.01:2),3)
or >> x=[0:.1:10];y=exp(-(x-5).^2);ipeak([x' y'],1)
T. C. O'Haver (,

Comments and Ratings (36)

Tom O'Haver

Tom O'Haver (view profile)

Note to Amir and others. If you want a plain function version of iPeak that does not plot or print anything, use one the the plain functions in this distribution: findpeaksG or findpeaksL or findpeaksfit or findpeaksb or findpeaksb3. All are described on iPeaks is strictly a keypress-operated interactive version.

Tom O'Haver

Tom O'Haver (view profile)


My understanding is that if an internal sub-function has the same name as an external function in the path, the internal function takes precedence. Is it the same for the Unix version?


arnold (view profile)

Hi Tom,

I'm using 2015b (full uni version) now and I'm getting an error when using findpeaksb or findpeaksfit:
Error: File: findpeaksb.m Line: 1035 Column: 29
Function with duplicate name "val2ind" cannot be defined.

There is actually such a function in:
C:\Program Files\MATLAB\R2015b\toolbox\phased\phased

this might call that problem. Any tips?

this is really a great upload
but could you please make some adjustment for those of us who really need to use it as only a function
as i said i try to use this in a loop to only get the intensity location of the peak and etc,
i tired to delete all disp( title( plot( and etc
but for some reason it still logs numerals 1 continuously
could you provide a way to stop all the plotting and etc
which would do 2 things
stop interference with my gui
secondly as the loop calls this function 100,000 it sould really help with speed of things

Tom O'Haver

Tom O'Haver (view profile)

Amir, you could delete all the "plot(", "subplot(", "xlabel", "ylabel", and "title" statements in the code. Or you could use the findpeaksG.m function instead of ipeak.m, which does not plot but only returns the peak table.

thanks for the great upload
it really works great
i have a question
i want to run this program in a loop (the ipeak.m)
i have done that and it works great.
but i don't need the code to plot it i just want the "ans" data
how to modify the code to turn of plotting
is it possible for you to change the code so the ipeak.m take a function like yes or no as input argument to wether plot the data or not


arnold (view profile)

what is the "width" this function gives for a gaussian fit i.e.? it is not n*sigma, so I'm at a loss ATM.

nevermind, I got it .... FWHM


Jochen (view profile)

Fantastic work! Thanks a lot!


Jochen (view profile)

Tom O'Haver

Tom O'Haver (view profile)

Oops, I forgot to include that function. I'll fix it, but in the meantime, here it is:

function [index,closestval]=val2ind(x,val)
% Returns the index and the value of the element of vector x that is closest to val
% If more than one element is equally close, returns vectors of indicies and values
% Tom O'Haver ( October 2006
% Examples: If x=[1 2 4 3 5 9 6 4 5 3 1], then val2ind(x,6)=7 and val2ind(x,5.1)=[5 9]
% [indices values]=val2ind(x,3.3) returns indices = [4 10] and values = [3 3]

aoli ao

Thanks so much for sharing this useful toolbox. I have a question here. when I tried to run ipeakdemo2.m or DemoFindPeaksb.m, it kept saying
??? Undefined function or method 'val2ind' for input arguments of type 'double'.

so how should do with this problem? Thanks for your help!

Tom O'Haver

Tom O'Haver (view profile)

iPeak version 5.9, submitted this morning, adds the percent fitting as the 6th column of the peak table. Thanks to helin for making this suggestion. Also available on


helin (view profile)

Thanks for your quick answer. I believe an estimate of fitting error would be extremely useful, so that we can have a quantified understanding of goodness of fit quickly. Thanks again.


helin (view profile)

Thanks for your quick answer. I believe an estimate of fitting error would be extremely useful, so that we can have a quantified understanding of goodness of fit. Thanks again.

Tom O'Haver

Tom O'Haver (view profile)

In iPeak, the fitting error is calculated only when you perform a curve fit (N or M keys). It's displayed in the x-axis label of the upper panel. You don't need to specify estimated/known peaks because it gets that info from the peak table.
But if you mean peak parameter error instead of fitting error, then you must use my ipf.m or peakfit.m programs instead, which include a bootstrap error estimate capability.
Would it be useful to add an estimate of fitting error to the usual peak parameter results?


helin (view profile)

Thanks for this great code. I want to learn if there is a way to calculate fitting error if we do not have any estimated/known peaks in contrast to 11-input case.

Tom O'Haver

Tom O'Haver (view profile)

Thanks, Bernhard, that's good advice. I'll look into "namespace" and "nlimt".

I do not believe "findpeaks" was a basic built-in function when I first created my function (back in Matlab 6.5), but I agree that I should change the name of my function to avoid the conflict.


Great and very useful work!
The only drawbacks are that the function findpeaks already comes with MATLAB and you make heavy use of global variables (can lead to bad side effects when used in conjunction with other code).
Consider using a namespace or rename the function and generally check mlint results (this should also improve performance).

Tom O'Haver

Tom O'Haver (view profile)

Thanks, Robert. After doing an iterative curve fit, the best-fit parameters are printed out in the command window. Or did you mean something else by "write the outcome"?


Robert (view profile)

Hi Tom, thanks for sharing this wonderful code. It is not possible to write the outcome obtained after iterative curve fitting to a variable or am I missing something?

Tom O'Haver

Tom O'Haver (view profile)

Version 4 is now available from It adds the 'H' key that more easily detects 'shoulders' and poorly-resolved peaks.

Tom O'Haver

Tom O'Haver (view profile)

Kenric Lam, thanks for the heads-up. That bug has been tracked down and fixed in iPeak version 3.9, just uploaded to the file exchange and also available at

Kenric Lam

It is a big trap that there is a subfunction 'findpeaks' in 'ipeak.m' that is the same to the separate file 'findpeaks.m', but the results given are different.

Eric Diaz

Wonderfully documented!

Eric Diaz

Great code Tom!


wonderful code !!!

Tom O'Haver

Tom O'Haver (view profile)

Version 3 adds iterative curve fitting of the detected peaks with selectable peak shapes.

Tom O'Haver

Tom O'Haver (view profile)

Version 2.0, June 24, 2011, adds two main new features. You can set the initial values of pan and zoom in optional input arguments 7 ('xcenter') and 8 ('xrange'). There is also an optional "peak identification" function if optional input arguments 9 ('MaxError'), 10 ('Positions'), and 11 ('Names') are included. Type Help ipeak for more info.

Tom O'Haver

Tom O'Haver (view profile)

Version 1.4 fixed a problem with missing functions.

Tom O'Haver

Tom O'Haver (view profile)

Version 1.3 has improved input argument flexibility, accepts signals as separate x and y vectors or a two-column or two-row matrix. Adds a function to restore the original signal after the baseline has been subtracted. Adds peak area to the peak table.

Tom O'Haver

Tom O'Haver (view profile)

Version 1.2 (April 2011) has improved graphic performance on recent versions of Matlab.

Tom O'Haver

Tom O'Haver (view profile)

That should work, Aurora. Download the iPeak function, put it in the Matlab path, then type "help ipeak".


Aurora (view profile)

I want to use it to detect density peaks in my 1 dimensional data. How to do this? Thanks!

Tom O'Haver

Tom O'Haver (view profile)

Version 1.1 posted 25 Apr 2009 adds keypress command "L" which toggles ON and OFF peak labels in the upper panel (zoomed-in segment). Each peak in that segment is labeled with peak position, height, and width. The labels change in real-time as the peak detection parameters are modified.



Bug fix


Improved performance for detecting narrow spikes in the flat-top mode (shapemode 2)


Fixed bug in halfwidth measurements of flat-top shape mode.


Version 7.5 April, 2016, Added Flat top peak shape (ShapeMode=2). Fixed bug in fitting error calculations.


Version 7.3 October, 2015, relocates peak numbers above peaks on upper and lower graphs; Reports FWHM for Voigt shapes (shape numbers 20 and 30).


Version 7.1 adds improved Octave compatibility and includes 33 model peak shapes for curve fitting.


Version 6 adds an ensemble averaging function (Shift-E).


Version 5.96 adds interpolation function (Shift-I).


Version 5.9 adds relative % fitting error as 6th column of peak table


Version 5.8: Includes version 4.2 of peakfit.m, adds Voigt profile shape and flat baseline mode for iterative least-squares fits.


Version 5.3 adds table of summary statistics of the maximum, minimum, average, percent standard deviation, and displays histograms of the peak intervals (the x-axis interval between adjacent detected peaks), heights, widths, and areas.


Version 5: Faster jump between peaks with spacebar and tab keys; other small bug fixes


Version 4.1: peakfit internal function updated to version 3.3. Bug fixes.


Version 4.0, August, 2012, adds 'H' key to help in detecting 'shoulders' and poorly-resolved peaks.


Typo correction.


Version 3.9: Bug fixes; replaced internal findpeaks function with version 4; FitWidth now equals number of points, not number of intervals.


Version 3.9: Bug fixes; replaced internal findpeaks function with version 4; FitWidth now equals number of points, not number of intervals.


Version 3.8 adds manual entry of AmpT after entering number of click points for background subtraction; '0' key to set minimum to zero.


Version 3.7, Adds valley detection mode (U key)


Bug fixes


Version 3.2 has better noise
discrimination, adds log y (Y key), autozero mode (T key), jump to next/previous peak (Space/Tab keys).


Version 3 adds iterative least-squares curve fitting with selected peak shapes.


Edited description


Version 2 allows specifying the initial values of pan and zoom. Also the 'IDpeak' function compares the found peak positions (maximum x-values) to a database of known peaks and identifies matching peaks in the signal.


Improved keyboard error handling


Includes embedded functions that were omitted in previous version.


Several enhancements to UI and input argument flexibility. Added baseline restore function.


Improved graphics performance on Matlab 7.8 (R2009a)


Version 1.1 adds "L" keypress to toggle peak labels in upper panel ON and OFF. Peak shown in the upper panel (zoom-in view) are labeled with peak position, height, and width, which changes in real-time as the peak detection parameters are varied.


Revised description

MATLAB Release
MATLAB 7.8 (R2009a)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video