BugFix: DC gain for discrete-time systems · python-control/python-control@310f580
@@ -228,7 +228,8 @@ def testArrayAccessSS(self):
228228229229assert sys1.dt == sys1_11.dt
230230231-def test_dcgain(self):
231+def test_dcgain_cont(self):
232+"""Test DC gain for continuous-time state-space systems"""
232233sys = StateSpace(-2.,6.,5.,0)
233234np.testing.assert_equal(sys.dcgain(), 15.)
234235@@ -239,6 +240,43 @@ def test_dcgain(self):
239240sys3 = StateSpace(0., 1., 1., 0.)
240241np.testing.assert_equal(sys3.dcgain(), np.nan)
241242243+def test_dcgain_discr(self):
244+"""Test DC gain for discrete-time state-space systems"""
245+# static gain
246+sys = StateSpace([], [], [], 2, True)
247+np.testing.assert_equal(sys.dcgain(), 2)
248+249+# averaging filter
250+sys = StateSpace(0.5, 0.5, 1, 0, True)
251+np.testing.assert_almost_equal(sys.dcgain(), 1)
252+253+# differencer
254+sys = StateSpace(0, 1, -1, 1, True)
255+np.testing.assert_equal(sys.dcgain(), 0)
256+257+# summer
258+sys = StateSpace(1, 1, 1, 0, True)
259+np.testing.assert_equal(sys.dcgain(), np.nan)
260+261+def test_dcgain_integrator(self):
262+"""DC gain when eigenvalue at DC returns appropriately sized array of nan"""
263+# the SISO case is also tested in test_dc_gain_{cont,discr}
264+import itertools
265+# iterate over input and output sizes, and continuous (dt=None) and discrete (dt=True) time
266+for inputs,outputs,dt in itertools.product(range(1,6),range(1,6),[None,True]):
267+states = max(inputs,outputs)
268+269+# a matrix that is singular at DC, and has no "useless" states as in _remove_useless_states
270+a = np.triu(np.tile(2,(states,states)))
271+# eigenvalues all +2, except for ...
272+a[0,0] = 0 if dt is None else 1
273+b = np.eye(max(inputs,states))[:states,:inputs]
274+c = np.eye(max(outputs,states))[:outputs,:states]
275+d = np.zeros((outputs,inputs))
276+sys = StateSpace(a,b,c,d,dt)
277+dc = np.squeeze(np.tile(np.nan,(outputs,inputs)))
278+np.testing.assert_array_equal(dc, sys.dcgain())
279+242280243281def test_scalarStaticGain(self):
244282"""Regression: can we create a scalar static gain?"""