Repository navigation
Expand file tree
/
Copy pathhr.py
More file actions
142 lines (112 loc) · 3.74 KB
/
Copy pathhr.py
File metadata and controls
142 lines (112 loc) · 3.74 KB
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
import numpy as np
def compute_hr(sig_len, qrs_inds, fs):
"""
Compute instantaneous heart rate from peak indices.
Parameters
----------
sig_len : int
The length of the corresponding signal.
qrs_inds : ndarray
The QRS index locations.
fs : int, float
The corresponding signal's sampling frequency.
Returns
-------
heart_rate : ndarray
An array of the instantaneous heart rate, with the length of the
corresponding signal. Contains numpy.nan where heart rate could
not be computed.
"""
heart_rate = np.full(sig_len, np.nan, dtype='float32')
if len(qrs_inds) < 2:
return heart_rate
for i in range(0, len(qrs_inds)-2):
a = qrs_inds[i]
b = qrs_inds[i+1]
c = qrs_inds[i+2]
rr = (b-a) * (1.0 / fs) * 1000
hr = 60000.0 / rr
heart_rate[b+1:c+1] = hr
heart_rate[qrs_inds[-1]:] = heart_rate[qrs_inds[-1]]
return heart_rate
def calc_rr(qrs_locs, fs=None, min_rr=None, max_rr=None, qrs_units='samples',
rr_units='samples'):
"""
Compute R-R intervals from QRS indices by extracting the time
differences.
Parameters
----------
qrs_locs : ndarray
1d array of QRS locations.
fs : float, optional
Sampling frequency of the original signal. Needed if
`qrs_units` does not match `rr_units`.
min_rr : float, optional
The minimum allowed R-R interval. Values below this are excluded
from the returned R-R intervals. Units are in `rr_units`.
max_rr : float, optional
The maximum allowed R-R interval. Values above this are excluded
from the returned R-R intervals. Units are in `rr_units`.
qrs_units : str, optional
The time unit of `qrs_locs`. Must be one of: 'samples',
'seconds'.
rr_units : str, optional
The desired time unit of the returned R-R intervals in. Must be
one of: 'samples', 'seconds'.
Returns
-------
rr : ndarray
Array of R-R intervals.
"""
rr = np.diff(qrs_locs)
# Empty input qrs_locs
if not len(rr):
return rr
# Convert to desired output rr units if needed
if qrs_units == 'samples' and rr_units == 'seconds':
rr = rr / fs
elif qrs_units == 'seconds' and rr_units == 'samples':
rr = rr * fs
# Apply R-R interval filters
if min_rr is not None:
rr = rr[rr > min_rr]
if max_rr is not None:
rr = rr[rr < max_rr]
return rr
def calc_mean_hr(rr, fs=None, min_rr=None, max_rr=None, rr_units='samples'):
"""
Compute mean heart rate in beats per minute, from a set of R-R
intervals. Returns 0 if rr is empty.
Parameters
----------
rr : ndarray
Array of R-R intervals.
fs : int, float
The corresponding signal's sampling frequency. Required if
'input_time_units' == 'samples'.
min_rr : float, optional
The minimum allowed R-R interval. Values below this are excluded
when calculating the heart rate. Units are in `rr_units`.
max_rr : float, optional
The maximum allowed R-R interval. Values above this are excluded
when calculating the heart rate. Units are in `rr_units`.
rr_units : str, optional
The time units of the input R-R intervals. Must be one of:
'samples', 'seconds'.
Returns
-------
mean_hr : float
The mean heart rate in beats per minute.
"""
if not len(rr):
return 0
if min_rr is not None:
rr = rr[rr > min_rr]
if max_rr is not None:
rr = rr[rr < max_rr]
mean_rr = np.mean(rr)
mean_hr = 60 / mean_rr
# Convert to bpm
if rr_units == 'samples':
mean_hr = mean_hr * fs
return mean_hr