Peakfit.m is a user-defined command window peak fitting function for Matlab or Octave, usable from a remote terminal. It is written as a self-contained function in a single m-file. (To view of download, click peakfit.m). It takes data in the form of a 2 x n matrix that has the independent variables (X-values) in row 1 and the dependent variables (Y-values) in row 2, or as a single dependent variable vector. The syntax is [FitResults, GOF, baseline, coeff, residuals, xi, yi, BootResults] = peakfit(signal, center, window, NumPeaks, peakshape, extra, NumTrials, start, BaselineMode, fixedparameters, plots, bipolar, minwidth, DELTA, clipheight)). Only the first input argument, the data matrix, is absolutely required; there are default values for all other inputs. All the input and output arguments are explained below.
You can download a ZIP file containing peakfit.m, DemoPeakFit.m, ipf.m, Demoipf.m, some sample data for testing, and a test script (testpeakfit.m or autotestpeakfit.m) that runs all the examples sequentially to test for proper operation. For a discussion of the accuracy and precision of peak parameter measurement using peakfit.m, click here.
If you have no idea where to start, you can use the Interactive Peak Fitter (ipf.m) to quickly try different fitting regions, peak shapes, numbers of peaks, baseline correction modes, number of trials, etc. When you get a good fit, you can press the "W" key to print out the command line statement for peakfit.m that will perform that fit in a single line of code, with or without graphics.
DemoPeakFit.m is a demonstration script for peakfit.m. It generates an overlapping Gaussian peak signal, adds normally-distributed noise, fits it with the peakfit.m function (in line 78), repeats this many times ("NumRepeats" in line 20), then compares the peak parameters (position, height, width, and area) of the measurements to their actual values and computes accuracy (percent error) and precision (percent relative standard deviation). You can change any of the initial values in lines 13-30. Here is a typical result for a two-peak signal with Gaussian peaks:
DemoPeakFitTime.m is a simple script that demonstrates how to apply multiple curve fits to a signal that is changing with time. The signal contains two noisy Gaussian peaks (similar to the illustration at the right) in which the peak position of the second peak increases with time and the other parameters remain constant (except for the noise). The script creates a set of 100 noisy signals (on line 5) containing two Gaussian peaks where the position of the second peak changes with time (from x=6 to 8) and the first peak remains the same. Then it fits a 2-Gaussian model to each of those signals (on line 8), stores the FitResults in a 100 x 2 x 5 matrix, displays the signals and the fits graphically with time (click to play animation), then plots the measured peak position of the two peaks vs time on line 12. Here's a real-data example with exponential pulse that varies over time.
For an example of automating the processing of multiple stored data files, see Appendix X: Batch Processing.
Which to use: iPeak, iSignal, or Peakfit? Read this comparison of all three. Or download these Matlab demos that compare iPeak.m with Peakfit.m for signals with a few peaks and signals with many peaks and that shows how to adjust iPeak to detect broad or narrow peaks. DemoPeakfitBootstrap demonstrates the ability of peakfit to compute estimates of the errors in the measured peak parameters. These are self-contained demos that include all required Matlab functions. Just place them in your path and click Run or type their name at the command prompt. Or you can download all these demos together in idemos.zip. peakfitVSfindpeaks.m performs a direct comparison of the peak parameter accuracy of findpeaks vs peakfit.
When a signal consists of lots of peaks on a highly variable background, the best approach is often to use peakfit's "center" and "window" arguments to break up the signal into segments containing smaller groups of overlapping peaks with their segments of background, isolating the peaks that do not overlap with other peaks. The reasons for this are several:
(a) peakfit.m works better if the number of variables for each fit is reduced,
(b) it's usually easier to compensate for the local background over those smaller segments,
(c) with smaller fits, you often don't need to supply starting guesses for the peak position and widths, and
(d) you can easily skip over peaks or data regions that you are not interested in
An easy way to do this is to use the interactive peak fitter ipf.m (described below) to explore various segments of the signal by panning and zooming and to try some trial fits and baseline correction settings, then press the "w" key to print out the peakfit syntax for that segment, with all its input arguments. Copy, paste, and edit the syntax for each segment as desired, then paste them into you code:
[FitResults1, GOF1] = peakfit(datamatrix, center1, window1...
[FitResults2, GOF2] = peakfit(datamatrix, center2, window2...
etc.
It is actually faster for the computer to execute a series of smaller peakfit() commands like that than a single longer one encompassing the entire data range in one go.
findpeaksfit.m is essentially a combination of findpeaks.m and peakfit.m. It uses the number of peaks found and the peak positions and widths determined by findpeaks as input for the peakfit.m function, which then fits the entire signal with the specified peak model. This combination function is more convenient that using findpeaks and peakfit separately. It yields better values that findpeaks alone, because peakfit fits the entire peak, not just the top part, and because it deals with non-Gaussian and overlapped peaks. However, it fits only those peaks that are found by findpeaks, so you will have to make sure that every peak that contributes to your signal is located by findpeaks. The syntax is
function [P,FitResults,LowestError,residuals,xi,yi] = findpeaksfit(x, y, SlopeThreshold, AmpThreshold, smoothwidth, peakgroup, smoothtype, peakshape, extra, NumTrials, BaselineMode, fixedparameters, plots)
The first seven input arguments are exactly the same as for the findpeaks.m function; if you have been using findpeaks or iPeak to find and measure peaks in your signals, you can use those same input argument values for findpeaksfit.m. The remaining six input arguments of findpeaksfit.m are for the peakfit function; if you have been using peakfit.m or ipf.m to fit peaks in your signals, you can use those same input argument values for findpeaksfit.m. Type "help findpeaksfit" for more information. This function is included in the ipf13.zip distribution.
The animation on the right was created by the demo script findpeaksfitdemo.m. It shows findpeaksfit finding and fitting the peaks in 150 signals, each of which has 1 to 3 noisy Lorentzian peaks in variable locations.
The script FindpeaksComparison.m compares the peak parameter accuracy of findpeaksG/L, findpeaksb, findpeaksb3, and findpeaksfit applied to a computer-generated signal with multiple peaks plus variable types and amounts of baseline and random noise. The last three of these functions include peak fitting equivalent to peakfit.m, in which the number of peaks and the "first guess" starting values are determined by findpeaksG/L.
Like the other Live Scripts described above, PeakFittingTool.mlx has a file browser button in line 1 and a pair of sliders in lines 4 and 5 for setting the desired segment to work on. But before opening a file, it's a good idea to temporarily de-select the "FitPeaks" check-box in line 14, then when you have set all the other controls, click it back on. That way you will avoid waiting for unnecessary curve fit operations until the appropriate settings are complete. (In challenging cases, curve fitting operations can be slow and can take several seconds in difficult cases).
1. Let's say that your shape has two iterated parameters and you are going to sacrifice the triangular shape, number 21. Open peakfit.m or ipf.m in the Matlab editor and re-write the old shape function ("triangular", located near line 3672 in ipf.m - there is one of those functions for each shape - by changing name of the function and the mathematics of the assignment statement (e.g. g = 1-(1./wid).*abs(x-pos);). You can use the same variables ('x' for the independent variable, 'pos' for peak position, 'wid' for peak width, etc.) Scale your function to have a peak height of 1.0 (e.g. after computing y as a function of x, divide by max(y)).
2. Use the search function in Matlab to find all instances of the name of the old function and replace it with the new name, checking "Wrap around" but leaving "Match case" and "Whole word" unchecked in the Search box. If you do it right, for example, all instances of "triangular" and all instances of "fittriangular' will be modified with your new name replacing "triangular". Save the result (or Save as... with a modified file name).
That's it! Your new shape will now be shape 21 (or whatever was the shape number of the old shape you sacrificed). In ipf.m, it will be activated by the same keystroke used by the old shape (e.g. Shift-T for the triangular, key number 84), and in iSignal and in iPeak, the menu of peak shapes will have been modified by the search and replace in step 2.
Contact the Authorized Rockford Fosgate Dealer you purchased this product from. If you need further assistance, call 1-800-669-9899 for Rockford Customer Service. You must obtain an RA# (Return Authorization number) to return any product to Rockford Fosgate. You are responsible for shipment of product to Rockford.
Prior to requesting an RA, try to diagnose the failure. Many products we receive are sent back with No Trouble Found, usually due to incorrect installation. Use our Knowledge Base for troubleshooting assistance.
b1e95dc632