Skip to content
Snippets Groups Projects
Commit d86cbdb8 authored by Oscar Gustafsson's avatar Oscar Gustafsson :bicyclist:
Browse files

Add utils module and functions

parent 3f9dd294
No related branches found
No related tags found
1 merge request!280Add utils module and functions
Pipeline #93884 passed
"""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]
......@@ -19,3 +19,4 @@ API
signal_generator.rst
simulation.rst
special_operations.rst
utils.rst
****************
``b_asic.utils``
****************
.. automodule:: b_asic.utils
:members:
:undoc-members:
"""
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]
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment