Lmfit对于您的情况似乎是一个不错的选择。下面的代码模拟添加了线性背景的高斯峰,并说明如何使用lmfit提取参数。后者具有许多其他易于集成的内置模型(Lorentzian,Voight等)。
import numpy as np
from lmfit.models import Model,LinearModel
from lmfit.models import GaussianModel,LorentzianModel
import matplotlib.pyplot as plt
def generate_gaussian(amp,mu,sigma_sq,slope=0,const=0):
x = np.linspace(mu-10*sigma_sq,mu+10*sigma_sq,num=200)
y_gauss = (amp/np.sqrt(2*np.pi*sigma_sq))*np.exp(-0.5*(x-mu)**2/sigma_sq)
y_linear = slope*x + const
y = y_gauss + y_linear
return x,y
# Gaussiand peak generation
amplitude = 6
center = 3884
variance = 4
slope = 0
intercept = 0.05
x,y = generate_gaussian(amplitude,center,variance,slope,intercept)
#Create a lmfit model: Gaussian peak + linear background
gaussian = GaussianModel()
background = LinearModel()
model = gaussian + background
#Find what model parameters you need to specify
print('parameter names: {}'.format(model.param_names))
print('independent variables: {}'.format(model.independent_vars))
#Model fit
result = model.fit(y,x=x,amplitude=3,center=3880,sigma=3,intercept=0.1)
y_fit = result.best_fit #the simulated intensity
result.best_values #the extracted peak parameters
# Comparison of the fitted spectrum with the original one
plt.plot(x,y,label='model spectrum')
plt.plot(x,y_fit,label='fitted spectrum')
plt.xlabel('wavelength,(angstroms')
plt.ylabel('intensity')
plt.legend()
输出:
parameter names: ['amplitude','center','sigma','slope','intercept']
independent variables: ['x']
result.best_values
Out[139]:
{'slope': 2.261379140543626e-13,'intercept': 0.04999999912168238,'amplitude': 6.000000000000174,'center': 3883.9999999999977,'sigma': 2.0000000000013993}
本文链接:https://www.f2er.com/3133978.html