Skip to content

Commit 774d36f

Browse files
committed
Refactors signal and annotation resampling MIT-LCP#281
1 parent 7889664 commit 774d36f

File tree

1 file changed

+11
-53
lines changed

1 file changed

+11
-53
lines changed

wfdb/processing/basic.py

Lines changed: 11 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -6,63 +6,27 @@
66
from wfdb.io.annotation import Annotation
77

88

9-
def resample_ann(resampled_t, ann_sample):
9+
def resample_ann(ann_sample, fs, fs_target):
1010
"""
1111
Compute the new annotation indices.
1212
1313
Parameters
1414
----------
15-
resampled_t : ndarray
16-
Array of signal locations as returned by scipy.signal.resample.
1715
ann_sample : ndarray
1816
Array of annotation locations.
17+
fs : int
18+
The starting sampling frequency.
19+
fs_target : int
20+
The desired sampling frequency.
1921
2022
Returns
2123
-------
2224
ndarray
2325
Array of resampled annotation locations.
2426
2527
"""
26-
tmp = np.zeros(len(resampled_t), dtype='int16')
27-
j = 0
28-
break_loop = 0
29-
tprec = resampled_t[j]
30-
for i, v in enumerate(ann_sample):
31-
break_loop = 0
32-
while True:
33-
d = False
34-
if v < tprec:
35-
j -= 1
36-
tprec = resampled_t[j]
37-
38-
if j+1 == len(resampled_t):
39-
tmp[j] += 1
40-
break
41-
42-
tnow = resampled_t[j+1]
43-
if tprec <= v and v <= tnow:
44-
if v-tprec < tnow-v:
45-
tmp[j] += 1
46-
else:
47-
tmp[j+1] += 1
48-
d = True
49-
j += 1
50-
tprec = tnow
51-
break_loop += 1
52-
if (break_loop > 1000):
53-
tmp[j] += 1
54-
break
55-
if d:
56-
break
57-
58-
idx = np.where(tmp>0)[0].astype('int64')
59-
res = []
60-
for i in idx:
61-
for j in range(tmp[i]):
62-
res.append(i)
63-
assert len(res) == len(ann_sample)
64-
65-
return np.asarray(res, dtype='int64')
28+
ratio = fs_target/fs
29+
return (ratio * ann_sample).astype(np.int64)
6630

6731

6832
def resample_sig(x, fs, fs_target):
@@ -125,10 +89,8 @@ def resample_singlechan(x, ann, fs, fs_target):
12589
Annotation containing resampled annotation locations.
12690
12791
"""
128-
resampled_x, resampled_t = resample_sig(x, fs, fs_target)
129-
130-
new_sample = resample_ann(resampled_t, ann.sample)
131-
assert ann.sample.shape == new_sample.shape
92+
resampled_x, _ = resample_sig(x, fs, fs_target)
93+
new_sample = resample_ann(ann.sample, fs, fs_target)
13294

13395
resampled_ann = Annotation(record_name=ann.record_name,
13496
extension=ann.extension,
@@ -171,15 +133,11 @@ def resample_multichan(xs, ann, fs, fs_target, resamp_ann_chan=0):
171133
assert resamp_ann_chan < xs.shape[1]
172134

173135
lx = []
174-
lt = None
175136
for chan in range(xs.shape[1]):
176-
resampled_x, resampled_t = resample_sig(xs[:, chan], fs, fs_target)
137+
resampled_x, _ = resample_sig(xs[:, chan], fs, fs_target)
177138
lx.append(resampled_x)
178-
if chan == resamp_ann_chan:
179-
lt = resampled_t
180139

181-
new_sample = resample_ann(lt, ann.sample)
182-
assert ann.sample.shape == new_sample.shape
140+
new_sample = resample_ann(ann.sample, fs, fs_target)
183141

184142
resampled_ann = Annotation(record_name=ann.record_name,
185143
extension=ann.extension,

0 commit comments

Comments
 (0)