scipy.signal.find_peaks(峰值检测)

本文介绍了如何利用Python的scipy.signal.find_peaks函数来识别信号序列中的峰值。通过设置不同的参数,如高度、阈值、距离、突出程度和宽度,可以灵活地筛选出满足特定条件的峰值。文中提供了多个示例,包括在ECG信号中找到峰值,以及结合其他条件如峰值宽度和突出程度来定位特殊信号。
摘要由CSDN通过智能技术生成

函数用法:

scipy.signal.find_peaks(x, height=None, threshold=None, distance=None, prominence=None, width=None, wlen=None, rel_height=0.5, plateau_size=None)

解释:输入

x: 带有峰值的信号序列

height: 低于指定height的信号都不考虑

threshold: 其与相邻样本的垂直距离

distance: 相邻峰之间的最小水平距离, 先移除较小的峰,直到所有剩余峰的条件都满足为止。

prominence: 个人理解是突起程度,详见peak_prominences

width: 波峰的宽度,详见peak_widths

plateau_size: 保证峰对应的平顶数目大于给定值

 返回值:

peaks: x对应的峰值的索引

properties:

height--> ‘peak_heights’

threshold-->‘left_thresholds’, ‘right_thresholds’

prominence-->‘prominences’, ‘right_bases’, ‘left_bases’

width-->‘width_heights’, ‘left_ips’, ‘right_ips’

plateau_size-->‘plateau_sizes’, left_edges’, ‘right_edges’

小列子01:

import matplotlib.pyplot as plt
from scipy.misc import electrocardiogram
from scipy.signal import find_peaks
import numpy as np
x = electrocardiogram()[2000:4000]
peaks, _ = find_peaks(x, height=0)
plt.plot(x)
plt.plot(peaks, x[peaks], "x")
plt.plot(np.zeros_like(x), "--", color="gray")
plt.show()

小列子02:

import matplotlib.pyplot as plt
from scipy.misc import electrocardiogram
from scipy.signal import find_peaks
import numpy as np
#选择大于0的
# x = electrocardiogram()[2000:4000]
# peaks, _ = find_peaks(x, height=0)
# plt.plot(x)
# plt.plot(peaks, x[peaks], "x")
# plt.plot(np.zeros_like(x), "--", color="gray")
# plt.show()

#小于0一下的
x = electrocardiogram()[2000:4000]
border = np.sin(np.linspace(0, 3 * np.pi, x.size))
peaks, _ = find_peaks(x, height=(-border, border))
plt.plot(x)
plt.plot(-border, "--", color="gray")
plt.plot(border, ":", color="gray")
plt.plot(peaks, x[peaks], "x")
plt.show()

小列子03:

import matplotlib.pyplot as plt
from scipy.misc import electrocardiogram
from scipy.signal import find_peaks
import numpy as np

#我们可以通过要求至少150个样本的距离来轻松选择心电图(ECG)中QRS络合物的位置
x = electrocardiogram()[2000:4000]
peaks, _ = find_peaks(x, distance=150)
plt.plot(x)
plt.plot(peaks, x[peaks], "x")
plt.show()

小列子04:

import matplotlib.pyplot as plt
from scipy.misc import electrocardiogram
from scipy.signal import find_peaks
import numpy as np

#通过将允许的突出限制为0.6
x = electrocardiogram()[2000:4000]
peaks, properties = find_peaks(x, prominence=(None, 0.6))
plt.plot(x)
plt.plot(peaks, x[peaks], "x")
plt.show()

小列子05:

import matplotlib.pyplot as plt
from scipy.misc import electrocardiogram
from scipy.signal import find_peaks
import numpy as np

#要仅选择非典型心跳,我们将两个条件结合起来:最小突出1和至少20个样本的宽度。
x = electrocardiogram()[17000:18000]
peaks, properties = find_peaks(x, prominence=1, width=20)

plt.plot(x)
plt.plot(peaks, x[peaks], "x")
plt.vlines(x=peaks, ymin=x[peaks] - properties["prominences"],
           ymax = x[peaks], color = "C1")
plt.hlines(y=properties["width_heights"], xmin=properties["left_ips"],
          xmax=properties["right_ips"], color = "C1")
plt.show()

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值