|
5 | 5 |
|
6 | 6 | import numpy as np |
7 | 7 | import pytest |
| 8 | +import cmath |
8 | 9 |
|
9 | | -from control import (StateSpace, TransferFunction, bode, common_timebase, |
10 | | - feedback, forced_response, impulse_response, |
11 | | - isctime, isdtime, rss, c2d, sample_system, step_response, |
12 | | - timebase) |
| 10 | +import control as ct |
| 11 | +from control import StateSpace, TransferFunction, bode, common_timebase, \ |
| 12 | + feedback, forced_response, impulse_response, isctime, isdtime, rss, \ |
| 13 | + c2d, sample_system, step_response, timebase |
13 | 14 |
|
14 | 15 |
|
15 | 16 | class TestDiscrete: |
@@ -526,3 +527,33 @@ def test_signal_names(self, tsys): |
526 | 527 | assert sysd_newnames.find_input('u') is None |
527 | 528 | assert sysd_newnames.find_output('y') == 0 |
528 | 529 | assert sysd_newnames.find_output('x') is None |
| 530 | + |
| 531 | + |
| 532 | +@pytest.mark.parametrize("num, den", [ |
| 533 | + ([1], [1, 1]), |
| 534 | + ([1, 2], [1, 3]), |
| 535 | + ([1, 2], [3, 4, 5]) |
| 536 | +]) |
| 537 | +@pytest.mark.parametrize("dt", [True, 0.1, 2]) |
| 538 | +@pytest.mark.parametrize("method", ['zoh', 'bilinear', 'matched']) |
| 539 | +def test_c2d_matched(num, den, dt, method): |
| 540 | + sys_ct = ct.tf(num, den) |
| 541 | + sys_dt = ct.sample_system(sys_ct, dt, method=method) |
| 542 | + assert sys_dt.dt == dt # make sure sampling time is OK |
| 543 | + assert cmath.isclose(sys_ct(0), sys_dt(1)) # check zero frequency gain |
| 544 | + assert cmath.isclose( |
| 545 | + sys_ct.dcgain(), sys_dt.dcgain()) # another way to check |
| 546 | + |
| 547 | + if method in ['zoh', 'matched']: |
| 548 | + # Make sure that poles were properly matched |
| 549 | + zpoles = sys_dt.poles() |
| 550 | + for cpole in sys_ct.poles(): |
| 551 | + zpole = zpoles[(np.abs(zpoles - cmath.exp(cpole * dt))).argmin()] |
| 552 | + assert cmath.isclose(cmath.exp(cpole * dt), zpole) |
| 553 | + |
| 554 | + if method in ['matched']: |
| 555 | + # Make sure that zeros were properly matched |
| 556 | + zzeros = sys_dt.zeros() |
| 557 | + for czero in sys_ct.zeros(): |
| 558 | + zzero = zzeros[(np.abs(zzeros - cmath.exp(czero * dt))).argmin()] |
| 559 | + assert cmath.isclose(cmath.exp(czero * dt), zzero) |
0 commit comments