diff --git a/b_asic/utils.py b/b_asic/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..99a5f5169a60f67d518ba2db722e73bd6ebe25ac --- /dev/null +++ b/b_asic/utils.py @@ -0,0 +1,47 @@ +"""B-ASIC Utils.""" + +from typing import List, Sequence + +from b_asic.types import Num + + +def interleave(*args) -> List[Num]: + """ + Interleave a number of arrays. + + For the input ``interleave([1, 2], [3, 4])``, return ``[1, 2, 3, 4]``. + + Parameters + ---------- + *args : a number of arrays + Arrays to interleave. Must be of the same length. + + Returns + ------- + + + """ + return [val for tup in zip(*args) for val in tup] + + +def downsample(a: Sequence[Num], factor: int, phase: int = 0) -> List[Num]: + """ + Downsample a sequence with an integer factor. + + Keeps every *factor* value, starting with *phase*. + + Parameters + ---------- + a : array + The array to downsample. + factor : int + The factor to downsample with. + phase : int, default: 0 + The phase of the downsampling. + + Returns + ------- + + + """ + return a[phase::factor] diff --git a/docs_sphinx/api/index.rst b/docs_sphinx/api/index.rst index f51f6ce2ee7678be2c5cf1b1d5afd4b6a23b01be..35981a0622ae7aea46d716266c731f2542d2e8a4 100644 --- a/docs_sphinx/api/index.rst +++ b/docs_sphinx/api/index.rst @@ -19,3 +19,4 @@ API signal_generator.rst simulation.rst special_operations.rst + utils.rst diff --git a/docs_sphinx/api/utils.rst b/docs_sphinx/api/utils.rst new file mode 100644 index 0000000000000000000000000000000000000000..b2487bd2086bd0c69ffbb0173b7a82e736e95ab0 --- /dev/null +++ b/docs_sphinx/api/utils.rst @@ -0,0 +1,7 @@ +**************** +``b_asic.utils`` +**************** + +.. automodule:: b_asic.utils + :members: + :undoc-members: diff --git a/test/test_utils.py b/test/test_utils.py new file mode 100644 index 0000000000000000000000000000000000000000..b67446a98fc1364ddeb4affcd156d9fb2773af83 --- /dev/null +++ b/test/test_utils.py @@ -0,0 +1,22 @@ +""" +B-ASIC test suite for the utils module. +""" + +from b_asic.utils import downsample, interleave + + +def test_interleave(): + a = [1, 2] + b = [3, 4] + assert interleave(a, b) == [1, 3, 2, 4] + + c = [5, 6] + assert interleave(a, b, c) == [1, 3, 5, 2, 4, 6] + + +def test_downsample(): + a = list(range(12)) + assert downsample(a, 6) == [0, 6] + assert downsample(a, 6, 3) == [3, 9] + assert downsample(a, 4) == [0, 4, 8] + assert downsample(a, 3, 1) == [1, 4, 7, 10]