简体   繁体   English

如何在存在线性背景和scipy的情况下执行高斯曲线拟合?

[英]How do I perform a gaussian curve fit in the presence of a linear background with scipy?

I have the given data set: 我有给定的数据集:

在此输入图像描述

Of which I would like to fit a Gaussian curve at the point where the red arrow is directed towards. 其中我想在红色箭头指向的点处拟合高斯曲线。 I have attempted to do so by restricting the data points to a range of channels close to the peak, using scipy.optimize.curve_fit and a gaussian function to obtain the fit as shown below. 我试图通过将数据点限制在接近峰值的通道范围内,使用scipy.optimize.curve_fitgaussian函数来获得拟合,如下所示。

在此输入图像描述

This method, however, does not take into account the slope of the background noise of the data points. 然而,该方法没有考虑数据点的背景噪声的斜率。 Thus affecting the accuracy of the position of the peak of the fitted curve by the above-mentioned method. 因此,通过上述方法影响拟合曲线的峰值位置的精度。

I would like to take into account this background slope. 我想考虑这个背景斜率。 How do I go about doing so in python? 我如何在python中这样做?

You have to somehow model the background and the Gaussian peak, and perhaps any other peaks in the spectrum. 你必须以某种方式模拟背景和高斯峰值,以及光谱中的任何其他峰值。 Your background looks to be roughly 1/x (or some other power of x ), but it might also be exponential. 你的背景看起来是大约1/x (或其他一些功率x ),但它也可能是指数。 You may know this, or you may find that plotting on a semi-log plot can help decide which of these forms is better. 您可能知道这一点,或者您可能会发现在半对数图上绘图可以帮助确定哪些形式更好。

To fit the background and Gaussian with curve_fit , you would have to write a model function that modeled both. 要使用curve_fit来拟合背景和高斯,你必须编写一个模拟两者的模型函数。 Allow me to recommend using lmfit ( http://lmfit.github.io/lmfit-py/ ) as it has several built-in models and can help you compose a model of several different line shapes. 请允许我建议使用lmfit( http://lmfit.github.io/lmfit-py/ ),因为它有几个内置模型,可以帮助您组成几种不同线形的模型。 An example that might be helpful for your problem is at ( http://lmfit.github.io/lmfit-py/builtin_models.html#example-3-fitting-multiple-peaks-and-using-prefixes ). 可能对您的问题有帮助的示例是( http://lmfit.github.io/lmfit-py/builtin_models.html#example-3-fitting-multiple-peaks-and-using-prefixes )。

A script to fit your data might look like 适合您数据的脚本可能看起来像

import numpy as np
from lmfit.models import PowerLawModel, ExponentialModel, GaussianModel

# make models for individual components
mod_expon = ExponentialModel(prefix='exp_')
mod_gauss = GaussianModel(prefix='g1_')

# sum components to make a composite model (add more if needed)
model  = mod_expon + mod_gauss

# create fitting parameters by name, give initial values
params = model.make_params(g1_amplitude=5, g1_center=55, g1_sigma=5, 
                           exp_amplitude=5, exp_decay=10)

# do fit
result = model.fit(ydata, params, x=xdata)

# print out fitting statistics, best-fit parameters, uncertainties,....
print(result.fit_report())

There are many more examples in the docs, including showing how to extract and plot the individual components, and so on. 文档中还有更多示例,包括如何提取和绘制各个组件,等等。

How I would do this is to use a fit that fits to both the signal and the background. 我如何做到这一点是使用适合信号背景的拟合。 That is, fit not just a Gaussian, but a fit that is a Guassian plus a function that fits the background. 也就是说,不仅适合高斯,而且适合高斯,加上适合背景的功能。 The first approximation to your background is a linear slope, so you could use a form like a*exp(-(x-x0)**2/w**2) + m*x + c . 背景的第一个近似值是线性斜率,因此您可以使用类似a*exp(-(x-x0)**2/w**2) + m*x + c

This gives you more fitting parameters, all of which are interdependent, but if you can give them reasonable initial values then the fit normally converges well. 这为您提供了更多的拟合参数,所有参数都是相互依赖的,但是如果您可以给它们合理的初始值,那么拟合通常会很好地收敛。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM