@@ -365,17 +365,19 @@ def processwfdbbytes(filename, dirname, pbdir, fmt, startbyte, readlen, nsig, sa
365
365
# Read the bytes from the file as unsigned integer blocks
366
366
sigbytes , bytesloaded = getdatbytes (filename , dirname , pbdir , fmt , nsamp , startbyte )
367
367
368
- print ('startbyte: ' , startbyte )
369
- print ('nsamp: ' , nsamp )
370
- print ('sigbytes: ' )
371
- print (sigbytes )
372
- print (len (sigbytes ))
373
- print (bytesloaded )
368
+ # use this for byte processing
369
+ processnsamp = nsamp
374
370
371
+ # For odd sampled records, imagine an extra sample and add an extra byte
372
+ # to simplify the processing step and remove the extra sample at the end.
373
+ if processnsamp % 2 :
374
+ sigbytes = np .append (sigbytes , 0 ).astype ('uint64' )
375
+ processnsamp += 1
375
376
376
- if tsampsperframe == nsig : # No extra samples/frame
377
+ # No extra samples/frame
378
+ if tsampsperframe == nsig :
377
379
# Turn the bytes into actual samples.
378
- sig = np .zeros (nsamp ) # 1d array of actual samples
380
+ sig = np .zeros (processnsamp ) # 1d array of actual samples
379
381
# One sample pair is stored in one byte triplet.
380
382
sig [0 ::2 ] = sigbytes [0 ::3 ] + 256 * \
381
383
np .bitwise_and (sigbytes [1 ::3 ], 0x0f ) # Even numbered samples
@@ -385,15 +387,21 @@ def processwfdbbytes(filename, dirname, pbdir, fmt, startbyte, readlen, nsig, sa
385
387
sig [1 ::2 ] = sigbytes [2 ::3 ] + 256 * np .bitwise_and (sigbytes [1 ::3 ] >> 4 , 0x0f )
386
388
if floorsamp : # Remove extra sample read
387
389
sig = sig [floorsamp :]
390
+
391
+ # Remove extra sample read if originally odd sampled
392
+ if nsamp % 2 :
393
+ sig = sig [:- 1 ]
394
+
388
395
# Reshape into final array of samples
389
396
sig = sig .reshape (readlen , nsig )
390
397
sig = sig .astype (int )
391
398
# Loaded values as unsigned. Convert to 2's complement form: values
392
399
# > 2^11-1 are negative.
393
400
sig [sig > 2047 ] -= 4096
394
- else : # At least one channel has multiple samples per frame. All extra samples are discarded.
401
+ # At least one channel has multiple samples per frame. All extra samples are discarded.
402
+ else :
395
403
# Turn the bytes into actual samples.
396
- sigall = np .zeros (nsamp ) # 1d array of actual samples
404
+ sigall = np .zeros (processnsamp ) # 1d array of actual samples
397
405
sigall [0 ::2 ] = sigbytes [0 ::3 ] + 256 * \
398
406
np .bitwise_and (sigbytes [1 ::3 ], 0x0f ) # Even numbered samples
399
407
@@ -403,6 +411,11 @@ def processwfdbbytes(filename, dirname, pbdir, fmt, startbyte, readlen, nsig, sa
403
411
np .bitwise_and (sigbytes [1 ::3 ] >> 4 , 0x0f )
404
412
if floorsamp : # Remove extra sample read
405
413
sigall = sigall [floorsamp :]
414
+
415
+ # Remove extra sample read if originally odd sampled
416
+ if nsamp % 2 :
417
+ sigall = sigall [:- 1 ]
418
+
406
419
# Convert to int64 to be able to hold -ve values
407
420
sigall = sigall .astype ('int' )
408
421
# Loaded values as unsigned. Convert to 2's complement form: values
@@ -592,6 +605,7 @@ def getdatbytes(filename, dirname, pbdir, fmt, nsamp, startbyte):
592
605
# count is the number of elements to read using np.fromfile
593
606
# bytecount is the number of bytes to read
594
607
if fmt == '212' :
608
+ # fmt 212
595
609
bytecount = int (np .ceil ((nsamp ) * 1.5 ))
596
610
count = bytecount
597
611
elif fmt == '310' :
@@ -846,13 +860,16 @@ def wrdatfile(filename, fmt, d_signals):
846
860
d_signals = d_signals .reshape (- 1 )
847
861
848
862
nsamp = len (d_signals )
849
- # Odd numbered number of samples. Fill in extra blank.
850
- if nsamp % 2 :
863
+ # use this for byte processing
864
+ processnsamp = nsamp
865
+
866
+ # Odd numbered number of samples. Fill in extra blank for following byte calculation.
867
+ if processnsamp % 2 :
851
868
d_signals = np .concatenate ([d_signals , np .array ([0 ])])
852
- nsamp += 1
869
+ processnsamp += 1
853
870
854
871
# The individual bytes to write
855
- bwrite = np .zeros ([int (1.5 * nsamp )], dtype = 'uint8' )
872
+ bwrite = np .zeros ([int (1.5 * processnsamp )], dtype = 'uint8' )
856
873
857
874
# Fill in the byte triplets
858
875
@@ -863,8 +880,10 @@ def wrdatfile(filename, fmt, d_signals):
863
880
# Triplet 3 from lowest 8 bits of sample 2
864
881
bwrite [2 ::3 ] = d_signals [1 ::2 ] & 255
865
882
866
- #bwrite = bwrite.astype('uint8')
867
-
883
+ # If we added an extra sample for byte calculation, remove the last byte (don't write)
884
+ if nsamp % 2 :
885
+ bwrite = bwrite [:- 1 ]
886
+
868
887
elif fmt == '16' :
869
888
# convert to 16 bit two's complement
870
889
d_signals [d_signals < 0 ] = d_signals [d_signals < 0 ] + 65536
0 commit comments