python 峰_python-如何在具有多个峰的数据集中查找FWH...

这篇博客介绍了一个Python脚本,用于在1D数据轮廓中检测峰值(或谷底),并计算其半高宽。脚本包含`detect_peaks`函数,该函数考虑了不同边缘条件、最小/最大峰值高度限制,并能处理NaN值。通过调整参数,用户可以应用于特定的数据集来寻找和分析峰值。
摘要由CSDN通过智能技术生成

我正在使用Python脚本,该脚本在特定值范围内沿一维轮廓查找峰(在我的情况下为谷)的索引.我的目的是测量每个感兴趣谷的半高宽.

This is an example of 1D profile

这是脚本:

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

如何执行此操作?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值