Code covered by the BSD License  

Highlights from
Peak Fitter

4.2

4.2 | 5 ratings Rate this file 130 Downloads (last 30 days) File Size: 8.08 KB File ID: #23611
image thumbnail

Peak Fitter

by Tom O'Haver

 

09 Apr 2009 (Updated 19 Jan 2012)

Command-line peak fitter for time-series signals. Version 2.3: Jan 2012.

| Watch this File

File Information
Description

[FitResults,MeanFitError]=PEAKFIT(signal,center,window...)
A command-line peak fitting program for time-series signals,
  written as a self-contained Matlab function in a single m-file.
  Uses an non-linear optimization algorithm to decompose a complex,
  overlapping-peak signal into its component parts. The objective
  is to determine whether your signal can be represented as the sum of
  fundamental underlying peaks shapes. Accepts signals of any length,
  including those with non-integer and non-uniform x-values. Fits
  Gaussian, equal-width Gaussians, exponentially-broadened Gaussian,
  Lorentzian, equal-width Lorentzians, Pearson, Logistic, exponential
  pulse, abd sigmoid shapes (expandable to other shapes). This is a command
  line version, usable from a remote terminal. It is capable of making
  multiple trial fits with sightly different starting values and taking
  the one with the lowest mean fit error.
 
T. C. O'Haver (toh@umd.edu). Version 2.3: January, 2012. Bug fixes in background subtraction modes and in handlng very small data sets.
 
Example 1:
  >> x=[0:.1:10];y=exp(-(x-5).^2);peakfit([x' y'])
  Fits exp(-x)^2 with a single Gaussian peak model.
  ans =
              1 5 1 1.665 1.7725
         Peak number Peak position Height Width Peak area
 
Example 2:
  x=[0:.1:10];y=exp(-(x-5).^2)+.1*randn(1,length(x));peakfit([x' y'])
  Like Example 1, except that random noise is added to the y data.
  ans =
              1 5.0279 0.9272 1.7948 1.7716
 
Example 3:
  x=[0:.1:10];y=exp(-(x-5).^2)+.5*exp(-(x-3).^2)+.1*randn(1,length(x));
  peakfit([x' y'],5,19,2,1,0,1)
  Fits a noisy two-peak signal with a double Gaussian model (NumPeaks=2).
  ans =
              1 3.0001 0.49489 1.642 0.86504
              2 4.9927 1.0016 1.6597 1.7696
 
Example 4:
    >> x=[0:.005:1];y=humps(x);peakfit([x' y'],.3,.7,1,4,3);
    Fits a portion of the humps function, 0.7 units wide and centered on
    x=0.3, with a single (NumPeaks=1) Pearson function (peakshape=4)
    with extra=3 (controls shape of Pearson function).
 
Example 5:
   >> x=[0:.005:1];y=(humps(x)+humps(x-.13)).^3;smatrix=[x' y'];
   >> [FitResults,MeanFitError]=peakfit(smatrix,.4,.7,2,1,0,10)
   Creates a data matrix 'smatrix', fits a portion to a two-peak Gaussian
   model, takes the best of 10 trials. Returns FitResults and MeanFitError.
   FitResults =
               1 0.4128 3.1114e+008 0.10448 3.4605e+007
               2 0.3161 2.8671e+008 0.098862 3.0174e+007
   MeanFitError =
         0.68048
 
Example 6:
  >> peakfit([x' y'],.4,.7,2,1,0,10,[.3 .1 .5 .1]);
  As above, but specifies the first-guess position and width of the two
  peaks, in the order [position1 width1 position2 width2]

Example 7:
[FitResults,LowestError,BestStart,xi,yi]=peakfit(smatrix,.4,.7,2,1,0,10)

As above, returns the vector x1 containing 100 interploated x-values for the model peaks and the matrix y1 containing the y values of each model peak at each xi. Type plot(xi,yi(1,:)) to plot peak 1 or plot(xi,yi) to plot all peaks.
 
  For more details, see
  http://terpconnect.umd.edu/~toh/spectrum/CurveFittingC.html and
  http://terpconnect.umd.edu/~toh/spectrum/InteractivePeakFitter.htm

Acknowledgements

The author wishes to acknowledge the following in the creation of this submission:
Interactive Peak Fitter (Version 7.1)

