test_dltisys.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. # Author: Jeffrey Armstrong <jeff@approximatrix.com>
  2. # April 4, 2011
  3. import warnings
  4. import numpy as np
  5. from pytest import raises as assert_raises
  6. from scipy._lib._array_api import (
  7. assert_array_almost_equal, assert_almost_equal, xp_assert_close, xp_assert_equal,
  8. )
  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 len(tout) == len(yout)
  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 len(yout) == 3
  99. for i in range(0, len(yout)):
  100. assert 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 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 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 len(yout) == 3
  134. for i in range(0, len(yout)):
  135. assert 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 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 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. xp_assert_equal(tout, np.arange(float(n)))
  159. xp_assert_equal(yout, np.zeros((n, 1)))
  160. xp_assert_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. xp_assert_equal(tout, np.arange(float(n)))
  170. expected = (0.5 ** np.arange(float(n))).reshape(-1, 1)
  171. xp_assert_equal(yout, expected)
  172. xp_assert_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. xp_assert_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. xp_assert_equal(yout, expected)
  192. xp_assert_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. xp_assert_close(ys[0][:, 0], stp0)
  209. xp_assert_close(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. xp_assert_close(yi[0][:, 0], y0)
  221. xp_assert_close(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. xp_assert_close(t, [0, 0.1, 0.2])
  226. xp_assert_equal(y.T, [[0, 1.0, 1.5]])
  227. t, (y,) = dimpulse(system, n=3)
  228. xp_assert_close(t, [0, 0.1, 0.2])
  229. xp_assert_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 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 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 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. xp_assert_equal(s.poles, [1.])
  279. xp_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. xp_assert_equal(s.poles, [1.])
  303. xp_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 warnings.catch_warnings():
  362. warnings.filterwarnings("ignore", "divide by zero", RuntimeWarning)
  363. warnings.filterwarnings(
  364. "ignore", "invalid value encountered", RuntimeWarning)
  365. w, H = dfreqresp(system, n=2)
  366. assert w[0] == 0. # a fail would give not-a-number
  367. def test_error(self):
  368. # Raise an error for continuous-time systems
  369. system = lti([1], [1, 1])
  370. assert_raises(AttributeError, dfreqresp, system)
  371. def test_from_state_space(self):
  372. # H(z) = 2 / z^3 - 0.5 * z^2
  373. system_TF = dlti([2], [1, -0.5, 0, 0])
  374. A = np.array([[0.5, 0, 0],
  375. [1, 0, 0],
  376. [0, 1, 0]])
  377. B = np.array([[1, 0, 0]]).T
  378. C = np.array([[0, 0, 2]])
  379. D = 0
  380. system_SS = dlti(A, B, C, D)
  381. w = 10.0**np.arange(-3,0,.5)
  382. with warnings.catch_warnings():
  383. warnings.simplefilter("ignore", BadCoefficients)
  384. w1, H1 = dfreqresp(system_TF, w=w)
  385. w2, H2 = dfreqresp(system_SS, w=w)
  386. assert_almost_equal(H1, H2)
  387. def test_from_zpk(self):
  388. # 1st order low-pass filter: H(s) = 0.3 / (z - 0.2),
  389. system_ZPK = dlti([],[0.2],0.3)
  390. system_TF = dlti(0.3, [1, -0.2])
  391. w = [0.1, 1, 10, 100]
  392. w1, H1 = dfreqresp(system_ZPK, w=w)
  393. w2, H2 = dfreqresp(system_TF, w=w)
  394. assert_almost_equal(H1, H2)
  395. class Test_bode:
  396. def test_manual(self):
  397. # Test bode() magnitude calculation (manual sanity check).
  398. # 1st order low-pass filter: H(s) = 0.3 / (z - 0.2),
  399. dt = 0.1
  400. system = TransferFunction(0.3, [1, -0.2], dt=dt)
  401. w = [0.1, 0.5, 1, np.pi]
  402. w2, mag, phase = dbode(system, w=w)
  403. # Test mag
  404. expected_mag = [-8.5329, -8.8396, -9.6162, -12.0412]
  405. assert_almost_equal(mag, expected_mag, decimal=4)
  406. # Test phase
  407. expected_phase = [-7.1575, -35.2814, -67.9809, -180.0000]
  408. assert_almost_equal(phase, expected_phase, decimal=4)
  409. # Test frequency
  410. xp_assert_equal(np.array(w) / dt, w2)
  411. def test_auto(self):
  412. # Test bode() magnitude calculation.
  413. # 1st order low-pass filter: H(s) = 0.3 / (z - 0.2),
  414. system = TransferFunction(0.3, [1, -0.2], dt=0.1)
  415. w = np.array([0.1, 0.5, 1, np.pi])
  416. w2, mag, phase = dbode(system, w=w)
  417. jw = np.exp(w * 1j)
  418. y = np.polyval(system.num, jw) / np.polyval(system.den, jw)
  419. # Test mag
  420. expected_mag = 20.0 * np.log10(abs(y))
  421. assert_almost_equal(mag, expected_mag)
  422. # Test phase
  423. expected_phase = np.rad2deg(np.angle(y))
  424. assert_almost_equal(phase, expected_phase)
  425. def test_range(self):
  426. # Test that bode() finds a reasonable frequency range.
  427. # 1st order low-pass filter: H(s) = 0.3 / (z - 0.2),
  428. dt = 0.1
  429. system = TransferFunction(0.3, [1, -0.2], dt=0.1)
  430. n = 10
  431. # Expected range is from 0.01 to 10.
  432. expected_w = np.linspace(0, np.pi, n, endpoint=False) / dt
  433. w, mag, phase = dbode(system, n=n)
  434. assert_almost_equal(w, expected_w)
  435. def test_pole_one(self):
  436. # Test that freqresp() doesn't fail on a system with a pole at 0.
  437. # integrator, pole at zero: H(s) = 1 / s
  438. system = TransferFunction([1], [1, -1], dt=0.1)
  439. with warnings.catch_warnings():
  440. warnings.filterwarnings("ignore", "divide by zero", RuntimeWarning)
  441. warnings.filterwarnings(
  442. "ignore", "invalid value encountered", RuntimeWarning)
  443. w, mag, phase = dbode(system, n=2)
  444. assert w[0] == 0. # a fail would give not-a-number
  445. def test_imaginary(self):
  446. # bode() should not fail on a system with pure imaginary poles.
  447. # The test passes if bode doesn't raise an exception.
  448. system = TransferFunction([1], [1, 0, 100], dt=0.1)
  449. dbode(system, n=2)
  450. def test_error(self):
  451. # Raise an error for continuous-time systems
  452. system = lti([1], [1, 1])
  453. assert_raises(AttributeError, dbode, system)
  454. class TestTransferFunctionZConversion:
  455. """Test private conversions between 'z' and 'z**-1' polynomials."""
  456. def test_full(self):
  457. # Numerator and denominator same order
  458. num = np.asarray([2.0, 3, 4])
  459. den = np.asarray([5.0, 6, 7])
  460. num2, den2 = TransferFunction._z_to_zinv(num, den)
  461. xp_assert_equal(num, num2)
  462. xp_assert_equal(den, den2)
  463. num2, den2 = TransferFunction._zinv_to_z(num, den)
  464. xp_assert_equal(num, num2)
  465. xp_assert_equal(den, den2)
  466. def test_numerator(self):
  467. # Numerator lower order than denominator
  468. num = np.asarray([2.0, 3])
  469. den = np.asarray([50, 6, 7])
  470. num2, den2 = TransferFunction._z_to_zinv(num, den)
  471. xp_assert_equal([0.0, 2, 3], num2)
  472. xp_assert_equal(den, den2)
  473. num2, den2 = TransferFunction._zinv_to_z(num, den)
  474. xp_assert_equal([2.0, 3, 0], num2)
  475. xp_assert_equal(den, den2)
  476. def test_denominator(self):
  477. # Numerator higher order than denominator
  478. num = np.asarray([2., 3, 4])
  479. den = np.asarray([5.0, 6])
  480. num2, den2 = TransferFunction._z_to_zinv(num, den)
  481. xp_assert_equal(num, num2)
  482. xp_assert_equal([0.0, 5, 6], den2)
  483. num2, den2 = TransferFunction._zinv_to_z(num, den)
  484. xp_assert_equal(num, num2)
  485. xp_assert_equal([5.0, 6, 0], den2)