我要分析一个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')
PPG信号看起来像this。要搜索我使用的峰值:
# 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.4710083 , 0.43884277, 0.46728516, 0.47094727,
0.44702148, 0.43029785, 0.44146729, 0.43933105, 0.41400146,
0.45318604, 0.44335938])])
[787 2513 4181 5773 7402 9057 10601 12194 13948 15768 17518 19335]
高亮显示峰值的信号看起来像this。
发布于 2021-04-05 04:44:59
heights_peak_0
是scipy.signal.find_peaks
返回的属性dict
您可以找到有关返回的here的更多信息
您可以使用heights_peak_0["peak_heights"]
提取包含所有峰值高度的数组
发布于 2021-04-05 04:44:48
# 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]
发布于 2021-04-05 04:45:44
根据docs,find_peaks()
函数返回一个由peaks
本身和properties
字典组成的元组。因为您只对峰值感兴趣,所以可以简单地忽略元组的第二个元素,只使用第一个元素。
假设您想要获得峰值的“坐标”,然后可以将峰值高度(y值)与其位置(x值)组合在一起,如下所示(基于docs中给出的第一个代码片段):
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)]
https://stackoverflow.com/questions/66945474
复制相似问题