Skip to content
Advertisement

How to use values of find_peak function Python

I have to analyse a PPG signal. I found something to find the peaks but I can’t use the values of the heights. They are stored in like a dictionary array or something and I don’t know how to extract the values out of it. I tried using dict.values() but that didn’t work.

 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')
 

The PPG signal looks like this. To search for the peaks I used:

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

Printing:

 {'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]

Signal with highlighted peaks looks like this.

Advertisement

Answer

heights_peak_0 is the properties dict returned by scipy.signal.find_peaks

You can find more information about what is returned here

You can extract the array containing all the heights of the peaks with heights_peak_0["peak_heights"]

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement