Python 如何在具有多个峰值的数据集中找到半高宽?

Python 如何在具有多个峰值的数据集中找到半高宽?,python,image,image-processing,curve-fitting,curve,Python,Image,Image Processing,Curve Fitting,Curve,我使用的是一个Python脚本,它可以在特定的值范围内沿1D配置文件查找本例中峰值或谷值的索引。我的目标是测量每个感兴趣山谷的半高宽。 以下是脚本: def detect_peaks (x, mnph=None, mxph=None, mpd=1, threshold=0, edge='rising', kpsh=False, valley=False, show=False, ax=None): #from __future__ import division, p

我使用的是一个Python脚本,它可以在特定的值范围内沿1D配置文件查找本例中峰值或谷值的索引。我的目标是测量每个感兴趣山谷的半高宽。

以下是脚本:

def detect_peaks (x, mnph=None, mxph=None, mpd=1, threshold=0, edge='rising',
             kpsh=False, valley=False, show=False, ax=None):
#from __future__ import division, print_function
import numpy as np
x = np.atleast_1d(x).astype('float64')
if x.size < 3:
    return np.array([], dtype=int)
if valley:
    x = -x
# find indices of all peaks
dx = x[1:] - x[:-1]
# handle NaN's
indnan = np.where(np.isnan(x))[0]
if indnan.size:
    x[indnan] = np.inf
    dx[np.where(np.isnan(dx))[0]] = np.inf
ine, ire, ife = np.array([[], [], []], dtype=int)
if not edge:
    ine = np.where((np.hstack((dx, 0)) < 0) & (np.hstack((0, dx)) > 0))[0]
else:
    if edge.lower() in ['rising', 'both']:
        ire = np.where((np.hstack((dx, 0)) <= 0) & (np.hstack((0, dx)) > 0))[0]
    if edge.lower() in ['falling', 'both']:
        ife = np.where((np.hstack((dx, 0)) < 0) & (np.hstack((0, dx)) >= 0))[0]
ind = np.unique(np.hstack((ine, ire, ife)))
# handle NaN's
if ind.size and indnan.size:
    # NaN's and values close to NaN's cannot be peaks
    ind = ind[np.in1d(ind, np.unique(np.hstack((indnan, indnan-1, indnan+1))), invert=True)]
# first and last values of x cannot be peaks
if ind.size and ind[0] == 0:
    ind = ind[1:]
if ind.size and ind[-1] == x.size-1:
    ind = ind[:-1]
"""ABOUT mnph and mxph => It works just on valleys, for peaks: REMOVE the minus ("-") below in the code"""
# remove valleys < minimum peak height
if ind.size and mnph is not None:
    ind = ind[-x[ind] >= mnph]
# remove valleys > maximum peak height
if ind.size and mxph is not None:
    ind = ind[-x[ind] <= mxph]
return ind
如何执行此操作?

您可以使用。我不认为SciPy中有函数,但是SciPy中有一个函数


这是一个教程。

如果你想找到每个山谷的半高宽,我的想法是,你需要为每个山谷单独的高斯拟合。从图像打印的外观来看,这可能有点难以自动化,但如果可以将大量高斯数与手动选择的谷槽固定值混合,则这可能是问题的第一个切入点。