1

I used the Gaussian fit with 3 gauss to adjust but datai but I utility data that sometimes my curve contains only two Gaussians in it not find the parameter remnants to use and but great an error is what there is a method that but allows to change with curve fit function use if two or three gaussians .

for my function main, i have this code :

FitGWPS = mainCurveFitGWPS(global_ws, period, All_Max_GWPS, DoupleDip)

and my code for fit is :

import numpy as np
from scipy.optimize import curve_fit

#Functions-----------------------------------------

#Gaussian function
def _1gaus(X,C,X_mean,sigma):
    return C*np.exp(-(X-X_mean)**2/(2*sigma**2))

def _3gaus(x, amp1,cen1,sigma1, amp2,cen2,sigma2, amp3,cen3,sigma3):
    return amp1*np.exp(-(x-cen1)**2/(2*sigma1**2)) +\
            amp2*np.exp(-(x-cen2)**2/(2*sigma2**2)) + amp3*np.exp(-(x- 
             cen3)**2/(2*sigma3**2))

def ParamFit (Gws, P, Max, popt_Firstgauss):
#Calculating the Lorentzian PDF values given Gaussian parameters and random variableX
    width=0

    Amp = []
    cen = []
    wid = []
    for j in range(len(Max-1)):
        Amp.append(0.8 * (Gws[Max[j]])) # Amplitude
        cen.append(P[Max[j]])  # Frequency
        if j == 0 : wid.append(0.3 + width * 2.)  # Width
        else : wid.append(0.3 +  popt_Firstgauss[2] * 2.)
        return Amp,wid,cen

def mainCurveFitGWPS(global_ws_in, period_in, All_Max_GWPS, DoupleDip):

#Calculating the Gaussian PDF values given Gaussian parameters and random variable X
# For the first fit we calculate with function of the max values

    mean = sum(period_in*(global_ws_in))/sum((global_ws_in ))                  
    sigma = np.sqrt(sum((global_ws_in)*(period_in-mean)**2)/sum((global_ws_in)))
    Cst = 1 / ( 2* np.pi * sigma)
    width=0
    Amp =  0.8 * (global_ws_in[All_Max_GWPS[0]]) # Amplitude
    cen = period_in[All_Max_GWPS[0]]  # Frequency
    wid = 0.3 + width * 2. #Width

    Amp = []
    cen = []
    wid = []
    for j in range(len(All_Max_GWPS-1)):
        Amp.append(0.8 * (global_ws_in[All_Max_GWPS[j]])) # Amplitude
        cen.append(period_in[All_Max_GWPS[j]])  # Frequency
        if j == 0 : wid.append(0.3 + width * 2.)
        else : wid.append(0.3 +  popt_gauss[2] * 2.)

    #do the fit!
    popt_gauss, pcov_gauss = curve_fit(_1gaus, period_in, global_ws_in, p0 = [Cst, 
                                        mean, sigma]) 
    FitGauss = _1gaus(period_in, *popt_gauss)

    #I use the center, amplitude, and sigma values which I used to create the fake 
    #data

    popt_3gauss, pcov_3gauss = curve_fit(_3gaus, period_in, global_ws_in, p0=[Amp[0], 
    cen[0], wid[0],Amp[1], cen[1], wid[1],Amp[2], cen[2], wid[2]], maxfev =5000)

    Fit3Gauss = _3gaus(period_in, *popt_3gauss)

    return Fit3Gauss

for example picture : enter image description here

and enter image description here

Vitalizzare
  • 4,496
  • 7
  • 13
  • 32

0 Answers0