-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhrcalc4.py
210 lines (167 loc) · 7.13 KB
/
hrcalc4.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# coding=utf-8
# This is a sample Python script.
import numpy as np
import matplotlib.pyplot as plt
SAMPLE_FREQ = 25.0
BUFFER_SIZE = 100
heart_rate_span = [10,250]
smoothing_size = 4
min_time_bw_samps = (60.0 / heart_rate_span[1])
xdata = np.arange(0.0, BUFFER_SIZE/SAMPLE_FREQ, 1/SAMPLE_FREQ)
def calc_hr_and_spo2(ir_data, red_data):
"""
By detecting peaks of PPG cycle and corresponding AC/DC
of red/infra-red signal, the an_ratio for the SPO2 is computed.
"""
for i in range(len(ir_data)):
if ir_data[i] < 90000.0:
ir_data[i] = 0
for i in range(len(red_data)):
if red_data[i] < 90000.0:
red_data[i] = 0
hr_valid = False
hr = 0
# get dc mean
ir_mean = int(np.mean(ir_data))
# remove DC mean and inver signal
# this lets peak detecter detect valley
x = -1 * (np.array(ir_data) - ir_mean)
x = np.convolve(x, np.ones((smoothing_size,)), 'same') / smoothing_size
ir_grad = np.gradient(x, xdata)
ir_grad[0:int(smoothing_size / 2) + 1] = np.zeros((int(smoothing_size / 2) + 1,))
ir_grad[-int(smoothing_size / 2) - 1:] = np.zeros((int(smoothing_size / 2) + 1,))
x = np.append(np.repeat(x[int(smoothing_size / 2)], int(smoothing_size / 2)),x[int(smoothing_size / 2):-int(smoothing_size / 2)])
x = np.append(x, np.repeat(x[-int(smoothing_size / 2)], int(smoothing_size / 2)))
peak_locs = np.where(ir_grad < -np.std(ir_grad))
if len(peak_locs[0]) > 0:
prev_pk = peak_locs[0][0]
true_peak_locs, pk_loc_span = [], []
for ii in peak_locs[0]:
y_pk = x[ii]
if (xdata[ii] - xdata[prev_pk]) < min_time_bw_samps:
pk_loc_span.append(ii)
else:
if pk_loc_span == []:
true_peak_locs.append(ii)
else:
true_peak_locs.append(int(np.mean(pk_loc_span)))
pk_loc_span = []
prev_pk = int(ii)
t_peaks = [xdata[kk] for kk in true_peak_locs]
if len(t_peaks) > 1:
hr_valid = True
hr = round(60.0 / np.mean(np.diff(t_peaks)),2)
# calculate threshold
n_th = int(np.mean(x))
n_th = 30 if n_th < 30 else n_th # min allowed
n_th = 60 if n_th > 60 else n_th # max allowed
ir_valley_locs, n_peaks = find_peaks(x, BUFFER_SIZE, n_th, 4, 15)
# ---------spo2---------
exact_ir_valley_locs_count = n_peaks
# FIXME: needed??
for i in range(exact_ir_valley_locs_count):
if ir_valley_locs[i] > BUFFER_SIZE:
spo2 = 0
spo2_valid = False
return hr, hr_valid, spo2, spo2_valid
i_ratio_count = 0
ratio = []
red_dc_max_index = -1
ir_dc_max_index = -1
for k in range(exact_ir_valley_locs_count-1):
red_dc_max = -16777216
ir_dc_max = -16777216
if ir_valley_locs[k+1] - ir_valley_locs[k] > 3:
for i in range(ir_valley_locs[k], ir_valley_locs[k+1]):
if ir_data[i] > ir_dc_max:
ir_dc_max = ir_data[i]
ir_dc_max_index = i
if red_data[i] > red_dc_max:
red_dc_max = red_data[i]
red_dc_max_index = i
red_ac = int((red_data[ir_valley_locs[k+1]] - red_data[ir_valley_locs[k]]) * (red_dc_max_index - ir_valley_locs[k]))
red_ac = red_data[ir_valley_locs[k]] + int(red_ac / (ir_valley_locs[k+1] - ir_valley_locs[k]))
red_ac = red_data[red_dc_max_index] - red_ac # subtract linear DC components from raw
ir_ac = int((ir_data[ir_valley_locs[k+1]] - ir_data[ir_valley_locs[k]]) * (ir_dc_max_index - ir_valley_locs[k]))
ir_ac = ir_data[ir_valley_locs[k]] + int(ir_ac / (ir_valley_locs[k+1] - ir_valley_locs[k]))
ir_ac = ir_data[ir_dc_max_index] - ir_ac # subtract linear DC components from raw
nume = red_ac * ir_dc_max
denom = ir_ac * red_dc_max
if (denom > 0 and i_ratio_count < 5) and nume != 0:
ratio.append(int(((nume * 100) & 0xffffffff) / denom))
i_ratio_count += 1
# choose median value since PPG signal may vary from beat to beat
ratio = sorted(ratio) # sort to ascending order
mid_index = int(i_ratio_count / 2)
ratio_ave = 0
if mid_index > 1:
ratio_ave = int((ratio[mid_index-1] + ratio[mid_index])/2)
else:
if len(ratio) != 0:
ratio_ave = ratio[mid_index]
if 2 < ratio_ave < 184:
spo2 = round(-45.060 * (ratio_ave**2) / 10000.0 + 30.054 * ratio_ave / 100.0 + 94.845, 2)
spo2_valid = True
else:
spo2 = 0
spo2_valid = False
return hr, hr_valid, spo2, spo2_valid
def find_peaks(x, size, min_height, min_dist, max_num):
"""
Find at most MAX_NUM peaks above MIN_HEIGHT separated by at least MIN_DISTANCE
"""
ir_valley_locs, n_peaks = find_peaks_above_min_height(x, size, min_height, max_num)
ir_valley_locs, n_peaks = remove_close_peaks(n_peaks, ir_valley_locs, x, min_dist)
n_peaks = min([n_peaks, max_num])
return ir_valley_locs, n_peaks
def find_peaks_above_min_height(x, size, min_height, max_num):
"""
Find all peaks above MIN_HEIGHT
"""
i = 0
n_peaks = 0
ir_valley_locs = [] # [0 for i in range(max_num)]
while i < size - 1:
if x[i] > min_height and x[i] > x[i-1]: # find the left edge of potential peaks
n_width = 1
# original condition i+n_width < size may cause IndexError
# so I changed the condition to i+n_width < size - 1
while i + n_width < size - 1 and x[i] == x[i+n_width]: # find flat peaks
n_width += 1
if x[i] > x[i+n_width] and n_peaks < max_num: # find the right edge of peaks
# ir_valley_locs[n_peaks] = i
ir_valley_locs.append(i)
n_peaks += 1 # original uses post increment
i += n_width + 1
else:
i += n_width
else:
i += 1
return ir_valley_locs, n_peaks
def remove_close_peaks(n_peaks, ir_valley_locs, x, min_dist):
"""
Remove peaks separated by less than MIN_DISTANCE
"""
# should be equal to maxim_sort_indices_descend
# order peaks from large to small
# should ignore index:0
sorted_indices = sorted(ir_valley_locs, key=lambda i: x[i])
sorted_indices.reverse()
# this "for" loop expression does not check finish condition
# for i in range(-1, n_peaks):
i = -1
while i < n_peaks:
old_n_peaks = n_peaks
n_peaks = i + 1
# this "for" loop expression does not check finish condition
# for j in (i + 1, old_n_peaks):
j = i + 1
while j < old_n_peaks:
n_dist = (sorted_indices[j] - sorted_indices[i]) if i != -1 else (sorted_indices[j] + 1) # lag-zero peak of autocorr is at index -1
if n_dist > min_dist or n_dist < -1 * min_dist:
sorted_indices[n_peaks] = sorted_indices[j]
n_peaks += 1 # original uses post increment
j += 1
i += 1
sorted_indices[:n_peaks] = sorted(sorted_indices[:n_peaks])
return sorted_indices, n_peaks