MATLAB release MATLAB 7.8 (R2009a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (23)
21 Mar 2010 Jake

Hi can someone help:
(I am a complete Matlab novice), I have my matrix 'signal' arranged in two columns, x ranges non uniformly from 0 to 1 and y starts at zero has a small peak on the start tail of the main peak (around10% size of main), I use peakfit(signal) and it throws up these errors:

??? Error using ==> optimset>checkfield at 310
Invalid value for OPTIONS parameter TypicalX: must be a matrix.

Error in ==> optimset at 248
        checkfield(Names{j,:},arg,optimtbx);

Error in ==> peakfit at 155
options = optimset('TolX',.001,'TypicalX',center,'Display','off' );

and I have no idea why?

thanks for any help!

22 Mar 2010 Tom O'Haver

Try turning your 'signal" matrix into its transpose, signal'

10 May 2010 Joe K

Wonderful program. Thank you very much for posting it Tom. My question is, how do I extract the data points (x,y) for the newly split peaks ?

19 Nov 2010 MB46

This program is fantastic. However, I've run into a slight problem. When fitting multiple gaussians, I want to be able to put in a condition requiring the width of all the gaussians to be the same. Is there an easy way to do this? And indeed to restrict some of the other fitting parameters?

14 Apr 2011 Tom O'Haver

Joe K, I've just uploaded an updated version that includes the option to extract the data points (x,y) for the newly split peaks. See http://terpconnect.umd.edu/~toh/spectrum/peakfit.m

16 Apr 2011 Tom O'Haver

I've added a demo script for this function. It's described on http://terpconnect.umd.edu/~toh/spectrum/InteractivePeakFitter.htm

19 Apr 2011 Victor Zhou

Is it require any toolbox to run the program?

19 Apr 2011 Tom O'Haver

No, no toolboxes required.

26 Apr 2011 Tom O'Haver

As suggested by MB46, the latest version (1.4) allows specifying that the widths of all gaussians be the same (select peak shape 6), and also the same for lorentzian peak fits (peak shape 7).

24 Jul 2011 John Bowen

This seems to use a parameter no longer supported in Matlab R2010B.. I get this erorr message:
??? Error using ==> optimset at 204
Unrecognized parameter name 'TypicalX'. Please see the optimset reference page in the documentation for a list of acceptable option parameters. Link to
reference page.

24 Jul 2011 Tom O'Haver

Sorry about that! It works fine in R2009a. I'll check it out and see if I can come up with something that works in both versions.

25 Jul 2011 Tom O'Haver

The latest version peakfit 1.6 removes the offending 'TypicalX', seemingly with no ill effects. There are also some other small changes to the format of the peak table on the figure. See http://terpconnect.umd.edu/~toh/spectrum/InteractivePeakFitter.htm

10 Aug 2011 Tom O'Haver

Version 1.8 takes AUTOZERO setting as 9th input argument (O=OFF, 1=ON), and it has improved data input argument flexibility.

31 Aug 2011 mohammad

how can use this for a matrix file? ( .xls file)
thanks

31 Aug 2011 Tom O'Haver

Load the .xls file into the Matlab workspace using File > Import... command. Then write a loop that goes through the matrix one row (or column) at a time and passes it to peakfit.m

12 Sep 2011 Sadik

Good job!

01 Nov 2011 Sahrul Hidayat

nice program. thanks for sharing

03 Nov 2011 AM

With Matlab R2009b I am getting an error even when I try example 1.

??? Input argument "n" is undefined.
Error in ==> linspace at 23
x = x2fx((0:n-2)');
Error in ==> peakfit at 392
xxx=linspace(min(xx),max(xx));

I tried this on Matlab 2008b and got the same errors. Not sure what I am missing.
Thanks for any help!
Would love to try this

04 Nov 2011 Tom O'Haver

AM, I think you are using the wrong function. In the current version of my peakfit.m (version 2.2), line 23 is a comment line. There is no line reading "x = x2fx((0:n-2)');" in any of my code. Download a fresh copy of the current version and try again.

04 Nov 2011 Tom O'Haver

AM, I just checked it, and these examples are working just fine on my system (Matlab 7.8 R2009a). This error is thrown by the built-in "linspace" function, not by my peakfit.m function. I'm at a loss to understand what is going on on your system. Could there be that much difference between R2009a and R2009b?

04 Nov 2011 AM

I read your last comment first, and check my linspace function and it does not have a default value of 100 points between values if non is specified. So I just put it in as 100 and it works! Not sure why my linspace is different. But I will also download the most current version as well.
Thanks!

06 Jan 2012 Bogdan Dzyubak

Does a pretty good job. May require zero padding to work if one of your peaks is close to the edge of data. In my case it would only fit one peak no matter how many I specified until I zero padded around my data.
I briefly tried using window+center for the same effect but didn't get the same result.

07 Jan 2012 Tom O'Haver

Thanks for the careful evaluation and feedback, Bogdan. I'll look into the "close to the edge" problem and see if I can either bypass the problem or add zero padding as an option.

Please login to add a comment or rating.
Updates
14 Apr 2009

Bug fixes. No major features added.

27 Apr 2009

Expanded description

13 Apr 2011

Added ability to access the x-y data for the individual fitted peaks, via the optional output parameters xi and yi.

25 Apr 2011

Added two new peakshapes: 6=Equal-width Gaussians, and 7=Equal-width Lorentzians.

04 May 2011

Expanded the description

25 Jul 2011

Several bug fixes. Reformatted peak table on graph.

10 Aug 2011

Version 1.8: Aug, 2011. Takes AUTOZERO setting as 9th input argument; improved data input argument flexibility.

24 Aug 2011

Bug fix

28 Sep 2011

Version 2.1: Sept, 2011. Accepts AUTOZERO 0 (none), 1 (linear), or 2 (quadratic).

20 Oct 2011

Version 2.2: October, 2011. Adds exponential pulse and sigmoid models

19 Jan 2012

Version 2.3: January, 2012. Bug fixes in background subtraction modes and
in handlng very small data sets.

19 Jan 2012

Version 2.3: January, 2012. Bug fixes in background subtraction modes and
in handlng very small data sets.

Tag Activity for this File
Tag Applied By Date/Time
chemistry Tom O'Haver 09 Apr 2009 12:27:47
curve fitting Tom O'Haver 09 Apr 2009 12:27:47
decomposition Tom O'Haver 09 Apr 2009 12:27:47
peak fitting Tom O'Haver 09 Apr 2009 12:27:47
physics Tom O'Haver 09 Apr 2009 12:27:47
time series Tom O'Haver 09 Apr 2009 12:27:47
signal processing Tom O'Haver 09 Apr 2009 12:27:47
optimization Tom O'Haver 09 Apr 2009 12:27:47
measurement Tom O'Haver 09 Apr 2009 12:27:47
statistics Tom O'Haver 09 Apr 2009 12:27:47
deconvolution Tom O'Haver 09 Apr 2009 12:27:47
resolution Tom O'Haver 09 Apr 2009 12:27:47

Contact us at files@mathworks.com