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

Add str-support for generators

parent dd7aaafa
No related branches found
No related tags found
1 merge request!166Add str-support for generators
Pipeline #88949 passed
......@@ -76,6 +76,9 @@ class Impulse(SignalGenerator):
def __call__(self, time: int) -> complex:
return 1 if time == self._delay else 0
def __repr__(self):
return f"Impulse({self._delay})" if self._delay else "Impulse()"
class Step(SignalGenerator):
"""
......@@ -93,6 +96,9 @@ class Step(SignalGenerator):
def __call__(self, time: int) -> complex:
return 1 if time >= self._delay else 0
def __repr__(self):
return f"Step({self._delay})" if self._delay else "Step()"
class Constant(SignalGenerator):
"""
......@@ -110,6 +116,9 @@ class Constant(SignalGenerator):
def __call__(self, time: int) -> complex:
return self._constant
def __str__(self):
return f"{self._constant}"
class ZeroPad(SignalGenerator):
"""
......@@ -130,6 +139,9 @@ class ZeroPad(SignalGenerator):
return self._data[time]
return 0.0
def __repr__(self):
return f"ZeroPad({self._data})"
class Sinusoid(SignalGenerator):
"""
......@@ -153,6 +165,13 @@ class Sinusoid(SignalGenerator):
def __call__(self, time: int) -> complex:
return sin(pi * (self._frequency * time + self._phase))
def __repr__(self):
return (
f"Sinusoid({self._frequency}, {self._phase})"
if self._phase
else f"Sinusoid({self._frequency})"
)
class AddGenerator:
"""
......@@ -168,6 +187,9 @@ class AddGenerator:
def __call__(self, time: int) -> complex:
return self._a(time) + self._b(time)
def __str__(self):
return f"{self._a} + {self._b}"
class SubGenerator:
"""
......@@ -183,6 +205,9 @@ class SubGenerator:
def __call__(self, time: int) -> complex:
return self._a(time) - self._b(time)
def __str__(self):
return f"{self._a} - {self._b}"
class MultGenerator:
"""
......@@ -198,6 +223,19 @@ class MultGenerator:
def __call__(self, time: int) -> complex:
return self._a(time) * self._b(time)
def __str__(self):
a = (
f"({self._a})"
if isinstance(self._a, (AddGenerator, SubGenerator))
else f"{self._a}"
)
b = (
f"({self._b})"
if isinstance(self._b, (AddGenerator, SubGenerator))
else f"{self._b}"
)
return f"{a} * {b}"
class DivGenerator:
"""
......@@ -212,3 +250,19 @@ class DivGenerator:
def __call__(self, time: int) -> complex:
return self._a(time) / self._b(time)
def __str__(self):
a = (
f"({self._a})"
if isinstance(self._a, (AddGenerator, SubGenerator))
else f"{self._a}"
)
b = (
f"({self._b})"
if isinstance(
self._b,
(AddGenerator, SubGenerator, MultGenerator, DivGenerator),
)
else f"{self._b}"
)
return f"{a} / {b}"
......@@ -12,12 +12,16 @@ def test_impulse():
assert g(1) == 0
assert g(2) == 0
assert str(g) == "Impulse()"
g = Impulse(1)
assert g(-1) == 0
assert g(0) == 0
assert g(1) == 1
assert g(2) == 0
assert str(g) == "Impulse(1)"
def test_step():
g = Step()
......@@ -26,12 +30,16 @@ def test_step():
assert g(1) == 1
assert g(2) == 1
assert str(g) == "Step()"
g = Step(1)
assert g(-1) == 0
assert g(0) == 0
assert g(1) == 1
assert g(2) == 1
assert str(g) == "Step(1)"
def test_constant():
g = Constant()
......@@ -40,12 +48,16 @@ def test_constant():
assert g(1) == 1
assert g(2) == 1
assert str(g) == "1.0"
g = Constant(0.5)
assert g(-1) == 0.5
assert g(0) == 0.5
assert g(1) == 0.5
assert g(2) == 0.5
assert str(g) == "0.5"
def test_zeropad():
g = ZeroPad([0.4, 0.6])
......@@ -54,6 +66,8 @@ def test_zeropad():
assert g(1) == 0.6
assert g(2) == 0
assert str(g) == "ZeroPad([0.4, 0.6])"
def test_sinusoid():
g = Sinusoid(0.5)
......@@ -62,12 +76,16 @@ def test_sinusoid():
assert g(2) == pytest.approx(0)
assert g(3) == -1
assert str(g) == "Sinusoid(0.5)"
g = Sinusoid(0.5, 0.25)
assert g(0) == pytest.approx(sqrt(2) / 2)
assert g(1) == pytest.approx(sqrt(2) / 2)
assert g(2) == pytest.approx(-sqrt(2) / 2)
assert g(3) == pytest.approx(-sqrt(2) / 2)
assert str(g) == "Sinusoid(0.5, 0.25)"
def test_addition():
g = Impulse() + Impulse(2)
......@@ -77,20 +95,26 @@ def test_addition():
assert g(2) == 1
assert g(3) == 0
g = 1 + Impulse(2)
assert str(g) == "Impulse() + Impulse(2)"
g = 1.0 + Impulse(2)
assert g(-1) == 1
assert g(0) == 1
assert g(1) == 1
assert g(2) == 2
assert g(3) == 1
g = Impulse(1) + 1
assert str(g) == "Impulse(2) + 1.0"
g = Impulse(1) + 1.0
assert g(-1) == 1
assert g(0) == 1
assert g(1) == 2
assert g(2) == 1
assert g(3) == 1
assert str(g) == "Impulse(1) + 1.0"
def test_subtraction():
g = Impulse() - Impulse(2)
......@@ -100,20 +124,26 @@ def test_subtraction():
assert g(2) == -1
assert g(3) == 0
g = 1 - Impulse(2)
assert str(g) == "Impulse() - Impulse(2)"
g = 1.0 - Impulse(2)
assert g(-1) == 1
assert g(0) == 1
assert g(1) == 1
assert g(2) == 0
assert g(3) == 1
g = Impulse(2) - 1
assert str(g) == "1.0 - Impulse(2)"
g = Impulse(2) - 1.0
assert g(-1) == -1
assert g(0) == -1
assert g(1) == -1
assert g(2) == 0
assert g(3) == -1
assert str(g) == "Impulse(2) - 1.0"
def test_multiplication():
g = Impulse() * 0.5
......@@ -122,17 +152,23 @@ def test_multiplication():
assert g(1) == 0
assert g(2) == 0
assert str(g) == "Impulse() * 0.5"
g = 2 * Sinusoid(0.5, 0.25)
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))
g = Step(1) * Sinusoid(0.5, 0.25)
assert str(g) == "Sinusoid(0.5, 0.25) * 2"
g = Step(1) * (Sinusoid(0.5, 0.25) + 1.0)
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)
assert g(1) == pytest.approx(sqrt(2) / 2 + 1)
assert g(2) == pytest.approx(-sqrt(2) / 2 + 1)
assert g(3) == pytest.approx(-sqrt(2) / 2 + 1)
assert str(g) == "Step(1) * (Sinusoid(0.5, 0.25) + 1.0)"
def test_division():
......@@ -142,13 +178,19 @@ def test_division():
assert g(1) == 0.5
assert g(2) == 0.5
assert str(g) == "Step() / 2"
g = 0.5 / Step()
assert g(0) == 0.5
assert g(1) == 0.5
assert g(2) == 0.5
assert str(g) == "0.5 / Step()"
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))
assert str(g) == "Sinusoid(0.5, 0.25) / (Step() * 0.5)"
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