我正在用Python编写一个脚本,用Scipy、Numpy和Matplotlib将峰形状拟合到光谱数据中。它可以同时拟合多个峰值。峰值分布(目前)是伪Voigt,它是高斯(又名正态)和洛伦兹(aka Cauchy)分布的线性组合。在
我有一个选项开关,可以让软件优化高斯和洛伦兹的贡献,也可以将其设置为一个固定值(其中0=纯高斯,1=纯洛伦兹)。工作正常,绘制拟合的峰值看起来像预期的那样。当我试图用scipy.integrate
计算峰值积分时,问题就开始了。在
到目前为止我试过了scipy.integrate.quad, scipy.积分.求积, scipy.integrate.fixed_quad以及scipy.integrate.romberg公司. 当峰值为纯高斯时,积分变为类似1.73476E-34
(不总是相同的数字),即使峰值明显比相邻的非纯高斯峰面积更大,但返回10到1000阶的有限积分。以下是相关部件的外观:
# Function defining the peak functions for plotting and integration
# WavNr: Wave number, the x-axis over which shall be integrated
# Pos: Peak center position
# Amp: Amplitude of the peak
# GammaL: Gamma parameter of the Lorentzian distribution
# FracL: Fraction of Lorentzian distribution
def PseudoVoigtFunction(WavNr, Pos, Amp, GammaL, FracL):
SigmaG = GammaL / np.sqrt(2*np.log(2)) # Calculate the sigma parameter for the Gaussian distribution from GammaL (coupled in Pseudo-Voigt)
LorentzPart = Amp * (GammaL**2 / ((WavNr - Pos)**2 + GammaL**2)) # Lorentzian distribution
GaussPart = Amp * np.exp( -((WavNr - Pos)/SigmaG)**2) # Gaussian distribution
Fit = FracL * LorentzPart + (1 - FracL) * GaussPart # Linear combination of the two parts (or distributions)
return Fit
这是plot函数通过以下方式调用的:
^{pr2}$效果很好。积分器也通过以下方式调用它:
PeakArea, PeakAreaError = integrate.quad(PseudoVoigtFunction, -np.inf, np.inf, args=(Pos, Amp, GammaL, FracL))
或其他由整合,所有结果都是相同的,如果FracL=0,那么peakrea=(几乎)0。在
我肯定问题是我太蠢了,不知道怎么做整合使用比我能找到的例子稍微复杂一些的函数。希望有人能看到我没有看到的明显错误。我花了两天时间搜索stackoverflow和Scipy文档,重新排列并完全重写代码,这些都让我一无所获。我怀疑整合它们与问题有某种关系,但据我所知,它们的排列似乎是正确的。在
提前谢谢你, 操作系统
你一定知道,间隔时间很长。:)高斯衰减非常快,所以除了峰值附近的间隔,高斯在数值上无法与0区分。我怀疑
quad
根本没有看到你的巅峰。在一个简单的解决办法是将积分分成两个区间(-inf,pos)和(pos,inf)。(你的函数是关于
Pos
对称的,所以你只需要两倍于(-inf,pos)的积分。)这里有一个例子。我不知道这些参数值是否接近您使用的典型值,但它们说明了这一点。在
^{pr2}$quad
认为积分是0:相反,在(-inf,pos)和(pos,inf)上集成:
所以(-inf,inf)上的积分约为3.010767。在
相关问题 更多 >
编程相关推荐