如果适合使用高斯拟合,您可以取找到的峰值数量并拟合 n 个高斯分布(每个峰值 1 个)加上背景噪声的常数变量(或者您可以在曲线中添加另一个高斯分布适合
import numpy as np
from scipy.optimize import curve_fit
from scipy.signal import find_peaks_cwt
from math import *
peak_indices = find_peaks_cwt(data, *args)
#make a fitting function that takes x number of peak widths
def makeFunction(indices, data):
def fitFunction(x, *args):
#sum of gaussian functions with centers at peak_indices and heights at data[peak_indices] plus a constant for background noise (args[-1])
return sum([data[indices[i]]*exp(-((x-indices[i])**2)/(2*args[i]**2)) for i in range(len(peak_indices))])+args[-1] #does my code golfing show? xD
return fitFunction
f = makeFunction(peak_indices, data)
#you must provide the initial guess of "np.ones(len(peak_indices)+1)" for the parameters, because f(x, *args) will otherwise take a variable number of arguments.
popt, pcov = curve_fit(f, np.arange(len(data)), data, np.ones(len(peak_indices)+1))
#standard deviations (widths) of each gaussian peak and the average of the background noise
stdevs, background = popt[:-1], popt[-1]
#covariance of result variables
stdevcov, bgcov = pcov[:-1], pcov[-1]
这应该给你一个开始的地方,但你可能需要调整优化函数来处理你的数据(或者可能根本工作,我没有测试)