【问题标题】:Finding width of peaks寻找峰宽
【发布时间】:2017-02-09 05:47:04
【问题描述】:

我有一个数据集,我正在尝试检测峰值以及这些峰值的左/右边界。

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

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

关于如何做到这一点的任何建议?我考虑过搜索拐点,但由于数据中的噪音,这不起作用。

【问题讨论】:

  • 拟合高斯分布是否合适?

标签: python numpy scipy


【解决方案1】:

如果适合使用高斯拟合,您可以取找到的峰值数量并拟合 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]

这应该给你一个开始的地方,但你可能需要调整优化函数来处理你的数据(或者可能根本工作,我没有测试)

【讨论】:

    猜你喜欢
    • 2015-02-10
    • 1970-01-01
    • 1970-01-01
    • 2021-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-26
    • 1970-01-01
    相关资源
    最近更新 更多