双高斯拟合与公共质心 Python

Double gaussian fit with common centroid Python

我正在尝试使用 scipy.optimization.curve_fit:

用双高斯函数拟合一些数据

Double gaussian fit with two centroids

def _2gaussian(x, amp1, cen1, sigma1, amp2, cen2, sigma2):
    g1 = amp1 * (1 / (sigma1 * (np.sqrt(2 * np.pi)))) * (np.exp((-1.0 / 2.0) * (((x - cen1) / sigma1) ** 2)))
    g2 = amp2 * (1 / (sigma2 * (np.sqrt(2 * np.pi)))) * (np.exp((-1.0 / 2.0) * (((x - cen2) / sigma2) ** 2)))
    return g1 + g2  # + cen2

所以我的问题是:我的数据非常对称,我试图对两个高斯函数使用带有 common/shared 质心的双高斯拟合。我试图在前面的公式中写 cen1 而不是 cen2 (并在参数中保留 cen2 )或者也只是完全消除 cen2 作为参数。这些都不起作用,因此获得的拟合只是一条平坦的曲线,中间有一个大尖峰。您对如何只拥有共享质心有什么建议吗?

Double gaussian with cen1 only

数据中的最低点从 y = 4000 左右开始。但是您的高斯函数没有偏移项,因此它始终从 y = 0 开始。您需要对数据进行归一化,或者添加一个偏移量,例如这个:

def _2gaussian(x, amp1, cen1, sigma1, amp2, cen2, sigma2, offset):
    g1 = amp1 * (1 / (sigma1 * (np.sqrt(2 * np.pi)))) * (np.exp((-1.0 / 2.0) * (((x - cen1) / sigma1) ** 2)))
    g2 = amp2 * (1 / (sigma2 * (np.sqrt(2 * np.pi)))) * (np.exp((-1.0 / 2.0) * (((x - cen2) / sigma2) ** 2)))
    return g1 + g2  + offset # + cen2

那么我强烈建议在 curve_fit 中使用 p0 参数,这样您就可以为拟合函数中的每个参数指定一个初始猜测值。在您的情况下,您对 offset 的猜测将是 4000。