Skip to content

Commit bfa0a37

Browse files
author
Benjamin Moody
committed
field2bytes: rearrange and add comments for clarity.
1 parent aad7b14 commit bfa0a37

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

wfdb/io/annotation.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,17 +1369,28 @@ def field2bytes(field, value):
13691369
sd = value[0]
13701370

13711371
data_bytes = []
1372-
# Add SKIP elements if value is too large
1373-
while sd > 0x7fffffff:
1374-
data_bytes += [0, 59 << 2, 0xff, 0x7f, 0xff, 0xff]
1375-
sd -= 0x7fffffff
1376-
if sd > 1023:
1372+
1373+
# Add SKIP element(s) if the sample difference is too large to
1374+
# be stored in the annotation type word.
1375+
#
1376+
# Each SKIP element consists of three words (6 bytes):
1377+
# - Bytes 0-1 contain the SKIP indicator (59 << 10)
1378+
# - Bytes 2-3 contain the high 16 bits of the sample difference
1379+
# - Bytes 4-5 contain the low 16 bits of the sample difference
1380+
# If the total difference exceeds 2**31 - 1, multiple skips must
1381+
# be used.
1382+
while sd > 1023:
1383+
n = min(sd, 0x7fffffff)
13771384
data_bytes += [0, 59 << 2,
1378-
(sd >> 16) & 255,
1379-
(sd >> 24) & 255,
1380-
(sd >> 0) & 255,
1381-
(sd >> 8) & 255]
1382-
sd = 0
1385+
(n >> 16) & 255,
1386+
(n >> 24) & 255,
1387+
(n >> 0) & 255,
1388+
(n >> 8) & 255]
1389+
sd -= n
1390+
1391+
# Annotation type itself is stored as a single word:
1392+
# - bits 0 to 9 store the sample difference (0 to 1023)
1393+
# - bits 10 to 15 store the type code
13831394
data_bytes += [sd & 255, ((sd & 768) >> 8) + 4 * typecode]
13841395

13851396
elif field == 'num':

0 commit comments

Comments
 (0)