首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用find_peak函数Python的值

如何使用find_peak函数Python的值
EN

Stack Overflow用户
提问于 2021-04-05 04:13:52
回答 3查看 370关注 0票数 0

我要分析一个PPG信号。我找到了一些东西来寻找山峰,但我不能使用高度的值。它们存储在像字典数组之类的地方,我不知道如何从中提取值。我试着使用dict.values(),但不起作用。

代码语言:javascript
运行
复制
 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。要搜索我使用的峰值:

代码语言:javascript
运行
复制
 # 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)

打印:

代码语言:javascript
运行
复制
 {'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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2021-04-05 04:44:59

heights_peak_0scipy.signal.find_peaks返回的属性dict

您可以找到有关返回的here的更多信息

您可以使用heights_peak_0["peak_heights"]提取包含所有峰值高度的数组

票数 0
EN

Stack Overflow用户

发布于 2021-04-05 04:44:48

代码语言:javascript
运行
复制
# 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]
票数 0
EN

Stack Overflow用户

发布于 2021-04-05 04:45:44

根据docsfind_peaks()函数返回一个由peaks本身和properties字典组成的元组。因为您只对峰值感兴趣,所以可以简单地忽略元组的第二个元素,只使用第一个元素。

假设您想要获得峰值的“坐标”,然后可以将峰值高度(y值)与其位置(x值)组合在一起,如下所示(基于docs中给出的第一个代码片段):

代码语言:javascript
运行
复制
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()

打印:

代码语言:javascript
运行
复制
[(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)]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66945474

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档