sigpyproc.io.bits#

Bit packing and unpacking functions.

This module contains functions for packing and unpacking bits.

Classes

BitsInfo

Class to handle bits info.

Functions

pack

Pack 1, 2 and 4-bit data into 8-bit numpy array.

unpack

Unpack 1, 2 and 4-bit data packed as 8-bit numpy array.

class sigpyproc.io.bits.BitsInfo(nbits, digi_sigma=NOTHING)[source]#

Bases: object

Class to handle bits info.

Parameters:
nbitsint

Number of bits.

digi_sigmafloat, optional

Sigma used to quantize data, by default default_sigma[nbits].

Attributes:
nbits
digi_sigma
dtype

Type of the data.

itemsize

Element size of this data-type object.

unpack

Whether to unpack/pack bits.

bitfact

Bit factor to unpack/pack bits.

bitorder

Bit order of the packed data.

digi_min

Minimum value used to quantize data.

digi_max

Maximum value used to quantize data.

digi_mean

Mean used to quantize data.

digi_scale

Scale used to quantize data.

Methods

quantize(arr_norm)

Quantize normalized data to given nbit-dependent mean and sigma.

to_dict()

Get a dict of all property attributes.

Raises:
ValueError

if input nbits not in [1, 2, 4, 8, 16, 32].

nbits_to_dtype = {1: '<u1', 2: '<u1', 4: '<u1', 8: '<u1', 16: '<u2', 32: '<f4'}#
default_sigma = {1: 0.5, 2: 1.5, 4: 6, 8: 6, 16: 6, 32: 6}#
default_bitorder = {1: 'little', 2: 'big', 4: 'big', 8: 'invalid', 16: 'invalid', 32: 'invalid'}#
nbits#
digi_sigma#
property dtype#

Type of the data.

Returns:
dtype

data type.

property itemsize#

Element size of this data-type object.

Returns:
int

Element size.

property unpack#

Whether to unpack/pack bits.

Returns:
bool

True if nbits in {1, 2, 4}, False otherwise.

property bitfact#

Bit factor to unpack/pack bits.

Returns:
int

8 // nbits if unpack/pack, 1 otherwise.

property bitorder#

Bit order of the packed data.

Returns:
str

Bit order.

property digi_min#

Minimum value used to quantize data.

Returns:
int

Minimum quantized value.

property digi_max#

Maximum value used to quantize data.

Returns:
int

Maximum quantized value.

property digi_mean#

Mean used to quantize data.

Returns:
float

Mean quantized value.

property digi_scale#

Scale used to quantize data.

Returns:
float

Scale quantized value.

to_dict()[source]#

Get a dict of all property attributes.

Returns:
dict

Property attributes.

quantize(arr_norm)[source]#

Quantize normalized data to given nbit-dependent mean and sigma.

Parameters:
arrndarray

A 1-D numpy array containing the data to quantize.

Returns:
ndarray

Quantized data array to nbits.

Notes

Values outside the dynamic range of the nbits will be clipped.

sigpyproc.io.bits.pack(array, nbits, packed=None, *, bitorder='big')[source]#

Pack 1, 2 and 4-bit data into 8-bit numpy array.

Parameters:
arrayNDArray[uint8]

Array to pack.

nbitsint

Number of bits of the unpacked data.

packedNDArray[uint8], optional

Array to pack into.

bitorderstr, optional

Bit order in which to pack the data.

Returns:
NDArray[uint8]

Packed array.

Raises:
ValueError

if input array is not uint8 type if nbits is not 1, 2, or 4 if bitorder is not big or little if unpacked array is not of the correct size

sigpyproc.io.bits.unpack(array, nbits, unpacked=None, *, bitorder='big')[source]#

Unpack 1, 2 and 4-bit data packed as 8-bit numpy array.

Parameters:
arrayNDArray[uint8]

Array to unpack.

nbitsint

Number of bits of the packed data.

unpackedNDArray[uint8], optional

Array to unpack into.

bitorderstr, optional

Bit order of the packed data.

Returns:
NDArray[uint8]

Unpacked array.

Raises:
ValueError

if input array is not uint8 type if nbits is not 1, 2, or 4 if bitorder is not big or little if unpacked array is not of the correct size