不久前,我也遇到了同样的问题,我发现这个函数运行得很好。这是一个Matlab等价物,试试看,告诉我们它是否对你有用。密码不是我的。在# %load ./../functions/detect_peaks.py

"""Detect peaks in data based on their amplitude and other features."""

from __future__ import division, print_function

import numpy as np

__author__ = "Marcos Duarte, https://github.com/demotu/BMC"

__version__ = "1.0.4"

__license__ = "MIT"

def detect_peaks(x, mph=None, mpd=1, threshold=0, edge='rising',

kpsh=False, valley=False, show=False, ax=None):

"""Detect peaks in data based on their amplitude and other features.

Parameters

x : 1D array_like

data.

mph : {None, number}, optional (default = None)

detect peaks that are greater than minimum peak height.

mpd : positive integer, optional (default = 1)

detect peaks that are at least separated by minimum peak distance (in

number of data).

threshold : positive number, optional (default = 0)

detect peaks (valleys) that are greater (smaller) than `threshold`

in relation to their immediate neighbors.

edge : {None, 'rising', 'falling', 'both'}, optional (default = 'rising')

for a flat peak, keep only the rising edge ('rising'), only the

falling edge ('falling'), both edges ('both'), or don't detect a

flat peak (None).

kpsh : bool, optional (default = False)

keep peaks with same height even if they are closer than `mpd`.

valley : bool, optional (default = False)

if True (1), detect valleys (local minima) instead of peaks.

show : bool, optional (default = False)

if True (1), plot data in matplotlib figure.

ax : a matplotlib.axes.Axes instance, optional (default = None).

Returns

-

ind : 1D array_like

indeces of the peaks in `x`.

Notes

-

The detection of valleys instead of peaks is performed internally by simply

negating the data: `ind_valleys = detect_peaks(-x)`

The function can handle NaN's

See this IPython Notebook [1]_.

References

.. [1] http://nbviewer.ipython.org/github/demotu/BMC/blob/master/notebooks/DetectPeaks.ipynb

Examples

>>> from detect_peaks import detect_peaks

>>> x = np.random.randn(100)

>>> x[60:81] = np.nan

>>> # detect all peaks and plot data

>>> ind = detect_peaks(x, show=True)

>>> print(ind)

>>> x = np.sin(2*np.pi*5*np.linspace(0, 1, 200)) + np.random.randn(200)/5

>>> # set minimum peak height = 0 and minimum peak distance = 20

>>> detect_peaks(x, mph=0, mpd=20, show=True)

>>> x = [0, 1, 0, 2, 0, 3, 0, 2, 0, 1, 0]

>>> # set minimum peak distance = 2

>>> detect_peaks(x, mpd=2, show=True)

>>> x = np.sin(2*np.pi*5*np.linspace(0, 1, 200)) + np.random.randn(200)/5

>>> # detection of valleys instead of peaks

>>> detect_peaks(x, mph=0, mpd=20, valley=True, show=True)

>>> x = [0, 1, 1, 0, 1, 1, 0]

>>> # detect both edges

>>> detect_peaks(x, edge='both', show=True)

>>> x = [-2, 1, -2, 2, 1, 1, 3, 0]

>>> # set threshold = 2

>>> detect_peaks(x, threshold = 2, show=True)

"""

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]

# remove peaks < minimum peak height

if ind.size and mph is not None:

ind = ind[x[ind] >= mph]

# remove peaks - neighbors < threshold

if ind.size and threshold > 0:

dx = np.min(np.vstack([x[ind]-x[ind-1], x[ind]-x[ind+1]]), axis=0)

ind = np.delete(ind, np.where(dx < threshold)[0])

# detect small peaks closer than minimum peak distance

if ind.size and mpd > 1:

ind = ind[np.argsort(x[ind])][::-1] # sort ind by peak height

idel = np.zeros(ind.size, dtype=bool)

for i in range(ind.size):

if not idel[i]:

# keep peaks with the same height if kpsh is True

idel = idel | (ind >= ind[i] - mpd) & (ind <= ind[i] + mpd) \

& (x[ind[i]] > x[ind] if kpsh else True)

idel[i] = 0 # Keep current peak

# remove the small peaks and sort back the indices by their occurrence

ind = np.sort(ind[~idel])

if show:

if indnan.size:

x[indnan] = np.nan

if valley:

x = -x

_plot(x, mph, mpd, threshold, edge, valley, ax, ind)

return ind

def _plot(x, mph, mpd, threshold, edge, valley, ax, ind):

"""Plot results of the detect_peaks function, see its help."""

try:

import matplotlib.pyplot as plt

except ImportError:

print('matplotlib is not available.')

else:

if ax is None:

_, ax = plt.subplots(1, 1, figsize=(8, 4))

ax.plot(x, 'b', lw=1)

if ind.size:

label = 'valley' if valley else 'peak'

label = label + 's' if ind.size > 1 else label

ax.plot(ind, x[ind], '+', mfc=None, mec='r', mew=2, ms=8,

label='%d %s' % (ind.size, label))

ax.legend(loc='best', framealpha=.5, numpoints=1)

ax.set_xlim(-.02*x.size, x.size*1.02-1)

ymin, ymax = x[np.isfinite(x)].min(), x[np.isfinite(x)].max()

yrange = ymax - ymin if ymax > ymin else 1

ax.set_ylim(ymin - 0.1*yrange, ymax + 0.1*yrange)

ax.set_xlabel('Data #', fontsize=14)

ax.set_ylabel('Amplitude', fontsize=14)

mode = 'Valley detection' if valley else 'Peak detection'

