blocksim 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- blocksim/__init__.py +524 -0
- blocksim/_logging.py +125 -0
- blocksim/aerospace.py +186 -0
- blocksim/analysis.py +1143 -0
- blocksim/astro.py +660 -0
- blocksim/atmosphere.py +150 -0
- blocksim/auxiliar.py +218 -0
- blocksim/avionics.py +992 -0
- blocksim/comm.py +237 -0
- blocksim/continuous.py +586 -0
- blocksim/control.py +159 -0
- blocksim/core.py +4522 -0
- blocksim/discontinuities.py +334 -0
- blocksim/discrete.py +833 -0
- blocksim/distributed.py +559 -0
- blocksim/driveline.py +496 -0
- blocksim/electrical.py +1086 -0
- blocksim/f16.py +409 -0
- blocksim/f16_hifi.py +250 -0
- blocksim/fmu.py +505 -0
- blocksim/fusion.py +984 -0
- blocksim/gemini.py +422 -0
- blocksim/hydraulics.py +816 -0
- blocksim/jsbsim.py +1284 -0
- blocksim/manipulator.py +928 -0
- blocksim/math.py +254 -0
- blocksim/mechanics.py +231 -0
- blocksim/power.py +1063 -0
- blocksim/process.py +595 -0
- blocksim/propulsion.py +1219 -0
- blocksim/py.typed +0 -0
- blocksim/radar.py +111 -0
- blocksim/robotics.py +164 -0
- blocksim/rotation.py +154 -0
- blocksim/simulator_claude.py +432 -0
- blocksim/sources.py +1846 -0
- blocksim/structural.py +613 -0
- blocksim/symbolic.py +934 -0
- blocksim/table.py +355 -0
- blocksim/thermal.py +395 -0
- blocksim/thermo.py +657 -0
- blocksim/vehicle.py +1274 -0
- blocksim-0.1.0.dist-info/METADATA +256 -0
- blocksim-0.1.0.dist-info/RECORD +47 -0
- blocksim-0.1.0.dist-info/WHEEL +4 -0
- blocksim-0.1.0.dist-info/licenses/COPYING +674 -0
- blocksim-0.1.0.dist-info/licenses/COPYING.LESSER +165 -0
blocksim/sources.py
ADDED
|
@@ -0,0 +1,1846 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Sources: the signals that drive a diagram.
|
|
3
|
+
|
|
4
|
+
Blocks with outputs and no inputs. Between them they cover the standard
|
|
5
|
+
test inputs -- step, ramp, sine, chirp, pulse train, noise -- plus
|
|
6
|
+
`Constant` for a fixed value, `Equation` for anything expressible as a
|
|
7
|
+
function of time, and `FromFile` for measured data.
|
|
8
|
+
|
|
9
|
+
Two things about them are worth knowing before choosing one.
|
|
10
|
+
|
|
11
|
+
**Discontinuous sources carry a clock, not a discontinuity.** A `Step`
|
|
12
|
+
that simply returned different values either side of its transition
|
|
13
|
+
would be a jump in the middle of an integration region, and an adaptive
|
|
14
|
+
solver meeting one takes a step, sees its error estimate explode,
|
|
15
|
+
rejects it, and repeats until the step is small enough to hide the jump.
|
|
16
|
+
So `Step`, `Pulse`, `Square`, `Sawtooth`, `Triangular`,
|
|
17
|
+
`RepeatingStair` and `Random` bind a clock on their transition times
|
|
18
|
+
instead: the scheduler cuts the timeline there, and each region is
|
|
19
|
+
integrated across a signal that is smooth within it. The
|
|
20
|
+
transition lands exactly on the requested time rather than within a
|
|
21
|
+
tolerance of it, which is why a sampled response in this library can be
|
|
22
|
+
compared against a closed form to machine precision.
|
|
23
|
+
|
|
24
|
+
That is also why a `Step` in a diagram is not a problem for `fuse()`: it
|
|
25
|
+
keeps a clock but its blocks do nothing on a tick, so the equations do
|
|
26
|
+
not change between samples.
|
|
27
|
+
|
|
28
|
+
**Everything here is stateless and reads only `t`.** No source has
|
|
29
|
+
inputs, so none of them can participate in an algebraic loop, and their
|
|
30
|
+
output at any instant depends on nothing the solver has computed. A
|
|
31
|
+
source can be evaluated at a trial time the solver later rejects. That
|
|
32
|
+
is harmless for every deterministic source here, and is the reason
|
|
33
|
+
`Random` is on a clock: a new draw per accepted sample rather than a
|
|
34
|
+
new draw per solver probe.
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
import numpy as np
|
|
38
|
+
from typing import Any
|
|
39
|
+
from .core import Block, Clock, PeriodicClock, ListClock
|
|
40
|
+
|
|
41
|
+
__all__ = [
|
|
42
|
+
"Constant",
|
|
43
|
+
"Equation",
|
|
44
|
+
"FromFile",
|
|
45
|
+
"Pulse",
|
|
46
|
+
"Ramp",
|
|
47
|
+
"Random",
|
|
48
|
+
"Repeating",
|
|
49
|
+
"Sine",
|
|
50
|
+
"Step",
|
|
51
|
+
"Square",
|
|
52
|
+
"Triangular",
|
|
53
|
+
"Sawtooth",
|
|
54
|
+
"Chirp",
|
|
55
|
+
"BandLimitedWhiteNoise",
|
|
56
|
+
"RepeatingStair",
|
|
57
|
+
]
|
|
58
|
+
|
|
59
|
+
from collections.abc import Callable, Iterable
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def is_iterable_of_floats(obj) -> bool:
|
|
63
|
+
"""Checks if an object is an iterable containing only float or integer values.
|
|
64
|
+
|
|
65
|
+
Args:
|
|
66
|
+
obj: The object to verify.
|
|
67
|
+
|
|
68
|
+
Returns:
|
|
69
|
+
True if the object is an Iterable and all its elements are floats or
|
|
70
|
+
integers, and it is not a string. Otherwise False.
|
|
71
|
+
"""
|
|
72
|
+
if not isinstance(obj, Iterable) or isinstance(obj, str):
|
|
73
|
+
return False
|
|
74
|
+
return all(isinstance(x, (float, int)) for x in obj)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def is_iterable_iterable_of_floats(obj, n: int) -> bool:
|
|
78
|
+
"""Checks if an object is an iterable of iterables, each containing exactly n floats/integers.
|
|
79
|
+
|
|
80
|
+
Args:
|
|
81
|
+
obj: The object to verify.
|
|
82
|
+
n: The expected length of each inner iterable.
|
|
83
|
+
|
|
84
|
+
Returns:
|
|
85
|
+
True if all conditions are met, otherwise False.
|
|
86
|
+
"""
|
|
87
|
+
if not isinstance(obj, Iterable) or isinstance(obj, str):
|
|
88
|
+
return False
|
|
89
|
+
obj_list = list(obj)
|
|
90
|
+
if len(obj_list) == 0:
|
|
91
|
+
return False
|
|
92
|
+
for sub in obj_list:
|
|
93
|
+
if not isinstance(sub, Iterable) or isinstance(sub, str):
|
|
94
|
+
return False
|
|
95
|
+
sub_list = list(sub)
|
|
96
|
+
if len(sub_list) != n:
|
|
97
|
+
return False
|
|
98
|
+
if not all(isinstance(x, (float, int)) for x in sub_list):
|
|
99
|
+
return False
|
|
100
|
+
return True
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
class Constant(Block):
|
|
104
|
+
"""A fixed value, of any width.
|
|
105
|
+
|
|
106
|
+
`value` may be a scalar or any iterable; its length sets the number
|
|
107
|
+
of output ports. The commonest block in any diagram, and usually the
|
|
108
|
+
right way to hold an input at a trim value, feed a setpoint, or stand
|
|
109
|
+
in for a signal that is not modelled yet.
|
|
110
|
+
|
|
111
|
+
To change it during a run, assign to `.value` between `run()` calls
|
|
112
|
+
-- it is read on every evaluation, not cached at construction. To
|
|
113
|
+
change it DURING a run, use `Step`, an `Equation`, or an event.
|
|
114
|
+
|
|
115
|
+
Inputs : none Outputs : y0..yn States : none
|
|
116
|
+
"""
|
|
117
|
+
|
|
118
|
+
def __init__(
|
|
119
|
+
self,
|
|
120
|
+
value: float | Iterable[float] = 1.0,
|
|
121
|
+
name: str | None = None,
|
|
122
|
+
outputs: list[tuple[str, Any]] | None = None,
|
|
123
|
+
):
|
|
124
|
+
"""Initializes the Constant block.
|
|
125
|
+
|
|
126
|
+
Args:
|
|
127
|
+
value: The constant output value(s). Can be a float, an iterable of floats,
|
|
128
|
+
or a numpy array.
|
|
129
|
+
name: Optional block name.
|
|
130
|
+
outputs: Optional output port configuration specs.
|
|
131
|
+
|
|
132
|
+
Raises:
|
|
133
|
+
TypeError: If `value` is not a float, iterable of floats, or numpy array.
|
|
134
|
+
ValueError: If `outputs` length does not match the dimensions of `value`.
|
|
135
|
+
"""
|
|
136
|
+
if isinstance(value, (float, int)):
|
|
137
|
+
num_outputs = 1
|
|
138
|
+
self.value = np.array([float(value)], dtype=float)
|
|
139
|
+
elif is_iterable_of_floats(value):
|
|
140
|
+
self.value = np.array([float(v) for v in value], dtype=float)
|
|
141
|
+
num_outputs = len(self.value)
|
|
142
|
+
elif isinstance(value, np.ndarray):
|
|
143
|
+
self.value = np.atleast_1d(value).astype(float)
|
|
144
|
+
num_outputs = len(self.value)
|
|
145
|
+
else:
|
|
146
|
+
raise TypeError(
|
|
147
|
+
"value must be a float, an iterable of floats, or a numpy array"
|
|
148
|
+
)
|
|
149
|
+
|
|
150
|
+
if outputs is not None:
|
|
151
|
+
if len(outputs) != num_outputs:
|
|
152
|
+
raise ValueError(
|
|
153
|
+
f"Length of outputs ({len(outputs)}) does not match num_outputs ({num_outputs})"
|
|
154
|
+
)
|
|
155
|
+
resolved_outputs = outputs
|
|
156
|
+
else:
|
|
157
|
+
resolved_outputs = [(f"y{i}", None) for i in range(num_outputs)]
|
|
158
|
+
|
|
159
|
+
super().__init__(
|
|
160
|
+
num_inputs=0,
|
|
161
|
+
num_outputs=num_outputs,
|
|
162
|
+
name=name,
|
|
163
|
+
outputs=resolved_outputs,
|
|
164
|
+
)
|
|
165
|
+
|
|
166
|
+
def output(self, t):
|
|
167
|
+
"""Returns the constant output at the current time.
|
|
168
|
+
|
|
169
|
+
Args:
|
|
170
|
+
t: The current simulation time.
|
|
171
|
+
|
|
172
|
+
Returns:
|
|
173
|
+
A numpy array containing the constant value(s).
|
|
174
|
+
"""
|
|
175
|
+
return self.value
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
class Step(Block):
|
|
179
|
+
"""Step input block.
|
|
180
|
+
|
|
181
|
+
Generates a step signal supporting single/multiple output ports and single/multiple
|
|
182
|
+
step time transitions per port. The step times are fixed at construction, so the
|
|
183
|
+
transitions are scheduled exactly with a one-shot ListClock.
|
|
184
|
+
|
|
185
|
+
Attributes:
|
|
186
|
+
|
|
187
|
+
step_time (float | Iterable[float]): The time transition(s) where the step(s) occur.
|
|
188
|
+
step_value (float ``| Iterable[float] |`` Iterable[Iterable[float]]): The value(s) of the step(s).
|
|
189
|
+
initial_value (float ``| Iterable[float] |`` None): The initial value(s) of the output before any step.
|
|
190
|
+
port_times (list[list[float]]): Time transitions for each output port.
|
|
191
|
+
port_values (list[list[float]]): Value transitions (including initial value) for each output port.
|
|
192
|
+
clock (ListClock): The bound clock scheduling transition events.
|
|
193
|
+
"""
|
|
194
|
+
|
|
195
|
+
def __init__(
|
|
196
|
+
self,
|
|
197
|
+
step_time: float | Iterable[float] = 0.0,
|
|
198
|
+
step_value: float | Iterable[float] | Iterable[Iterable[float]] = 1.0,
|
|
199
|
+
initial_value: float | Iterable[float] | None = None,
|
|
200
|
+
name: str | None = None,
|
|
201
|
+
outputs: list[tuple[str, Any]] | None = None,
|
|
202
|
+
):
|
|
203
|
+
"""Initializes the Step block.
|
|
204
|
+
|
|
205
|
+
Args:
|
|
206
|
+
step_time: The transition time(s). Can be a float or an iterable of floats.
|
|
207
|
+
step_value: The step value(s). Can be a float, iterable of floats, or iterable of iterables.
|
|
208
|
+
initial_value: The initial output value(s) before transition. Defaults to 0.0.
|
|
209
|
+
name: Optional block name.
|
|
210
|
+
outputs: Optional output port configuration specs.
|
|
211
|
+
|
|
212
|
+
Raises:
|
|
213
|
+
TypeError: If parameter types do not match the expected specifications.
|
|
214
|
+
ValueError: If length or size mismatches occur between parameters.
|
|
215
|
+
"""
|
|
216
|
+
self.step_time = step_time
|
|
217
|
+
self.step_value = step_value
|
|
218
|
+
self.initial_value = initial_value
|
|
219
|
+
|
|
220
|
+
# Parse step_time
|
|
221
|
+
if isinstance(step_time, (float, int)):
|
|
222
|
+
times_list = [float(step_time)]
|
|
223
|
+
is_times_single = True
|
|
224
|
+
elif is_iterable_of_floats(step_time):
|
|
225
|
+
times_list = [float(t) for t in step_time]
|
|
226
|
+
is_times_single = False
|
|
227
|
+
else:
|
|
228
|
+
raise TypeError("step_time must be a float or an iterable of floats")
|
|
229
|
+
|
|
230
|
+
num_step_changes = len(times_list)
|
|
231
|
+
|
|
232
|
+
# Parse step_value and determine num_outputs:
|
|
233
|
+
if is_times_single:
|
|
234
|
+
if isinstance(step_value, (float, int)):
|
|
235
|
+
num_outputs = 1
|
|
236
|
+
values_list = [[float(step_value)]]
|
|
237
|
+
elif is_iterable_of_floats(step_value):
|
|
238
|
+
num_outputs = len(list(step_value))
|
|
239
|
+
values_list = [[float(v) for v in step_value]]
|
|
240
|
+
elif isinstance(step_value, Iterable) and not isinstance(step_value, str):
|
|
241
|
+
val_list = list(step_value)
|
|
242
|
+
if len(val_list) == 1 and is_iterable_of_floats(val_list[0]):
|
|
243
|
+
num_outputs = len(list(val_list[0]))
|
|
244
|
+
values_list = [[float(v) for v in val_list[0]]]
|
|
245
|
+
else:
|
|
246
|
+
raise ValueError("Invalid combination of step_time and step_value")
|
|
247
|
+
else:
|
|
248
|
+
raise TypeError(
|
|
249
|
+
"step_value must be a float, an iterable of floats, or an iterable of iterables of floats"
|
|
250
|
+
)
|
|
251
|
+
else:
|
|
252
|
+
if not isinstance(step_value, Iterable) or isinstance(step_value, str):
|
|
253
|
+
raise TypeError(
|
|
254
|
+
"step_value must be an iterable of the same length as step_time"
|
|
255
|
+
)
|
|
256
|
+
val_list = list(step_value)
|
|
257
|
+
if len(val_list) != num_step_changes:
|
|
258
|
+
raise ValueError(
|
|
259
|
+
"The length of step_time and step_value must be the same"
|
|
260
|
+
)
|
|
261
|
+
|
|
262
|
+
if is_iterable_of_floats(val_list):
|
|
263
|
+
num_outputs = 1
|
|
264
|
+
values_list = [[float(v)] for v in val_list]
|
|
265
|
+
else:
|
|
266
|
+
if (
|
|
267
|
+
len(val_list) > 0
|
|
268
|
+
and isinstance(val_list[0], Iterable)
|
|
269
|
+
and not isinstance(val_list[0], str)
|
|
270
|
+
):
|
|
271
|
+
n = len(list(val_list[0]))
|
|
272
|
+
if is_iterable_iterable_of_floats(val_list, n):
|
|
273
|
+
num_outputs = n
|
|
274
|
+
values_list = [[float(x) for x in sub] for sub in val_list]
|
|
275
|
+
else:
|
|
276
|
+
raise ValueError(
|
|
277
|
+
"All elements of step_value must be iterables of the same length containing floats"
|
|
278
|
+
)
|
|
279
|
+
else:
|
|
280
|
+
raise TypeError(
|
|
281
|
+
"step_value must be an iterable of floats or an iterable of iterables of floats"
|
|
282
|
+
)
|
|
283
|
+
|
|
284
|
+
# Parse initial_value
|
|
285
|
+
if initial_value is None:
|
|
286
|
+
if num_outputs == 1:
|
|
287
|
+
initial_values_list = [0.0]
|
|
288
|
+
else:
|
|
289
|
+
initial_values_list = [0.0] * num_outputs
|
|
290
|
+
else:
|
|
291
|
+
if isinstance(initial_value, (float, int)):
|
|
292
|
+
initial_values_list = [float(initial_value)] * num_outputs
|
|
293
|
+
elif is_iterable_of_floats(initial_value):
|
|
294
|
+
initial_values_list = [float(v) for v in initial_value]
|
|
295
|
+
if len(initial_values_list) != num_outputs:
|
|
296
|
+
raise ValueError(
|
|
297
|
+
"The length of initial_value must match num_outputs"
|
|
298
|
+
)
|
|
299
|
+
else:
|
|
300
|
+
raise TypeError(
|
|
301
|
+
"initial_value must be a float, an iterable of floats, or None"
|
|
302
|
+
)
|
|
303
|
+
|
|
304
|
+
# Check the output ports
|
|
305
|
+
if outputs is not None:
|
|
306
|
+
if len(outputs) != num_outputs:
|
|
307
|
+
raise ValueError(
|
|
308
|
+
f"Length of outputs ({len(outputs)}) does not match num_outputs ({num_outputs})"
|
|
309
|
+
)
|
|
310
|
+
resolved_outputs = outputs
|
|
311
|
+
else:
|
|
312
|
+
resolved_outputs = [(f"y{i}", None) for i in range(num_outputs)]
|
|
313
|
+
|
|
314
|
+
# Sort times and corresponding values
|
|
315
|
+
sorted_pairs = sorted(zip(times_list, values_list), key=lambda x: x[0])
|
|
316
|
+
sorted_times = [p[0] for p in sorted_pairs]
|
|
317
|
+
sorted_values = [p[1] for p in sorted_pairs]
|
|
318
|
+
|
|
319
|
+
self.port_times = []
|
|
320
|
+
self.port_values = []
|
|
321
|
+
all_clock_times = set()
|
|
322
|
+
|
|
323
|
+
for i in range(num_outputs):
|
|
324
|
+
self.port_times.append(sorted_times)
|
|
325
|
+
|
|
326
|
+
vals_i = [initial_values_list[i]]
|
|
327
|
+
for j in range(len(sorted_times)):
|
|
328
|
+
vals_i.append(sorted_values[j][i])
|
|
329
|
+
self.port_values.append(vals_i)
|
|
330
|
+
|
|
331
|
+
for t in sorted_times:
|
|
332
|
+
all_clock_times.add(t)
|
|
333
|
+
|
|
334
|
+
super().__init__(
|
|
335
|
+
num_inputs=0,
|
|
336
|
+
num_outputs=num_outputs,
|
|
337
|
+
name=name,
|
|
338
|
+
outputs=resolved_outputs,
|
|
339
|
+
)
|
|
340
|
+
|
|
341
|
+
clock_times = sorted(list(all_clock_times))
|
|
342
|
+
if clock_times:
|
|
343
|
+
self.clock = ListClock(times=clock_times, name=f"{self.name}_clock")
|
|
344
|
+
self.clock.bind(self)
|
|
345
|
+
|
|
346
|
+
def output(self, t):
|
|
347
|
+
"""Returns the step output values at the current simulation time.
|
|
348
|
+
|
|
349
|
+
Args:
|
|
350
|
+
t: The current simulation time.
|
|
351
|
+
|
|
352
|
+
Returns:
|
|
353
|
+
A numpy array containing the computed step value(s) at time `t`.
|
|
354
|
+
"""
|
|
355
|
+
if not self.port_times or not self.port_times[0]:
|
|
356
|
+
return np.array(
|
|
357
|
+
[self.port_values[i][0] for i in range(self.num_outputs)], dtype=float
|
|
358
|
+
)
|
|
359
|
+
|
|
360
|
+
times = self.port_times[0]
|
|
361
|
+
idx = 0
|
|
362
|
+
for tj in times:
|
|
363
|
+
if t >= tj:
|
|
364
|
+
idx += 1
|
|
365
|
+
else:
|
|
366
|
+
break
|
|
367
|
+
|
|
368
|
+
out = np.array(
|
|
369
|
+
[self.port_values[i][idx] for i in range(self.num_outputs)], dtype=float
|
|
370
|
+
)
|
|
371
|
+
return out
|
|
372
|
+
|
|
373
|
+
|
|
374
|
+
class Sine(Block):
|
|
375
|
+
"""Sine wave generator block.
|
|
376
|
+
|
|
377
|
+
Generates one or more sine waves of the form:
|
|
378
|
+
y = amplitude * sin(frequency * t + phase) + bias
|
|
379
|
+
|
|
380
|
+
Attributes:
|
|
381
|
+
amplitude (numpy.ndarray): The amplitude of each sine wave output.
|
|
382
|
+
frequency (numpy.ndarray): The frequency (rad/s) of each sine wave.
|
|
383
|
+
phase (numpy.ndarray): The phase offset (rad) of each sine wave.
|
|
384
|
+
bias (numpy.ndarray): The bias/offset value of each sine wave.
|
|
385
|
+
"""
|
|
386
|
+
|
|
387
|
+
def __init__(
|
|
388
|
+
self,
|
|
389
|
+
amplitude: float | Iterable[float] = 1.0,
|
|
390
|
+
frequency: float | Iterable[float] = 1.0,
|
|
391
|
+
phase: float | Iterable[float] = 0.0,
|
|
392
|
+
bias: float | Iterable[float] = 0.0,
|
|
393
|
+
name: str | None = None,
|
|
394
|
+
outputs: list[tuple[str, Any]] | None = None,
|
|
395
|
+
):
|
|
396
|
+
"""Initializes the Sine wave block.
|
|
397
|
+
|
|
398
|
+
Args:
|
|
399
|
+
amplitude: The sine amplitude(s). Can be a float or an iterable of floats.
|
|
400
|
+
frequency: The sine frequency/frequencies. Can be a float or an iterable of floats.
|
|
401
|
+
phase: The phase offset(s). Can be a float or an iterable of floats.
|
|
402
|
+
bias: The bias offset(s). Can be a float or an iterable of floats.
|
|
403
|
+
name: Optional block name.
|
|
404
|
+
outputs: Optional output port configuration specs.
|
|
405
|
+
|
|
406
|
+
Raises:
|
|
407
|
+
TypeError: If parameters are not float, iterables of floats, or numpy arrays.
|
|
408
|
+
ValueError: If there is a length mismatch between multiple iterable parameters,
|
|
409
|
+
or if the length of `outputs` does not match `num_outputs`.
|
|
410
|
+
"""
|
|
411
|
+
# Determine num_outputs based on lengths of iterable arguments
|
|
412
|
+
lengths = []
|
|
413
|
+
for val in (amplitude, frequency, phase, bias):
|
|
414
|
+
if is_iterable_of_floats(val):
|
|
415
|
+
lengths.append(len(list(val)))
|
|
416
|
+
elif isinstance(val, np.ndarray):
|
|
417
|
+
lengths.append(len(val))
|
|
418
|
+
|
|
419
|
+
if len(lengths) > 0:
|
|
420
|
+
if not all(length == lengths[0] for length in lengths):
|
|
421
|
+
raise ValueError(
|
|
422
|
+
"All iterable arguments of Sine must have the same length"
|
|
423
|
+
)
|
|
424
|
+
num_outputs = lengths[0]
|
|
425
|
+
else:
|
|
426
|
+
num_outputs = 1
|
|
427
|
+
|
|
428
|
+
def expand_val(val, num):
|
|
429
|
+
if isinstance(val, (float, int)):
|
|
430
|
+
return np.full(num, float(val))
|
|
431
|
+
elif is_iterable_of_floats(val):
|
|
432
|
+
return np.array([float(x) for x in val])
|
|
433
|
+
elif isinstance(val, np.ndarray):
|
|
434
|
+
return val.astype(float)
|
|
435
|
+
else:
|
|
436
|
+
raise TypeError(
|
|
437
|
+
"Arguments must be floats, iterables of floats, or numpy arrays"
|
|
438
|
+
)
|
|
439
|
+
|
|
440
|
+
self.amplitude = expand_val(amplitude, num_outputs)
|
|
441
|
+
self.frequency = expand_val(frequency, num_outputs)
|
|
442
|
+
self.phase = expand_val(phase, num_outputs)
|
|
443
|
+
self.bias = expand_val(bias, num_outputs)
|
|
444
|
+
|
|
445
|
+
if outputs is not None:
|
|
446
|
+
if len(outputs) != num_outputs:
|
|
447
|
+
raise ValueError(
|
|
448
|
+
f"Length of outputs ({len(outputs)}) does not match num_outputs ({num_outputs})"
|
|
449
|
+
)
|
|
450
|
+
resolved_outputs = outputs
|
|
451
|
+
else:
|
|
452
|
+
if num_outputs == 1:
|
|
453
|
+
resolved_outputs = [("y", None)]
|
|
454
|
+
else:
|
|
455
|
+
resolved_outputs = [(f"y{i}", None) for i in range(num_outputs)]
|
|
456
|
+
|
|
457
|
+
super().__init__(
|
|
458
|
+
num_inputs=0,
|
|
459
|
+
num_outputs=num_outputs,
|
|
460
|
+
name=name,
|
|
461
|
+
outputs=resolved_outputs,
|
|
462
|
+
)
|
|
463
|
+
|
|
464
|
+
def output(self, t):
|
|
465
|
+
"""Calculates and returns the sine wave output(s) at time t.
|
|
466
|
+
|
|
467
|
+
Args:
|
|
468
|
+
t: The current simulation time.
|
|
469
|
+
|
|
470
|
+
Returns:
|
|
471
|
+
A numpy array containing the computed sine wave value(s).
|
|
472
|
+
"""
|
|
473
|
+
return self.amplitude * np.sin(self.frequency * t + self.phase) + self.bias
|
|
474
|
+
|
|
475
|
+
|
|
476
|
+
class FromFile(Block):
|
|
477
|
+
"""Reads signal data from a text file and interpolates values at the current time.
|
|
478
|
+
|
|
479
|
+
The file format should have columns separated by space, tab, or comma.
|
|
480
|
+
The first column must be time, and subsequent columns are output signal values.
|
|
481
|
+
|
|
482
|
+
Attributes:
|
|
483
|
+
filepath (str): Path to the loaded text file.
|
|
484
|
+
time_data (numpy.ndarray): The time array loaded from the first column.
|
|
485
|
+
val_data (numpy.ndarray): The output signal array loaded from the remaining columns.
|
|
486
|
+
initial_value (numpy.ndarray): The initial value(s) before the file's start time.
|
|
487
|
+
"""
|
|
488
|
+
|
|
489
|
+
def __init__(
|
|
490
|
+
self,
|
|
491
|
+
filepath: str,
|
|
492
|
+
initial_value: float | Iterable[float] | None = None,
|
|
493
|
+
name: str | None = None,
|
|
494
|
+
outputs: list[tuple[str, Any]] | None = None,
|
|
495
|
+
):
|
|
496
|
+
"""Initializes the FromFile block.
|
|
497
|
+
|
|
498
|
+
Args:
|
|
499
|
+
filepath: Path to the text file containing columns of data.
|
|
500
|
+
initial_value: Optional initial value(s) before the start time.
|
|
501
|
+
name: Optional block name.
|
|
502
|
+
outputs: Optional output port configuration specs.
|
|
503
|
+
|
|
504
|
+
Raises:
|
|
505
|
+
ValueError: If the file data contains fewer than two columns,
|
|
506
|
+
if the length of `initial_value` does not match `num_outputs`,
|
|
507
|
+
or if the length of `outputs` does not match `num_outputs`.
|
|
508
|
+
TypeError: If `initial_value` is not a float, iterable of floats, or None.
|
|
509
|
+
"""
|
|
510
|
+
self.filepath = filepath
|
|
511
|
+
data = np.loadtxt(filepath)
|
|
512
|
+
if data.ndim == 1:
|
|
513
|
+
# If the file has a single row or is just a flat array, raise error
|
|
514
|
+
raise ValueError(
|
|
515
|
+
"File data must contain at least two columns (time and at least one signal)."
|
|
516
|
+
)
|
|
517
|
+
|
|
518
|
+
self.time_data = data[:, 0]
|
|
519
|
+
self.val_data = data[:, 1:]
|
|
520
|
+
if self.val_data.ndim == 1:
|
|
521
|
+
self.val_data = self.val_data.reshape(-1, 1)
|
|
522
|
+
|
|
523
|
+
num_outputs = self.val_data.shape[1]
|
|
524
|
+
|
|
525
|
+
if initial_value is None:
|
|
526
|
+
self.initial_value = np.zeros(num_outputs, dtype=float)
|
|
527
|
+
elif isinstance(initial_value, (float, int)):
|
|
528
|
+
self.initial_value = np.full(num_outputs, float(initial_value))
|
|
529
|
+
elif is_iterable_of_floats(initial_value):
|
|
530
|
+
self.initial_value = np.array([float(v) for v in initial_value])
|
|
531
|
+
if len(self.initial_value) != num_outputs:
|
|
532
|
+
raise ValueError("The length of initial_value must match num_outputs")
|
|
533
|
+
elif isinstance(initial_value, np.ndarray):
|
|
534
|
+
self.initial_value = np.atleast_1d(initial_value).astype(float)
|
|
535
|
+
if len(self.initial_value) != num_outputs:
|
|
536
|
+
raise ValueError("The length of initial_value must match num_outputs")
|
|
537
|
+
else:
|
|
538
|
+
raise TypeError(
|
|
539
|
+
"initial_value must be a float, an iterable of floats, or None"
|
|
540
|
+
)
|
|
541
|
+
|
|
542
|
+
if outputs is not None:
|
|
543
|
+
if len(outputs) != num_outputs:
|
|
544
|
+
raise ValueError(
|
|
545
|
+
f"Length of outputs ({len(outputs)}) does not match num_outputs ({num_outputs})"
|
|
546
|
+
)
|
|
547
|
+
resolved_outputs = outputs
|
|
548
|
+
else:
|
|
549
|
+
resolved_outputs = [(f"y{i}", None) for i in range(num_outputs)]
|
|
550
|
+
|
|
551
|
+
super().__init__(
|
|
552
|
+
num_inputs=0,
|
|
553
|
+
num_outputs=num_outputs,
|
|
554
|
+
name=name,
|
|
555
|
+
outputs=resolved_outputs,
|
|
556
|
+
)
|
|
557
|
+
|
|
558
|
+
def output(self, t):
|
|
559
|
+
"""Calculates and returns the interpolated values at time t.
|
|
560
|
+
|
|
561
|
+
Args:
|
|
562
|
+
t: The current simulation time.
|
|
563
|
+
|
|
564
|
+
Returns:
|
|
565
|
+
A numpy array containing the interpolated output values.
|
|
566
|
+
"""
|
|
567
|
+
if t < self.time_data[0]:
|
|
568
|
+
return self.initial_value
|
|
569
|
+
|
|
570
|
+
y = np.zeros(self.num_outputs)
|
|
571
|
+
for i in range(self.num_outputs):
|
|
572
|
+
y[i] = np.interp(t, self.time_data, self.val_data[:, i])
|
|
573
|
+
return y
|
|
574
|
+
|
|
575
|
+
|
|
576
|
+
class Ramp(Block):
|
|
577
|
+
"""Generates a ramp signal starting at a specified time.
|
|
578
|
+
|
|
579
|
+
Supports multi-dimensional output ports if any parameter is a vector.
|
|
580
|
+
|
|
581
|
+
Attributes:
|
|
582
|
+
slope (numpy.ndarray): The slope of the ramp(s).
|
|
583
|
+
start_time (numpy.ndarray): The starting time(s) of the ramp(s).
|
|
584
|
+
initial_value (numpy.ndarray): The initial value(s) before the start time.
|
|
585
|
+
"""
|
|
586
|
+
|
|
587
|
+
def __init__(
|
|
588
|
+
self,
|
|
589
|
+
slope: float | Iterable[float] = 1.0,
|
|
590
|
+
start_time: float | Iterable[float] = 0.0,
|
|
591
|
+
initial_value: float | Iterable[float] = 0.0,
|
|
592
|
+
name: str | None = None,
|
|
593
|
+
outputs: list[tuple[str, Any]] | None = None,
|
|
594
|
+
):
|
|
595
|
+
"""Initializes the Ramp block.
|
|
596
|
+
|
|
597
|
+
Args:
|
|
598
|
+
slope: The slope(s) of the ramp. Can be a float or an iterable of floats.
|
|
599
|
+
start_time: The starting time(s) of the ramp. Can be a float or an iterable of floats.
|
|
600
|
+
initial_value: The initial output value(s) before the start time. Can be a float or an iterable of floats.
|
|
601
|
+
name: Optional block name.
|
|
602
|
+
outputs: Optional output port configuration specs.
|
|
603
|
+
|
|
604
|
+
Raises:
|
|
605
|
+
TypeError: If parameters are not float, iterables of floats, or numpy arrays.
|
|
606
|
+
ValueError: If there is a length mismatch between multiple iterable parameters,
|
|
607
|
+
or if the length of `outputs` does not match `num_outputs`.
|
|
608
|
+
"""
|
|
609
|
+
# Determine num_outputs based on lengths of iterable arguments
|
|
610
|
+
lengths = []
|
|
611
|
+
for val in (slope, start_time, initial_value):
|
|
612
|
+
if is_iterable_of_floats(val):
|
|
613
|
+
lengths.append(len(list(val)))
|
|
614
|
+
elif isinstance(val, np.ndarray):
|
|
615
|
+
lengths.append(len(val))
|
|
616
|
+
|
|
617
|
+
if len(lengths) > 0:
|
|
618
|
+
if not all(length == lengths[0] for length in lengths):
|
|
619
|
+
raise ValueError(
|
|
620
|
+
"All iterable arguments of Ramp must have the same length"
|
|
621
|
+
)
|
|
622
|
+
num_outputs = lengths[0]
|
|
623
|
+
else:
|
|
624
|
+
num_outputs = 1
|
|
625
|
+
|
|
626
|
+
def expand_val(val, num):
|
|
627
|
+
if isinstance(val, (float, int)):
|
|
628
|
+
return np.full(num, float(val))
|
|
629
|
+
elif is_iterable_of_floats(val):
|
|
630
|
+
return np.array([float(x) for x in val])
|
|
631
|
+
elif isinstance(val, np.ndarray):
|
|
632
|
+
return val.astype(float)
|
|
633
|
+
else:
|
|
634
|
+
raise TypeError(
|
|
635
|
+
"Arguments must be floats, iterables of floats, or numpy arrays"
|
|
636
|
+
)
|
|
637
|
+
|
|
638
|
+
self.slope = expand_val(slope, num_outputs)
|
|
639
|
+
self.start_time = expand_val(start_time, num_outputs)
|
|
640
|
+
self.initial_value = expand_val(initial_value, num_outputs)
|
|
641
|
+
|
|
642
|
+
if outputs is not None:
|
|
643
|
+
if len(outputs) != num_outputs:
|
|
644
|
+
raise ValueError(
|
|
645
|
+
f"Length of outputs ({len(outputs)}) does not match num_outputs ({num_outputs})"
|
|
646
|
+
)
|
|
647
|
+
resolved_outputs = outputs
|
|
648
|
+
else:
|
|
649
|
+
if num_outputs == 1:
|
|
650
|
+
resolved_outputs = [("y", None)]
|
|
651
|
+
else:
|
|
652
|
+
resolved_outputs = [(f"y{i}", None) for i in range(num_outputs)]
|
|
653
|
+
|
|
654
|
+
super().__init__(
|
|
655
|
+
num_inputs=0,
|
|
656
|
+
num_outputs=num_outputs,
|
|
657
|
+
name=name,
|
|
658
|
+
outputs=resolved_outputs,
|
|
659
|
+
)
|
|
660
|
+
|
|
661
|
+
def output(self, t):
|
|
662
|
+
"""Calculates and returns the ramp output(s) at time t.
|
|
663
|
+
|
|
664
|
+
Args:
|
|
665
|
+
t: The current simulation time.
|
|
666
|
+
|
|
667
|
+
Returns:
|
|
668
|
+
A numpy array containing the computed ramp value(s).
|
|
669
|
+
"""
|
|
670
|
+
mask = t < self.start_time
|
|
671
|
+
val = self.initial_value + self.slope * (t - self.start_time)
|
|
672
|
+
val[mask] = self.initial_value[mask]
|
|
673
|
+
return val
|
|
674
|
+
|
|
675
|
+
|
|
676
|
+
class Repeating(Block):
|
|
677
|
+
"""Generates a repeating sequence by periodic modulo lookup.
|
|
678
|
+
|
|
679
|
+
Attributes:
|
|
680
|
+
times (numpy.ndarray): The time points defining the sequence period.
|
|
681
|
+
values (numpy.ndarray): The output signal values at the corresponding time points.
|
|
682
|
+
period (float): The sequence period length.
|
|
683
|
+
"""
|
|
684
|
+
|
|
685
|
+
def __init__(
|
|
686
|
+
self,
|
|
687
|
+
times: Iterable[float] = (0.0, 1.0),
|
|
688
|
+
values: Iterable[float] | Iterable[Iterable[float]] = (0.0, 1.0),
|
|
689
|
+
name: str | None = None,
|
|
690
|
+
outputs: list[tuple[str, Any]] | None = None,
|
|
691
|
+
):
|
|
692
|
+
"""Initializes the Repeating block.
|
|
693
|
+
|
|
694
|
+
Args:
|
|
695
|
+
times: The time points of the sequence period.
|
|
696
|
+
values: The values corresponding to each time point.
|
|
697
|
+
name: Optional block name.
|
|
698
|
+
outputs: Optional output port configuration specs.
|
|
699
|
+
|
|
700
|
+
Raises:
|
|
701
|
+
ValueError: If there are fewer than two points in `times`,
|
|
702
|
+
if `times` does not define a positive period,
|
|
703
|
+
if the length of `times` and `values` do not match,
|
|
704
|
+
or if the length of `outputs` does not match `num_outputs`.
|
|
705
|
+
"""
|
|
706
|
+
self.times = np.asarray(times, dtype=float)
|
|
707
|
+
self.values = np.asarray(values, dtype=float)
|
|
708
|
+
|
|
709
|
+
if len(self.times) < 2:
|
|
710
|
+
raise ValueError("Repeating sequence requires at least two points in time.")
|
|
711
|
+
|
|
712
|
+
self.period = self.times[-1] - self.times[0]
|
|
713
|
+
if self.period <= 0:
|
|
714
|
+
raise ValueError(
|
|
715
|
+
"Ending time must be greater than starting time to define a period."
|
|
716
|
+
)
|
|
717
|
+
|
|
718
|
+
if self.values.ndim == 1:
|
|
719
|
+
self.values = self.values.reshape(-1, 1)
|
|
720
|
+
|
|
721
|
+
if len(self.times) != len(self.values):
|
|
722
|
+
raise ValueError("The length of times and values must match.")
|
|
723
|
+
|
|
724
|
+
num_outputs = self.values.shape[1]
|
|
725
|
+
|
|
726
|
+
if outputs is not None:
|
|
727
|
+
if len(outputs) != num_outputs:
|
|
728
|
+
raise ValueError(
|
|
729
|
+
f"Length of outputs ({len(outputs)}) does not match num_outputs ({num_outputs})"
|
|
730
|
+
)
|
|
731
|
+
resolved_outputs = outputs
|
|
732
|
+
else:
|
|
733
|
+
resolved_outputs = [(f"y{i}", None) for i in range(num_outputs)]
|
|
734
|
+
|
|
735
|
+
super().__init__(
|
|
736
|
+
num_inputs=0,
|
|
737
|
+
num_outputs=num_outputs,
|
|
738
|
+
name=name,
|
|
739
|
+
outputs=resolved_outputs,
|
|
740
|
+
)
|
|
741
|
+
|
|
742
|
+
def output(self, t):
|
|
743
|
+
"""Calculates and returns the periodic lookup value at time t.
|
|
744
|
+
|
|
745
|
+
Args:
|
|
746
|
+
t: The current simulation time.
|
|
747
|
+
|
|
748
|
+
Returns:
|
|
749
|
+
A numpy array containing the interpolated periodic value(s).
|
|
750
|
+
"""
|
|
751
|
+
if t < self.times[0]:
|
|
752
|
+
return self.values[0]
|
|
753
|
+
|
|
754
|
+
t_rel = (t - self.times[0]) % self.period
|
|
755
|
+
t_lookup = self.times[0] + t_rel
|
|
756
|
+
|
|
757
|
+
y = np.zeros(self.num_outputs)
|
|
758
|
+
for i in range(self.num_outputs):
|
|
759
|
+
y[i] = np.interp(t_lookup, self.times, self.values[:, i])
|
|
760
|
+
return y
|
|
761
|
+
|
|
762
|
+
|
|
763
|
+
class Random(Block):
|
|
764
|
+
"""Generates discrete random (Gaussian) noise updated at clock ticks.
|
|
765
|
+
|
|
766
|
+
Attributes:
|
|
767
|
+
mean (numpy.ndarray): The mean value(s) of the normal distribution.
|
|
768
|
+
std (numpy.ndarray): The standard deviation(s) of the normal distribution.
|
|
769
|
+
rng (numpy.random.Generator): The random number generator instance.
|
|
770
|
+
y_hold (numpy.ndarray): The current state/output value of the random noise.
|
|
771
|
+
"""
|
|
772
|
+
|
|
773
|
+
def __init__(
|
|
774
|
+
self,
|
|
775
|
+
mean: float | Iterable[float] = 0.0,
|
|
776
|
+
std: float | Iterable[float] = 1.0,
|
|
777
|
+
period: float = 0.1,
|
|
778
|
+
clock: Clock | None = None,
|
|
779
|
+
seed: int | None = None,
|
|
780
|
+
name: str | None = None,
|
|
781
|
+
outputs: list[tuple[str, Any]] | None = None,
|
|
782
|
+
):
|
|
783
|
+
"""Initializes the Random noise block.
|
|
784
|
+
|
|
785
|
+
Args:
|
|
786
|
+
mean: The mean value(s) of the Gaussian noise.
|
|
787
|
+
std: The standard deviation(s) of the Gaussian noise.
|
|
788
|
+
period: The update period if no clock is supplied.
|
|
789
|
+
clock: Optional custom Clock to schedule updates.
|
|
790
|
+
seed: Optional seed for the random number generator.
|
|
791
|
+
name: Optional block name.
|
|
792
|
+
outputs: Optional output port configuration specs.
|
|
793
|
+
|
|
794
|
+
Raises:
|
|
795
|
+
TypeError: If parameters are not float, iterables of floats, or numpy arrays.
|
|
796
|
+
ValueError: If there is a length mismatch between mean and std,
|
|
797
|
+
or if the length of `outputs` does not match `num_outputs`.
|
|
798
|
+
"""
|
|
799
|
+
# Determine num_outputs based on lengths of iterable arguments
|
|
800
|
+
lengths = []
|
|
801
|
+
for val in (mean, std):
|
|
802
|
+
if is_iterable_of_floats(val):
|
|
803
|
+
lengths.append(len(list(val)))
|
|
804
|
+
elif isinstance(val, np.ndarray):
|
|
805
|
+
lengths.append(len(val))
|
|
806
|
+
|
|
807
|
+
if len(lengths) > 0:
|
|
808
|
+
if not all(length == lengths[0] for length in lengths):
|
|
809
|
+
raise ValueError(
|
|
810
|
+
"All iterable arguments of Random must have the same length"
|
|
811
|
+
)
|
|
812
|
+
num_outputs = lengths[0]
|
|
813
|
+
else:
|
|
814
|
+
num_outputs = 1
|
|
815
|
+
|
|
816
|
+
def expand_val(val, num):
|
|
817
|
+
if isinstance(val, (float, int)):
|
|
818
|
+
return np.full(num, float(val))
|
|
819
|
+
elif is_iterable_of_floats(val):
|
|
820
|
+
return np.array([float(x) for x in val])
|
|
821
|
+
elif isinstance(val, np.ndarray):
|
|
822
|
+
return val.astype(float)
|
|
823
|
+
else:
|
|
824
|
+
raise TypeError(
|
|
825
|
+
"Arguments must be floats, iterables of floats, or numpy arrays"
|
|
826
|
+
)
|
|
827
|
+
|
|
828
|
+
self.mean = expand_val(mean, num_outputs)
|
|
829
|
+
self.std = expand_val(std, num_outputs)
|
|
830
|
+
self.rng = np.random.default_rng(seed)
|
|
831
|
+
|
|
832
|
+
if outputs is not None:
|
|
833
|
+
if len(outputs) != num_outputs:
|
|
834
|
+
raise ValueError(
|
|
835
|
+
f"Length of outputs ({len(outputs)}) does not match num_outputs ({num_outputs})"
|
|
836
|
+
)
|
|
837
|
+
resolved_outputs = outputs
|
|
838
|
+
else:
|
|
839
|
+
if num_outputs == 1:
|
|
840
|
+
resolved_outputs = [("y", None)]
|
|
841
|
+
else:
|
|
842
|
+
resolved_outputs = [(f"y{i}", None) for i in range(num_outputs)]
|
|
843
|
+
|
|
844
|
+
super().__init__(
|
|
845
|
+
num_inputs=0,
|
|
846
|
+
num_outputs=num_outputs,
|
|
847
|
+
num_discrete=num_outputs,
|
|
848
|
+
clock=clock,
|
|
849
|
+
name=name,
|
|
850
|
+
outputs=resolved_outputs,
|
|
851
|
+
)
|
|
852
|
+
|
|
853
|
+
if self.clock is None:
|
|
854
|
+
self.clock = PeriodicClock(
|
|
855
|
+
period=period, start=0.0, name=f"{self.name}_clock"
|
|
856
|
+
)
|
|
857
|
+
self.clock.bind(self)
|
|
858
|
+
|
|
859
|
+
self.y_hold = self.rng.normal(self.mean, self.std)
|
|
860
|
+
|
|
861
|
+
def output(self, t):
|
|
862
|
+
"""Returns the current held random noise value.
|
|
863
|
+
|
|
864
|
+
Args:
|
|
865
|
+
t: The current simulation time.
|
|
866
|
+
|
|
867
|
+
Returns:
|
|
868
|
+
A numpy array containing the current random noise values.
|
|
869
|
+
"""
|
|
870
|
+
return self.y_hold
|
|
871
|
+
|
|
872
|
+
def update(self, t):
|
|
873
|
+
"""Updates the held random noise state by sampling from the normal distribution.
|
|
874
|
+
|
|
875
|
+
Args:
|
|
876
|
+
t: The current simulation time.
|
|
877
|
+
"""
|
|
878
|
+
self.y_hold = self.rng.normal(self.mean, self.std)
|
|
879
|
+
|
|
880
|
+
|
|
881
|
+
class _PulseEdgeClock(Clock):
|
|
882
|
+
"""Ticks at both edges of a periodic pulse (rising, then falling, each cycle).
|
|
883
|
+
|
|
884
|
+
Allows Region splitting in the Simulator to stop solve_ivp exactly at
|
|
885
|
+
every transition instead of letting the adaptive stepper step over it.
|
|
886
|
+
|
|
887
|
+
Attributes:
|
|
888
|
+
period (float): The pulse period.
|
|
889
|
+
offsets (list[float]): Offsets representing the rising and falling edges.
|
|
890
|
+
phase_delay (float): The phase delay offset.
|
|
891
|
+
name (str): Name of the clock.
|
|
892
|
+
counter (int): Current tick counter.
|
|
893
|
+
"""
|
|
894
|
+
|
|
895
|
+
def __init__(self, period, duty_cycle, phase_delay=0.0, name=None):
|
|
896
|
+
"""Initializes the Pulse edge clock.
|
|
897
|
+
|
|
898
|
+
Args:
|
|
899
|
+
period: The pulse period.
|
|
900
|
+
duty_cycle: The pulse duty cycle.
|
|
901
|
+
phase_delay: The pulse phase delay.
|
|
902
|
+
name: Optional clock name.
|
|
903
|
+
"""
|
|
904
|
+
super().__init__()
|
|
905
|
+
self.period = period
|
|
906
|
+
self.offsets = [0.0, duty_cycle * period]
|
|
907
|
+
self.phase_delay = phase_delay
|
|
908
|
+
self.name = name or f"PulseClock_{id(self) % 10000}"
|
|
909
|
+
self.counter = 0
|
|
910
|
+
|
|
911
|
+
def next_tick(self):
|
|
912
|
+
"""Advances the clock's tick counter."""
|
|
913
|
+
self.counter += 1
|
|
914
|
+
|
|
915
|
+
def actual_tick(self) -> float:
|
|
916
|
+
"""Calculates and returns the timestamp of the current tick.
|
|
917
|
+
|
|
918
|
+
Returns:
|
|
919
|
+
The timestamp of the current tick.
|
|
920
|
+
"""
|
|
921
|
+
n = len(self.offsets)
|
|
922
|
+
cycle, phase = divmod(self.counter, n)
|
|
923
|
+
return self.phase_delay + cycle * self.period + self.offsets[phase]
|
|
924
|
+
|
|
925
|
+
def end(self):
|
|
926
|
+
"""Returns whether the clock has finished scheduling ticks.
|
|
927
|
+
|
|
928
|
+
Returns:
|
|
929
|
+
Always False (infinite periodic clock).
|
|
930
|
+
"""
|
|
931
|
+
return False
|
|
932
|
+
|
|
933
|
+
|
|
934
|
+
class Pulse(Block):
|
|
935
|
+
"""Generates a periodic pulse train.
|
|
936
|
+
|
|
937
|
+
period, duty_cycle and phase_delay are all fixed at construction, so
|
|
938
|
+
both edges of every pulse are known in advance. A Clock
|
|
939
|
+
schedules exact stops at them.
|
|
940
|
+
|
|
941
|
+
Attributes:
|
|
942
|
+
period (float): The pulse period.
|
|
943
|
+
duty_cycle (float): The pulse duty cycle.
|
|
944
|
+
phase_delay (float): The pulse phase delay.
|
|
945
|
+
amplitude (numpy.ndarray): The pulse amplitude(s).
|
|
946
|
+
clock (Clock): Bound edge-clock if duty_cycle is between (0, 1).
|
|
947
|
+
"""
|
|
948
|
+
|
|
949
|
+
def __init__(
|
|
950
|
+
self,
|
|
951
|
+
period: float = 2.0,
|
|
952
|
+
duty_cycle: float = 0.5,
|
|
953
|
+
amplitude: float | Iterable[float] = 1.0,
|
|
954
|
+
phase_delay: float = 0.0,
|
|
955
|
+
name: str | None = None,
|
|
956
|
+
outputs: list[tuple[str, Any]] | None = None,
|
|
957
|
+
):
|
|
958
|
+
"""Initializes the Pulse block.
|
|
959
|
+
|
|
960
|
+
Args:
|
|
961
|
+
period: The period of the pulse.
|
|
962
|
+
duty_cycle: The duty cycle of the pulse.
|
|
963
|
+
amplitude: The amplitude(s) of the pulse.
|
|
964
|
+
phase_delay: The phase delay of the pulse.
|
|
965
|
+
name: Optional block name.
|
|
966
|
+
outputs: Optional output port configuration specs.
|
|
967
|
+
|
|
968
|
+
Raises:
|
|
969
|
+
TypeError: If `amplitude` is not a float, iterable of floats, or numpy array.
|
|
970
|
+
ValueError: If the length of `outputs` does not match `num_outputs`.
|
|
971
|
+
"""
|
|
972
|
+
if isinstance(amplitude, (float, int)):
|
|
973
|
+
self.amplitude = np.array([float(amplitude)], dtype=float)
|
|
974
|
+
num_outputs = 1
|
|
975
|
+
elif is_iterable_of_floats(amplitude):
|
|
976
|
+
self.amplitude = np.array([float(a) for a in amplitude], dtype=float)
|
|
977
|
+
num_outputs = len(self.amplitude)
|
|
978
|
+
elif isinstance(amplitude, np.ndarray):
|
|
979
|
+
self.amplitude = np.atleast_1d(amplitude).astype(float)
|
|
980
|
+
num_outputs = len(self.amplitude)
|
|
981
|
+
else:
|
|
982
|
+
raise TypeError(
|
|
983
|
+
"amplitude must be a float, an iterable of floats, or a numpy array"
|
|
984
|
+
)
|
|
985
|
+
|
|
986
|
+
self.period = period
|
|
987
|
+
self.duty_cycle = duty_cycle
|
|
988
|
+
self.phase_delay = phase_delay
|
|
989
|
+
|
|
990
|
+
if outputs is not None:
|
|
991
|
+
if len(outputs) != num_outputs:
|
|
992
|
+
raise ValueError(
|
|
993
|
+
f"Length of outputs ({len(outputs)}) does not match num_outputs ({num_outputs})"
|
|
994
|
+
)
|
|
995
|
+
resolved_outputs = outputs
|
|
996
|
+
else:
|
|
997
|
+
if num_outputs == 1:
|
|
998
|
+
resolved_outputs = [("y", None)]
|
|
999
|
+
else:
|
|
1000
|
+
resolved_outputs = [(f"y{i}", None) for i in range(num_outputs)]
|
|
1001
|
+
|
|
1002
|
+
super().__init__(
|
|
1003
|
+
num_inputs=0,
|
|
1004
|
+
num_outputs=num_outputs,
|
|
1005
|
+
name=name,
|
|
1006
|
+
outputs=resolved_outputs,
|
|
1007
|
+
)
|
|
1008
|
+
|
|
1009
|
+
if 0.0 < duty_cycle < 1.0:
|
|
1010
|
+
self.clock = _PulseEdgeClock(
|
|
1011
|
+
period=period,
|
|
1012
|
+
duty_cycle=duty_cycle,
|
|
1013
|
+
phase_delay=phase_delay,
|
|
1014
|
+
name=f"{self.name}_clock",
|
|
1015
|
+
)
|
|
1016
|
+
self.clock.bind(self)
|
|
1017
|
+
|
|
1018
|
+
def update(self, t):
|
|
1019
|
+
"""Dummy update method required by bound Clock transitions.
|
|
1020
|
+
|
|
1021
|
+
Args:
|
|
1022
|
+
t: The current simulation time.
|
|
1023
|
+
"""
|
|
1024
|
+
pass # no state to update; only here so the edge-clock ticks are legal
|
|
1025
|
+
|
|
1026
|
+
def output(self, t):
|
|
1027
|
+
"""Calculates and returns the pulse output at time t.
|
|
1028
|
+
|
|
1029
|
+
Args:
|
|
1030
|
+
t: The current simulation time.
|
|
1031
|
+
|
|
1032
|
+
Returns:
|
|
1033
|
+
A numpy array containing the computed pulse value(s).
|
|
1034
|
+
"""
|
|
1035
|
+
if t < self.phase_delay:
|
|
1036
|
+
return np.zeros(self.num_outputs, dtype=float)
|
|
1037
|
+
|
|
1038
|
+
t_rel = (t - self.phase_delay) % self.period
|
|
1039
|
+
if t_rel < self.period * self.duty_cycle:
|
|
1040
|
+
return self.amplitude
|
|
1041
|
+
return np.zeros(self.num_outputs, dtype=float)
|
|
1042
|
+
|
|
1043
|
+
|
|
1044
|
+
class Equation(Block):
|
|
1045
|
+
"""An arbitrary function of time: `y = func(t)`.
|
|
1046
|
+
|
|
1047
|
+
The escape hatch. Anything expressible in closed form goes here
|
|
1048
|
+
without writing a `Block` subclass -- a swept setpoint, a measured
|
|
1049
|
+
profile fitted to a polynomial, a disturbance with a shape no other
|
|
1050
|
+
source has.
|
|
1051
|
+
|
|
1052
|
+
`func` takes the scalar time and returns either a scalar or something
|
|
1053
|
+
of length `num_outputs`. It is called on every evaluation, including
|
|
1054
|
+
at trial times the solver may later reject, so it must be a pure
|
|
1055
|
+
function of `t`: keeping state inside it, or counting the calls, will
|
|
1056
|
+
produce a signal that depends on the solver's step pattern.
|
|
1057
|
+
|
|
1058
|
+
**A discontinuous `func` is the one thing to avoid here.** This block
|
|
1059
|
+
binds no clock, so a jump inside it lands in the middle of an
|
|
1060
|
+
integration region and costs the solver its order. If the signal has
|
|
1061
|
+
edges, build it from `Step` or `Pulse`, which put the edges on a
|
|
1062
|
+
clock, or cap `dt_max`.
|
|
1063
|
+
|
|
1064
|
+
For `System.fuse()` the function must also be something sympy can
|
|
1065
|
+
write down; a plain numpy lambda usually is, a table lookup is not.
|
|
1066
|
+
|
|
1067
|
+
Inputs : none Outputs : y0..yn States : none
|
|
1068
|
+
"""
|
|
1069
|
+
|
|
1070
|
+
def __init__(
|
|
1071
|
+
self,
|
|
1072
|
+
func: Callable[[float], float | Iterable[float]],
|
|
1073
|
+
num_outputs: int = 1,
|
|
1074
|
+
name: str | None = None,
|
|
1075
|
+
outputs: list[tuple[str, Any]] | None = None,
|
|
1076
|
+
):
|
|
1077
|
+
"""Initializes the Equation block.
|
|
1078
|
+
|
|
1079
|
+
Args:
|
|
1080
|
+
func: The callable function mapping time `t` to output value(s).
|
|
1081
|
+
num_outputs: The number of outputs generated by the function.
|
|
1082
|
+
name: Optional block name.
|
|
1083
|
+
outputs: Optional output port configuration specs.
|
|
1084
|
+
|
|
1085
|
+
Raises:
|
|
1086
|
+
ValueError: If the length of `outputs` does not match `num_outputs`.
|
|
1087
|
+
"""
|
|
1088
|
+
self.func = func
|
|
1089
|
+
|
|
1090
|
+
if outputs is not None:
|
|
1091
|
+
if len(outputs) != num_outputs:
|
|
1092
|
+
raise ValueError(
|
|
1093
|
+
f"Length of outputs ({len(outputs)}) does not match num_outputs ({num_outputs})"
|
|
1094
|
+
)
|
|
1095
|
+
resolved_outputs = outputs
|
|
1096
|
+
else:
|
|
1097
|
+
resolved_outputs = [(f"y{i}", None) for i in range(num_outputs)]
|
|
1098
|
+
|
|
1099
|
+
super().__init__(
|
|
1100
|
+
num_inputs=0,
|
|
1101
|
+
num_outputs=num_outputs,
|
|
1102
|
+
name=name,
|
|
1103
|
+
outputs=resolved_outputs,
|
|
1104
|
+
)
|
|
1105
|
+
|
|
1106
|
+
def output(self, t):
|
|
1107
|
+
"""Calculates and returns the equation output at time t.
|
|
1108
|
+
|
|
1109
|
+
Args:
|
|
1110
|
+
t: The current simulation time.
|
|
1111
|
+
|
|
1112
|
+
Returns:
|
|
1113
|
+
A numpy array containing the evaluated equation values.
|
|
1114
|
+
"""
|
|
1115
|
+
val = self.func(t)
|
|
1116
|
+
return np.atleast_1d(val).astype(float)
|
|
1117
|
+
|
|
1118
|
+
|
|
1119
|
+
class Square(Block):
|
|
1120
|
+
"""Generates a periodic square wave.
|
|
1121
|
+
|
|
1122
|
+
Attributes:
|
|
1123
|
+
period (float): The wave period.
|
|
1124
|
+
duty_cycle (float): The wave duty cycle (fraction of period when wave is high).
|
|
1125
|
+
phase_delay (float): Phase delay in seconds.
|
|
1126
|
+
amplitude (numpy.ndarray): Amplitude of each square wave output.
|
|
1127
|
+
bias (numpy.ndarray): Bias/offset of each square wave output.
|
|
1128
|
+
clock (Clock): Bound edge-clock if duty_cycle is between (0, 1).
|
|
1129
|
+
"""
|
|
1130
|
+
|
|
1131
|
+
def __init__(
|
|
1132
|
+
self,
|
|
1133
|
+
period: float = 2.0,
|
|
1134
|
+
duty_cycle: float = 0.5,
|
|
1135
|
+
amplitude: float | Iterable[float] = 1.0,
|
|
1136
|
+
phase_delay: float = 0.0,
|
|
1137
|
+
bias: float | Iterable[float] = 0.0,
|
|
1138
|
+
name: str | None = None,
|
|
1139
|
+
outputs: list[tuple[str, Any]] | None = None,
|
|
1140
|
+
):
|
|
1141
|
+
"""Initializes the Square wave block.
|
|
1142
|
+
|
|
1143
|
+
Args:
|
|
1144
|
+
period: Period of the wave.
|
|
1145
|
+
duty_cycle: Duty cycle of the wave (0 to 1).
|
|
1146
|
+
amplitude: Amplitude of the wave. Can be a float or an iterable of floats.
|
|
1147
|
+
phase_delay: Phase delay in seconds.
|
|
1148
|
+
bias: Bias offset of the wave. Can be a float or an iterable of floats.
|
|
1149
|
+
name: Optional block name.
|
|
1150
|
+
outputs: Optional output port configuration specs.
|
|
1151
|
+
|
|
1152
|
+
Raises:
|
|
1153
|
+
TypeError: If amplitude/bias are not float, iterable, or numpy array.
|
|
1154
|
+
ValueError: If there is a length mismatch between iterable amplitude/bias,
|
|
1155
|
+
or if the length of `outputs` does not match `num_outputs`.
|
|
1156
|
+
"""
|
|
1157
|
+
lengths = []
|
|
1158
|
+
for val in (amplitude, bias):
|
|
1159
|
+
if is_iterable_of_floats(val):
|
|
1160
|
+
lengths.append(len(list(val)))
|
|
1161
|
+
elif isinstance(val, np.ndarray):
|
|
1162
|
+
lengths.append(len(val))
|
|
1163
|
+
|
|
1164
|
+
if len(lengths) > 0:
|
|
1165
|
+
if not all(length == lengths[0] for length in lengths):
|
|
1166
|
+
raise ValueError(
|
|
1167
|
+
"All iterable arguments of Square must have the same length"
|
|
1168
|
+
)
|
|
1169
|
+
num_outputs = lengths[0]
|
|
1170
|
+
else:
|
|
1171
|
+
num_outputs = 1
|
|
1172
|
+
|
|
1173
|
+
def expand_val(val, num):
|
|
1174
|
+
if isinstance(val, (float, int)):
|
|
1175
|
+
return np.full(num, float(val))
|
|
1176
|
+
elif is_iterable_of_floats(val):
|
|
1177
|
+
return np.array([float(x) for x in val])
|
|
1178
|
+
elif isinstance(val, np.ndarray):
|
|
1179
|
+
return val.astype(float)
|
|
1180
|
+
else:
|
|
1181
|
+
raise TypeError(
|
|
1182
|
+
"Arguments must be floats, iterables of floats, or numpy arrays"
|
|
1183
|
+
)
|
|
1184
|
+
|
|
1185
|
+
self.amplitude = expand_val(amplitude, num_outputs)
|
|
1186
|
+
self.bias = expand_val(bias, num_outputs)
|
|
1187
|
+
self.period = period
|
|
1188
|
+
self.duty_cycle = duty_cycle
|
|
1189
|
+
self.phase_delay = phase_delay
|
|
1190
|
+
|
|
1191
|
+
if outputs is not None:
|
|
1192
|
+
if len(outputs) != num_outputs:
|
|
1193
|
+
raise ValueError(
|
|
1194
|
+
f"Length of outputs ({len(outputs)}) does not match num_outputs ({num_outputs})"
|
|
1195
|
+
)
|
|
1196
|
+
resolved_outputs = outputs
|
|
1197
|
+
else:
|
|
1198
|
+
if num_outputs == 1:
|
|
1199
|
+
resolved_outputs = [("y", None)]
|
|
1200
|
+
else:
|
|
1201
|
+
resolved_outputs = [(f"y{i}", None) for i in range(num_outputs)]
|
|
1202
|
+
|
|
1203
|
+
super().__init__(
|
|
1204
|
+
num_inputs=0,
|
|
1205
|
+
num_outputs=num_outputs,
|
|
1206
|
+
name=name,
|
|
1207
|
+
outputs=resolved_outputs,
|
|
1208
|
+
)
|
|
1209
|
+
|
|
1210
|
+
if 0.0 < duty_cycle < 1.0:
|
|
1211
|
+
self.clock = _PulseEdgeClock(
|
|
1212
|
+
period=period,
|
|
1213
|
+
duty_cycle=duty_cycle,
|
|
1214
|
+
phase_delay=phase_delay,
|
|
1215
|
+
name=f"{self.name}_clock",
|
|
1216
|
+
)
|
|
1217
|
+
self.clock.bind(self)
|
|
1218
|
+
|
|
1219
|
+
def update(self, t):
|
|
1220
|
+
"""Dummy update method required by bound Clock transitions.
|
|
1221
|
+
|
|
1222
|
+
Args:
|
|
1223
|
+
t: The current simulation time.
|
|
1224
|
+
"""
|
|
1225
|
+
pass
|
|
1226
|
+
|
|
1227
|
+
def output(self, t):
|
|
1228
|
+
"""Calculates and returns the square wave output at time t.
|
|
1229
|
+
|
|
1230
|
+
Args:
|
|
1231
|
+
t: The current simulation time.
|
|
1232
|
+
|
|
1233
|
+
Returns:
|
|
1234
|
+
A numpy array containing the computed square wave value(s).
|
|
1235
|
+
"""
|
|
1236
|
+
if t < self.phase_delay:
|
|
1237
|
+
return self.bias - self.amplitude
|
|
1238
|
+
|
|
1239
|
+
t_rel = (t - self.phase_delay) % self.period
|
|
1240
|
+
if t_rel < self.period * self.duty_cycle:
|
|
1241
|
+
return self.bias + self.amplitude
|
|
1242
|
+
return self.bias - self.amplitude
|
|
1243
|
+
|
|
1244
|
+
|
|
1245
|
+
class Triangular(Block):
|
|
1246
|
+
"""Generates a periodic triangular wave.
|
|
1247
|
+
|
|
1248
|
+
Attributes:
|
|
1249
|
+
period (float): The wave period.
|
|
1250
|
+
phase_delay (float): Phase delay in seconds.
|
|
1251
|
+
width (float): Duty cycle / rising width fraction of the triangle (0 to 1).
|
|
1252
|
+
amplitude (numpy.ndarray): Amplitude of each triangular wave output.
|
|
1253
|
+
bias (numpy.ndarray): Bias/offset of each triangular wave output.
|
|
1254
|
+
clock (Clock): Bound edge-clock if width is between (0, 1).
|
|
1255
|
+
"""
|
|
1256
|
+
|
|
1257
|
+
def __init__(
|
|
1258
|
+
self,
|
|
1259
|
+
period: float = 2.0,
|
|
1260
|
+
amplitude: float | Iterable[float] = 1.0,
|
|
1261
|
+
phase_delay: float = 0.0,
|
|
1262
|
+
bias: float | Iterable[float] = 0.0,
|
|
1263
|
+
width: float = 0.5,
|
|
1264
|
+
name: str | None = None,
|
|
1265
|
+
outputs: list[tuple[str, Any]] | None = None,
|
|
1266
|
+
):
|
|
1267
|
+
"""Initializes the Triangular wave block.
|
|
1268
|
+
|
|
1269
|
+
Args:
|
|
1270
|
+
period: Period of the wave.
|
|
1271
|
+
amplitude: Amplitude of the wave. Can be a float or an iterable of floats.
|
|
1272
|
+
phase_delay: Phase delay in seconds.
|
|
1273
|
+
bias: Bias offset of the wave. Can be a float or an iterable of floats.
|
|
1274
|
+
width: Rise duration fraction of period (0.5 is symmetric triangle).
|
|
1275
|
+
name: Optional block name.
|
|
1276
|
+
outputs: Optional output port configuration specs.
|
|
1277
|
+
|
|
1278
|
+
Raises:
|
|
1279
|
+
TypeError: If amplitude/bias are not float, iterable, or numpy array.
|
|
1280
|
+
ValueError: If there is a length mismatch between iterable amplitude/bias,
|
|
1281
|
+
or if the length of `outputs` does not match `num_outputs`.
|
|
1282
|
+
"""
|
|
1283
|
+
lengths = []
|
|
1284
|
+
for val in (amplitude, bias):
|
|
1285
|
+
if is_iterable_of_floats(val):
|
|
1286
|
+
lengths.append(len(list(val)))
|
|
1287
|
+
elif isinstance(val, np.ndarray):
|
|
1288
|
+
lengths.append(len(val))
|
|
1289
|
+
|
|
1290
|
+
if len(lengths) > 0:
|
|
1291
|
+
if not all(length == lengths[0] for length in lengths):
|
|
1292
|
+
raise ValueError(
|
|
1293
|
+
"All iterable arguments of Triangular must have the same length"
|
|
1294
|
+
)
|
|
1295
|
+
num_outputs = lengths[0]
|
|
1296
|
+
else:
|
|
1297
|
+
num_outputs = 1
|
|
1298
|
+
|
|
1299
|
+
def expand_val(val, num):
|
|
1300
|
+
if isinstance(val, (float, int)):
|
|
1301
|
+
return np.full(num, float(val))
|
|
1302
|
+
elif is_iterable_of_floats(val):
|
|
1303
|
+
return np.array([float(x) for x in val])
|
|
1304
|
+
elif isinstance(val, np.ndarray):
|
|
1305
|
+
return val.astype(float)
|
|
1306
|
+
else:
|
|
1307
|
+
raise TypeError(
|
|
1308
|
+
"Arguments must be floats, iterables of floats, or numpy arrays"
|
|
1309
|
+
)
|
|
1310
|
+
|
|
1311
|
+
self.amplitude = expand_val(amplitude, num_outputs)
|
|
1312
|
+
self.bias = expand_val(bias, num_outputs)
|
|
1313
|
+
self.period = period
|
|
1314
|
+
self.phase_delay = phase_delay
|
|
1315
|
+
self.width = width
|
|
1316
|
+
|
|
1317
|
+
if outputs is not None:
|
|
1318
|
+
if len(outputs) != num_outputs:
|
|
1319
|
+
raise ValueError(
|
|
1320
|
+
f"Length of outputs ({len(outputs)}) does not match num_outputs ({num_outputs})"
|
|
1321
|
+
)
|
|
1322
|
+
resolved_outputs = outputs
|
|
1323
|
+
else:
|
|
1324
|
+
if num_outputs == 1:
|
|
1325
|
+
resolved_outputs = [("y", None)]
|
|
1326
|
+
else:
|
|
1327
|
+
resolved_outputs = [(f"y{i}", None) for i in range(num_outputs)]
|
|
1328
|
+
|
|
1329
|
+
super().__init__(
|
|
1330
|
+
num_inputs=0,
|
|
1331
|
+
num_outputs=num_outputs,
|
|
1332
|
+
name=name,
|
|
1333
|
+
outputs=resolved_outputs,
|
|
1334
|
+
)
|
|
1335
|
+
|
|
1336
|
+
if 0.0 < width < 1.0:
|
|
1337
|
+
self.clock = _PulseEdgeClock(
|
|
1338
|
+
period=period,
|
|
1339
|
+
duty_cycle=width,
|
|
1340
|
+
phase_delay=phase_delay,
|
|
1341
|
+
name=f"{self.name}_clock",
|
|
1342
|
+
)
|
|
1343
|
+
self.clock.bind(self)
|
|
1344
|
+
|
|
1345
|
+
def update(self, t):
|
|
1346
|
+
"""Dummy update method required by bound Clock transitions.
|
|
1347
|
+
|
|
1348
|
+
Args:
|
|
1349
|
+
t: The current simulation time.
|
|
1350
|
+
"""
|
|
1351
|
+
pass
|
|
1352
|
+
|
|
1353
|
+
def output(self, t):
|
|
1354
|
+
"""Calculates and returns the triangular wave output at time t.
|
|
1355
|
+
|
|
1356
|
+
Args:
|
|
1357
|
+
t: The current simulation time.
|
|
1358
|
+
|
|
1359
|
+
Returns:
|
|
1360
|
+
A numpy array containing the computed triangular wave value(s).
|
|
1361
|
+
"""
|
|
1362
|
+
if t < self.phase_delay:
|
|
1363
|
+
return self.bias - self.amplitude
|
|
1364
|
+
|
|
1365
|
+
t_rel = (t - self.phase_delay) % self.period
|
|
1366
|
+
w = self.width
|
|
1367
|
+
if w <= 0.0:
|
|
1368
|
+
return (
|
|
1369
|
+
self.bias + self.amplitude - 2 * self.amplitude * (t_rel / self.period)
|
|
1370
|
+
)
|
|
1371
|
+
elif w >= 1.0:
|
|
1372
|
+
return (
|
|
1373
|
+
self.bias - self.amplitude + 2 * self.amplitude * (t_rel / self.period)
|
|
1374
|
+
)
|
|
1375
|
+
else:
|
|
1376
|
+
t_switch = self.period * w
|
|
1377
|
+
if t_rel < t_switch:
|
|
1378
|
+
return (
|
|
1379
|
+
self.bias - self.amplitude + 2 * self.amplitude * (t_rel / t_switch)
|
|
1380
|
+
)
|
|
1381
|
+
else:
|
|
1382
|
+
return (
|
|
1383
|
+
self.bias
|
|
1384
|
+
+ self.amplitude
|
|
1385
|
+
- 2
|
|
1386
|
+
* self.amplitude
|
|
1387
|
+
* ((t_rel - t_switch) / (self.period - t_switch))
|
|
1388
|
+
)
|
|
1389
|
+
|
|
1390
|
+
|
|
1391
|
+
class Sawtooth(Block):
|
|
1392
|
+
"""Generates a periodic sawtooth wave.
|
|
1393
|
+
|
|
1394
|
+
Attributes:
|
|
1395
|
+
period (float): The wave period.
|
|
1396
|
+
phase_delay (float): Phase delay in seconds.
|
|
1397
|
+
width (float): Duty cycle / rising width fraction of the sawtooth (0 to 1).
|
|
1398
|
+
amplitude (numpy.ndarray): Amplitude of each sawtooth wave output.
|
|
1399
|
+
bias (numpy.ndarray): Bias/offset of each sawtooth wave output.
|
|
1400
|
+
clock (Clock): Bound edge-clock if width is between (0, 1).
|
|
1401
|
+
"""
|
|
1402
|
+
|
|
1403
|
+
def __init__(
|
|
1404
|
+
self,
|
|
1405
|
+
period: float = 2.0,
|
|
1406
|
+
amplitude: float | Iterable[float] = 1.0,
|
|
1407
|
+
phase_delay: float = 0.0,
|
|
1408
|
+
bias: float | Iterable[float] = 0.0,
|
|
1409
|
+
width: float = 1.0,
|
|
1410
|
+
name: str | None = None,
|
|
1411
|
+
outputs: list[tuple[str, Any]] | None = None,
|
|
1412
|
+
):
|
|
1413
|
+
"""Initializes the Sawtooth wave block.
|
|
1414
|
+
|
|
1415
|
+
Args:
|
|
1416
|
+
period: Period of the wave.
|
|
1417
|
+
amplitude: Amplitude of the wave. Can be a float or an iterable of floats.
|
|
1418
|
+
phase_delay: Phase delay in seconds.
|
|
1419
|
+
bias: Bias offset of the wave. Can be a float or an iterable of floats.
|
|
1420
|
+
width: Rise duration fraction of period (1.0 is standard rising sawtooth).
|
|
1421
|
+
name: Optional block name.
|
|
1422
|
+
outputs: Optional output port configuration specs.
|
|
1423
|
+
|
|
1424
|
+
Raises:
|
|
1425
|
+
TypeError: If amplitude/bias are not float, iterable, or numpy array.
|
|
1426
|
+
ValueError: If there is a length mismatch between iterable amplitude/bias,
|
|
1427
|
+
or if the length of `outputs` does not match `num_outputs`.
|
|
1428
|
+
"""
|
|
1429
|
+
lengths = []
|
|
1430
|
+
for val in (amplitude, bias):
|
|
1431
|
+
if is_iterable_of_floats(val):
|
|
1432
|
+
lengths.append(len(list(val)))
|
|
1433
|
+
elif isinstance(val, np.ndarray):
|
|
1434
|
+
lengths.append(len(val))
|
|
1435
|
+
|
|
1436
|
+
if len(lengths) > 0:
|
|
1437
|
+
if not all(length == lengths[0] for length in lengths):
|
|
1438
|
+
raise ValueError(
|
|
1439
|
+
"All iterable arguments of Sawtooth must have the same length"
|
|
1440
|
+
)
|
|
1441
|
+
num_outputs = lengths[0]
|
|
1442
|
+
else:
|
|
1443
|
+
num_outputs = 1
|
|
1444
|
+
|
|
1445
|
+
def expand_val(val, num):
|
|
1446
|
+
if isinstance(val, (float, int)):
|
|
1447
|
+
return np.full(num, float(val))
|
|
1448
|
+
elif is_iterable_of_floats(val):
|
|
1449
|
+
return np.array([float(x) for x in val])
|
|
1450
|
+
elif isinstance(val, np.ndarray):
|
|
1451
|
+
return val.astype(float)
|
|
1452
|
+
else:
|
|
1453
|
+
raise TypeError(
|
|
1454
|
+
"Arguments must be floats, iterables of floats, or numpy arrays"
|
|
1455
|
+
)
|
|
1456
|
+
|
|
1457
|
+
self.amplitude = expand_val(amplitude, num_outputs)
|
|
1458
|
+
self.bias = expand_val(bias, num_outputs)
|
|
1459
|
+
self.period = period
|
|
1460
|
+
self.phase_delay = phase_delay
|
|
1461
|
+
self.width = width
|
|
1462
|
+
|
|
1463
|
+
if outputs is not None:
|
|
1464
|
+
if len(outputs) != num_outputs:
|
|
1465
|
+
raise ValueError(
|
|
1466
|
+
f"Length of outputs ({len(outputs)}) does not match num_outputs ({num_outputs})"
|
|
1467
|
+
)
|
|
1468
|
+
resolved_outputs = outputs
|
|
1469
|
+
else:
|
|
1470
|
+
if num_outputs == 1:
|
|
1471
|
+
resolved_outputs = [("y", None)]
|
|
1472
|
+
else:
|
|
1473
|
+
resolved_outputs = [(f"y{i}", None) for i in range(num_outputs)]
|
|
1474
|
+
|
|
1475
|
+
super().__init__(
|
|
1476
|
+
num_inputs=0,
|
|
1477
|
+
num_outputs=num_outputs,
|
|
1478
|
+
name=name,
|
|
1479
|
+
outputs=resolved_outputs,
|
|
1480
|
+
)
|
|
1481
|
+
|
|
1482
|
+
if 0.0 < width < 1.0:
|
|
1483
|
+
self.clock = _PulseEdgeClock(
|
|
1484
|
+
period=period,
|
|
1485
|
+
duty_cycle=width,
|
|
1486
|
+
phase_delay=phase_delay,
|
|
1487
|
+
name=f"{self.name}_clock",
|
|
1488
|
+
)
|
|
1489
|
+
self.clock.bind(self)
|
|
1490
|
+
elif width == 1.0 or width == 0.0:
|
|
1491
|
+
self.clock = PeriodicClock(
|
|
1492
|
+
period=period, start=phase_delay, name=f"{self.name}_clock"
|
|
1493
|
+
)
|
|
1494
|
+
self.clock.bind(self)
|
|
1495
|
+
|
|
1496
|
+
def update(self, t):
|
|
1497
|
+
"""Dummy update method required by bound Clock transitions.
|
|
1498
|
+
|
|
1499
|
+
Args:
|
|
1500
|
+
t: The current simulation time.
|
|
1501
|
+
"""
|
|
1502
|
+
pass
|
|
1503
|
+
|
|
1504
|
+
def output(self, t):
|
|
1505
|
+
"""Calculates and returns the sawtooth wave output at time t.
|
|
1506
|
+
|
|
1507
|
+
Args:
|
|
1508
|
+
t: The current simulation time.
|
|
1509
|
+
|
|
1510
|
+
Returns:
|
|
1511
|
+
A numpy array containing the computed sawtooth wave value(s).
|
|
1512
|
+
"""
|
|
1513
|
+
if t < self.phase_delay:
|
|
1514
|
+
return self.bias - self.amplitude
|
|
1515
|
+
|
|
1516
|
+
t_rel = (t - self.phase_delay) % self.period
|
|
1517
|
+
w = self.width
|
|
1518
|
+
if w <= 0.0:
|
|
1519
|
+
return (
|
|
1520
|
+
self.bias + self.amplitude - 2 * self.amplitude * (t_rel / self.period)
|
|
1521
|
+
)
|
|
1522
|
+
elif w >= 1.0:
|
|
1523
|
+
return (
|
|
1524
|
+
self.bias - self.amplitude + 2 * self.amplitude * (t_rel / self.period)
|
|
1525
|
+
)
|
|
1526
|
+
else:
|
|
1527
|
+
t_switch = self.period * w
|
|
1528
|
+
if t_rel < t_switch:
|
|
1529
|
+
return (
|
|
1530
|
+
self.bias - self.amplitude + 2 * self.amplitude * (t_rel / t_switch)
|
|
1531
|
+
)
|
|
1532
|
+
else:
|
|
1533
|
+
return (
|
|
1534
|
+
self.bias
|
|
1535
|
+
+ self.amplitude
|
|
1536
|
+
- 2
|
|
1537
|
+
* self.amplitude
|
|
1538
|
+
* ((t_rel - t_switch) / (self.period - t_switch))
|
|
1539
|
+
)
|
|
1540
|
+
|
|
1541
|
+
|
|
1542
|
+
class Chirp(Block):
|
|
1543
|
+
"""Generates a frequency sweep (chirp) signal.
|
|
1544
|
+
|
|
1545
|
+
theta(t) = 2 * pi * (f0 * t + 0.5 * (f1 - f0) / t1 * t^2) + phase
|
|
1546
|
+
y = amplitude * sin(theta(t)) + bias
|
|
1547
|
+
|
|
1548
|
+
Attributes:
|
|
1549
|
+
f0 (float): Start frequency in Hz.
|
|
1550
|
+
f1 (float): End frequency in Hz.
|
|
1551
|
+
t1 (float): Sweep time in seconds.
|
|
1552
|
+
amplitude (numpy.ndarray): Amplitude of each chirp output.
|
|
1553
|
+
phase (numpy.ndarray): Initial phase offset of each chirp output in radians.
|
|
1554
|
+
bias (numpy.ndarray): Bias/offset of each chirp output.
|
|
1555
|
+
"""
|
|
1556
|
+
|
|
1557
|
+
def __init__(
|
|
1558
|
+
self,
|
|
1559
|
+
f0: float = 0.0,
|
|
1560
|
+
f1: float = 10.0,
|
|
1561
|
+
t1: float = 10.0,
|
|
1562
|
+
amplitude: float | Iterable[float] = 1.0,
|
|
1563
|
+
phase: float | Iterable[float] = 0.0,
|
|
1564
|
+
bias: float | Iterable[float] = 0.0,
|
|
1565
|
+
name: str | None = None,
|
|
1566
|
+
outputs: list[tuple[str, Any]] | None = None,
|
|
1567
|
+
):
|
|
1568
|
+
"""Initializes the Chirp block.
|
|
1569
|
+
|
|
1570
|
+
Args:
|
|
1571
|
+
f0: Start frequency in Hz.
|
|
1572
|
+
f1: End frequency in Hz.
|
|
1573
|
+
t1: Sweep time in seconds.
|
|
1574
|
+
amplitude: Amplitude(s) of the chirp. Can be a float or an iterable of floats.
|
|
1575
|
+
phase: Initial phase offset(s) in radians. Can be a float or an iterable of floats.
|
|
1576
|
+
bias: Bias offset(s) of the chirp. Can be a float or an iterable of floats.
|
|
1577
|
+
name: Optional block name.
|
|
1578
|
+
outputs: Optional output port configuration specs.
|
|
1579
|
+
|
|
1580
|
+
Raises:
|
|
1581
|
+
TypeError: If amplitude/phase/bias are not float, iterable, or numpy array.
|
|
1582
|
+
ValueError: If there is a length mismatch between iterable amplitude/phase/bias,
|
|
1583
|
+
or if the length of `outputs` does not match `num_outputs`.
|
|
1584
|
+
"""
|
|
1585
|
+
lengths = []
|
|
1586
|
+
for val in (amplitude, phase, bias):
|
|
1587
|
+
if is_iterable_of_floats(val):
|
|
1588
|
+
lengths.append(len(list(val)))
|
|
1589
|
+
elif isinstance(val, np.ndarray):
|
|
1590
|
+
lengths.append(len(val))
|
|
1591
|
+
|
|
1592
|
+
if len(lengths) > 0:
|
|
1593
|
+
if not all(length == lengths[0] for length in lengths):
|
|
1594
|
+
raise ValueError(
|
|
1595
|
+
"All iterable arguments of Chirp must have the same length"
|
|
1596
|
+
)
|
|
1597
|
+
num_outputs = lengths[0]
|
|
1598
|
+
else:
|
|
1599
|
+
num_outputs = 1
|
|
1600
|
+
|
|
1601
|
+
def expand_val(val, num):
|
|
1602
|
+
if isinstance(val, (float, int)):
|
|
1603
|
+
return np.full(num, float(val))
|
|
1604
|
+
elif is_iterable_of_floats(val):
|
|
1605
|
+
return np.array([float(x) for x in val])
|
|
1606
|
+
elif isinstance(val, np.ndarray):
|
|
1607
|
+
return val.astype(float)
|
|
1608
|
+
else:
|
|
1609
|
+
raise TypeError(
|
|
1610
|
+
"Arguments must be floats, iterables of floats, or numpy arrays"
|
|
1611
|
+
)
|
|
1612
|
+
|
|
1613
|
+
self.amplitude = expand_val(amplitude, num_outputs)
|
|
1614
|
+
self.phase = expand_val(phase, num_outputs)
|
|
1615
|
+
self.bias = expand_val(bias, num_outputs)
|
|
1616
|
+
self.f0 = f0
|
|
1617
|
+
self.f1 = f1
|
|
1618
|
+
self.t1 = t1
|
|
1619
|
+
|
|
1620
|
+
if outputs is not None:
|
|
1621
|
+
if len(outputs) != num_outputs:
|
|
1622
|
+
raise ValueError(
|
|
1623
|
+
f"Length of outputs ({len(outputs)}) does not match num_outputs ({num_outputs})"
|
|
1624
|
+
)
|
|
1625
|
+
resolved_outputs = outputs
|
|
1626
|
+
else:
|
|
1627
|
+
if num_outputs == 1:
|
|
1628
|
+
resolved_outputs = [("y", None)]
|
|
1629
|
+
else:
|
|
1630
|
+
resolved_outputs = [(f"y{i}", None) for i in range(num_outputs)]
|
|
1631
|
+
|
|
1632
|
+
super().__init__(
|
|
1633
|
+
num_inputs=0,
|
|
1634
|
+
num_outputs=num_outputs,
|
|
1635
|
+
name=name,
|
|
1636
|
+
outputs=resolved_outputs,
|
|
1637
|
+
)
|
|
1638
|
+
|
|
1639
|
+
def output(self, t):
|
|
1640
|
+
"""Calculates and returns the chirp output at time t.
|
|
1641
|
+
|
|
1642
|
+
Args:
|
|
1643
|
+
t: The current simulation time.
|
|
1644
|
+
|
|
1645
|
+
Returns:
|
|
1646
|
+
A numpy array containing the computed chirp value(s).
|
|
1647
|
+
"""
|
|
1648
|
+
theta = (
|
|
1649
|
+
2.0 * np.pi * (self.f0 * t + 0.5 * (self.f1 - self.f0) / self.t1 * t**2)
|
|
1650
|
+
+ self.phase
|
|
1651
|
+
)
|
|
1652
|
+
return self.amplitude * np.sin(theta) + self.bias
|
|
1653
|
+
|
|
1654
|
+
|
|
1655
|
+
class _StairClock(Clock):
|
|
1656
|
+
"""Ticks at transition times of a repeating sequence staircase.
|
|
1657
|
+
|
|
1658
|
+
Attributes:
|
|
1659
|
+
durations (numpy.ndarray): The durations of each staircase level.
|
|
1660
|
+
period (float): Total period of one sequence cycle.
|
|
1661
|
+
offsets (numpy.ndarray): Transition times within one period cycle.
|
|
1662
|
+
name (str): Name of the clock.
|
|
1663
|
+
counter (int): Current tick counter.
|
|
1664
|
+
"""
|
|
1665
|
+
|
|
1666
|
+
def __init__(self, durations, name=None):
|
|
1667
|
+
"""Initializes the Stair Clock.
|
|
1668
|
+
|
|
1669
|
+
Args:
|
|
1670
|
+
durations: The durations of each level.
|
|
1671
|
+
name: Optional clock name.
|
|
1672
|
+
"""
|
|
1673
|
+
super().__init__()
|
|
1674
|
+
self.durations = np.asarray(durations, dtype=float)
|
|
1675
|
+
self.period = np.sum(self.durations)
|
|
1676
|
+
self.offsets = np.concatenate([[0.0], np.cumsum(self.durations)[:-1]])
|
|
1677
|
+
self.name = name or f"StairClock_{id(self) % 10000}"
|
|
1678
|
+
self.counter = 0
|
|
1679
|
+
|
|
1680
|
+
def next_tick(self):
|
|
1681
|
+
"""Advances the clock's tick counter."""
|
|
1682
|
+
self.counter += 1
|
|
1683
|
+
|
|
1684
|
+
def actual_tick(self) -> float:
|
|
1685
|
+
"""Calculates and returns the timestamp of the current tick.
|
|
1686
|
+
|
|
1687
|
+
Returns:
|
|
1688
|
+
The timestamp of the current tick.
|
|
1689
|
+
"""
|
|
1690
|
+
n = len(self.offsets)
|
|
1691
|
+
cycle, phase = divmod(self.counter, n)
|
|
1692
|
+
return cycle * self.period + self.offsets[phase]
|
|
1693
|
+
|
|
1694
|
+
def end(self):
|
|
1695
|
+
"""Returns whether the clock has finished scheduling ticks.
|
|
1696
|
+
|
|
1697
|
+
Returns:
|
|
1698
|
+
Always False (infinite periodic clock).
|
|
1699
|
+
"""
|
|
1700
|
+
return False
|
|
1701
|
+
|
|
1702
|
+
|
|
1703
|
+
class RepeatingStair(Block):
|
|
1704
|
+
"""Generates a repeating sequence staircase.
|
|
1705
|
+
|
|
1706
|
+
Attributes:
|
|
1707
|
+
durations (numpy.ndarray): Durations of each staircase level.
|
|
1708
|
+
values (numpy.ndarray): Values of the staircase at each level.
|
|
1709
|
+
period (float): Total period of one sequence cycle.
|
|
1710
|
+
offsets (numpy.ndarray): Transition times within one period cycle.
|
|
1711
|
+
clock (Clock): Bound stair clock scheduling transitions.
|
|
1712
|
+
"""
|
|
1713
|
+
|
|
1714
|
+
def __init__(
|
|
1715
|
+
self,
|
|
1716
|
+
durations: Iterable[float] = (1.0, 1.0),
|
|
1717
|
+
values: Iterable[float] | Iterable[Iterable[float]] = (0.0, 1.0),
|
|
1718
|
+
name: str | None = None,
|
|
1719
|
+
outputs: list[tuple[str, Any]] | None = None,
|
|
1720
|
+
):
|
|
1721
|
+
"""Initializes the RepeatingStair block.
|
|
1722
|
+
|
|
1723
|
+
Args:
|
|
1724
|
+
durations: The durations of each level.
|
|
1725
|
+
values: The values corresponding to each level.
|
|
1726
|
+
name: Optional block name.
|
|
1727
|
+
outputs: Optional output port specs.
|
|
1728
|
+
|
|
1729
|
+
Raises:
|
|
1730
|
+
ValueError: If length of durations and values do not match,
|
|
1731
|
+
or if duration sum is not positive,
|
|
1732
|
+
or if outputs length does not match num_outputs.
|
|
1733
|
+
"""
|
|
1734
|
+
self.durations = np.asarray(durations, dtype=float)
|
|
1735
|
+
self.values = np.asarray(values, dtype=float)
|
|
1736
|
+
|
|
1737
|
+
if self.values.ndim == 1:
|
|
1738
|
+
self.values = self.values.reshape(-1, 1)
|
|
1739
|
+
|
|
1740
|
+
if len(self.durations) != len(self.values):
|
|
1741
|
+
raise ValueError("The length of durations and values must match.")
|
|
1742
|
+
|
|
1743
|
+
self.period = np.sum(self.durations)
|
|
1744
|
+
if self.period <= 0.0:
|
|
1745
|
+
raise ValueError("The sum of durations must be greater than zero.")
|
|
1746
|
+
|
|
1747
|
+
self.offsets = np.concatenate([[0.0], np.cumsum(self.durations)[:-1]])
|
|
1748
|
+
num_outputs = self.values.shape[1]
|
|
1749
|
+
|
|
1750
|
+
if outputs is not None:
|
|
1751
|
+
if len(outputs) != num_outputs:
|
|
1752
|
+
raise ValueError(
|
|
1753
|
+
f"Length of outputs ({len(outputs)}) does not match num_outputs ({num_outputs})"
|
|
1754
|
+
)
|
|
1755
|
+
resolved_outputs = outputs
|
|
1756
|
+
else:
|
|
1757
|
+
resolved_outputs = [(f"y{i}", None) for i in range(num_outputs)]
|
|
1758
|
+
|
|
1759
|
+
super().__init__(
|
|
1760
|
+
num_inputs=0,
|
|
1761
|
+
num_outputs=num_outputs,
|
|
1762
|
+
name=name,
|
|
1763
|
+
outputs=resolved_outputs,
|
|
1764
|
+
)
|
|
1765
|
+
|
|
1766
|
+
self.clock = _StairClock(durations=self.durations, name=f"{self.name}_clock")
|
|
1767
|
+
self.clock.bind(self)
|
|
1768
|
+
|
|
1769
|
+
def update(self, t):
|
|
1770
|
+
"""Dummy update method required by bound Clock transitions.
|
|
1771
|
+
|
|
1772
|
+
Args:
|
|
1773
|
+
t: The current simulation time.
|
|
1774
|
+
"""
|
|
1775
|
+
pass
|
|
1776
|
+
|
|
1777
|
+
def output(self, t):
|
|
1778
|
+
"""Calculates and returns the staircase output at time t.
|
|
1779
|
+
|
|
1780
|
+
Args:
|
|
1781
|
+
t: The current simulation time.
|
|
1782
|
+
|
|
1783
|
+
Returns:
|
|
1784
|
+
A numpy array containing the computed staircase value(s).
|
|
1785
|
+
"""
|
|
1786
|
+
t_rel = t % self.period
|
|
1787
|
+
idx = 0
|
|
1788
|
+
for tj in self.offsets:
|
|
1789
|
+
if t_rel >= tj - 1e-9:
|
|
1790
|
+
idx += 1
|
|
1791
|
+
else:
|
|
1792
|
+
break
|
|
1793
|
+
active_idx = idx - 1
|
|
1794
|
+
return self.values[active_idx]
|
|
1795
|
+
|
|
1796
|
+
|
|
1797
|
+
class BandLimitedWhiteNoise(Random):
|
|
1798
|
+
"""Generates band limited white noise with specified PSD and sample time.
|
|
1799
|
+
|
|
1800
|
+
Attributes:
|
|
1801
|
+
psd (numpy.ndarray): Power Spectral Density / Noise power of the noise.
|
|
1802
|
+
sample_time (float): Correlation / sample time of the noise.
|
|
1803
|
+
"""
|
|
1804
|
+
|
|
1805
|
+
def __init__(
|
|
1806
|
+
self,
|
|
1807
|
+
psd: float | Iterable[float] = 0.1,
|
|
1808
|
+
sample_time: float = 0.1,
|
|
1809
|
+
seed: int | None = None,
|
|
1810
|
+
name: str | None = None,
|
|
1811
|
+
outputs: list[tuple[str, Any]] | None = None,
|
|
1812
|
+
):
|
|
1813
|
+
"""Initializes the Band Limited White Noise block.
|
|
1814
|
+
|
|
1815
|
+
Args:
|
|
1816
|
+
psd: Power Spectral Density. Can be a float or an iterable of floats.
|
|
1817
|
+
sample_time: Correlation / sample time.
|
|
1818
|
+
seed: Optional seed for the random number generator.
|
|
1819
|
+
name: Optional block name.
|
|
1820
|
+
outputs: Optional output port configuration specs.
|
|
1821
|
+
|
|
1822
|
+
Raises:
|
|
1823
|
+
TypeError: If `psd` is not a float, iterable of floats, or numpy array.
|
|
1824
|
+
"""
|
|
1825
|
+
if isinstance(psd, (float, int)):
|
|
1826
|
+
self.psd = np.array([float(psd)])
|
|
1827
|
+
elif is_iterable_of_floats(psd):
|
|
1828
|
+
self.psd = np.array([float(p) for p in psd])
|
|
1829
|
+
elif isinstance(psd, np.ndarray):
|
|
1830
|
+
self.psd = np.atleast_1d(psd).astype(float)
|
|
1831
|
+
else:
|
|
1832
|
+
raise TypeError(
|
|
1833
|
+
"psd must be a float, an iterable of floats, or a numpy array"
|
|
1834
|
+
)
|
|
1835
|
+
|
|
1836
|
+
self.sample_time = sample_time
|
|
1837
|
+
std_noise = np.sqrt(self.psd / sample_time)
|
|
1838
|
+
|
|
1839
|
+
super().__init__(
|
|
1840
|
+
mean=0.0,
|
|
1841
|
+
std=std_noise,
|
|
1842
|
+
period=sample_time,
|
|
1843
|
+
seed=seed,
|
|
1844
|
+
name=name,
|
|
1845
|
+
outputs=outputs,
|
|
1846
|
+
)
|