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.
JavaScript
x
17
17
1
import matplotlib.pyplot as plt
2
import numpy as np
3
from scipy.signal import savgol_filter
4
5
data = pd.read_excel('test_heartpy.xlsx')
6
arr = np.array(data)
7
time = arr[1:,0] # time in s
8
ECG = arr[1:,1] # ECG
9
PPG = arr[1:,2] # PPG
10
filtered = savgol_filter(PPG, 251, 3)
11
12
plt.plot(time, filtered)
13
plt.xlabel('Time (in s)')
14
plt.ylabel('PPG')
15
plt.grid('on')
16
17
The PPG signal looks like this. To search for the peaks I used:
JavaScript
1
15
15
1
# searching peaks
2
from scipy.signal import find_peaks
3
4
peaks, heights_peak_0 = find_peaks(PPG, height=0.2)
5
heights_peak = heights_peak_0.values()
6
plt.plot(PPG)
7
plt.plot(peaks, np.asarray(PPG)[peaks], "x")
8
plt.plot(np.zeros_like(PPG), "--", color="gray")
9
plt.title("PPG peaks")
10
plt.show()
11
print(heights_peak_0)
12
print(heights_peak)
13
print(peaks)
14
15
Printing:
JavaScript
1
10
10
1
{'peak_heights': array([0.4822998 , 0.4710083 , 0.43884277, 0.46728516, 0.47094727,
2
0.44702148, 0.43029785, 0.44146729, 0.43933105, 0.41400146,
3
0.45318604, 0.44335938])}
4
5
dict_values([array([0.4822998 , 0.4710083 , 0.43884277, 0.46728516, 0.47094727,
6
0.44702148, 0.43029785, 0.44146729, 0.43933105, 0.41400146,
7
0.45318604, 0.44335938])])
8
9
[787 2513 4181 5773 7402 9057 10601 12194 13948 15768 17518 19335]
10
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"]