I believe I am successfully implementing gaussian fitting using curve fit from scipy. But the problem that I am running into is that... the fit isn't so great, because the optimized parameter is changing the centroid.
data =np.loadtxt('mock.txt')
my_x=data[:,0]
my_y=data[:,1]
def gauss(x,mu,sigma,A):
return A*np.exp(-(x-mu)**2/2/sigma**2)
def trimodal_gauss(x,mu1,sigma1,A1,mu2,sigma2,A2,mu3,sigma3,A3):
return gauss(x,mu1,sigma1,A1)+gauss(x,mu2,sigma2,A2)+gauss(x,mu3,sigma3,A3)
"""""
Gaussian fitting parameters recognized in each file
"""""
first_centroid=(10180.4*2+9)/9
second_centroid=(10180.4*2+(58.6934*1)+7)/9
third_centroid=(10180.4*2+(58.6934*2)+5)/9
centroid=[]
centroid+=(first_centroid,second_centroid,third_centroid)
apparent_resolving_power=1200
sigma=[]
for i in range(len(centroid)):
sigma.append(centroid[i]/((apparent_resolving_power)*2.355))
height=[1,1,1]
p=[]
p = np.array([list(t) for t in zip(centroid, sigma, height)]).flatten()
popt, pcov = curve_fit(trimodal_gauss,my_x,my_y,p0=p)
I understand that there are a lot of peaks here but I really need it to fit only three Gaussians but at the right centroid(given in my initial guess). In other words, I really don't hope that the centroid I give is not changing. Has anyone encountered a challenge as such? and could please help me what I could do to make it happen?