ax.set_title("%s (mph=%s, mpd=%d, threshold=%s, edge='%s')"

% (mode, str(mph), mpd, str(threshold), edge))

# plt.grid()

plt.show()

pantompkins matlab,Matlab对Python的findpeaks算法相关推荐

  1. 凸包计算几何matlab,計算幾何-凸包算法 Python實現與Matlab動畫演示

    凸包算法是計算幾何中的最經典問題之一了.給定一個點集,計算其凸包.凸包是什么就不羅嗦了 本文給出了<計算幾何--算法與應用>中一書所列凸包算法的Python實現和Matlab實現,並給出了 ...

  2. Python 机器学习——线性代数和矩阵运算:从matlab迁移到python

    诚然,没有一门语言能够撼动matlab的矩阵或科学计算在学术圈的地位,因其简洁的语法(matrix是其基本数据类型),因其矩阵运算的便捷,因其术业有专攻(matlab:为科学计算而生),因其名字mat ...

  3. TensorFlow1.14或TensorFlow2内部获取mfcc原理探索(matlab复现或python复现)

    MFCC概述 声音是因为物体振动而产生的声波,是可以被人或动物的听觉器官所感知的波动现象.声音有多种特性,比如音色.音调.响度.频率.人耳是能够通过这些特征区分出声音的种类,那么如何让机器去区别不同种 ...

  4. (Matlab实现)蚂蚁狮子优化算法在电力系统中的应用

    目录 1 知识一网打尽 2 蚂蚁狮子优化算法在电力系统经济调度中的应用 3 运行结果 4 Matlab代码实现 1 知识一网打尽 这里总结一位博主的电力系统经济调度目录 蚂蚁狮子优化算法(完整Matl ...

  5. 数学建模matlab和python_参加数学建模用 MATLAB,还是 Python?

    你应该学习吉他还是钢琴?你应该学习足球还是篮球?你应该学习化学还是物理学?这些问题的答案取决于你的兴趣点在哪里,方向选择是什么,而后再说选什么 :让我们借助比较通俗的方式切入,来讨论Matlab和Py ...

  6. 基于MATLAB的RSSI 和 PLE 定位算法,并通过卡尔曼滤波器减少非视距误差

    基于MATLAB的RSSI 和 PLE 定位算法,并通过卡尔曼滤波器减少非视距误差 根据上面的课题要求,我们知道在室内未知信道环境下,进行RSS估计效果较差,而本课题所要求的是在室内未知PL的情况下进 ...

  7. 基于matlab的捷联惯导算法设计及仿真,基于 Matlab 的捷联惯导算法设计及仿真1doc.doc...

    基于 Matlab 的捷联惯导算法设计及仿真1doc 基于 Matlab 的捷联惯导算法设计及仿真1 严恭敏 西北工业大学航海学院,西安 (710072) E-mail:yangongmin@163. ...

  8. m 文件 dll matlab 中调用_如何在matlab中调用python程序

    现在python很火,很多代码都是python写的,如果你和我一样,习惯了使用matlab,还想在matlab中调用Python的代码,应该怎么办呢?其中一条思路:首先在matlab中调用系统脚本命令 ...

  9. php调用python绘图程序_如何在matlab中调用python程序

    现在python很火,很多代码都是python写的,如果你和我一样,习惯了使用matlab,还想在matlab中调用Python的代码,应该怎么办呢?其中一条思路:首先在matlab中调用系统脚本命令 ...

  10. matlab+信号+mpf,Python和Matlab中平均频率的差异

    我有这个EMG signal,我想根据这个article绘制平均功率频率.我使用以下代码在Matlab中实现它:clear all; close all; EMG=load('EMG.txt'); N ...

最新文章

  1. oracle 导入流程,Oracle数据库(旧)空间数据表创建与数据导入流程说明V2.0
  2. Java小结(四)——折半查找、选择排序、冒泡排序
  3. ImageWatch的使用
  4. 自然语言处理的一些链接
  5. 基于OpenCL的mean filter性能
  6. sqlserver 微信 读取_Sql Server使用链接服务器远程取数据_sqlserver
  7. 【面向对象】面向对象的分析与设计概述
  8. 判断当前用户是否为root
  9. java string sscanf_倾情奉献——JAVA sscanf函数!!!
  10. 会动的图解 (二) 怎么让goroutine跑一半就退出?
  11. mybatis配置 SqlMapConfig.xml user.xml
  12. 基于 TCP Socket 的服务器与客户端的简单连接
  13. (0.2.2)如何下载mysql数据库(二进制、RPM、源码、YUM源)
  14. mysql group by 分组查询
  15. 谈一谈Normalize.css
  16. cubemx stm32 陶晶驰 串口屏 基于YXY通信原理的串口屏驱动代码
  17. 医学影像分割论文合集
  18. 在eclipse中查看jar包
  19. Windows 10/11 官方下载工具 镜像制作U盘启动盘 快速安装
  20. Ubuntu的网络共享

热门文章

  1. ARM公司为何如此成功
  2. php里用钢笔画曲线,ps钢笔工具怎么抠图
  3. 使用 Moment.js 吧时间戳生成格式化时间
  4. 总结 27 类深度学习主要神经网络:结构图及应用
  5. 物联网开发笔记(5)- 使用Wokwi仿真树莓派Pico实现LED灯交替闪烁(续)
  6. XFTP中文目录乱码
  7. ArcGIS中.shp矢量文件和.lyr图层文件的区别
  8. eclipse/Myeclipse注释模板修改
  9. CryEngine5.3 问题
  10. 2022年G1工业锅炉司炉考试试题及答案