0

I am trying to learn gaussian fitting using scipy and least-squares in Python.

I have a further question relates the answer to this question How to fit a double Gaussian distribution in Python?,

From the code of the answer, how can I estimate the parameters c1, mu1, sigma1, c2, mu2, sigma2

in params = [c1, mu1, sigma1, c2, mu2, sigma2], since I want to use leastsq?

  • The answer says "Starting values found by inspection", so you could just look at your data. Otherwise you could use random values or maybe the quantiles of the data. – ForceBru May 05 '21 at 21:03
  • You could also check out `lmfit` which provides differential evolution or guess the parameters with differential evolution as done in the [SO example](https://stackoverflow.com/a/54380367/803359) – mikuszefski May 06 '21 at 06:10

1 Answers1

0

Hope this answer will not be too late.. There's a simple way to estimate those parameters actually.

There it is how I do it in my own code for gaussian fitting on spectra :

#Détermination des paramètres initiaux
            mu0, m = xdata[roiDeb], ydata[roiDeb]
            for j in range(roiDeb+1, roiFin+1) :
                if ydata[j] > m:
                    mu0 = xdata[j]
                    m = ydata[j]
            h0 = m
            fwhmd, fwhmf, sigma0 = ydata[roiDeb], ydata[roiFin+1], 0
            for j in range(roiDeb, mu0+1) :
                if ydata[j] > h0/2 :
                    fwhmd = j
                    break
            for j in range(mu0, roiFin+1) :
                if ydata[j] < h0/2 :
                    fwhmf = j
                    break
            sigma0 = (fwhmf-fwhmd)/2.355

To determinate the centroid you can just do a if condition and check for the higher y value in your region of interest.

Then you can calculate the full width at half maximum (FWHM) at both sides.

To finish the formula : sigma = FWHM / 2.355 can be demonstrate simply (or can be find on internet)

I let you discover by yourself how to use those values to do a gaussian fit...