如何解决如何使用 find_peak 函数 Python
我必须分析 PPG 信号。我找到了一些可以找到峰值的东西,但我无法使用高度值。它们像字典数组或其他东西一样存储,我不知道如何从中提取值。我尝试使用 dict.values()
但这没有用。
import matplotlib.pyplot as plt
import numpy as np
from scipy.signal import savgol_filter
data = pd.read_excel('test_heartpy.xlsx')
arr = np.array(data)
time = arr[1:,0] # time in s
ECG = arr[1:,1] # ECG
PPG = arr[1:,2] # PPG
filtered = savgol_filter(PPG,251,3)
plt.plot(time,filtered)
plt.xlabel('Time (in s)')
plt.ylabel('PPG')
plt.grid('on')
# searching peaks
from scipy.signal import find_peaks
peaks,heights_peak_0 = find_peaks(PPG,height=0.2)
heights_peak = heights_peak_0.values()
plt.plot(PPG)
plt.plot(peaks,np.asarray(PPG)[peaks],"x")
plt.plot(np.zeros_like(PPG),"--",color="gray")
plt.title("PPG peaks")
plt.show()
print(heights_peak_0)
print(heights_peak)
print(peaks)
印刷:
{'peak_heights': array([0.4822998,0.4710083,0.43884277,0.46728516,0.47094727,0.44702148,0.43029785,0.44146729,0.43933105,0.41400146,0.45318604,0.44335938])}
dict_values([array([0.4822998,0.44335938])])
[787 2513 4181 5773 7402 9057 10601 12194 13948 15768 17518 19335]
解决方法
# the following will give you an array with the values of peaks
heights_peak_0['peak_heights']
# peaks seem to be the indices where find_peaks function foud peaks in the original signal. So you can get the peak values this way also
PPG[peaks]
,
heights_peak_0
是 scipy.signal.find_peaks
您可以找到有关返回内容的更多信息 here
您可以使用 heights_peak_0["peak_heights"]
根据docs,find_peaks()
函数返回由peaks
本身和properties
dict 组成的元组。由于您只对峰值感兴趣,您可以简单地忽略元组的第二个元素而只使用第一个元素。
假设您想要获得峰的“坐标”,则可以像这样将峰高(y 值)与其位置(x 值)结合起来(基于 {{3 }}):
import matplotlib.pyplot as plt
from scipy.misc import electrocardiogram
from scipy.signal import find_peaks
x = electrocardiogram()[2000:4000]
peaks,_ = find_peaks(x,distance=150)
peaks_x_values = peaks
peaks_y_values = x[peaks]
peak_coordinates = list(zip(peaks_x_values,peaks_y_values))
print(peak_coordinates)
plt.plot(x)
plt.plot(peaks_x_values,peaks_y_values,"x")
plt.show()
印刷:
[(65,0.705),(251,1.155),(431,1.705),(608,1.96),(779,1.925),(956,2.09),(1125,1.745),(1292,1.37),(1456,1.2),(1614,0.81),(1776,0.665),(1948,0.665)]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。