python - 如何使用 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')
PPG 信号如下所示。要搜索我使用的峰:
# 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]
带有突出峰值的信号看起来像这样。
解决方案
# 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]
推荐阅读
- docker - 无法通过 docker-compose.yml 中的“标签”设置 Traefik
- jquery - 添加新 div 时背景图像不透明度增加
- css - 网站和 Invsion(设计师和前端开发人员工具)之间的字体差异
- arduino - Arduino IDE 可以使用 avrdude 来刷 hex 文件,但是 ubuntu 上的命令行不能
- javascript - 如何获取 HTTP 请求的类型
- javascript - 函数名称是否也应该反映在 promise.then() 上称为回调的函数的操作?
- javascript - 将文字数字(三)乘以 1(四)
- ios - 是否可以更改 UINavigationBar 的字体大小
- c# - 二次方程求解器不起作用
- python - 由于我的优化器,我的 Keras 神经网络精度是否总是停留在 ~0.55 左右?