b_asic.utils

B-ASIC Utilities.

b_asic.utils.decompose(a: Sequence[int | float | complex | type[Any] | dtype[Any] | _SupportsDType[dtype[Any]] | tuple[Any, Any] | list[Any] | _DTypeDict | str | None | APyFixed | APyCFixed | APyFloat | APyCFloat], factor: int) list[list[int | float | complex | type[Any] | dtype[Any] | _SupportsDType[dtype[Any]] | tuple[Any, Any] | list[Any] | _DTypeDict | str | None | APyFixed | APyCFixed | APyFloat | APyCFloat]]

Polyphase decompose signal a into factor parts.

Return factor lists, each with every factor value.

Parameters:
aarray

The array to polyphase decompose.

factorint

The number of polyphase components with.

Examples

>>> from b_asic.utils import decompose
...
... a = list(range(6))
... decompose(a, 2)
[[0, 2, 4], [1, 3, 5]]
>>> decompose(a, 3)
[[0, 3], [1, 4], [2, 5]]
b_asic.utils.downsample(a: Sequence[int | float | complex | type[Any] | dtype[Any] | _SupportsDType[dtype[Any]] | tuple[Any, Any] | list[Any] | _DTypeDict | str | None | APyFixed | APyCFixed | APyFloat | APyCFloat], factor: int, phase: int = 0) list[int | float | complex | type[Any] | dtype[Any] | _SupportsDType[dtype[Any]] | tuple[Any, Any] | list[Any] | _DTypeDict | str | None | APyFixed | APyCFixed | APyFloat | APyCFloat]

Downsample a sequence with an integer factor.

Keeps every factor value, starting with phase.

Parameters:
aarray

The array to downsample.

factorint

The factor to downsample with.

phaseint, default: 0

The phase of the downsampling.

Examples

>>> from b_asic.utils import downsample
...
... a = list(range(6))
... downsample(a, 3)
[0, 3]
>>> downsample(a, 3, 1)
[1, 4]
b_asic.utils.float_to_csd(n: float | int) tuple[list[int], int]

Convert a number (int or float) to its Canonical Signed Digit (CSD) representation with MSB first.

Returns (csd, frac_bits) where csd is a list of digits (LSB first) in {-1, 0, 1} and frac_bits is the number of fractional bits (can be negative).

Parameters:
nfloat or int

The number to convert.

b_asic.utils.int_to_csd(n: int) list[int]

Return the Canonical Signed Digit (CSD) representation of an integer with MSB first.

Parameters:
nint

The integer to convert.

b_asic.utils.interleave(*args) list[int | float | complex | type[Any] | dtype[Any] | _SupportsDType[dtype[Any]] | tuple[Any, Any] | list[Any] | _DTypeDict | str | None | APyFixed | APyCFixed | APyFloat | APyCFloat]

Interleave a number of arrays.

Parameters:
*argsa number of arrays

Arrays to interleave. Must be of the same length.

Examples

>>> from b_asic.utils import interleave
...
... a = [1, 2]
... b = [3, 4]
... interleave(a, b)
[1, 3, 2, 4]
>>> c = [-1, 0]
... interleave(a, b, c)
[1, 3, -1, 2, 4, 0]
b_asic.utils.upsample(a: Sequence[int | float | complex | type[Any] | dtype[Any] | _SupportsDType[dtype[Any]] | tuple[Any, Any] | list[Any] | _DTypeDict | str | None | APyFixed | APyCFixed | APyFloat | APyCFloat], factor: int, phase: int = 0) list[int | float | complex | type[Any] | dtype[Any] | _SupportsDType[dtype[Any]] | tuple[Any, Any] | list[Any] | _DTypeDict | str | None | APyFixed | APyCFixed | APyFloat | APyCFloat]

Upsample a sequence with an integer factor.

Insert factor - 1 zeros between every value, starting with phase zeros.

Parameters:
aarray

The array to upsample.

factorint

The factor to upsample with.

phaseint, default: 0

The phase of the upsampling.

Examples

>>> from b_asic.utils import upsample
...
... a = list(range(1, 4))
... upsample(a, 3)
[1, 0, 0, 2, 0, 0, 3, 0, 0]
>>> upsample(a, 3, 1)
[0, 1, 0, 0, 2, 0, 0, 3, 0]