-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathctrlpy.py
More file actions
518 lines (362 loc) · 18.2 KB
/
ctrlpy.py
File metadata and controls
518 lines (362 loc) · 18.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
"""
This module is designed for solving control theory and signal theory basic problems.
"""
from functools import wraps
from typing import Callable
from collections import namedtuple
import numpy as np
import sympy as sp
import matplotlib.pyplot as plt
from scipy import signal
PlotData = namedtuple('PlotData', ['xlabel', 'ylabel', 'log_scale', 'title'])
def simplifier(func: Callable[..., sp.Expr]) -> Callable[..., sp.Expr]:
"""
Decorator to simplify the result of a function that returns a SymPy expression.
This decorator takes a function that returns a SymPy expression, applies
SymPy's `simplify` function to the result, and returns the simplified expression.
:param func: The function that returns a SymPy expression.
:return: A decorated function that returns a simplified SymPy expression.
"""
@wraps(func)
def wrapper(*args, **kwargs) -> sp.Expr:
res = func(*args, **kwargs)
return sp.simplify(res)
return wrapper
def process(expr: sp.Expr, s: sp.Symbol) -> sp.Expr:
"""
Process a SymPy expression representing a transfer function.
This function simplifies and separates the given expression into its numerator and denominator.
:param expr: The expression representing the transfer function.
:param s: The symbol used in the transfer function.
:return: The processed transfer function with its numerator and denominator separated.
"""
simple = sp.simplify(expr)
num = sp.collect(sp.expand(sp.numer(simple)), s)
den = sp.collect(sp.expand(sp.denom(simple)), s)
return num / den
class ExprMissingVariableError(ValueError):
"""
Custom exception for cases where a SymPy expression is missing a required variable.
:param variable_name: The variable that is missing from the expression.
"""
def __init__(self, variable_name: sp.Symbol):
"""
Initialize the exception.
:param variable_name: The variable that is missing from the expression.
"""
super().__init__(f'The expression must contain {variable_name}.')
class InvalidSymbolError(ValueError):
"""
Exception raised when a symbol variable is invalid.
This exception is raised when a symbol variable is considered invalid for use in the
`System.from_string` method. A valid symbol variable must contain exactly one character
and be an instance of either the `sympy.Symbol` class or a string of length 1.
:param message: The optional custom error message. If not provided, a default message
is used.
"""
def __init__(self, message: str | None = None):
if message is None:
message = 'A valid symbol variable must contain exactly one character ' \
'and be an instance of either the sympy.Symbol class or a string of length 1.'
super().__init__(message)
class System:
"""
Represents a linear time-invariant (LTI) system with a transfer function.
This class encapsulates the properties and behavior of an LTI system, including its transfer function and various
operations related to signal processing and control systems.
:param tf: The transfer function of the system.
Attributes:
_s (sympy.Symbol): The Laplace variable 's' used in the transfer function.
_t (sympy.Symbol): The time variable 't' used for time-domain signal representations.
_w (sympy.Symbol): The angular frequency variable 'w' used in frequency domain operations.
Methods:
__init__(tf): Initialize the System with a transfer function.
from_string(string, symbol): Create a System from a string representation.
__repr__(): Return a string representation of the System.
__str__(): Return a human-readable string representation of the System.
__eq__(other): Check if two Systems are equal.
tf_coefs: Get the coefficients of the transfer function as NumPy arrays.
lti: Get the scipy.signal.lti instance of the System.
response(inp_signal): Calculate the system's response to an input signal.
step_response(): Calculate the system's step response.
delta_response(): Calculate the system's delta (Dirac delta) response.
Note: This class is primarily intended for symbolic math and control systems applications.
"""
_s: sp.Symbol
_t: sp.Symbol
_w: sp.Symbol
_s = sp.symbols('s')
_t, _w = sp.symbols('t w', real=True)
def __init__(self, tf: sp.Expr):
"""
Initialize the System with a transfer function.
:param tf: The transfer function of the system.
:raises ExprMissingVariableError: If the Laplace variable 's' is not found in the transfer function.
"""
if self._s not in tf.free_symbols:
raise ExprMissingVariableError(self._s)
self.tf = process(tf, self._s)
@classmethod
def from_string(cls, string: str, symbol: sp.Symbol | str) -> 'System':
"""
Create a System from a string representation of the transfer function.
This class method allows the creation of a System instance from a string representation of a transfer function.
The string representation should use the specified symbol as a placeholder for the Laplace variable 's' in the
transfer function.
:param string: A string representation of the transfer function.
:param symbol: The symbol variable representing the Laplace variable 's'.
:return: A System instance with the transfer function.
:raises InvalidSymbolError: If the symbol variable is invalid.
"""
if isinstance(symbol, sp.Symbol) or isinstance(symbol, str) and len(symbol) == 1:
return cls(process(sp.sympify(string, locals={symbol: cls._s}), cls._s))
raise InvalidSymbolError
def __repr__(self) -> str:
"""
Return a string representation of the System.
"""
return f'System({self.tf})'
def __str__(self) -> str:
"""
Return a human-readable string representation of the System.
"""
return f'{{System with transfer function: {str(self.tf)}}}'
def __eq__(self, other: object) -> bool:
"""
Check if two Systems are equal.
:param other: The other System to compare with.
:return: True if the Systems are equal, False if the Systems are not equal
and NotImplemented if comparing System with another type.
"""
if isinstance(other, System):
return self.tf == other.tf
return NotImplemented
@property
def tf_coefs(self) -> tuple[np.ndarray, np.ndarray]:
"""
Get the coefficients of the transfer function as NumPy arrays.
:return: A tuple containing NumPy arrays of numerator and denominator coefficients.
"""
num, den = sp.fraction(self.tf)
num_coeffs = np.array(sp.Poly(num, self._s).all_coeffs(), dtype=float)
den_coeffs = np.array(sp.Poly(den, self._s).all_coeffs(), dtype=float)
return num_coeffs, den_coeffs
@property
def lti(self) -> signal.lti:
"""
Get the scipy.signal.lti instance of the System.
:return: The scipy.signal.lti instance.
"""
return signal.lti(*self.tf_coefs)
@simplifier
def response(self, inp_signal: sp.Expr | str) -> sp.Expr:
"""
Calculate the system's response to an input signal.
:param inp_signal: The input signal as a symbolic expression or a string representation.
:type inp_signal: sympy.Expr | str
:return: The symbolic expression representing the system's response.
"""
if isinstance(inp_signal, str):
inp_signal = sp.sympify(inp_signal, locals={'t': self._t})
u = sp.laplace_transform(inp_signal, self._t, self._s)[0]
y = u * self.tf
return sp.inverse_laplace_transform(y, self._s, self._t)
@simplifier
def step_response(self) -> sp.Expr:
"""
Calculate the system's step response.
:return: The symbolic expression representing the system's step response.
"""
return self.response('1')
@simplifier
def delta_response(self) -> sp.Expr:
"""
Calculate the system's delta (Dirac delta) response.
:return: The symbolic expression representing the system's delta response.
"""
return self.response('DiracDelta(t)')
@simplifier
def s_freqs(sy: System) -> sp.Expr:
"""
Calculate the symbolic frequency response of a system in terms of a frequency variable 'w'.
This function computes the symbolic frequency response of a given system in terms of a frequency variable 'w'.
It replaces the Laplace variable 's' with 'jw', where 'j' is the imaginary unit,
and then returns the symbolic frequency response expression.
:param sy: The system for which to calculate the symbolic frequency response.
:return: The symbolic frequency response expression in terms of the complex frequency variable 'w'.
"""
w = sp.symbols('w', real=True)
h_jw = sy.tf.subs({'s': sp.I * w})
return h_jw
def create_plot_data(xlabel: str, ylabel: str, log_scale: bool, title: str) -> PlotData:
"""
Create a `PlotData` instance with the specified plot settings.
:param xlabel: The label for the x-axis.
:param ylabel: The label for the y-axis.
:param log_scale: Whether to use a logarithmic scale for the plot.
:param title: The title of the plot.
:return: A `PlotData` instance representing the plot settings.
"""
return PlotData(xlabel, ylabel, log_scale, title)
def plot(
sy: System,
function: Callable[[System, int | np.ndarray | None], tuple[np.ndarray, np.ndarray]],
plot_range: tuple[float, float],
plot_data: PlotData,
func_to_compare_with: Callable[[float | np.ndarray], float | np.ndarray] | None = None,
):
"""
Plot the response of a system using the specified function.
:param sy: The system for which to plot the response.
:param function: A function that calculates the response given the system and a frequency range.
:param plot_range: A tuple specifying the start and end values of the x-axis.
:param plot_data: A `PlotData` instance containing plot settings, including labels and scale information.
:param func_to_compare_with: A function to compare with the numerical plot (optional).
**Note**:
- If `func_to_compare_with` is provided, both the numerical and analytical plots will be displayed with a legend.
- When `plot_data.log_scale` is `True`, the x-axis will be displayed in a logarithmic scale.
- TODO: Implement proper yticks, including -3 and -17, for log_magnitude and other cases.
"""
x = np.linspace(*plot_range, 1000)
plt.plot(*function(sy, x), label='Numerical')
if func_to_compare_with is not None:
plt.plot(x, func_to_compare_with(x), label='Analytical')
plt.legend()
if plot_data.log_scale:
plt.xscale('log')
plt.xlabel(plot_data.xlabel)
plt.ylabel(plot_data.ylabel)
plt.grid()
plt.title(plot_data.title)
plt.show()
@simplifier
def s_re_freq(sy: System) -> sp.Expr:
"""
Calculate the real part of the symbolic frequency response for a system.
:param sy: The system for which to calculate the real part of the symbolic frequency response.
:return: The symbolic expression representing the real part of the frequency response.
"""
return sp.re(s_freqs(sy))
@simplifier
def s_im_freq(sy: System) -> sp.Expr:
"""
Calculate the imaginary part of the symbolic frequency response for a system.
:param sy: The system for which to calculate the imaginary part of the symbolic frequency response.
:return: The symbolic expression representing the imaginary part of the frequency response.
"""
return sp.im(s_freqs(sy))
@simplifier
def s_magnitude_freq(sy: System) -> sp.Expr:
"""
Calculate the magnitude of the symbolic frequency response for a system.
:param sy: The system for which to calculate the magnitude of the symbolic frequency response.
:return: The symbolic expression representing the magnitude of the frequency response.
"""
return sp.sqrt(s_re_freq(sy) ** 2 + s_im_freq(sy) ** 2)
@simplifier
def s_phase_freq(sy: System) -> sp.Expr:
"""
Calculate the phase of the symbolic frequency response for a system.
:param sy: The system for which to calculate the phase of the symbolic frequency response.
:return: The symbolic expression representing the phase of the frequency response.
"""
return sp.atan2(s_im_freq(sy), s_re_freq(sy))
@simplifier
def s_log_magnitude(sy: System) -> sp.Expr:
"""
Calculate the logarithm of the magnitude of the symbolic frequency response for a system.
:param sy: The system for which to calculate the logarithm of the magnitude of the symbolic frequency response.
:return: The symbolic expression representing the logarithm of the magnitude of the frequency response.
"""
return 20 * sp.log(s_magnitude_freq(sy)) / sp.log(10)
def n_freqs(sy: System, w: int | np.ndarray | None = None) -> tuple[np.ndarray, np.ndarray]:
"""
Calculate frequency response data for a system.
This function computes the frequency response data for a given system in a complex form.
:param sy: The system for which to calculate the frequency response.
:param w: The angular frequencies at which to calculate the response. If None, the default range is used.
:return: A tuple containing the angular frequencies and frequency response magnitudes.
"""
return signal.freqs(*sy.tf_coefs, worN=w)
def n_re_freq(sy: System, w: int | np.ndarray | None = None) -> tuple[np.ndarray, np.ndarray]:
"""
Calculate the real part of the frequency response data for a system.
This function computes the real part of the frequency response data for a given system.
:param sy: The system for which to calculate the real part of the frequency response.
:param w: The angular frequencies at which to calculate the response. If None, the default range is used.
:return: A tuple containing the angular frequencies and the real part of the frequency response magnitudes.
"""
w, h = n_freqs(sy, w)
return w, np.real(h)
def n_im_freq(sy: System, w: int | np.ndarray | None = None) -> tuple[np.ndarray, np.ndarray]:
"""
Calculate the imaginary part of the frequency response data for a system.
This function computes the imaginary part of the frequency response data for a given system.
:param sy: The system for which to calculate the imaginary part of the frequency response.
:param w: The angular frequencies at which to calculate the response. If None, the default range is used.
:return: A tuple containing the angular frequencies and the imaginary part of the frequency response magnitudes.
"""
w, h = n_freqs(sy, w)
return w, np.imag(h)
def n_magnitude_freq(sy: System, w: int | np.ndarray | None = None) -> tuple[np.ndarray, np.ndarray]:
"""
Calculate the magnitude-frequency response data for a system.
This function computes the magnitude of the frequency response data for a given system.
:param sy: The system for which to calculate the magnitude of the frequency response.
:param w: The angular frequencies at which to calculate the response. If None, the default range is used.
:return: A tuple containing the angular frequencies and the magnitude of the frequency response magnitudes.
"""
w, h = n_freqs(sy, w)
return w, np.abs(h)
def n_phase_freq(sy: System, w: int | np.ndarray | None = None) -> tuple[np.ndarray, np.ndarray]:
"""
Calculate the phase-frequency response data for a system.
This function computes the phase of the frequency response data for a given system.
:param sy: The system for which to calculate the phase of the frequency response.
:param w: The angular frequencies at which to calculate the response. If None, the default range is used.
:return: A tuple containing the angular frequencies and the phase of the frequency response magnitudes.
"""
w, h = n_freqs(sy, w)
return w, np.angle(h)
def n_log_magnitude_freq(sy: System, w: int | np.ndarray | None = None) -> tuple[np.ndarray, np.ndarray]:
"""
Calculate the logarithmic magnitude response data for a system.
This function computes the logarithmic magnitude response data for a given system.
:param sy: The system for which to calculate the logarithm of the magnitude of the frequency response.
:param w: The angular frequencies at which to calculate the response. If None, the default range is used.
:return: A tuple containing the angular frequencies and the logarithm of the magnitude of the frequency response
magnitudes.
"""
w, h = n_freqs(sy, w)
return w, 20 * np.log10(np.abs(h))
def __nyquist(sy: System, w: int | np.ndarray | None = None) -> tuple[np.ndarray, np.ndarray]:
"""
Compute the Nyquist plot data for a system.
This function computes the Nyquist plot data for a given system by separately calculating
the real and imaginary parts of the frequency response at specified angular frequencies.
:param sy: The system for which to calculate the Nyquist plot data.
:param w: The angular frequencies at which to calculate the response. If None, the default range is used.
:return: A tuple containing the real and imaginary parts of the Nyquist plot data.
"""
_, h1 = n_re_freq(sy, w)
_, h2 = n_im_freq(sy, w)
return h1, h2
def plot_nyquist(sy: System, w: int | np.ndarray | None = None, title: str | None = None) -> None:
"""
Plot the Nyquist diagram for a system.
This function generates a Nyquist plot for a given system by plotting the real and imaginary parts
of the frequency response at specified angular frequencies.
:param sy: The system for which to plot the Nyquist diagram.
:param w: The angular frequencies at which to calculate the Nyquist plot. If None, the default range is used.
:param title: Optional title for the Nyquist plot. If None, a default title is used.
"""
re, im = __nyquist(sy, w)
plt.plot(re, im)
if title is not None:
plt.title(title)
else:
plt.title('Nyquist')
plt.xlabel('Re H(jw)')
plt.ylabel('Im H(jw)')
plt.grid()
plt.show()