diff --git a/b_asic/signal_generator.py b/b_asic/signal_generator.py index 6b6eb94ddc3e4584b469da0f5310071af08d48ea..27b12518651275b0e4ad8e2357b96d75da9752d0 100644 --- a/b_asic/signal_generator.py +++ b/b_asic/signal_generator.py @@ -268,6 +268,34 @@ class Uniform(SignalGenerator): return f"Uniform({args})" +class Delay(SignalGenerator): + """ + Signal generator that delays the value of another signal generator. + + This can used to easily delay a sequence during simulation. + + .. note:: Although the purpose is to delay, it is also possible to look ahead by + providing a negative delay. + + Parameters + ---------- + generator : SignalGenerator + The signal generator to delay the output of. + delay : int, default: 1 + The number of time units to delay the generated signal. + """ + + def __init__(self, generator: SignalGenerator, delay: int = 1) -> None: + self._generator = generator + self._delay = delay + + def __call__(self, time: int) -> complex: + return self._generator(time - self._delay) + + def __repr__(self) -> str: + return f"Delay({self._generator!r}, {self._delay})" + + class _AddGenerator(SignalGenerator): """ Signal generator that adds two signals. diff --git a/test/test_signal_generator.py b/test/test_signal_generator.py index 97093a52102d510ec5741844e9cab12496145175..b982e6cc6530d4b04d9dc43a1187a1bd5dee2e30 100644 --- a/test/test_signal_generator.py +++ b/test/test_signal_generator.py @@ -4,6 +4,7 @@ import pytest from b_asic.signal_generator import ( Constant, + Delay, Gaussian, Impulse, Sinusoid, @@ -99,6 +100,22 @@ def test_sinusoid(): assert str(g) == "Sinusoid(0.5, 0.25)" +def test_delay(): + gref = Sinusoid(0.5) + g = Delay(gref) + assert g(0) == gref(-1) + assert g(2) == gref(1) + + assert str(g) == "Delay(Sinusoid(0.5), 1)" + + gref = Sinusoid(0.5) + g = Delay(gref, 3) + assert g(0) == gref(-3) + assert g(5) == gref(2) + + assert str(g) == "Delay(Sinusoid(0.5), 3)" + + def test_gaussian(): g = Gaussian(1234) assert g(0) == pytest.approx(-1.6038368053963015)