Skip to content

Commit 85a480c

Browse files
committed
finalize MIMO implementation: remove duplicate code, unit test updates
1 parent e838b4b commit 85a480c

File tree

4 files changed

+186
-93
lines changed

4 files changed

+186
-93
lines changed

control/iosys.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1428,8 +1428,9 @@ def input_output_response(sys, T, U=0., X0=0, params={}, method='RK45',
14281428
for i in range(len(T)):
14291429
u = U[i] if len(U.shape) == 1 else U[:, i]
14301430
y[:, i] = sys._out(T[i], [], u)
1431-
return _process_time_response(sys, T, y, [], transpose=transpose,
1432-
return_x=return_x, squeeze=squeeze)
1431+
return _process_time_response(
1432+
sys, T, y, np.array((0, 0, np.asarray(T).size)),
1433+
transpose=transpose, return_x=return_x, squeeze=squeeze)
14331434

14341435
# create X0 if not given, test if X0 has correct shape
14351436
X0 = _check_convert_array(X0, [(nstates,), (nstates, 1)],

control/tests/matlab2_test.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,25 +121,25 @@ def test_step(self, SISO_mats, MIMO_mats, mplcleanup):
121121
#print("gain:", dcgain(sys))
122122

123123
subplot2grid(plot_shape, (0, 0))
124-
t, y = step(sys)
124+
y, t = step(sys)
125125
plot(t, y)
126126

127127
subplot2grid(plot_shape, (0, 1))
128128
T = linspace(0, 2, 100)
129129
X0 = array([1, 1])
130-
t, y = step(sys, T, X0)
130+
y, t = step(sys, T, X0)
131131
plot(t, y)
132132

133133
# Test output of state vector
134-
t, y, x = step(sys, return_x=True)
134+
y, t, x = step(sys, return_x=True)
135135

136136
#Test MIMO system
137137
A, B, C, D = MIMO_mats
138138
sys = ss(A, B, C, D)
139139

140140
subplot2grid(plot_shape, (0, 2))
141-
t, y = step(sys)
142-
plot(t, y)
141+
y, t = step(sys)
142+
plot(t, y[:, 0, 0])
143143

144144
def test_impulse(self, SISO_mats, mplcleanup):
145145
A, B, C, D = SISO_mats
@@ -168,8 +168,8 @@ def test_impulse_mimo(self, MIMO_mats, mplcleanup):
168168
#Test MIMO system
169169
A, B, C, D = MIMO_mats
170170
sys = ss(A, B, C, D)
171-
t, y = impulse(sys)
172-
plot(t, y, label='MIMO System')
171+
y, t = impulse(sys)
172+
plot(t, y[:, :, 0], label='MIMO System')
173173

174174
legend(loc='best')
175175
#show()

control/tests/timeresp_test.py

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ def test_impulse_response_mimo(self, mimo_ss2):
347347
yref_notrim = np.zeros((2, len(t)))
348348
yref_notrim[:1, :] = yref
349349
_t, yy = impulse_response(sys, T=t, input=0)
350-
np.testing.assert_array_almost_equal(yy, yref_notrim, decimal=4)
350+
np.testing.assert_array_almost_equal(yy[:,0,:], yref_notrim, decimal=4)
351351

352352
@pytest.mark.skipif(StrictVersion(sp.__version__) < "1.3",
353353
reason="requires SciPy 1.3 or greater")
@@ -770,7 +770,7 @@ def test_squeeze(self, fcn, nstate, nout, ninp, squeeze, shape1, shape2):
770770
# Check the states as well
771771
_, yvec, xvec = ct.impulse_response(
772772
sys, tvec, squeeze=squeeze, return_x=True)
773-
if sys.issiso() and squeeze is not False:
773+
if sys.issiso():
774774
assert xvec.shape == (sys.states, 8)
775775
else:
776776
assert xvec.shape == (sys.states, sys.inputs, 8)
@@ -783,7 +783,7 @@ def test_squeeze(self, fcn, nstate, nout, ninp, squeeze, shape1, shape2):
783783
# Check the states as well
784784
_, yvec, xvec = ct.step_response(
785785
sys, tvec, squeeze=squeeze, return_x=True)
786-
if sys.issiso() and squeeze is not False:
786+
if sys.issiso():
787787
assert xvec.shape == (sys.states, 8)
788788
else:
789789
assert xvec.shape == (sys.states, sys.inputs, 8)
@@ -894,3 +894,37 @@ def test_squeeze_0_8_4(self, nstate, nout, ninp, squeeze, shape):
894894

895895
_, yvec = ct.initial_response(sys, tvec, 1, squeeze=squeeze)
896896
assert yvec.shape == shape
897+
898+
@pytest.mark.parametrize(
899+
"nstate, nout, ninp, squeeze, ysh_in, ysh_no, xsh_in", [
900+
[4, 1, 1, None, (8,), (8,), (8, 4)],
901+
[4, 1, 1, True, (8,), (8,), (8, 4)],
902+
[4, 1, 1, False, (8, 1, 1), (8, 1), (8, 4)],
903+
[4, 2, 1, None, (8, 2, 1), (8, 2), (8, 4, 1)],
904+
[4, 2, 1, True, (8, 2), (8, 2), (8, 4, 1)],
905+
[4, 2, 1, False, (8, 2, 1), (8, 2), (8, 4, 1)],
906+
[4, 1, 2, None, (8, 1, 2), (8, 1), (8, 4, 2)],
907+
[4, 1, 2, True, (8, 2), (8,), (8, 4, 2)],
908+
[4, 1, 2, False, (8, 1, 2), (8, 1), (8, 4, 2)],
909+
[4, 2, 2, None, (8, 2, 2), (8, 2), (8, 4, 2)],
910+
[4, 2, 2, True, (8, 2, 2), (8, 2), (8, 4, 2)],
911+
[4, 2, 2, False, (8, 2, 2), (8, 2), (8, 4, 2)],
912+
])
913+
def test_response_transpose(
914+
self, nstate, nout, ninp, squeeze, ysh_in, ysh_no, xsh_in):
915+
sys = ct.rss(nstate, nout, ninp)
916+
T = np.linspace(0, 1, 8)
917+
918+
# Step response - input indexed
919+
t, y, x = ct.step_response(
920+
sys, T, transpose=True, return_x=True, squeeze=squeeze)
921+
assert t.shape == (T.size, )
922+
assert y.shape == ysh_in
923+
assert x.shape == xsh_in
924+
925+
# Initial response - no input indexing
926+
t, y, x = ct.initial_response(
927+
sys, T, 1, transpose=True, return_x=True, squeeze=squeeze)
928+
assert t.shape == (T.size, )
929+
assert y.shape == ysh_no
930+
assert x.shape == (T.size, sys.states)

0 commit comments

Comments
 (0)