Skip to content

Commit f50e46c

Browse files
committed
work on cqrs
1 parent a053e2c commit f50e46c

File tree

1 file changed

+50
-18
lines changed

1 file changed

+50
-18
lines changed

wfdb/processing/cqrs.py

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,23 @@ class Conf(object):
1111
"""
1212
Initial configuration object
1313
"""
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+
1528
self.hr = hr
29+
self.hr_max = hr_max
30+
self.hr_min = hr_min
1631
self.qrs_width = qrs_width
1732

1833

@@ -26,7 +41,10 @@ def __init__(self, sig, fs, conf):
2641
self.sig_len = len(sig)
2742

2843
# 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+
3048
self.qrs_width = conf.qrs_width * fs
3149

3250

@@ -54,24 +72,28 @@ def mwi(self):
5472
def learn_params(self):
5573
"""
5674
Learn the following:
57-
- rr interval
58-
-
75+
- qrs threshold
5976
6077
"""
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]
6682

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)
6886
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]
6990

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
7393

74-
pdb.set_trace()
94+
return
95+
96+
7597

7698
def detect(self, sampfrom=0, sampto='end'):
7799
"""
@@ -88,6 +110,18 @@ def detect(self, sampfrom=0, sampto='end'):
88110

89111
self.learn_params()
90112

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+
91125
return
92126

93127
for i in range(self.sig_len):
@@ -144,8 +178,6 @@ def cqrs_detect(sig, fs, conf=Conf()):
144178

145179
cqrs.detect()
146180

147-
plt.plot(cqrs.sig_f, 'b')
148-
plt.plot(cqrs.sig_i, 'r')
149-
plt.show()
150181

151-
#return cqrs.annotation
182+
183+
return cqrs.qrs_inds

0 commit comments

Comments
 (0)