Skip to content

fix control.matlab.lsim bug for discrete time system #765

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions control/tests/matlab_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ def testStep(self, siso):
np.testing.assert_array_almost_equal(yout, youttrue, decimal=4)
np.testing.assert_array_almost_equal(tout, t)

@slycotonly
def testStep_mimo(self, mimo):
"""Test step for MIMO system"""
sys = mimo.ss1
Expand Down Expand Up @@ -267,7 +266,6 @@ def testImpulse(self, siso):
np.testing.assert_array_almost_equal(yout, youttrue, decimal=4)
np.testing.assert_array_almost_equal(tout, t)

@slycotonly
def testImpulse_mimo(self, mimo):
"""Test impulse() for MIMO system"""
t = np.linspace(0, 1, 10)
Expand Down Expand Up @@ -296,7 +294,6 @@ def testInitial(self, siso):
np.testing.assert_array_almost_equal(yout, youttrue, decimal=4)
np.testing.assert_array_almost_equal(tout, t)

@slycotonly
def testInitial_mimo(self, mimo):
"""Test initial() for MIMO system"""
t = np.linspace(0, 1, 10)
Expand Down Expand Up @@ -333,7 +330,6 @@ def testLsim(self, siso):
yout, _t, _xout = lsim(siso.ss1, u, t, x0)
np.testing.assert_array_almost_equal(yout, youttrue, decimal=4)

@slycotonly
def testLsim_mimo(self, mimo):
"""Test lsim() for MIMO system.

Expand All @@ -352,6 +348,25 @@ def testLsim_mimo(self, mimo):
yout, _t, _xout = lsim(mimo.ss1, u, t, x0)
np.testing.assert_array_almost_equal(yout, youttrue, decimal=4)

def test_lsim_mimo_dtime(self):
# https://github.com/python-control/python-control/issues/764
time = np.linspace(0.0, 511.0e-6, 512)
DAC = np.sin(time)
ADC = np.cos(time)

input_Kalman = np.transpose(
np.concatenate(([[DAC]], [[ADC]]), axis=1)[0])
Af = [[0.45768416, -0.42025511], [-0.43354791, 0.51961178]]
Bf = [[2.84368641, 52.05922305], [-1.47286557, -19.94861943]]
Cf = [[1.0, 0.0], [0.0, 1.0]]
Df = [[0.0, 0.0], [0.0, 0.0]]

ss_Kalman = ss(Af, Bf, Cf, Df, 1.0e-6)
y_est, t, x_est = lsim(ss_Kalman, input_Kalman, time)
assert y_est.shape == (time.size, ss_Kalman.ninputs)
assert t.shape == (time.size, )
assert x_est.shape == (time.size, ss_Kalman.nstates)

def testMargin(self, siso):
"""Test margin()"""
#! TODO: check results to make sure they are OK
Expand Down Expand Up @@ -582,7 +597,6 @@ def testSISOssdata(self, siso):
for i in range(len(ssdata_1)):
np.testing.assert_array_almost_equal(ssdata_1[i], ssdata_2[i])

@slycotonly
def testMIMOssdata(self, mimo):
"""Test ssdata() MIMO"""
m = (mimo.ss1.A, mimo.ss1.B, mimo.ss1.C, mimo.ss1.D)
Expand Down
4 changes: 2 additions & 2 deletions control/tests/timeresp_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,10 +698,10 @@ def test_forced_response_invalid_d(self, tsystem):
"""Test invalid parameters dtime with sys.dt > 0."""
with pytest.raises(ValueError, match="can't both be zero"):
forced_response(tsystem.sys)
with pytest.raises(ValueError, match="must have same elements"):
with pytest.raises(ValueError, match="Parameter ``U``: Wrong shape"):
forced_response(tsystem.sys,
T=tsystem.t, U=np.random.randn(1, 12))
with pytest.raises(ValueError, match="must have same elements"):
with pytest.raises(ValueError, match="Parameter ``U``: Wrong shape"):
forced_response(tsystem.sys,
T=tsystem.t, U=np.random.randn(12))
with pytest.raises(ValueError, match="must match sampling time"):
Expand Down
5 changes: 0 additions & 5 deletions control/timeresp.py
Original file line number Diff line number Diff line change
Expand Up @@ -972,11 +972,6 @@ def forced_response(sys, T=None, U=0., X0=0., transpose=False,
dt = 1. if sys.dt in [True, None] else sys.dt
T = np.array(range(n_steps)) * dt
else:
# Make sure the input vector and time vector have same length
if (U.ndim == 1 and U.shape[0] != T.shape[0]) or \
(U.ndim > 1 and U.shape[1] != T.shape[0]):
raise ValueError('Parameter ``T`` must have same elements as'
' the number of columns in input array ``U``')
if U.ndim == 0:
U = np.full((n_inputs, T.shape[0]), U)
else:
Expand Down