2. Usage#

2.1. Codec B#

Here, we describe the usage of the skecg.cs.codec_b module which contains the implementation of our proposed codec.

Some example scripts using this module can be found inside the scripts directory. Below are the basic steps for using the codec. These steps assume that the ECG signal has been acquired.

Import relevant modules:

from decimal import Decimal
from jax import random
import skecg.cs.codec_b as codec

2.1.1. Encoding#

Prepare the encoder configuration:

n=512
m=256
d=4
w=16
q_nmse_limit = Decimal((0, (1,), -2))
c_nmse_limit = Decimal((0, (2,), -2))
params = codec.EncoderParams(key=random.PRNGKey(0), 
    n=n, m=m, d=d, w=w, adaptive=True,
    q=0, q_nmse_limit=q_nmse_limit, c_nmse_limit=c_nmse_limit)

Encode ECG signal into a bitstream

coded_ecg = codec.encode(params, ecg)

coded_ecg is a (named) tuple of type codec.EncodedData which includes the bitstream for the encoded ECG signal and encoding summary.

Accessing the encoding summary information:

info = coded_ecg.info
print(info)

Accessing the bitstream generated by the encoder

coded_ecg.bits

You can save the bitstream into a file or transmit it over the network.

2.1.2. Decoding#

We will be using the BSBL reconstruction algorithm for decoding. It has a block-size parameter.

Decode ECG signal from the encoded bitstream:

block_size = 32
decoded_ecg = codec.decode(coded_ecg.bits, block_size)

The decoded_ecg result is a named tuple of type codec.DecodedData.

Accessing the decoded ECG signal:

decoded_ecg.x

We provide a utility function to measure compression statistics (bit savings and reconstruction quality):

stats = codec.compression_stats(ecg, coded_ecg, decoded_ecg)