首页 > 解决方案 > 如何使用 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]

带有突出峰值的信号看起来像这样

标签: pythondictionaryscipy

解决方案


# 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]

推荐阅读