[SciPy-user] Peak Fitting

Christian Kristukat ckkart at hoc.net
Thu Mar 30 07:43:51 EST 2006


basvandijk at home.nl wrote:
> Hello,
> 
> I would like to integrate some data. The data has a somewhat similar form as the
> picture of the peak in [1]. I think a good function describing the data is:

The peak in  [1] actually is a gaussian, the formula is given below the graph:

amp * exp(-(x-center)**2/(2*width**2))

> I don't know much about linear algebra but I've read I can use a non linear
> least square fitting method to fit the data. Maybe I can also use others? My
> question is can I do this with Scipy and if so how?

with scipy you can do a leastsq fit like this
assuming x and y are arrays with your data:

from scipy import optimize

def residuals(amp,center,width):
    return y-(amp * exp(-(x-center)**2/(2*width**2)))

guess = [2.0, 4.5, 0.2]   # the inital guess for the parameters

out = optimize.leastsq(residuals, guess)

solution = out[0]

print solution

Regards, Christian




More information about the SciPy-User mailing list