python - 查找峰宽

标签 python numpy scipy

我有一个数据集,我试图在该数据集上检测峰值和这些峰值的左/右边界。

我成功地使用了 scipy find_peaks_cwt找到峰值,但我不知道如何识别这些峰值的左右边界。

在下图中,峰上的线和圆标记是通过编程方式绘制的,但我现在正尝试通过编程方式绘制方形标记。

enter image description here

关于如何做到这一点有什么建议吗?我考虑过搜索拐点,但由于数据中存在噪音,这行不通。

最佳答案

如果高斯拟合合适,您可以采用找到的峰数并拟合 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/

相关文章:

python - 在Python 2.7中打印X型图案

python - 是否有 python 2.6 附带的任何 linux 发行版?

python - numpy 中的行专业和列专业是什么?

numpy - 将图像从 RGB 转换为 HSV 颜色空间

python - Numpy转置乘法问题

python - QWebView : is it possible to highlight terms and do keyboard navigation?

python - 子类 QWebView 对超链接点击没有反应

python - 如何设置嵌套 numpy ndarray 的 dtype?

python - NumPy - 将值环绕任意边界

python - 从 numpy 中的一维数组创建方形 nxn 矩阵的最快方法