|
6 | 6 | from wfdb.io.annotation import Annotation |
7 | 7 |
|
8 | 8 |
|
9 | | -def resample_ann(resampled_t, ann_sample): |
| 9 | +def resample_ann(ann_sample, fs, fs_target): |
10 | 10 | """ |
11 | 11 | Compute the new annotation indices. |
12 | 12 |
|
13 | 13 | Parameters |
14 | 14 | ---------- |
15 | | - resampled_t : ndarray |
16 | | - Array of signal locations as returned by scipy.signal.resample. |
17 | 15 | ann_sample : ndarray |
18 | 16 | Array of annotation locations. |
| 17 | + fs : int |
| 18 | + The starting sampling frequency. |
| 19 | + fs_target : int |
| 20 | + The desired sampling frequency. |
19 | 21 |
|
20 | 22 | Returns |
21 | 23 | ------- |
22 | 24 | ndarray |
23 | 25 | Array of resampled annotation locations. |
24 | 26 |
|
25 | 27 | """ |
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) |
66 | 30 |
|
67 | 31 |
|
68 | 32 | def resample_sig(x, fs, fs_target): |
@@ -125,10 +89,8 @@ def resample_singlechan(x, ann, fs, fs_target): |
125 | 89 | Annotation containing resampled annotation locations. |
126 | 90 |
|
127 | 91 | """ |
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) |
132 | 94 |
|
133 | 95 | resampled_ann = Annotation(record_name=ann.record_name, |
134 | 96 | extension=ann.extension, |
@@ -171,15 +133,11 @@ def resample_multichan(xs, ann, fs, fs_target, resamp_ann_chan=0): |
171 | 133 | assert resamp_ann_chan < xs.shape[1] |
172 | 134 |
|
173 | 135 | lx = [] |
174 | | - lt = None |
175 | 136 | 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) |
177 | 138 | lx.append(resampled_x) |
178 | | - if chan == resamp_ann_chan: |
179 | | - lt = resampled_t |
180 | 139 |
|
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) |
183 | 141 |
|
184 | 142 | resampled_ann = Annotation(record_name=ann.record_name, |
185 | 143 | extension=ann.extension, |
|
0 commit comments