-
Notifications
You must be signed in to change notification settings - Fork 458
Expand file tree
/
Copy pathfrd_test.py
More file actions
469 lines (404 loc) · 18.7 KB
/
Copy pathfrd_test.py
File metadata and controls
469 lines (404 loc) · 18.7 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
#!/usr/bin/env python
#
# frd_test.py - test FRD class
# RvP, 4 Oct 2012
import unittest
import sys as pysys
import numpy as np
import control as ct
from control.statesp import StateSpace
from control.xferfcn import TransferFunction
from control.frdata import FRD, _convertToFRD, FrequencyResponseData
from control import bdalg
from control import freqplot
from control.exception import slycot_check
import matplotlib.pyplot as plt
class TestFRD(unittest.TestCase):
"""These are tests for functionality and correct reporting of the
frequency response data class."""
def testBadInputType(self):
"""Give the constructor invalid input types."""
self.assertRaises(ValueError, FRD)
self.assertRaises(TypeError, FRD, [1])
def testInconsistentDimension(self):
self.assertRaises(TypeError, FRD, [1, 1], [1, 2, 3])
def testSISOtf(self):
# get a SISO transfer function
h = TransferFunction([1], [1, 2, 2])
omega = np.logspace(-1, 2, 10)
frd = FRD(h, omega)
assert isinstance(frd, FRD)
np.testing.assert_array_almost_equal(
frd.freqresp([1.0]), h.freqresp([1.0]))
def testOperators(self):
# get two SISO transfer functions
h1 = TransferFunction([1], [1, 2, 2])
h2 = TransferFunction([1], [0.1, 1])
omega = np.logspace(-1, 2, 10)
f1 = FRD(h1, omega)
f2 = FRD(h2, omega)
np.testing.assert_array_almost_equal(
(f1 + f2).freqresp([0.1, 1.0, 10])[0],
(h1 + h2).freqresp([0.1, 1.0, 10])[0])
np.testing.assert_array_almost_equal(
(f1 + f2).freqresp([0.1, 1.0, 10])[1],
(h1 + h2).freqresp([0.1, 1.0, 10])[1])
np.testing.assert_array_almost_equal(
(f1 - f2).freqresp([0.1, 1.0, 10])[0],
(h1 - h2).freqresp([0.1, 1.0, 10])[0])
np.testing.assert_array_almost_equal(
(f1 - f2).freqresp([0.1, 1.0, 10])[1],
(h1 - h2).freqresp([0.1, 1.0, 10])[1])
# multiplication and division
np.testing.assert_array_almost_equal(
(f1 * f2).freqresp([0.1, 1.0, 10])[1],
(h1 * h2).freqresp([0.1, 1.0, 10])[1])
np.testing.assert_array_almost_equal(
(f1 / f2).freqresp([0.1, 1.0, 10])[1],
(h1 / h2).freqresp([0.1, 1.0, 10])[1])
# with default conversion from scalar
np.testing.assert_array_almost_equal(
(f1 * 1.5).freqresp([0.1, 1.0, 10])[1],
(h1 * 1.5).freqresp([0.1, 1.0, 10])[1])
np.testing.assert_array_almost_equal(
(f1 / 1.7).freqresp([0.1, 1.0, 10])[1],
(h1 / 1.7).freqresp([0.1, 1.0, 10])[1])
np.testing.assert_array_almost_equal(
(2.2 * f2).freqresp([0.1, 1.0, 10])[1],
(2.2 * h2).freqresp([0.1, 1.0, 10])[1])
np.testing.assert_array_almost_equal(
(1.3 / f2).freqresp([0.1, 1.0, 10])[1],
(1.3 / h2).freqresp([0.1, 1.0, 10])[1])
def testOperatorsTf(self):
# get two SISO transfer functions
h1 = TransferFunction([1], [1, 2, 2])
h2 = TransferFunction([1], [0.1, 1])
omega = np.logspace(-1, 2, 10)
f1 = FRD(h1, omega)
f2 = FRD(h2, omega)
f2 # reference to avoid pyflakes error
np.testing.assert_array_almost_equal(
(f1 + h2).freqresp([0.1, 1.0, 10])[0],
(h1 + h2).freqresp([0.1, 1.0, 10])[0])
np.testing.assert_array_almost_equal(
(f1 + h2).freqresp([0.1, 1.0, 10])[1],
(h1 + h2).freqresp([0.1, 1.0, 10])[1])
np.testing.assert_array_almost_equal(
(f1 - h2).freqresp([0.1, 1.0, 10])[0],
(h1 - h2).freqresp([0.1, 1.0, 10])[0])
np.testing.assert_array_almost_equal(
(f1 - h2).freqresp([0.1, 1.0, 10])[1],
(h1 - h2).freqresp([0.1, 1.0, 10])[1])
# multiplication and division
np.testing.assert_array_almost_equal(
(f1 * h2).freqresp([0.1, 1.0, 10])[1],
(h1 * h2).freqresp([0.1, 1.0, 10])[1])
np.testing.assert_array_almost_equal(
(f1 / h2).freqresp([0.1, 1.0, 10])[1],
(h1 / h2).freqresp([0.1, 1.0, 10])[1])
# the reverse does not work
def testbdalg(self):
# get two SISO transfer functions
h1 = TransferFunction([1], [1, 2, 2])
h2 = TransferFunction([1], [0.1, 1])
omega = np.logspace(-1, 2, 10)
f1 = FRD(h1, omega)
f2 = FRD(h2, omega)
np.testing.assert_array_almost_equal(
(bdalg.series(f1, f2)).freqresp([0.1, 1.0, 10])[0],
(bdalg.series(h1, h2)).freqresp([0.1, 1.0, 10])[0])
np.testing.assert_array_almost_equal(
(bdalg.parallel(f1, f2)).freqresp([0.1, 1.0, 10])[0],
(bdalg.parallel(h1, h2)).freqresp([0.1, 1.0, 10])[0])
np.testing.assert_array_almost_equal(
(bdalg.feedback(f1, f2)).freqresp([0.1, 1.0, 10])[0],
(bdalg.feedback(h1, h2)).freqresp([0.1, 1.0, 10])[0])
np.testing.assert_array_almost_equal(
(bdalg.negate(f1)).freqresp([0.1, 1.0, 10])[0],
(bdalg.negate(h1)).freqresp([0.1, 1.0, 10])[0])
# append() and connect() not implemented for FRD objects
# np.testing.assert_array_almost_equal(
# (bdalg.append(f1, f2)).freqresp([0.1, 1.0, 10])[0],
# (bdalg.append(h1, h2)).freqresp([0.1, 1.0, 10])[0])
#
# f3 = bdalg.append(f1, f2, f2)
# h3 = bdalg.append(h1, h2, h2)
# Q = np.mat([ [1, 2], [2, -1] ])
# np.testing.assert_array_almost_equal(
# (bdalg.connect(f3, Q, [2], [1])).freqresp([0.1, 1.0, 10])[0],
# (bdalg.connect(h3, Q, [2], [1])).freqresp([0.1, 1.0, 10])[0])
def testFeedback(self):
h1 = TransferFunction([1], [1, 2, 2])
omega = np.logspace(-1, 2, 10)
f1 = FRD(h1, omega)
np.testing.assert_array_almost_equal(
f1.feedback(1).freqresp([0.1, 1.0, 10])[0],
h1.feedback(1).freqresp([0.1, 1.0, 10])[0])
# Make sure default argument also works
np.testing.assert_array_almost_equal(
f1.feedback().freqresp([0.1, 1.0, 10])[0],
h1.feedback().freqresp([0.1, 1.0, 10])[0])
def testFeedback2(self):
h2 = StateSpace([[-1.0, 0], [0, -2.0]], [[0.4], [0.1]],
[[1.0, 0], [0, 1]], [[0.0], [0.0]])
# h2.feedback([[0.3, 0.2], [0.1, 0.1]])
def testAuto(self):
omega = np.logspace(-1, 2, 10)
f1 = _convertToFRD(1, omega)
f2 = _convertToFRD(np.matrix([[1, 0], [0.1, -1]]), omega)
f2 = _convertToFRD([[1, 0], [0.1, -1]], omega)
f1, f2 # reference to avoid pyflakes error
def testNyquist(self):
h1 = TransferFunction([1], [1, 2, 2])
omega = np.logspace(-1, 2, 40)
f1 = FRD(h1, omega, smooth=True)
freqplot.nyquist(f1, np.logspace(-1, 2, 100))
# plt.savefig('/dev/null', format='svg')
plt.figure(2)
freqplot.nyquist(f1, f1.omega)
# plt.savefig('/dev/null', format='svg')
@unittest.skipIf(not slycot_check(), "slycot not installed")
def testMIMO(self):
sys = StateSpace([[-0.5, 0.0], [0.0, -1.0]],
[[1.0, 0.0], [0.0, 1.0]],
[[1.0, 0.0], [0.0, 1.0]],
[[0.0, 0.0], [0.0, 0.0]])
omega = np.logspace(-1, 2, 10)
f1 = FRD(sys, omega)
np.testing.assert_array_almost_equal(
sys.freqresp([0.1, 1.0, 10])[0],
f1.freqresp([0.1, 1.0, 10])[0])
np.testing.assert_array_almost_equal(
sys.freqresp([0.1, 1.0, 10])[1],
f1.freqresp([0.1, 1.0, 10])[1])
@unittest.skipIf(not slycot_check(), "slycot not installed")
def testMIMOfb(self):
sys = StateSpace([[-0.5, 0.0], [0.0, -1.0]],
[[1.0, 0.0], [0.0, 1.0]],
[[1.0, 0.0], [0.0, 1.0]],
[[0.0, 0.0], [0.0, 0.0]])
omega = np.logspace(-1, 2, 10)
f1 = FRD(sys, omega).feedback([[0.1, 0.3], [0.0, 1.0]])
f2 = FRD(sys.feedback([[0.1, 0.3], [0.0, 1.0]]), omega)
np.testing.assert_array_almost_equal(
f1.freqresp([0.1, 1.0, 10])[0],
f2.freqresp([0.1, 1.0, 10])[0])
np.testing.assert_array_almost_equal(
f1.freqresp([0.1, 1.0, 10])[1],
f2.freqresp([0.1, 1.0, 10])[1])
@unittest.skipIf(not slycot_check(), "slycot not installed")
def testMIMOfb2(self):
sys = StateSpace(np.matrix('-2.0 0 0; 0 -1 1; 0 0 -3'),
np.matrix('1.0 0; 0 0; 0 1'),
np.eye(3), np.zeros((3, 2)))
omega = np.logspace(-1, 2, 10)
K = np.matrix('1 0.3 0; 0.1 0 0')
f1 = FRD(sys, omega).feedback(K)
f2 = FRD(sys.feedback(K), omega)
np.testing.assert_array_almost_equal(
f1.freqresp([0.1, 1.0, 10])[0],
f2.freqresp([0.1, 1.0, 10])[0])
np.testing.assert_array_almost_equal(
f1.freqresp([0.1, 1.0, 10])[1],
f2.freqresp([0.1, 1.0, 10])[1])
@unittest.skipIf(not slycot_check(), "slycot not installed")
def testMIMOMult(self):
sys = StateSpace([[-0.5, 0.0], [0.0, -1.0]],
[[1.0, 0.0], [0.0, 1.0]],
[[1.0, 0.0], [0.0, 1.0]],
[[0.0, 0.0], [0.0, 0.0]])
omega = np.logspace(-1, 2, 10)
f1 = FRD(sys, omega)
f2 = FRD(sys, omega)
np.testing.assert_array_almost_equal(
(f1*f2).freqresp([0.1, 1.0, 10])[0],
(sys*sys).freqresp([0.1, 1.0, 10])[0])
np.testing.assert_array_almost_equal(
(f1*f2).freqresp([0.1, 1.0, 10])[1],
(sys*sys).freqresp([0.1, 1.0, 10])[1])
@unittest.skipIf(not slycot_check(), "slycot not installed")
def testMIMOSmooth(self):
sys = StateSpace([[-0.5, 0.0], [0.0, -1.0]],
[[1.0, 0.0], [0.0, 1.0]],
[[1.0, 0.0], [0.0, 1.0], [1.0, 1.0]],
[[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]])
sys2 = np.matrix([[1, 0, 0], [0, 1, 0]]) * sys
omega = np.logspace(-1, 2, 10)
f1 = FRD(sys, omega, smooth=True)
f2 = FRD(sys2, omega, smooth=True)
np.testing.assert_array_almost_equal(
(f1*f2).freqresp([0.1, 1.0, 10])[0],
(sys*sys2).freqresp([0.1, 1.0, 10])[0])
np.testing.assert_array_almost_equal(
(f1*f2).freqresp([0.1, 1.0, 10])[1],
(sys*sys2).freqresp([0.1, 1.0, 10])[1])
np.testing.assert_array_almost_equal(
(f1*f2).freqresp([0.1, 1.0, 10])[2],
(sys*sys2).freqresp([0.1, 1.0, 10])[2])
def testAgainstOctave(self):
# with data from octave:
# sys = ss([-2 0 0; 0 -1 1; 0 0 -3],
# [1 0; 0 0; 0 1], eye(3), zeros(3,2))
# bfr = frd(bsys, [1])
sys = StateSpace(np.matrix('-2.0 0 0; 0 -1 1; 0 0 -3'),
np.matrix('1.0 0; 0 0; 0 1'),
np.eye(3), np.zeros((3, 2)))
omega = np.logspace(-1, 2, 10)
f1 = FRD(sys, omega)
np.testing.assert_array_almost_equal(
(f1.freqresp([1.0])[0] *
np.exp(1j*f1.freqresp([1.0])[1])).reshape(3, 2),
np.matrix('0.4-0.2j 0; 0 0.1-0.2j; 0 0.3-0.1j'))
def test_string_representation(self):
sys = FRD([1, 2, 3], [4, 5, 6])
print(sys) # Just print without checking
def test_frequency_mismatch(self):
# Overlapping but non-equal frequency ranges
sys1 = FRD([1, 2, 3], [4, 5, 6])
sys2 = FRD([2, 3, 4], [5, 6, 7])
self.assertRaises(NotImplementedError, FRD.__add__, sys1, sys2)
# One frequency range is a subset of another
sys1 = FRD([1, 2, 3], [4, 5, 6])
sys2 = FRD([2, 3], [4, 5])
self.assertRaises(NotImplementedError, FRD.__add__, sys1, sys2)
def test_size_mismatch(self):
sys1 = FRD(ct.rss(2, 2, 2), np.logspace(-1, 1, 10))
# Different number of inputs
sys2 = FRD(ct.rss(3, 1, 2), np.logspace(-1, 1, 10))
self.assertRaises(ValueError, FRD.__add__, sys1, sys2)
# Different number of outputs
sys2 = FRD(ct.rss(3, 2, 1), np.logspace(-1, 1, 10))
self.assertRaises(ValueError, FRD.__add__, sys1, sys2)
# Inputs and outputs don't match
self.assertRaises(ValueError, FRD.__mul__, sys2, sys1)
# Feedback mismatch
self.assertRaises(ValueError, FRD.feedback, sys2, sys1)
def test_operator_conversion(self):
sys_tf = ct.tf([1], [1, 2, 1])
frd_tf = FRD(sys_tf, np.logspace(-1, 1, 10))
frd_2 = FRD(2 * np.ones(10), np.logspace(-1, 1, 10))
# Make sure that we can add, multiply, and feedback constants
sys_add = frd_tf + 2
chk_add = frd_tf + frd_2
np.testing.assert_array_almost_equal(sys_add.omega, chk_add.omega)
np.testing.assert_array_almost_equal(sys_add.fresp, chk_add.fresp)
sys_radd = 2 + frd_tf
chk_radd = frd_2 + frd_tf
np.testing.assert_array_almost_equal(sys_radd.omega, chk_radd.omega)
np.testing.assert_array_almost_equal(sys_radd.fresp, chk_radd.fresp)
sys_sub = frd_tf - 2
chk_sub = frd_tf - frd_2
np.testing.assert_array_almost_equal(sys_sub.omega, chk_sub.omega)
np.testing.assert_array_almost_equal(sys_sub.fresp, chk_sub.fresp)
sys_rsub = 2 - frd_tf
chk_rsub = frd_2 - frd_tf
np.testing.assert_array_almost_equal(sys_rsub.omega, chk_rsub.omega)
np.testing.assert_array_almost_equal(sys_rsub.fresp, chk_rsub.fresp)
sys_mul = frd_tf * 2
chk_mul = frd_tf * frd_2
np.testing.assert_array_almost_equal(sys_mul.omega, chk_mul.omega)
np.testing.assert_array_almost_equal(sys_mul.fresp, chk_mul.fresp)
sys_rmul = 2 * frd_tf
chk_rmul = frd_2 * frd_tf
np.testing.assert_array_almost_equal(sys_rmul.omega, chk_rmul.omega)
np.testing.assert_array_almost_equal(sys_rmul.fresp, chk_rmul.fresp)
sys_rdiv = 2 / frd_tf
chk_rdiv = frd_2 / frd_tf
np.testing.assert_array_almost_equal(sys_rdiv.omega, chk_rdiv.omega)
np.testing.assert_array_almost_equal(sys_rdiv.fresp, chk_rdiv.fresp)
sys_pow = frd_tf**2
chk_pow = FRD(sys_tf**2, np.logspace(-1, 1, 10))
np.testing.assert_array_almost_equal(sys_pow.omega, chk_pow.omega)
np.testing.assert_array_almost_equal(sys_pow.fresp, chk_pow.fresp)
sys_pow = frd_tf**-2
chk_pow = FRD(sys_tf**-2, np.logspace(-1, 1, 10))
np.testing.assert_array_almost_equal(sys_pow.omega, chk_pow.omega)
np.testing.assert_array_almost_equal(sys_pow.fresp, chk_pow.fresp)
# Assertion error if we try to raise to a non-integer power
self.assertRaises(ValueError, FRD.__pow__, frd_tf, 0.5)
# Selected testing on transfer function conversion
sys_add = frd_2 + sys_tf
chk_add = frd_2 + frd_tf
np.testing.assert_array_almost_equal(sys_add.omega, chk_add.omega)
np.testing.assert_array_almost_equal(sys_add.fresp, chk_add.fresp)
# Input/output mismatch size mismatch in rmul
sys1 = FRD(ct.rss(2, 2, 2), np.logspace(-1, 1, 10))
self.assertRaises(ValueError, FRD.__rmul__, frd_2, sys1)
# Make sure conversion of something random generates exception
self.assertRaises(TypeError, FRD.__add__, frd_tf, 'string')
def test_eval(self):
sys_tf = ct.tf([1], [1, 2, 1])
frd_tf = FRD(sys_tf, np.logspace(-1, 1, 3))
np.testing.assert_almost_equal(sys_tf.evalfr(1), frd_tf.eval(1))
# Should get an error if we evaluate at an unknown frequency
self.assertRaises(ValueError, frd_tf.eval, 2)
# This test only works in Python 3 due to a conflict with the same
# warning type in other test modules (frd_test.py). See
# https://bugs.python.org/issue4180 for more details
@unittest.skipIf(pysys.version_info < (3, 0), "test requires Python 3+")
def test_evalfr_deprecated(self):
sys_tf = ct.tf([1], [1, 2, 1])
frd_tf = FRD(sys_tf, np.logspace(-1, 1, 3))
# Deprecated version of the call (should generate warning)
import warnings
with warnings.catch_warnings():
# Make warnings generate an exception
warnings.simplefilter('error')
# Make sure that we get a pending deprecation warning
self.assertRaises(PendingDeprecationWarning, frd_tf.evalfr, 1.)
# FRD.evalfr() is being deprecated
import warnings
with warnings.catch_warnings():
# Make warnings generate an exception
warnings.simplefilter('error')
# Make sure that we get a pending deprecation warning
self.assertRaises(PendingDeprecationWarning, frd_tf.evalfr, 1.)
def test_repr_str(self):
# repr printing
array = np.array
sys0 = FrequencyResponseData([1.0, 0.9+0.1j, 0.1+2j, 0.05+3j],
[0.1, 1.0, 10.0, 100.0])
sys1 = FrequencyResponseData(sys0.fresp, sys0.omega, smooth=True)
ref0 = "FrequencyResponseData(" \
"array([[[1. +0.j , 0.9 +0.1j, 0.1 +2.j , 0.05+3.j ]]])," \
" array([ 0.1, 1. , 10. , 100. ]))"
ref1 = ref0[:-1] + ", smooth=True)"
sysm = FrequencyResponseData(
np.matmul(array([[1],[2]]), sys0.fresp), sys0.omega)
self.assertEqual(repr(sys0), ref0)
self.assertEqual(repr(sys1), ref1)
sys0r = eval(repr(sys0))
np.testing.assert_array_almost_equal(sys0r.fresp, sys0.fresp)
np.testing.assert_array_almost_equal(sys0r.omega, sys0.omega)
sys1r = eval(repr(sys1))
np.testing.assert_array_almost_equal(sys1r.fresp, sys1.fresp)
np.testing.assert_array_almost_equal(sys1r.omega, sys1.omega)
assert(sys1.ifunc is not None)
refs = """Frequency response data
Freq [rad/s] Response
------------ ---------------------
0.100 1 +0j
1.000 0.9 +0.1j
10.000 0.1 +2j
100.000 0.05 +3j"""
self.assertEqual(str(sys0), refs)
self.assertEqual(str(sys1), refs)
# print multi-input system
refm = """Frequency response data
Input 1 to output 1:
Freq [rad/s] Response
------------ ---------------------
0.100 1 +0j
1.000 0.9 +0.1j
10.000 0.1 +2j
100.000 0.05 +3j
Input 2 to output 1:
Freq [rad/s] Response
------------ ---------------------
0.100 2 +0j
1.000 1.8 +0.2j
10.000 0.2 +4j
100.000 0.1 +6j"""
self.assertEqual(str(sysm), refm)
if __name__ == "__main__":
unittest.main()