我有一个数据集,我试图在该数据集上检测峰值和这些峰值的左/右边界。
我成功地使用了 scipy find_peaks_cwt
找到峰值,但我不知道如何识别这些峰值的左右边界。
在下图中,峰上的线和圆标记是通过编程方式绘制的,但我现在正尝试通过编程方式绘制方形标记。
关于如何做到这一点有什么建议吗?我考虑过搜索拐点,但由于数据中存在噪音,这行不通。
最佳答案
如果高斯拟合合适,您可以采用找到的峰数并拟合 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]
这应该为您提供了一个起点,但您可能需要调整优化函数以处理您的数据(或者可能根本无法工作,我没有测试)
关于python - 查找峰宽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39795062/