diff --git a/src/wfdb/io/_signal.py b/src/wfdb/io/_signal.py index 92e52741..ec081aeb 100644 --- a/src/wfdb/io/_signal.py +++ b/src/wfdb/io/_signal.py @@ -12,6 +12,26 @@ MAX_I32 = 2147483647 MIN_I32 = -2147483648 +CHECKSUM_MODULUS = 65536 + + +def _checksum(values): + """Return the WFDB checksum of one channel of digital samples. + + Parameters + ---------- + values : array_like + The digital samples of a single channel. + + Returns + ------- + int + The sum of the samples modulo 65536. + + """ + return int(np.sum(values) % CHECKSUM_MODULUS) + + # Formats in which all samples align with integer (power-of-two) boundaries ALIGNED_FMTS = ["8", "16", "32", "61", "80", "160"] # Formats in which not all samples align with integer boundaries @@ -941,10 +961,9 @@ def calc_checksum(self, expanded=False): """ if expanded: - cs = [int(np.sum(s) % 65536) for s in self.e_d_signal] + cs = [_checksum(s) for s in self.e_d_signal] else: - cs = np.sum(self.d_signal, 0) % 65536 - cs = [int(c) for c in cs] + cs = [_checksum(s) for s in self.d_signal.T] return cs def wr_dat_files(self, expanded=False, write_dir="", wfdb_archive=None): diff --git a/src/wfdb/io/convert/matlab.py b/src/wfdb/io/convert/matlab.py index 717c3ebd..6d2e7f83 100644 --- a/src/wfdb/io/convert/matlab.py +++ b/src/wfdb/io/convert/matlab.py @@ -4,7 +4,7 @@ import numpy as np -from wfdb.io._signal import _digi_nan +from wfdb.io._signal import _checksum, _digi_nan from wfdb.io.record import rdrecord @@ -185,13 +185,12 @@ def wfdb_to_mat(record_name, pn_dir=None, sampfrom=0, sampto=None, channels=None # Ensure the signal size does not exceed the 2^31 byte limit max_length = int((2**31) / bytes_per_element / record.n_sig) - desired_length = record.sig_len # Snip record - if desired_length > max_length: + if record.sig_len > max_length: raise Exception("Can't write .mat file: data size exceeds 2GB limit") # Bytes of actual data - bytes_of_data = bytes_per_element * record.n_sig * desired_length + bytes_of_data = bytes_per_element * record.n_sig * record.sig_len # This is the remaining number of bytes that don't fit into integer # multiple of 8: i.e. if 18 bytes, bytes_remain = 2, from 17 to 18 bytes_remain = bytes_of_data % 8 @@ -241,7 +240,7 @@ def wfdb_to_mat(record_name, pn_dir=None, sampfrom=0, sampto=None, channels=None # Number of signals (4 bytes) f.write(struct.pack("