From 057c5ca322308f4e0918d4767a58955edb9359a9 Mon Sep 17 00:00:00 2001 From: Richard Murray Date: Sat, 24 Feb 2018 11:37:16 -0800 Subject: [PATCH 1/3] Fix namespace and logic errrors in statesp.freqresp + unit tests As pointed out in PR #192, there were some namespace errors in the statesp module related to checking whether the frequency range as valid for discrete time systems. I added a unit test that covered this code (and exposed the error) and fixed up the namespace problems (as in PR #192). There was also a logic error in the way that frequencies were checked and the warning message referred to the wrong function. --- control/statesp.py | 6 ++-- control/tests/freqresp_test.py | 63 +++++++++++++++++++++++++++++++++- 2 files changed, 64 insertions(+), 5 deletions(-) diff --git a/control/statesp.py b/control/statesp.py index bff14d241..eb0f23f54 100644 --- a/control/statesp.py +++ b/control/statesp.py @@ -460,10 +460,8 @@ def freqresp(self, omega): if isdtime(self, strict=True): dt = timebase(self) cmplx_freqs = exp(1.j * omega * dt) - if ((omega * dt).any() > pi): - warn_message = ("evalfr: frequency evaluation" - " above Nyquist frequency") - warnings.warn(warn_message) + if (max(omega) * dt > math.pi): + warn("freqresp: frequency evaluation above Nyquist frequency") else: cmplx_freqs = omega * 1.j diff --git a/control/tests/freqresp_test.py b/control/tests/freqresp_test.py index 9e8dd44ce..ef90166a7 100644 --- a/control/tests/freqresp_test.py +++ b/control/tests/freqresp_test.py @@ -10,7 +10,8 @@ import numpy as np import control as ctrl from control.statesp import StateSpace -from control.matlab import ss, tf, bode +from control.xferfcn import TransferFunction +from control.matlab import ss, tf, bode, rss from control.exception import slycot_check import matplotlib.pyplot as plt @@ -107,6 +108,66 @@ def test_mimo(self): #plt.figure(4) #bode(sysMIMO,self.omega) + def test_discrete(self): + # Test discrete time frequency response + + # SISO state space systems with either fixed or unspecified sampling times + sys = rss(3, 1, 1) + siso_ss1d = StateSpace(sys.A, sys.B, sys.C, sys.D, 0.1) + siso_ss2d = StateSpace(sys.A, sys.B, sys.C, sys.D, True) + + # MIMO state space systems with either fixed or unspecified sampling times + A = [[-3., 4., 2.], [-1., -3., 0.], [2., 5., 3.]] + B = [[1., 4.], [-3., -3.], [-2., 1.]] + C = [[4., 2., -3.], [1., 4., 3.]] + D = [[-2., 4.], [0., 1.]] + mimo_ss1d = StateSpace(A, B, C, D, 0.1) + mimo_ss2d = StateSpace(A, B, C, D, True) + + # SISO transfer functions + siso_tf1d = TransferFunction([1, 1], [1, 2, 1], 0.1) + siso_tf2d = TransferFunction([1, 1], [1, 2, 1], True) + + # Go through each system and call the code, checking return types + for sys in (siso_ss1d, siso_ss2d, mimo_ss1d, mimo_ss2d, + siso_tf1d, siso_tf2d): + # Set frequency range to just below Nyquist freq (for Bode) + omega_ok = np.linspace(10e-4,0.99,100) * np.pi/sys.dt + + # Test frequency response + ret = sys.freqresp(omega_ok) + + # Check for warning if frequency is out of range + import warnings + with warnings.catch_warnings(record=True) as w: + omega_bad = np.linspace(10e-4,1.1,10) * np.pi/sys.dt + ret = sys.freqresp(omega_bad) + print("len(w) =", len(w)) + assert len(w) == 1 + assert "above" in str(w[-1].message) + assert "Nyquist" in str(w[-1].message) + + # Test bode plots (currently only implemented for SISO) + if (sys.inputs == 1 and sys.outputs == 1): + # Generic call (frequency range calculated automatically) + ret_ss = bode(sys) + + # Convert to transfer function and test bode again + systf = tf(sys); + ret_tf = bode(systf) + + # Make sure we can pass a frequency range + bode(sys, omega_ok) + + else: + # Calling bode should generate a not implemented error + try: + ret_ss = bode(sys) + raise RuntimeError("MIMO bode seems to be implemented?") + except NotImplementedError: + # This is where we should end up (so do nothing) + continue + def suite(): return unittest.TestLoader().loadTestsFromTestCase(TestTimeresp) From 55f4bb6056b900bf61b619b5566b39ab96cdba9e Mon Sep 17 00:00:00 2001 From: Richard Murray Date: Sat, 24 Feb 2018 16:34:49 -0800 Subject: [PATCH 2/3] Fix warning check for python 2.7 Python 2.7 represses repeated warnings in a way that causes the unit test to fail => make sure that warnings are always generated --- control/tests/freqresp_test.py | 1 + 1 file changed, 1 insertion(+) diff --git a/control/tests/freqresp_test.py b/control/tests/freqresp_test.py index ef90166a7..311ce31c5 100644 --- a/control/tests/freqresp_test.py +++ b/control/tests/freqresp_test.py @@ -139,6 +139,7 @@ def test_discrete(self): # Check for warning if frequency is out of range import warnings + warnings.simplefilter('always', UserWarning) # don't supress with warnings.catch_warnings(record=True) as w: omega_bad = np.linspace(10e-4,1.1,10) * np.pi/sys.dt ret = sys.freqresp(omega_bad) From f2af185bdff0209c3bbed322d58c5d7aff150ee4 Mon Sep 17 00:00:00 2001 From: Richard Murray Date: Mon, 2 Jul 2018 16:19:07 -0700 Subject: [PATCH 3/3] updated freqresp unit tests + use abs(omega) in statespace.freqresp --- control/statesp.py | 2 +- control/tests/freqresp_test.py | 21 ++++++++------------- 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/control/statesp.py b/control/statesp.py index eb0f23f54..3d1bab672 100644 --- a/control/statesp.py +++ b/control/statesp.py @@ -460,7 +460,7 @@ def freqresp(self, omega): if isdtime(self, strict=True): dt = timebase(self) cmplx_freqs = exp(1.j * omega * dt) - if (max(omega) * dt > math.pi): + if max(abs(omega)) * dt > math.pi: warn("freqresp: frequency evaluation above Nyquist frequency") else: cmplx_freqs = omega * 1.j diff --git a/control/tests/freqresp_test.py b/control/tests/freqresp_test.py index 311ce31c5..a6fc49a7f 100644 --- a/control/tests/freqresp_test.py +++ b/control/tests/freqresp_test.py @@ -46,7 +46,7 @@ def test_superimpose(self): ctrl.bode_plot(ctrl.tf([5], [1, 1])) # Check to make sure there are two axes and that each axes has two lines - assert len(plt.gcf().axes) == 2 + self.assertEqual(len(plt.gcf().axes), 2) for ax in plt.gcf().axes: # Make sure there are 2 lines in each subplot assert len(ax.get_lines()) == 2 @@ -56,7 +56,7 @@ def test_superimpose(self): ctrl.bode_plot([ctrl.tf([1], [1,2,1]), ctrl.tf([5], [1, 1])]) # Check to make sure there are two axes and that each axes has two lines - assert len(plt.gcf().axes) == 2 + self.assertEqual(len(plt.gcf().axes), 2) for ax in plt.gcf().axes: # Make sure there are 2 lines in each subplot assert len(ax.get_lines()) == 2 @@ -68,7 +68,7 @@ def test_superimpose(self): ctrl.bode_plot(ctrl.tf([5], [1, 1])) # Check to make sure there are two axes and that each axes has one line - assert len(plt.gcf().axes) == 2 + self.assertEqual(len(plt.gcf().axes), 2) for ax in plt.gcf().axes: # Make sure there is only 1 line in the subplot assert len(ax.get_lines()) == 1 @@ -78,7 +78,7 @@ def test_superimpose(self): if ax.get_label() == 'control-bode-magnitude': break ax.semilogx([1e-2, 1e1], 20 * np.log10([1, 1]), 'k-') - assert len(ax.get_lines()) == 2 + self.assertEqual(len(ax.get_lines()), 2) def test_doubleint(self): # 30 May 2016, RMM: added to replicate typecast bug in freqresp.py @@ -144,9 +144,9 @@ def test_discrete(self): omega_bad = np.linspace(10e-4,1.1,10) * np.pi/sys.dt ret = sys.freqresp(omega_bad) print("len(w) =", len(w)) - assert len(w) == 1 - assert "above" in str(w[-1].message) - assert "Nyquist" in str(w[-1].message) + self.assertEqual(len(w), 1) + self.assertIn("above", str(w[-1].message)) + self.assertIn("Nyquist", str(w[-1].message)) # Test bode plots (currently only implemented for SISO) if (sys.inputs == 1 and sys.outputs == 1): @@ -162,12 +162,7 @@ def test_discrete(self): else: # Calling bode should generate a not implemented error - try: - ret_ss = bode(sys) - raise RuntimeError("MIMO bode seems to be implemented?") - except NotImplementedError: - # This is where we should end up (so do nothing) - continue + self.assertRaises(NotImplementedError, bode, (sys,)) def suite(): return unittest.TestLoader().loadTestsFromTestCase(TestTimeresp)