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

Add division support for generators

parent 457e040c
No related branches found
No related tags found
1 merge request!165Add division support for generators
Pipeline #88945 passed
......@@ -49,6 +49,16 @@ class SignalGenerator:
return MultGenerator(self, Constant(other))
return MultGenerator(self, other)
def __truediv__(self, other) -> "MulGenerator":
if isinstance(other, Number):
return DivGenerator(self, Constant(other))
return DivGenerator(self, other)
def __rtruediv__(self, other) -> "MulGenerator":
if isinstance(other, Number):
return DivGenerator(Constant(other), self)
return DivGenerator(other, self)
class Impulse(SignalGenerator):
"""
......@@ -187,3 +197,18 @@ class MultGenerator:
def __call__(self, time: int) -> complex:
return self._a(time) * self._b(time)
class DivGenerator:
"""
Signal generator that divides two signals.
"""
def __init__(
self, a: SignalGenerator, b: SignalGenerator
) -> Callable[[int], complex]:
self._a = a
self._b = b
def __call__(self, time: int) -> complex:
return self._a(time) / self._b(time)
......@@ -127,3 +127,28 @@ def test_multiplication():
assert g(1) == pytest.approx(sqrt(2))
assert g(2) == pytest.approx(-sqrt(2))
assert g(3) == pytest.approx(-sqrt(2))
g = Step(1) * Sinusoid(0.5, 0.25)
assert g(0) == 0
assert g(1) == pytest.approx(sqrt(2) / 2)
assert g(2) == pytest.approx(-sqrt(2) / 2)
assert g(3) == pytest.approx(-sqrt(2) / 2)
def test_division():
g = Step() / 2
assert g(-1) == 0.0
assert g(0) == 0.5
assert g(1) == 0.5
assert g(2) == 0.5
g = 0.5 / Step()
assert g(0) == 0.5
assert g(1) == 0.5
assert g(2) == 0.5
g = Sinusoid(0.5, 0.25) / (0.5 * Step())
assert g(0) == pytest.approx(sqrt(2))
assert g(1) == pytest.approx(sqrt(2))
assert g(2) == pytest.approx(-sqrt(2))
assert g(3) == pytest.approx(-sqrt(2))
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