Newer
Older
"""
B-ASIC signal generators
These can be used as input to Simulation to algorithmically provide signal values.
"""
from typing import Callable, Sequence
class SignalGenerator:
"""
Base class for signal generators.
Handles operator overloading and defined the ``__call__`` method that should be overridden.
"""
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
def __call__(self, time: int) -> complex:
raise NotImplementedError
def __add__(self, other) -> "AddGenerator":
if isinstance(other, Number):
return AddGenerator(self, Constant(other))
return AddGenerator(self, other)
def __radd__(self, other) -> "AddGenerator":
if isinstance(other, Number):
return AddGenerator(self, Constant(other))
return AddGenerator(self, other)
def __sub__(self, other) -> "SubGenerator":
if isinstance(other, Number):
return SubGenerator(self, Constant(other))
return SubGenerator(self, other)
def __rsub__(self, other) -> "SubGenerator":
if isinstance(other, Number):
return SubGenerator(Constant(other), self)
return SubGenerator(other, self)
def __mul__(self, other) -> "MulGenerator":
if isinstance(other, Number):
return MultGenerator(self, Constant(other))
return MultGenerator(self, other)
def __rmul__(self, other) -> "MulGenerator":
if isinstance(other, Number):
return MultGenerator(self, Constant(other))
return MultGenerator(self, other)
class Impulse(SignalGenerator):
"""
Signal generator that creates an impulse at a given delay.
Parameters
----------
delay : int, default: 0
The delay before the signal goes to 1 for one sample.
"""
def __init__(self, delay: int = 0) -> Callable[[int], complex]:
self._delay = delay
def __call__(self, time: int) -> complex:
return 1 if time == self._delay else 0
class Step(SignalGenerator):
"""
Signal generator that creates a step at a given delay.
Parameters
----------
delay : int, default: 0
The delay before the signal goes to 1.
"""
def __init__(self, delay: int = 0) -> Callable[[int], complex]:
self._delay = delay
def __call__(self, time: int) -> complex:
return 1 if time >= self._delay else 0
class Constant(SignalGenerator):
"""
Signal generator that outputs a constant value.
Parameters
----------
constant : complex, default: 1.0
The constant.
"""
def __init__(self, constant: complex = 1.0) -> Callable[[int], complex]:
self._constant = constant
def __call__(self, time: int) -> complex:
return self._constant
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
class ZeroPad(SignalGenerator):
"""
Signal generator that pads a sequence with zeros.
Parameters
----------
data : 1-D array
The data that should be padded.
"""
def __init__(self, data: Sequence[complex]) -> Callable[[int], complex]:
self._data = data
self._len = len(data)
def __call__(self, time: int) -> complex:
if 0 <= time < self._len:
return self._data[time]
return 0.0
class Sinusoid(SignalGenerator):
"""
Signal generator that generates a sinusoid.
Parameters
----------
frequency : float
The normalized frequency of the sinusoid. Should normally be in the
interval [0, 1], where 1 corresponds to half the sample rate.
phase : float, default: 0
The normalized phase offset.
"""
def __init__(
self, frequency: float, phase: float = 0.0
) -> Callable[[int], complex]:
self._frequency = frequency
self._phase = phase
def __call__(self, time: int) -> complex:
return sin(pi * (self._frequency * time + self._phase))
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
class AddGenerator:
"""
Signal generator that adds 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)
class SubGenerator:
"""
Signal generator that subtracts 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)
class MultGenerator:
"""
Signal generator that multiplies 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)