From d86cbdb83278186eb18008bace52a92abd0b26da Mon Sep 17 00:00:00 2001 From: Oscar Gustafsson <oscar.gustafsson@gmail.com> Date: Fri, 7 Apr 2023 13:07:34 +0200 Subject: [PATCH] Add utils module and functions --- b_asic/utils.py | 47 +++++++++++++++++++++++++++++++++++++++ docs_sphinx/api/index.rst | 1 + docs_sphinx/api/utils.rst | 7 ++++++ test/test_utils.py | 22 ++++++++++++++++++ 4 files changed, 77 insertions(+) create mode 100644 b_asic/utils.py create mode 100644 docs_sphinx/api/utils.rst create mode 100644 test/test_utils.py diff --git a/b_asic/utils.py b/b_asic/utils.py new file mode 100644 index 00000000..99a5f516 --- /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 f51f6ce2..35981a06 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 00000000..b2487bd2 --- /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 00000000..b67446a9 --- /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] -- GitLab