-
Notifications
You must be signed in to change notification settings - Fork 83
/
hrv.py
307 lines (222 loc) · 9.67 KB
/
hrv.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
"""
A collection of heartrate variability algorithms for
both the timedomain and frequency domain.
Copyright (C) 2019 Luis Howell & Bernd Porr
GPL GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007
"""
import numpy as np
import random
import subprocess
from scipy.interpolate import interp1d
from gatspy.periodic import LombScargleFast
class HRV:
"""
Heartrate variability class which calcualtes the standard HRV
parameters with the help of Python functions and for cross
validation also via the physionet's get_hrv script.
"""
def __init__(self, sampling_frequency):
"""
Constructor takes the sampling frequency.
All rr_sample data is in sample number and
will assume it's at this sampling rate.
"""
self.fs = float(sampling_frequency)
self.period = 1.0/sampling_frequency
def _intervals(self, rr_samples):
"""Calculate the RR intervals in ms from sample numbers.
:param rr_samples: R peak sample locations
:type rr_samples: array_like
:return: RR intervals in milliseconds
:rtype: ndarray
"""
rr_intervals = np.diff(np.array(rr_samples)*self.period*1000)
return rr_intervals
def _timestamps(self, rr_samples):
"""Calculate the timestamps in ms from sample locations.
:param rr_samples: R peak sample locations
:type rr_samples: array_like
:return: The timestamps in milliseconds
:rtype: array_like
"""
ts = np.array(rr_samples)*self.period*1000
return ts
def _succ_diffs(self, rr_samples):
"""Calculate the successive differences of R peaks.
:param rr_samples: R peak sample locations
:type rr_samples: array_like
:return: The successive differences of R peaks
:rtype: ndarray
"""
rr_ints = self._intervals(rr_samples)
succ_diffs = []
for i in range(len(rr_ints)-1):
diff = rr_ints[i+1] - rr_ints[i]
succ_diffs.append(diff)
return np.array(succ_diffs)
def SDNN(self, rr_samples, normalise=False):
"""Calculate SDNN, the standard deviation of NN intervals.
:param rr_samples: R peak sample locations
:type rr_samples: array_like
:param normalise: normalise the SDNN against the average RR interval, defaults to False
:type normalise: bool, optional
:return: SDNN, the standard deviation of NN intervals
:rtype: float
"""
rr_intervals = self._intervals(rr_samples)
rr_std = np.std(rr_intervals)
if normalise:
rr_mean_interval = np.mean(rr_intervals)
rr_std = rr_std/rr_mean_interval
return rr_std
def SDANN(self, rr_samples, average_period=5.0, normalise=False):
"""Calculate SDANN, the standard deviation of the average RR intervals calculated over short periods.
:param rr_samples: R peak sample locations
:type rr_samples: array_like
:param average_period: The averging period in minutes, defaults to 5.0
:type average_period: float, optional
:param normalise: normalise the SDANN against the average RR interval, defaults to False
:type normalise: bool, optional
:return: SDANN, the standard deviation of the average RR intervals calculated over short periods
:rtype: float
"""
average_period_samples = int(self.fs*average_period*60)
average_rr_intervals = []
rr_samples = np.array(rr_samples)
sections = int((np.max(rr_samples)/average_period_samples)+0.5)
if sections<1:
sections = 1
for i in range(sections):
idx = np.where((rr_samples>=(i*average_period_samples)) &
(rr_samples<((i+1)*average_period_samples)))
idx = idx[0]
section_rr = rr_samples[idx[0]:idx[-1]+1]
avg_rr_int = np.mean(self._intervals(section_rr))
average_rr_intervals.append(avg_rr_int)
rr_std = np.std(average_rr_intervals)
if normalise:
rr_mean_interval = np.mean(average_rr_intervals)
rr_std = rr_std/rr_mean_interval
return rr_std
def RMSSD(self, rr_samples, normalise = False):
"""Calculate RMSSD (root mean square of successive differences).
:param rr_samples: R peak sample locations
:type rr_samples: array_like
:param normalise: normalise the RMSSD against the average RR interval, defaults to False
:type normalise: bool, optional
:return: RMSSD (root mean square of successive differences)
:rtype: float
"""
succ_diffs = self._succ_diffs(rr_samples)
succ_diffs = succ_diffs*succ_diffs
rms = np.sqrt(np.mean(succ_diffs))
if normalise:
rms = rms / np.mean(self._intervals(rr_samples))
return rms
def SDSD(self, rr_samples):
"""Calculate SDSD (standard deviation of successive differences), the standard deviation of the successive differences between adjacent NNs.
:param rr_samples: R peak sample locations
:type rr_samples: array_like
:return: SDSD (standard deviation of successive differences)
:rtype: float
"""
succ_diffs = self._succ_diffs(rr_samples)
return np.std(succ_diffs)
def NN50(self, rr_samples):
"""Calculate NN50, the number of pairs of successive NNs that differ by more than 50 ms.
:param rr_samples: R peak sample locations
:type rr_samples: array_like
:return: NN50
:rtype: float
"""
succ_diffs = self._succ_diffs(rr_samples)
over_50 = np.where(abs(succ_diffs)>50)
over_50 = over_50[0]
return len(over_50)
def pNN50(self, rr_samples):
"""Calculate pNN50, the proportion of NN50 divided by total number of NNs.
:param rr_samples: R peak sample locations
:type rr_samples: array_like
:return: pNN50
:rtype: float
"""
return self.NN50(rr_samples)/(len(rr_samples)-1)
def NN20(self, rr_samples):
"""Calculate NN20, the number of pairs of successive NNs that differ by more than 20 ms.
:param rr_samples: R peak sample locations
:type rr_samples: array_like
:return: NN20
:rtype: float
"""
succ_diffs = self._succ_diffs(rr_samples)
over_20 = np.where(abs(succ_diffs)>20)
over_20 = over_20[0]
return len(over_20)
def pNN20(self, rr_samples):
"""Calculate pNN20, the proportion of NN20 divided by total number of NNs.
:param rr_samples: R peak sample locations
:type rr_samples: array_like
:return: pNN20
:rtype: float
"""
return self.NN20(rr_samples)/(len(rr_samples)-1)
def HR(self, rr_samples):
"""Calculate heart-rates from R peak samples.
:param rr_samples: R peak sample locations
:type rr_samples: array_like
:return: Heart-rates in BPM
:rtype: ndarray
"""
rr_intervals = np.diff(rr_samples)
heart_rates = 60.0/(rr_intervals/float(self.fs))
return heart_rates
def add_rr_error(self, rr_samples, error):
"""
Adds jitter to the heartrate timestamps.
The error and the rr_samples are in timestamps.
Returns the noisy timestamps in samples.
"""
if error==0:
return rr_samples
error_values = np.random.randint(-abs(error), abs(error)+1, len(rr_samples))
noisy_rr_samples = rr_samples+error_values
return noisy_rr_samples
def fAnalysis(self, rr_samples):
"""
Frequency analysis to calc self.lf, self.hf, returns the LF/HF-ratio and
also calculates the spectrum as pairs of (self.f_hr_axis,self.f_hr).
The input arrary is in sample points where R peaks have been detected.
"""
# discrete timestamps
self.hr_discrete = self._intervals(rr_samples) / 1000
# hr positions in time
self.t_hr_discrete = [i/self.fs for i in rr_samples[1:]]
# now let's create function which approximates the hr(t) relationship
self.hr_func = interp1d(self.t_hr_discrete, self.hr_discrete)
# we take 1024 samples for a linear time array for hr(t)
nsamp = 1000
# linear time array for the heartrate
self.t_hr_linear = np.linspace(self.t_hr_discrete[1],
self.t_hr_discrete[len(self.t_hr_discrete)-2],
num=nsamp)
# duration in secs of the heartrate array minus the ends b/c of the approx
duration = self.t_hr_discrete[len(self.t_hr_discrete)-2] - self.t_hr_discrete[1];
# heartrate linearly approximated between discrete samples
self.hr_linear = self.hr_func(self.t_hr_linear)
model = LombScargleFast().fit(self.t_hr_discrete, self.hr_discrete, 1E-2)
fmax = 1
fmin = 0.01
df = (fmax - fmin) / nsamp
self.f_hr = model.score_frequency_grid(fmin, df, nsamp)
self.f_hr_axis = fmin + df * np.arange(nsamp)
# lf
self.lf = 0
# hf
self.hf = 0
for i in range(0,int(nsamp/2)):
if (self.f_hr_axis[i] >= 0.04) and (self.f_hr_axis[i] <= 0.15):
self.lf = self.lf + self.f_hr[i]
if (self.f_hr_axis[i] >= 0.15) and (self.f_hr_axis[i] <= 0.4):
self.hf = self.hf + self.f_hr[i]
# hf
return self.lf/self.hf