我试图用两个高斯峰来拟合我的数据,但这显然是错误的。我不知道哪里错了。下面是脚本。我参考了很多不同的写作方法,但没有一个适合。我想我错过了一些东西并且没有注意到。另外,我尝试考虑均值理论。但没有任何改变。我可以使用 labview 来拟合,但不能用 python 来完成。希望能找出错误。我想知道是不是拟合的PTV或“popt”有问题。另外,我发现我的“pcov”值是无限的,这看起来很奇怪。
import numpy as np
data = np.loadtxt('C:/Users/user/Desktop/sheet1 test.txt', delimiter=',')
print(data.shape)
print(data.dtype)
import matplotlib.pyplot as plt
x = data[:, 0]
y = data[:, 1]
#fitting function
from scipy.optimize import curve_fit
#individual peak
def gauss(x,a,w,p,c):
#C = 0
fun_val = 0 + ((a/1.253*w)*np.exp(-2*((x-p)**2/w**2)))
return fun_val
#double peak
def d_gauss(x, a1, p1,a2, p2):
#w1 = 0.049
#w2 = 0.029
#c1 = c2 = 0
fun_val1 = 0 + ((a1/1.253*0.049)*np.exp(-2*((x-p1)**2/0.049**2)))
fun_val2 = 0 + ((a2/1.253*0.029)*np.exp(-2*((x-p2)**2/0.029**2)))
return fun_val1 + fun_val2
initial_gauss_peak = [1.43896,186.097, 1.4743,393.263]
#bnd = ( (lower limit ) , ( upper limit ) )
bnd = ((1.4,10 ,1.45,100) , (1.5 ,1000 ,1.52,5000) )
#fitting
popt,pcov = curve_fit(d_gauss,x,y, p0 = initial_gauss_peak, bounds = bnd)
yfit = gauss(x, popt[0], popt[1] ,popt[2], popt[3])
yfit1 = gauss(x, popt[0], 0.049 ,popt[1], 0)
yfit2 = gauss(x, popt[2], 0.029 ,popt[3], 0)
plt.plot(x,y ,'o', label = 'data')
plt.plot(x,yfit1 , label = 'peak 1' , color = 'b')
plt.plot(x,yfit2 , label = 'peak 2' , color = 'c')
plt.plot(x ,yfit ,linewidth = 2, label = 'fit' , color = 'k')
plt.legend()
#plt.plot(x, y)
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.title('Scatter Plot')
plt.show()