@@ -11,8 +11,23 @@ class Conf(object):
11
11
"""
12
12
Initial configuration object
13
13
"""
14
- def __init__ (self , hr = 75 , qrs_width = 0.1 ):
14
+ def __init__ (self , hr = 75 , hr_max = 200 , hr_min = 25 , qrs_width = 0.1 ):
15
+ """
16
+ Parameters
17
+ ----------
18
+ hr : int or float, optional
19
+ Heart rate in beats per minute
20
+ qrs_width : int or float, optional
21
+ Expected qrs width in seconds
22
+ hr_max : int or float, optional
23
+ Hard maximum heart rate between two beats, in beats per minute
24
+ hr_min : int or float, optional
25
+ Hard minimum heart rate between two beats, in beats per minute
26
+ """
27
+
15
28
self .hr = hr
29
+ self .hr_max = hr_max
30
+ self .hr_min = hr_min
16
31
self .qrs_width = qrs_width
17
32
18
33
@@ -26,7 +41,10 @@ def __init__(self, sig, fs, conf):
26
41
self .sig_len = len (sig )
27
42
28
43
# These values are in samples
29
- self .rr = 60 * fs / conf .hr
44
+ self .rr = 60 * self .fs / conf .hr
45
+ self .rr_max = 60 * self .fs / conf .hr_min
46
+ self .rr_min = 60 * self .fs / conf .hr_max
47
+
30
48
self .qrs_width = conf .qrs_width * fs
31
49
32
50
@@ -54,24 +72,28 @@ def mwi(self):
54
72
def learn_params (self ):
55
73
"""
56
74
Learn the following:
57
- - rr interval
58
- -
75
+ - qrs threshold
59
76
60
77
"""
61
- # Find the dominant peaks of the signal
62
- dominant_peaks = self .sig_i [find_dominant_peaks (self .sig_i ,
63
- int (self .qrs_width / 2 ))]
64
- # Cluster and use the largest cluster mean as expected param
65
- kmeans = KMeans (n_clusters = 2 , random_state = 0 ).fit (dominant_peaks )
78
+ # Find the dominant peaks of the signal. Store indices.
79
+ self .dominant_peak_inds = find_dominant_peaks (self .sig_i ,
80
+ int (self .qrs_width / 2 ))
81
+ dominant_peaks = self .sig_i [self .dominant_peak_inds ]
66
82
67
- # The mean value of the top cluster
83
+ # Cluster the peaks. The peaks in the largest amplitude cluster are
84
+ # treated as qrs peaks. Use these to determine qrs detection thresholds
85
+ kmeans = KMeans (n_clusters = 2 , random_state = 0 ).fit (dominant_peaks )
68
86
top_cluster_ind = np .argmax (kmeans .cluster_centers_ )
87
+
88
+ self .qrs_peak_inds = self .dominant_peak_inds [np .where (kmeans .labels_ == top_cluster_ind )]
89
+ qrs_peaks = self .sig_i [self .qrs_peak_inds ]
69
90
70
- self .qrs_thresh = max (kmeans .cluster_centers_ )
71
- # The standard deviation of the top cluster
72
- self .qrs_dev = np .std (dominant_peaks [np .where (kmeans .labels_ == top_cluster_ind )])
91
+ self .qrs_thresh = np .mean (qrs_peaks ) / 3
92
+ self .qrs_thresh_min = self .qrs_thresh / 3
73
93
74
- pdb .set_trace ()
94
+ return
95
+
96
+
75
97
76
98
def detect (self , sampfrom = 0 , sampto = 'end' ):
77
99
"""
@@ -88,6 +110,18 @@ def detect(self, sampfrom=0, sampto='end'):
88
110
89
111
self .learn_params ()
90
112
113
+
114
+ # plt.plot(self.sig_f, 'b')
115
+ # plt.plot(self.sig_i, 'r')
116
+ # #plt.plot(self.dominant_peak_inds, self.sig_i[self.dominant_peak_inds], 'k*')
117
+ # plt.plot(self.qrs_peak_inds, self.sig_i[self.qrs_peak_inds], 'g*')
118
+ # plt.title('Derived ECG signals')
119
+ # plt.legend(['Badpass filtered','Moving wave integration'])
120
+ # plt.show()
121
+
122
+ plt .plot (self .sig , 'b' )
123
+ plt .plot (self .qrs_peak_inds , self .sig [self .qrs_peak_inds ], 'r*' )
124
+
91
125
return
92
126
93
127
for i in range (self .sig_len ):
@@ -144,8 +178,6 @@ def cqrs_detect(sig, fs, conf=Conf()):
144
178
145
179
cqrs .detect ()
146
180
147
- plt .plot (cqrs .sig_f , 'b' )
148
- plt .plot (cqrs .sig_i , 'r' )
149
- plt .show ()
150
181
151
- #return cqrs.annotation
182
+
183
+ return cqrs .qrs_inds
0 commit comments