forked from python-control/python-control
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxferfcn_test.py
More file actions
619 lines (488 loc) · 25 KB
/
Copy pathxferfcn_test.py
File metadata and controls
619 lines (488 loc) · 25 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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
#!/usr/bin/env python
#
# xferfcn_test.py - test TransferFunction class
# RMM, 30 Mar 2011 (based on TestXferFcn from v0.4a)
import unittest
import numpy as np
from control.statesp import StateSpace, _convertToStateSpace, rss
from control.xferfcn import TransferFunction, _convert_to_transfer_function, ss2tf
from control.lti import evalfr
from control.exception import slycot_check
# from control.lti import isdtime
class TestXferFcn(unittest.TestCase):
"""These are tests for functionality and correct reporting of the transfer
function class. Throughout these tests, we will give different input
formats to the xTranferFunction constructor, to try to break it. These
tests have been verified in MATLAB."""
# Tests for raising exceptions.
def test_constructor_bad_input_type(self):
"""Give the constructor invalid input types."""
self.assertRaises(TypeError, TransferFunction, [[0., 1.], [2., 3.]], [[5., 2.], [3., 0.]])
def test_constructor_inconsistent_dimension(self):
"""Give the constructor a numerator and denominator of different
sizes."""
self.assertRaises(ValueError, TransferFunction, [[[1.]]], [[[1.], [2., 3.]]])
self.assertRaises(ValueError, TransferFunction, [[[1.]]], [[[1.]], [[2., 3.]]])
self.assertRaises(ValueError, TransferFunction, [[[1.]]],
[[[1.], [1., 2.]], [[5., 2.], [2., 3.]]])
def test_constructor_inconsistent_columns(self):
"""Give the constructor inputs that do not have the same number of
columns in each row."""
self.assertRaises(ValueError, TransferFunction, 1., [[[1.]], [[2.], [3.]]])
self.assertRaises(ValueError, TransferFunction, [[[1.]], [[2.], [3.]]], 1.)
def test_constructor_zero_denominator(self):
"""Give the constructor a transfer function with a zero denominator."""
self.assertRaises(ValueError, TransferFunction, 1., 0.)
self.assertRaises(ValueError, TransferFunction,
[[[1.], [2., 3.]], [[-1., 4.], [3., 2.]]],
[[[1., 0.], [0.]], [[0., 0.], [2.]]])
def test_add_inconsistent_dimension(self):
"""Add two transfer function matrices of different sizes."""
sys1 = TransferFunction([[[1., 2.]]], [[[4., 5.]]])
sys2 = TransferFunction([[[4., 3.]], [[1., 2.]]], [[[1., 6.]], [[2., 4.]]])
self.assertRaises(ValueError, sys1.__add__, sys2)
self.assertRaises(ValueError, sys1.__sub__, sys2)
self.assertRaises(ValueError, sys1.__radd__, sys2)
self.assertRaises(ValueError, sys1.__rsub__, sys2)
def test_mul_inconsistent_dimension(self):
"""Multiply two transfer function matrices of incompatible sizes."""
sys1 = TransferFunction([[[1., 2.], [4., 5.]], [[2., 5.], [4., 3.]]],
[[[6., 2.], [4., 1.]], [[6., 7.], [2., 4.]]])
sys2 = TransferFunction([[[1.]], [[2.]], [[3.]]], [[[4.]], [[5.]], [[6.]]])
self.assertRaises(ValueError, sys1.__mul__, sys2)
self.assertRaises(ValueError, sys2.__mul__, sys1)
self.assertRaises(ValueError, sys1.__rmul__, sys2)
self.assertRaises(ValueError, sys2.__rmul__, sys1)
# Tests for TransferFunction._truncatecoeff
def test_truncate_coefficients_non_null_numerator(self):
"""Remove extraneous zeros in polynomial representations."""
sys1 = TransferFunction([0., 0., 1., 2.], [[[0., 0., 0., 3., 2., 1.]]])
np.testing.assert_array_equal(sys1.num, [[[1., 2.]]])
np.testing.assert_array_equal(sys1.den, [[[3., 2., 1.]]])
def test_truncate_coefficients_null_numerator(self):
"""Remove extraneous zeros in polynomial representations."""
sys1 = TransferFunction([0., 0., 0.], 1.)
np.testing.assert_array_equal(sys1.num, [[[0.]]])
np.testing.assert_array_equal(sys1.den, [[[1.]]])
# Tests for TransferFunction.__neg__
def test_reverse_sign_scalar(self):
"""Negate a direct feedthrough system."""
sys1 = TransferFunction(2., np.array([-3.]))
sys2 = - sys1
np.testing.assert_array_equal(sys2.num, [[[-2.]]])
np.testing.assert_array_equal(sys2.den, [[[-3.]]])
def test_reverse_sign_siso(self):
"""Negate a SISO system."""
sys1 = TransferFunction([1., 3., 5], [1., 6., 2., -1.])
sys2 = - sys1
np.testing.assert_array_equal(sys2.num, [[[-1., -3., -5.]]])
np.testing.assert_array_equal(sys2.den, [[[1., 6., 2., -1.]]])
@unittest.skipIf(not slycot_check(), "slycot not installed")
def test_reverse_sign_mimo(self):
"""Negate a MIMO system."""
num1 = [[[1., 2.], [0., 3.], [2., -1.]],
[[1.], [4., 0.], [1., -4., 3.]]]
num3 = [[[-1., -2.], [0., -3.], [-2., 1.]],
[[-1.], [-4., 0.], [-1., 4., -3.]]]
den1 = [[[-3., 2., 4.], [1., 0., 0.], [2., -1.]],
[[3., 0., .0], [2., -1., -1.], [1.]]]
sys1 = TransferFunction(num1, den1)
sys2 = - sys1
sys3 = TransferFunction(num3, den1)
for i in range(sys3.outputs):
for j in range(sys3.inputs):
np.testing.assert_array_equal(sys2.num[i][j], sys3.num[i][j])
np.testing.assert_array_equal(sys2.den[i][j], sys3.den[i][j])
# Tests for TransferFunction.__add__
def test_add_scalar(self):
"""Add two direct feedthrough systems."""
sys1 = TransferFunction(1., [[[1.]]])
sys2 = TransferFunction(np.array([2.]), [1.])
sys3 = sys1 + sys2
np.testing.assert_array_equal(sys3.num, 3.)
np.testing.assert_array_equal(sys3.den, 1.)
def test_add_siso(self):
"""Add two SISO systems."""
sys1 = TransferFunction([1., 3., 5], [1., 6., 2., -1])
sys2 = TransferFunction([[np.array([-1., 3.])]], [[[1., 0., -1.]]])
sys3 = sys1 + sys2
# If sys3.num is [[[0., 20., 4., -8.]]], then this is wrong!
np.testing.assert_array_equal(sys3.num, [[[20., 4., -8]]])
np.testing.assert_array_equal(sys3.den, [[[1., 6., 1., -7., -2., 1.]]])
@unittest.skipIf(not slycot_check(), "slycot not installed")
def test_add_mimo(self):
"""Add two MIMO systems."""
num1 = [[[1., 2.], [0., 3.], [2., -1.]],
[[1.], [4., 0.], [1., -4., 3.]]]
den1 = [[[-3., 2., 4.], [1., 0., 0.], [2., -1.]],
[[3., 0., .0], [2., -1., -1.], [1.]]]
num2 = [[[0., 0., -1], [2.], [-1., -1.]],
[[1., 2.], [-1., -2.], [4.]]]
den2 = [[[-1.], [1., 2., 3.], [-1., -1.]],
[[-4., -3., 2.], [0., 1.], [1., 0.]]]
num3 = [[[3., -3., -6], [5., 6., 9.], [-4., -2., 2]],
[[3., 2., -3., 2], [-2., -3., 7., 2.], [1., -4., 3., 4]]]
den3 = [[[3., -2., -4.], [1., 2., 3., 0., 0.], [-2., -1., 1.]],
[[-12., -9., 6., 0., 0.], [2., -1., -1.], [1., 0.]]]
sys1 = TransferFunction(num1, den1)
sys2 = TransferFunction(num2, den2)
sys3 = sys1 + sys2
for i in range(sys3.outputs):
for j in range(sys3.inputs):
np.testing.assert_array_equal(sys3.num[i][j], num3[i][j])
np.testing.assert_array_equal(sys3.den[i][j], den3[i][j])
# Tests for TransferFunction.__sub__
def test_subtract_scalar(self):
"""Subtract two direct feedthrough systems."""
sys1 = TransferFunction(1., [[[1.]]])
sys2 = TransferFunction(np.array([2.]), [1.])
sys3 = sys1 - sys2
np.testing.assert_array_equal(sys3.num, -1.)
np.testing.assert_array_equal(sys3.den, 1.)
def test_subtract_siso(self):
"""Subtract two SISO systems."""
sys1 = TransferFunction([1., 3., 5], [1., 6., 2., -1])
sys2 = TransferFunction([[np.array([-1., 3.])]], [[[1., 0., -1.]]])
sys3 = sys1 - sys2
sys4 = sys2 - sys1
np.testing.assert_array_equal(sys3.num, [[[2., 6., -12., -10., -2.]]])
np.testing.assert_array_equal(sys3.den, [[[1., 6., 1., -7., -2., 1.]]])
np.testing.assert_array_equal(sys4.num, [[[-2., -6., 12., 10., 2.]]])
np.testing.assert_array_equal(sys4.den, [[[1., 6., 1., -7., -2., 1.]]])
@unittest.skipIf(not slycot_check(), "slycot not installed")
def test_subtract_mimo(self):
"""Subtract two MIMO systems."""
num1 = [[[1., 2.], [0., 3.], [2., -1.]],
[[1.], [4., 0.], [1., -4., 3.]]]
den1 = [[[-3., 2., 4.], [1., 0., 0.], [2., -1.]],
[[3., 0., .0], [2., -1., -1.], [1.]]]
num2 = [[[0., 0., -1], [2.], [-1., -1.]],
[[1., 2.], [-1., -2.], [4.]]]
den2 = [[[-1.], [1., 2., 3.], [-1., -1.]],
[[-4., -3., 2.], [0., 1.], [1., 0.]]]
num3 = [[[-3., 1., 2.], [1., 6., 9.], [0.]],
[[-3., -10., -3., 2], [2., 3., 1., -2], [1., -4., 3., -4]]]
den3 = [[[3., -2., -4], [1., 2., 3., 0., 0.], [1]],
[[-12., -9., 6., 0., 0.], [2., -1., -1], [1., 0.]]]
sys1 = TransferFunction(num1, den1)
sys2 = TransferFunction(num2, den2)
sys3 = sys1 - sys2
for i in range(sys3.outputs):
for j in range(sys3.inputs):
np.testing.assert_array_equal(sys3.num[i][j], num3[i][j])
np.testing.assert_array_equal(sys3.den[i][j], den3[i][j])
# Tests for TransferFunction.__mul__
def test_multiply_scalar(self):
"""Multiply two direct feedthrough systems."""
sys1 = TransferFunction(2., [1.])
sys2 = TransferFunction(1., 4.)
sys3 = sys1 * sys2
sys4 = sys1 * sys2
np.testing.assert_array_equal(sys3.num, [[[2.]]])
np.testing.assert_array_equal(sys3.den, [[[4.]]])
np.testing.assert_array_equal(sys3.num, sys4.num)
np.testing.assert_array_equal(sys3.den, sys4.den)
def test_multiply_siso(self):
"""Multiply two SISO systems."""
sys1 = TransferFunction([1., 3., 5], [1., 6., 2., -1])
sys2 = TransferFunction([[[-1., 3.]]], [[[1., 0., -1.]]])
sys3 = sys1 * sys2
sys4 = sys2 * sys1
np.testing.assert_array_equal(sys3.num, [[[-1., 0., 4., 15.]]])
np.testing.assert_array_equal(sys3.den, [[[1., 6., 1., -7., -2., 1.]]])
np.testing.assert_array_equal(sys3.num, sys4.num)
np.testing.assert_array_equal(sys3.den, sys4.den)
@unittest.skipIf(not slycot_check(), "slycot not installed")
def test_multiply_mimo(self):
"""Multiply two MIMO systems."""
num1 = [[[1., 2.], [0., 3.], [2., -1.]],
[[1.], [4., 0.], [1., -4., 3.]]]
den1 = [[[-3., 2., 4.], [1., 0., 0.], [2., -1.]],
[[3., 0., .0], [2., -1., -1.], [1.]]]
num2 = [[[0., 1., 2.]],
[[1., -5.]],
[[-2., 1., 4.]]]
den2 = [[[1., 0., 0., 0.]],
[[-2., 1., 3.]],
[[4., -1., -1., 0.]]]
num3 = [[[-24., 52., -14., 245., -490., -115., 467., -95., -56., 12.,
0., 0., 0.]],
[[24., -132., 138., 345., -768., -106., 510., 41., -79., -69.,
-23., 17., 6., 0.]]]
den3 = [[[48., -92., -84., 183., 44., -97., -2., 12., 0., 0., 0., 0.,
0., 0.]],
[[-48., 60., 84., -81., -45., 21., 9., 0., 0., 0., 0., 0., 0.]]]
sys1 = TransferFunction(num1, den1)
sys2 = TransferFunction(num2, den2)
sys3 = sys1 * sys2
for i in range(sys3.outputs):
for j in range(sys3.inputs):
np.testing.assert_array_equal(sys3.num[i][j], num3[i][j])
np.testing.assert_array_equal(sys3.den[i][j], den3[i][j])
# Tests for TransferFunction.__div__
def test_divide_scalar(self):
"""Divide two direct feedthrough systems."""
sys1 = TransferFunction(np.array([3.]), -4.)
sys2 = TransferFunction(5., 2.)
sys3 = sys1 / sys2
np.testing.assert_array_equal(sys3.num, [[[6.]]])
np.testing.assert_array_equal(sys3.den, [[[-20.]]])
def test_divide_siso(self):
"""Divide two SISO systems."""
sys1 = TransferFunction([1., 3., 5], [1., 6., 2., -1])
sys2 = TransferFunction([[[-1., 3.]]], [[[1., 0., -1.]]])
sys3 = sys1 / sys2
sys4 = sys2 / sys1
np.testing.assert_array_equal(sys3.num, [[[1., 3., 4., -3., -5.]]])
np.testing.assert_array_equal(sys3.den, [[[-1., -3., 16., 7., -3.]]])
np.testing.assert_array_equal(sys4.num, sys3.den)
np.testing.assert_array_equal(sys4.den, sys3.num)
def test_evalfr_siso(self):
"""Evaluate the frequency response of a SISO system at one frequency."""
sys = TransferFunction([1., 3., 5], [1., 6., 2., -1])
np.testing.assert_array_almost_equal(evalfr(sys, 1j),
np.array([[-0.5 - 0.5j]]))
np.testing.assert_array_almost_equal(evalfr(sys, 32j),
np.array([[0.00281959302585077 - 0.030628473607392j]]))
# Test call version as well
np.testing.assert_almost_equal(sys(1.j), -0.5 - 0.5j)
np.testing.assert_almost_equal(sys(32.j), 0.00281959302585077 - 0.030628473607392j)
# Test internal version (with real argument)
np.testing.assert_array_almost_equal(sys._evalfr(1.),
np.array([[-0.5 - 0.5j]]))
np.testing.assert_array_almost_equal(sys._evalfr(32.),
np.array([[0.00281959302585077 - 0.030628473607392j]]))
# Deprecated version of the call (should generate warning)
import warnings
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
sys.evalfr(1.)
assert len(w) == 1
assert issubclass(w[-1].category, PendingDeprecationWarning)
@unittest.skipIf(not slycot_check(), "slycot not installed")
def test_evalfr_mimo(self):
"""Evaluate the frequency response of a MIMO system at one frequency."""
num = [[[1., 2.], [0., 3.], [2., -1.]],
[[1.], [4., 0.], [1., -4., 3.]]]
den = [[[-3., 2., 4.], [1., 0., 0.], [2., -1.]],
[[3., 0., .0], [2., -1., -1.], [1.]]]
sys = TransferFunction(num, den)
resp = [[0.147058823529412 + 0.0882352941176471j, -0.75, 1.],
[-0.083333333333333, -0.188235294117647 - 0.847058823529412j,
-1. - 8.j]]
np.testing.assert_array_almost_equal(sys._evalfr(2.), resp)
# Test call version as well
np.testing.assert_array_almost_equal(sys(2.j), resp)
def test_freqresp_siso(self):
"""Evaluate the magnitude and phase of a SISO system at multiple frequencies."""
sys = TransferFunction([1., 3., 5], [1., 6., 2., -1])
truemag = [[[4.63507337473906, 0.707106781186548, 0.0866592803995351]]]
truephase = [[[-2.89596891081488, -2.35619449019234,
-1.32655885133871]]]
trueomega = [0.1, 1., 10.]
mag, phase, omega = sys.freqresp(trueomega)
np.testing.assert_array_almost_equal(mag, truemag)
np.testing.assert_array_almost_equal(phase, truephase)
np.testing.assert_array_almost_equal(omega, trueomega)
@unittest.skipIf(not slycot_check(), "slycot not installed")
def test_freqresp_mimo(self):
"""Evaluate the magnitude and phase of a MIMO system at multiple frequencies."""
num = [[[1., 2.], [0., 3.], [2., -1.]],
[[1.], [4., 0.], [1., -4., 3.]]]
den = [[[-3., 2., 4.], [1., 0., 0.], [2., -1.]],
[[3., 0., .0], [2., -1., -1.], [1.]]]
sys = TransferFunction(num, den)
true_omega = [0.1, 1., 10.]
true_mag = [[[0.496287094505259, 0.307147558416976, 0.0334738176210382],
[300., 3., 0.03], [1., 1., 1.]],
[[33.3333333333333, 0.333333333333333, 0.00333333333333333],
[0.390285696125482, 1.26491106406735, 0.198759144198533],
[3.01663720059274, 4.47213595499958, 104.92378186093]]]
true_phase = [[[3.7128711165168e-4, 0.185347949995695, 1.30770596539255],
[-np.pi, -np.pi, -np.pi], [0., 0., 0.]],
[[-np.pi, -np.pi, -np.pi],
[-1.66852323415362, -1.89254688119154, -1.62050658356412],
[-0.132989648369409, -1.1071487177940, -2.7504672066207]]]
mag, phase, omega = sys.freqresp(true_omega)
np.testing.assert_array_almost_equal(mag, true_mag)
np.testing.assert_array_almost_equal(phase, true_phase)
np.testing.assert_array_equal(omega, true_omega)
# Tests for TransferFunction.pole and TransferFunction.zero.
@unittest.skipIf(not slycot_check(), "slycot not installed")
def test_pole_mimo(self):
"""Test for correct MIMO poles."""
sys = TransferFunction([[[1.], [1.]], [[1.], [1.]]],
[[[1., 2.], [1., 3.]], [[1., 4., 4.], [1., 9., 14.]]])
p = sys.pole()
np.testing.assert_array_almost_equal(p, [-2., -2., -7., -3., -2.])
@unittest.skipIf(not slycot_check(), "slycot not installed")
def test_double_cancelling_poles_siso(self):
H = TransferFunction([1, 1], [1, 2, 1])
p = H.pole()
np.testing.assert_array_almost_equal(p, [-1, -1])
# Tests for TransferFunction.feedback
def test_feedback_siso(self):
"""Test for correct SISO transfer function feedback."""
sys1 = TransferFunction([-1., 4.], [1., 3., 5.])
sys2 = TransferFunction([2., 3., 0.], [1., -3., 4., 0])
sys3 = sys1.feedback(sys2)
sys4 = sys1.feedback(sys2, 1)
np.testing.assert_array_equal(sys3.num, [[[-1., 7., -16., 16., 0.]]])
np.testing.assert_array_equal(sys3.den, [[[1., 0., -2., 2., 32., 0.]]])
np.testing.assert_array_equal(sys4.num, [[[-1., 7., -16., 16., 0.]]])
np.testing.assert_array_equal(sys4.den, [[[1., 0., 2., -8., 8., 0.]]])
@unittest.skipIf(not slycot_check(), "slycot not installed")
def test_convert_to_transfer_function(self):
"""Test for correct state space to transfer function conversion."""
A = [[1., -2.], [-3., 4.]]
B = [[6., 5.], [4., 3.]]
C = [[1., -2.], [3., -4.], [5., -6.]]
D = [[1., 0.], [0., 1.], [1., 0.]]
sys = StateSpace(A, B, C, D)
tfsys = _convert_to_transfer_function(sys)
num = [[np.array([1., -7., 10.]), np.array([-1., 10.])],
[np.array([2., -8.]), np.array([1., -2., -8.])],
[np.array([1., 1., -30.]), np.array([7., -22.])]]
den = [[np.array([1., -5., -2.]) for _ in range(sys.inputs)]
for _ in range(sys.outputs)]
for i in range(sys.outputs):
for j in range(sys.inputs):
np.testing.assert_array_almost_equal(tfsys.num[i][j], num[i][j])
np.testing.assert_array_almost_equal(tfsys.den[i][j], den[i][j])
def test_minreal(self):
"""Try the minreal function, and also test easy entry by creation
of a Laplace variable s"""
s = TransferFunction([1, 0], [1])
h = (s + 1) * (s + 2.00000000001) / (s + 2) / (s**2 + s + 1)
hm = h.minreal()
hr = (s + 1) / (s**2 + s + 1)
np.testing.assert_array_almost_equal(hm.num[0][0], hr.num[0][0])
np.testing.assert_array_almost_equal(hm.den[0][0], hr.den[0][0])
np.testing.assert_equal(hm.dt, hr.dt)
def test_minreal_2(self):
"""This one gave a problem, due to poly([]) giving simply 1
instead of numpy.array([1])"""
s = TransferFunction([1, 0], [1])
G = 6205/(s*(s**2 + 13*s + 1281))
Heq = G.feedback(1)
H1 = 1/(s+5)
H2a = Heq/H1
H2b = H2a.minreal()
hr = 6205/(s**2+8*s+1241)
np.testing.assert_array_almost_equal(H2b.num[0][0], hr.num[0][0])
np.testing.assert_array_almost_equal(H2b.den[0][0], hr.den[0][0])
np.testing.assert_equal(H2b.dt, hr.dt)
def test_minreal_3(self):
"""Regression test for minreal of tf([1,1],[1,1])"""
g = TransferFunction([1,1],[1,1]).minreal()
np.testing.assert_array_almost_equal(1.0, g.num[0][0])
np.testing.assert_array_almost_equal(1.0, g.den[0][0])
np.testing.assert_equal(None, g.dt)
def test_minreal_4(self):
"""Check minreal on discrete TFs."""
T = 0.01
z = TransferFunction([1, 0], [1], T)
h = (z - 1.00000000001) * (z + 1.0000000001) / (z**2 - 1)
hm = h.minreal()
hr = TransferFunction([1], [1], T)
np.testing.assert_array_almost_equal(hm.num[0][0], hr.num[0][0])
np.testing.assert_equal(hr.dt, hm.dt)
@unittest.skipIf(not slycot_check(), "slycot not installed")
def test_state_space_conversion_mimo(self):
"""Test conversion of a single input, two-output state-space
system against the same TF"""
s = TransferFunction([1, 0], [1])
b0 = 0.2
b1 = 0.1
b2 = 0.5
a0 = 2.3
a1 = 6.3
a2 = 3.6
a3 = 1.0
h = (b0 + b1*s + b2*s**2)/(a0 + a1*s + a2*s**2 + a3*s**3)
H = TransferFunction([[h.num[0][0]], [(h*s).num[0][0]]],
[[h.den[0][0]], [h.den[0][0]]])
sys = _convertToStateSpace(H)
H2 = _convert_to_transfer_function(sys)
np.testing.assert_array_almost_equal(H.num[0][0], H2.num[0][0])
np.testing.assert_array_almost_equal(H.den[0][0], H2.den[0][0])
np.testing.assert_array_almost_equal(H.num[1][0], H2.num[1][0])
np.testing.assert_array_almost_equal(H.den[1][0], H2.den[1][0])
@unittest.skipIf(not slycot_check(), "slycot not installed")
def test_indexing(self):
tm = ss2tf(rss(5, 3, 3))
# scalar indexing
sys01 = tm[0, 1]
np.testing.assert_array_almost_equal(sys01.num[0][0], tm.num[0][1])
np.testing.assert_array_almost_equal(sys01.den[0][0], tm.den[0][1])
# slice indexing
sys = tm[:2, 1:3]
np.testing.assert_array_almost_equal(sys.num[0][0], tm.num[0][1])
np.testing.assert_array_almost_equal(sys.den[0][0], tm.den[0][1])
np.testing.assert_array_almost_equal(sys.num[0][1], tm.num[0][2])
np.testing.assert_array_almost_equal(sys.den[0][1], tm.den[0][2])
np.testing.assert_array_almost_equal(sys.num[1][0], tm.num[1][1])
np.testing.assert_array_almost_equal(sys.den[1][0], tm.den[1][1])
np.testing.assert_array_almost_equal(sys.num[1][1], tm.num[1][2])
np.testing.assert_array_almost_equal(sys.den[1][1], tm.den[1][2])
def test_matrix_multiply(self):
"""MIMO transfer functions should be multiplyable by constant
matrices"""
s = TransferFunction([1, 0], [1])
b0 = 0.2
b1 = 0.1
b2 = 0.5
a0 = 2.3
a1 = 6.3
a2 = 3.6
a3 = 1.0
h = (b0 + b1*s + b2*s**2)/(a0 + a1*s + a2*s**2 + a3*s**3)
H = TransferFunction([[h.num[0][0]], [(h*s).num[0][0]]],
[[h.den[0][0]], [h.den[0][0]]])
H1 = (np.matrix([[1.0, 0]])*H).minreal()
H2 = (np.matrix([[0, 1.0]])*H).minreal()
np.testing.assert_array_almost_equal(H.num[0][0], H1.num[0][0])
np.testing.assert_array_almost_equal(H.den[0][0], H1.den[0][0])
np.testing.assert_array_almost_equal(H.num[1][0], H2.num[0][0])
np.testing.assert_array_almost_equal(H.den[1][0], H2.den[0][0])
def test_dcgain_cont(self):
"""Test DC gain for continuous-time transfer functions"""
sys = TransferFunction(6, 3)
np.testing.assert_equal(sys.dcgain(), 2)
sys2 = TransferFunction(6, [1, 3])
np.testing.assert_equal(sys2.dcgain(), 2)
sys3 = TransferFunction(6, [1, 0])
np.testing.assert_equal(sys3.dcgain(), np.inf)
num = [[[15], [21], [33]], [[10], [14], [22]]]
den = [[[1, 3], [2, 3], [3, 3]], [[1, 5], [2, 7], [3, 11]]]
sys4 = TransferFunction(num, den)
expected = [[5, 7, 11], [2, 2, 2]]
np.testing.assert_array_equal(sys4.dcgain(), expected)
def test_dcgain_discr(self):
"""Test DC gain for discrete-time transfer functions"""
# static gain
sys = TransferFunction(6, 3, True)
np.testing.assert_equal(sys.dcgain(), 2)
# averaging filter
sys = TransferFunction(0.5, [1, -0.5], True)
np.testing.assert_almost_equal(sys.dcgain(), 1)
# differencer
sys = TransferFunction(1, [1, -1], True)
np.testing.assert_equal(sys.dcgain(), np.inf)
# summer
# causes a RuntimeWarning due to the divide by zero
sys = TransferFunction([1,-1], [1], True)
np.testing.assert_equal(sys.dcgain(), 0)
def test_ss2tf(self):
A = np.array([[-4, -1], [-1, -4]])
B = np.array([[1], [3]])
C = np.array([[3, 1]])
D = 0
sys = ss2tf(A, B, C, D)
true_sys = TransferFunction([6., 14.], [1., 8., 15.])
np.testing.assert_almost_equal(sys.num, true_sys.num)
np.testing.assert_almost_equal(sys.den, true_sys.den)
def suite():
return unittest.TestLoader().loadTestsFromTestCase(TestXferFcn)
if __name__ == "__main__":
unittest.main()