【发布时间】:2016-10-06 00:49:20
【问题描述】:
我正在编写一个脚本,使用 Scipy、Numpy 和 Matplotlib 在 Python 中将峰形拟合到光谱数据。它可以一次拟合多个峰。峰值轮廓(目前)是 Pseudo-Voigt,它是高斯(又名正态)和洛伦兹(又名 Cauchy)分布的线性组合。
我有一个选项开关,可以让软件优化高斯和洛伦兹的贡献,也可以将其设置为固定值(其中 0 = 纯高斯,1 = 纯洛伦兹)。正常工作,绘制拟合的峰值看起来符合预期。当我尝试使用scipy.integrate 计算峰值的积分时,问题就开始了。
到目前为止,我尝试了 scipy.integrate.quad、scipy.integrate.quadrature、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
这是绘图函数通过以下方式调用的:
Fit = PseudoVoigtFunction(WavNr, Pos, Amp, GammaL, FracL)
效果很好。积分器也通过以下方式调用它:
PeakArea, PeakAreaError = integrate.quad(PseudoVoigtFunction, -np.inf, np.inf, args=(Pos, Amp, GammaL, FracL))
或 scipy.integrate 提供的任何其他变体,都具有相同的结果,如果 FracL = 0,则 PeakArea =(几乎)0。
我确定问题是我太愚蠢了,无法弄清楚 scipy.integrate 如何使用比我找不到示例的稍微复杂的函数工作。希望有人看到我没有看到的明显错误。两天的搜索 stackoverflow 和 Scipy Docs 以及重新排列和完全重写我的代码让我一无所获。我怀疑 scipy.integrate 中的参数在某种程度上与问题有关,但据我所知,它们似乎排列正确。
提前致谢, 操作系统
【问题讨论】:
标签: python numpy scipy data-fitting integrate