test_dltisys.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. # Author: Jeffrey Armstrong <jeff@approximatrix.com>
  2. # April 4, 2011
  3. import numpy as np
  4. from numpy.testing import (assert_equal,
  5. assert_array_almost_equal, assert_array_equal,
  6. assert_allclose, assert_, assert_almost_equal,
  7. suppress_warnings)
  8. from pytest import raises as assert_raises
  9. from scipy.signal import (dlsim, dstep, dimpulse, tf2zpk, lti, dlti,
  10. StateSpace, TransferFunction, ZerosPolesGain,
  11. dfreqresp, dbode, BadCoefficients)
  12. class TestDLTI:
  13. def test_dlsim(self):
  14. a = np.asarray([[0.9, 0.1], [-0.2, 0.9]])
  15. b = np.asarray([[0.4, 0.1, -0.1], [0.0, 0.05, 0.0]])
  16. c = np.asarray([[0.1, 0.3]])
  17. d = np.asarray([[0.0, -0.1, 0.0]])
  18. dt = 0.5
  19. # Create an input matrix with inputs down the columns (3 cols) and its
  20. # respective time input vector
  21. u = np.hstack((np.linspace(0, 4.0, num=5)[:, np.newaxis],
  22. np.full((5, 1), 0.01),
  23. np.full((5, 1), -0.002)))
  24. t_in = np.linspace(0, 2.0, num=5)
  25. # Define the known result
  26. yout_truth = np.array([[-0.001,
  27. -0.00073,
  28. 0.039446,
  29. 0.0915387,
  30. 0.13195948]]).T
  31. xout_truth = np.asarray([[0, 0],
  32. [0.0012, 0.0005],
  33. [0.40233, 0.00071],
  34. [1.163368, -0.079327],
  35. [2.2402985, -0.3035679]])
  36. tout, yout, xout = dlsim((a, b, c, d, dt), u, t_in)
  37. assert_array_almost_equal(yout_truth, yout)
  38. assert_array_almost_equal(xout_truth, xout)
  39. assert_array_almost_equal(t_in, tout)
  40. # Make sure input with single-dimension doesn't raise error
  41. dlsim((1, 2, 3), 4)
  42. # Interpolated control - inputs should have different time steps
  43. # than the discrete model uses internally
  44. u_sparse = u[[0, 4], :]
  45. t_sparse = np.asarray([0.0, 2.0])
  46. tout, yout, xout = dlsim((a, b, c, d, dt), u_sparse, t_sparse)
  47. assert_array_almost_equal(yout_truth, yout)
  48. assert_array_almost_equal(xout_truth, xout)
  49. assert_equal(len(tout), yout.shape[0])
  50. # Transfer functions (assume dt = 0.5)
  51. num = np.asarray([1.0, -0.1])
  52. den = np.asarray([0.3, 1.0, 0.2])
  53. yout_truth = np.array([[0.0,
  54. 0.0,
  55. 3.33333333333333,
  56. -4.77777777777778,
  57. 23.0370370370370]]).T
  58. # Assume use of the first column of the control input built earlier
  59. tout, yout = dlsim((num, den, 0.5), u[:, 0], t_in)
  60. assert_array_almost_equal(yout, yout_truth)
  61. assert_array_almost_equal(t_in, tout)
  62. # Retest the same with a 1-D input vector
  63. uflat = np.asarray(u[:, 0])
  64. uflat = uflat.reshape((5,))
  65. tout, yout = dlsim((num, den, 0.5), uflat, t_in)
  66. assert_array_almost_equal(yout, yout_truth)
  67. assert_array_almost_equal(t_in, tout)
  68. # zeros-poles-gain representation
  69. zd = np.array([0.5, -0.5])
  70. pd = np.array([1.j / np.sqrt(2), -1.j / np.sqrt(2)])
  71. k = 1.0
  72. yout_truth = np.array([[0.0, 1.0, 2.0, 2.25, 2.5]]).T
  73. tout, yout = dlsim((zd, pd, k, 0.5), u[:, 0], t_in)
  74. assert_array_almost_equal(yout, yout_truth)
  75. assert_array_almost_equal(t_in, tout)
  76. # Raise an error for continuous-time systems
  77. system = lti([1], [1, 1])
  78. assert_raises(AttributeError, dlsim, system, u)
  79. def test_dstep(self):
  80. a = np.asarray([[0.9, 0.1], [-0.2, 0.9]])
  81. b = np.asarray([[0.4, 0.1, -0.1], [0.0, 0.05, 0.0]])
  82. c = np.asarray([[0.1, 0.3]])
  83. d = np.asarray([[0.0, -0.1, 0.0]])
  84. dt = 0.5
  85. # Because b.shape[1] == 3, dstep should result in a tuple of three
  86. # result vectors
  87. yout_step_truth = (np.asarray([0.0, 0.04, 0.052, 0.0404, 0.00956,
  88. -0.036324, -0.093318, -0.15782348,
  89. -0.226628324, -0.2969374948]),
  90. np.asarray([-0.1, -0.075, -0.058, -0.04815,
  91. -0.04453, -0.0461895, -0.0521812,
  92. -0.061588875, -0.073549579,
  93. -0.08727047595]),
  94. np.asarray([0.0, -0.01, -0.013, -0.0101, -0.00239,
  95. 0.009081, 0.0233295, 0.03945587,
  96. 0.056657081, 0.0742343737]))
  97. tout, yout = dstep((a, b, c, d, dt), n=10)
  98. assert_equal(len(yout), 3)
  99. for i in range(0, len(yout)):
  100. assert_equal(yout[i].shape[0], 10)
  101. assert_array_almost_equal(yout[i].flatten(), yout_step_truth[i])
  102. # Check that the other two inputs (tf, zpk) will work as well
  103. tfin = ([1.0], [1.0, 1.0], 0.5)
  104. yout_tfstep = np.asarray([0.0, 1.0, 0.0])
  105. tout, yout = dstep(tfin, n=3)
  106. assert_equal(len(yout), 1)
  107. assert_array_almost_equal(yout[0].flatten(), yout_tfstep)
  108. zpkin = tf2zpk(tfin[0], tfin[1]) + (0.5,)
  109. tout, yout = dstep(zpkin, n=3)
  110. assert_equal(len(yout), 1)
  111. assert_array_almost_equal(yout[0].flatten(), yout_tfstep)
  112. # Raise an error for continuous-time systems
  113. system = lti([1], [1, 1])
  114. assert_raises(AttributeError, dstep, system)
  115. def test_dimpulse(self):
  116. a = np.asarray([[0.9, 0.1], [-0.2, 0.9]])
  117. b = np.asarray([[0.4, 0.1, -0.1], [0.0, 0.05, 0.0]])
  118. c = np.asarray([[0.1, 0.3]])
  119. d = np.asarray([[0.0, -0.1, 0.0]])
  120. dt = 0.5
  121. # Because b.shape[1] == 3, dimpulse should result in a tuple of three
  122. # result vectors
  123. yout_imp_truth = (np.asarray([0.0, 0.04, 0.012, -0.0116, -0.03084,
  124. -0.045884, -0.056994, -0.06450548,
  125. -0.068804844, -0.0703091708]),
  126. np.asarray([-0.1, 0.025, 0.017, 0.00985, 0.00362,
  127. -0.0016595, -0.0059917, -0.009407675,
  128. -0.011960704, -0.01372089695]),
  129. np.asarray([0.0, -0.01, -0.003, 0.0029, 0.00771,
  130. 0.011471, 0.0142485, 0.01612637,
  131. 0.017201211, 0.0175772927]))
  132. tout, yout = dimpulse((a, b, c, d, dt), n=10)
  133. assert_equal(len(yout), 3)
  134. for i in range(0, len(yout)):
  135. assert_equal(yout[i].shape[0], 10)
  136. assert_array_almost_equal(yout[i].flatten(), yout_imp_truth[i])
  137. # Check that the other two inputs (tf, zpk) will work as well
  138. tfin = ([1.0], [1.0, 1.0], 0.5)
  139. yout_tfimpulse = np.asarray([0.0, 1.0, -1.0])
  140. tout, yout = dimpulse(tfin, n=3)
  141. assert_equal(len(yout), 1)
  142. assert_array_almost_equal(yout[0].flatten(), yout_tfimpulse)
  143. zpkin = tf2zpk(tfin[0], tfin[1]) + (0.5,)
  144. tout, yout = dimpulse(zpkin, n=3)
  145. assert_equal(len(yout), 1)
  146. assert_array_almost_equal(yout[0].flatten(), yout_tfimpulse)
  147. # Raise an error for continuous-time systems
  148. system = lti([1], [1, 1])
  149. assert_raises(AttributeError, dimpulse, system)
  150. def test_dlsim_trivial(self):
  151. a = np.array([[0.0]])
  152. b = np.array([[0.0]])
  153. c = np.array([[0.0]])
  154. d = np.array([[0.0]])
  155. n = 5
  156. u = np.zeros(n).reshape(-1, 1)
  157. tout, yout, xout = dlsim((a, b, c, d, 1), u)
  158. assert_array_equal(tout, np.arange(float(n)))
  159. assert_array_equal(yout, np.zeros((n, 1)))
  160. assert_array_equal(xout, np.zeros((n, 1)))
  161. def test_dlsim_simple1d(self):
  162. a = np.array([[0.5]])
  163. b = np.array([[0.0]])
  164. c = np.array([[1.0]])
  165. d = np.array([[0.0]])
  166. n = 5
  167. u = np.zeros(n).reshape(-1, 1)
  168. tout, yout, xout = dlsim((a, b, c, d, 1), u, x0=1)
  169. assert_array_equal(tout, np.arange(float(n)))
  170. expected = (0.5 ** np.arange(float(n))).reshape(-1, 1)
  171. assert_array_equal(yout, expected)
  172. assert_array_equal(xout, expected)
  173. def test_dlsim_simple2d(self):
  174. lambda1 = 0.5
  175. lambda2 = 0.25
  176. a = np.array([[lambda1, 0.0],
  177. [0.0, lambda2]])
  178. b = np.array([[0.0],
  179. [0.0]])
  180. c = np.array([[1.0, 0.0],
  181. [0.0, 1.0]])
  182. d = np.array([[0.0],
  183. [0.0]])
  184. n = 5
  185. u = np.zeros(n).reshape(-1, 1)
  186. tout, yout, xout = dlsim((a, b, c, d, 1), u, x0=1)
  187. assert_array_equal(tout, np.arange(float(n)))
  188. # The analytical solution:
  189. expected = (np.array([lambda1, lambda2]) **
  190. np.arange(float(n)).reshape(-1, 1))
  191. assert_array_equal(yout, expected)
  192. assert_array_equal(xout, expected)
  193. def test_more_step_and_impulse(self):
  194. lambda1 = 0.5
  195. lambda2 = 0.75
  196. a = np.array([[lambda1, 0.0],
  197. [0.0, lambda2]])
  198. b = np.array([[1.0, 0.0],
  199. [0.0, 1.0]])
  200. c = np.array([[1.0, 1.0]])
  201. d = np.array([[0.0, 0.0]])
  202. n = 10
  203. # Check a step response.
  204. ts, ys = dstep((a, b, c, d, 1), n=n)
  205. # Create the exact step response.
  206. stp0 = (1.0 / (1 - lambda1)) * (1.0 - lambda1 ** np.arange(n))
  207. stp1 = (1.0 / (1 - lambda2)) * (1.0 - lambda2 ** np.arange(n))
  208. assert_allclose(ys[0][:, 0], stp0)
  209. assert_allclose(ys[1][:, 0], stp1)
  210. # Check an impulse response with an initial condition.
  211. x0 = np.array([1.0, 1.0])
  212. ti, yi = dimpulse((a, b, c, d, 1), n=n, x0=x0)
  213. # Create the exact impulse response.
  214. imp = (np.array([lambda1, lambda2]) **
  215. np.arange(-1, n + 1).reshape(-1, 1))
  216. imp[0, :] = 0.0
  217. # Analytical solution to impulse response
  218. y0 = imp[:n, 0] + np.dot(imp[1:n + 1, :], x0)
  219. y1 = imp[:n, 1] + np.dot(imp[1:n + 1, :], x0)
  220. assert_allclose(yi[0][:, 0], y0)
  221. assert_allclose(yi[1][:, 0], y1)
  222. # Check that dt=0.1, n=3 gives 3 time values.
  223. system = ([1.0], [1.0, -0.5], 0.1)
  224. t, (y,) = dstep(system, n=3)
  225. assert_allclose(t, [0, 0.1, 0.2])
  226. assert_array_equal(y.T, [[0, 1.0, 1.5]])
  227. t, (y,) = dimpulse(system, n=3)
  228. assert_allclose(t, [0, 0.1, 0.2])
  229. assert_array_equal(y.T, [[0, 1, 0.5]])
  230. class TestDlti:
  231. def test_dlti_instantiation(self):
  232. # Test that lti can be instantiated.
  233. dt = 0.05
  234. # TransferFunction
  235. s = dlti([1], [-1], dt=dt)
  236. assert_(isinstance(s, TransferFunction))
  237. assert_(isinstance(s, dlti))
  238. assert_(not isinstance(s, lti))
  239. assert_equal(s.dt, dt)
  240. # ZerosPolesGain
  241. s = dlti(np.array([]), np.array([-1]), 1, dt=dt)
  242. assert_(isinstance(s, ZerosPolesGain))
  243. assert_(isinstance(s, dlti))
  244. assert_(not isinstance(s, lti))
  245. assert_equal(s.dt, dt)
  246. # StateSpace
  247. s = dlti([1], [-1], 1, 3, dt=dt)
  248. assert_(isinstance(s, StateSpace))
  249. assert_(isinstance(s, dlti))
  250. assert_(not isinstance(s, lti))
  251. assert_equal(s.dt, dt)
  252. # Number of inputs
  253. assert_raises(ValueError, dlti, 1)
  254. assert_raises(ValueError, dlti, 1, 1, 1, 1, 1)
  255. class TestStateSpaceDisc:
  256. def test_initialization(self):
  257. # Check that all initializations work
  258. dt = 0.05
  259. StateSpace(1, 1, 1, 1, dt=dt)
  260. StateSpace([1], [2], [3], [4], dt=dt)
  261. StateSpace(np.array([[1, 2], [3, 4]]), np.array([[1], [2]]),
  262. np.array([[1, 0]]), np.array([[0]]), dt=dt)
  263. StateSpace(1, 1, 1, 1, dt=True)
  264. def test_conversion(self):
  265. # Check the conversion functions
  266. s = StateSpace(1, 2, 3, 4, dt=0.05)
  267. assert_(isinstance(s.to_ss(), StateSpace))
  268. assert_(isinstance(s.to_tf(), TransferFunction))
  269. assert_(isinstance(s.to_zpk(), ZerosPolesGain))
  270. # Make sure copies work
  271. assert_(StateSpace(s) is not s)
  272. assert_(s.to_ss() is not s)
  273. def test_properties(self):
  274. # Test setters/getters for cross class properties.
  275. # This implicitly tests to_tf() and to_zpk()
  276. # Getters
  277. s = StateSpace(1, 1, 1, 1, dt=0.05)
  278. assert_equal(s.poles, [1])
  279. assert_equal(s.zeros, [0])
  280. class TestTransferFunction:
  281. def test_initialization(self):
  282. # Check that all initializations work
  283. dt = 0.05
  284. TransferFunction(1, 1, dt=dt)
  285. TransferFunction([1], [2], dt=dt)
  286. TransferFunction(np.array([1]), np.array([2]), dt=dt)
  287. TransferFunction(1, 1, dt=True)
  288. def test_conversion(self):
  289. # Check the conversion functions
  290. s = TransferFunction([1, 0], [1, -1], dt=0.05)
  291. assert_(isinstance(s.to_ss(), StateSpace))
  292. assert_(isinstance(s.to_tf(), TransferFunction))
  293. assert_(isinstance(s.to_zpk(), ZerosPolesGain))
  294. # Make sure copies work
  295. assert_(TransferFunction(s) is not s)
  296. assert_(s.to_tf() is not s)
  297. def test_properties(self):
  298. # Test setters/getters for cross class properties.
  299. # This implicitly tests to_ss() and to_zpk()
  300. # Getters
  301. s = TransferFunction([1, 0], [1, -1], dt=0.05)
  302. assert_equal(s.poles, [1])
  303. assert_equal(s.zeros, [0])
  304. class TestZerosPolesGain:
  305. def test_initialization(self):
  306. # Check that all initializations work
  307. dt = 0.05
  308. ZerosPolesGain(1, 1, 1, dt=dt)
  309. ZerosPolesGain([1], [2], 1, dt=dt)
  310. ZerosPolesGain(np.array([1]), np.array([2]), 1, dt=dt)
  311. ZerosPolesGain(1, 1, 1, dt=True)
  312. def test_conversion(self):
  313. # Check the conversion functions
  314. s = ZerosPolesGain(1, 2, 3, dt=0.05)
  315. assert_(isinstance(s.to_ss(), StateSpace))
  316. assert_(isinstance(s.to_tf(), TransferFunction))
  317. assert_(isinstance(s.to_zpk(), ZerosPolesGain))
  318. # Make sure copies work
  319. assert_(ZerosPolesGain(s) is not s)
  320. assert_(s.to_zpk() is not s)
  321. class Test_dfreqresp:
  322. def test_manual(self):
  323. # Test dfreqresp() real part calculation (manual sanity check).
  324. # 1st order low-pass filter: H(z) = 1 / (z - 0.2),
  325. system = TransferFunction(1, [1, -0.2], dt=0.1)
  326. w = [0.1, 1, 10]
  327. w, H = dfreqresp(system, w=w)
  328. # test real
  329. expected_re = [1.2383, 0.4130, -0.7553]
  330. assert_almost_equal(H.real, expected_re, decimal=4)
  331. # test imag
  332. expected_im = [-0.1555, -1.0214, 0.3955]
  333. assert_almost_equal(H.imag, expected_im, decimal=4)
  334. def test_auto(self):
  335. # Test dfreqresp() real part calculation.
  336. # 1st order low-pass filter: H(z) = 1 / (z - 0.2),
  337. system = TransferFunction(1, [1, -0.2], dt=0.1)
  338. w = [0.1, 1, 10, 100]
  339. w, H = dfreqresp(system, w=w)
  340. jw = np.exp(w * 1j)
  341. y = np.polyval(system.num, jw) / np.polyval(system.den, jw)
  342. # test real
  343. expected_re = y.real
  344. assert_almost_equal(H.real, expected_re)
  345. # test imag
  346. expected_im = y.imag
  347. assert_almost_equal(H.imag, expected_im)
  348. def test_freq_range(self):
  349. # Test that freqresp() finds a reasonable frequency range.
  350. # 1st order low-pass filter: H(z) = 1 / (z - 0.2),
  351. # Expected range is from 0.01 to 10.
  352. system = TransferFunction(1, [1, -0.2], dt=0.1)
  353. n = 10
  354. expected_w = np.linspace(0, np.pi, 10, endpoint=False)
  355. w, H = dfreqresp(system, n=n)
  356. assert_almost_equal(w, expected_w)
  357. def test_pole_one(self):
  358. # Test that freqresp() doesn't fail on a system with a pole at 0.
  359. # integrator, pole at zero: H(s) = 1 / s
  360. system = TransferFunction([1], [1, -1], dt=0.1)
  361. with suppress_warnings() as sup:
  362. sup.filter(RuntimeWarning, message="divide by zero")
  363. sup.filter(RuntimeWarning, message="invalid value encountered")
  364. w, H = dfreqresp(system, n=2)
  365. assert_equal(w[0], 0.) # a fail would give not-a-number
  366. def test_error(self):
  367. # Raise an error for continuous-time systems
  368. system = lti([1], [1, 1])
  369. assert_raises(AttributeError, dfreqresp, system)
  370. def test_from_state_space(self):
  371. # H(z) = 2 / z^3 - 0.5 * z^2
  372. system_TF = dlti([2], [1, -0.5, 0, 0])
  373. A = np.array([[0.5, 0, 0],
  374. [1, 0, 0],
  375. [0, 1, 0]])
  376. B = np.array([[1, 0, 0]]).T
  377. C = np.array([[0, 0, 2]])
  378. D = 0
  379. system_SS = dlti(A, B, C, D)
  380. w = 10.0**np.arange(-3,0,.5)
  381. with suppress_warnings() as sup:
  382. sup.filter(BadCoefficients)
  383. w1, H1 = dfreqresp(system_TF, w=w)
  384. w2, H2 = dfreqresp(system_SS, w=w)
  385. assert_almost_equal(H1, H2)
  386. def test_from_zpk(self):
  387. # 1st order low-pass filter: H(s) = 0.3 / (z - 0.2),
  388. system_ZPK = dlti([],[0.2],0.3)
  389. system_TF = dlti(0.3, [1, -0.2])
  390. w = [0.1, 1, 10, 100]
  391. w1, H1 = dfreqresp(system_ZPK, w=w)
  392. w2, H2 = dfreqresp(system_TF, w=w)
  393. assert_almost_equal(H1, H2)
  394. class Test_bode:
  395. def test_manual(self):
  396. # Test bode() magnitude calculation (manual sanity check).
  397. # 1st order low-pass filter: H(s) = 0.3 / (z - 0.2),
  398. dt = 0.1
  399. system = TransferFunction(0.3, [1, -0.2], dt=dt)
  400. w = [0.1, 0.5, 1, np.pi]
  401. w2, mag, phase = dbode(system, w=w)
  402. # Test mag
  403. expected_mag = [-8.5329, -8.8396, -9.6162, -12.0412]
  404. assert_almost_equal(mag, expected_mag, decimal=4)
  405. # Test phase
  406. expected_phase = [-7.1575, -35.2814, -67.9809, -180.0000]
  407. assert_almost_equal(phase, expected_phase, decimal=4)
  408. # Test frequency
  409. assert_equal(np.array(w) / dt, w2)
  410. def test_auto(self):
  411. # Test bode() magnitude calculation.
  412. # 1st order low-pass filter: H(s) = 0.3 / (z - 0.2),
  413. system = TransferFunction(0.3, [1, -0.2], dt=0.1)
  414. w = np.array([0.1, 0.5, 1, np.pi])
  415. w2, mag, phase = dbode(system, w=w)
  416. jw = np.exp(w * 1j)
  417. y = np.polyval(system.num, jw) / np.polyval(system.den, jw)
  418. # Test mag
  419. expected_mag = 20.0 * np.log10(abs(y))
  420. assert_almost_equal(mag, expected_mag)
  421. # Test phase
  422. expected_phase = np.rad2deg(np.angle(y))
  423. assert_almost_equal(phase, expected_phase)
  424. def test_range(self):
  425. # Test that bode() finds a reasonable frequency range.
  426. # 1st order low-pass filter: H(s) = 0.3 / (z - 0.2),
  427. dt = 0.1
  428. system = TransferFunction(0.3, [1, -0.2], dt=0.1)
  429. n = 10
  430. # Expected range is from 0.01 to 10.
  431. expected_w = np.linspace(0, np.pi, n, endpoint=False) / dt
  432. w, mag, phase = dbode(system, n=n)
  433. assert_almost_equal(w, expected_w)
  434. def test_pole_one(self):
  435. # Test that freqresp() doesn't fail on a system with a pole at 0.
  436. # integrator, pole at zero: H(s) = 1 / s
  437. system = TransferFunction([1], [1, -1], dt=0.1)
  438. with suppress_warnings() as sup:
  439. sup.filter(RuntimeWarning, message="divide by zero")
  440. sup.filter(RuntimeWarning, message="invalid value encountered")
  441. w, mag, phase = dbode(system, n=2)
  442. assert_equal(w[0], 0.) # a fail would give not-a-number
  443. def test_imaginary(self):
  444. # bode() should not fail on a system with pure imaginary poles.
  445. # The test passes if bode doesn't raise an exception.
  446. system = TransferFunction([1], [1, 0, 100], dt=0.1)
  447. dbode(system, n=2)
  448. def test_error(self):
  449. # Raise an error for continuous-time systems
  450. system = lti([1], [1, 1])
  451. assert_raises(AttributeError, dbode, system)
  452. class TestTransferFunctionZConversion:
  453. """Test private conversions between 'z' and 'z**-1' polynomials."""
  454. def test_full(self):
  455. # Numerator and denominator same order
  456. num = [2, 3, 4]
  457. den = [5, 6, 7]
  458. num2, den2 = TransferFunction._z_to_zinv(num, den)
  459. assert_equal(num, num2)
  460. assert_equal(den, den2)
  461. num2, den2 = TransferFunction._zinv_to_z(num, den)
  462. assert_equal(num, num2)
  463. assert_equal(den, den2)
  464. def test_numerator(self):
  465. # Numerator lower order than denominator
  466. num = [2, 3]
  467. den = [5, 6, 7]
  468. num2, den2 = TransferFunction._z_to_zinv(num, den)
  469. assert_equal([0, 2, 3], num2)
  470. assert_equal(den, den2)
  471. num2, den2 = TransferFunction._zinv_to_z(num, den)
  472. assert_equal([2, 3, 0], num2)
  473. assert_equal(den, den2)
  474. def test_denominator(self):
  475. # Numerator higher order than denominator
  476. num = [2, 3, 4]
  477. den = [5, 6]
  478. num2, den2 = TransferFunction._z_to_zinv(num, den)
  479. assert_equal(num, num2)
  480. assert_equal([0, 5, 6], den2)
  481. num2, den2 = TransferFunction._zinv_to_z(num, den)
  482. assert_equal(num, num2)
  483. assert_equal([5, 6, 0], den2)