def test_Feedback(self): # Feedback blk1 = system.Gain(gain=2) blk = system.Feedback(block=blk1) assert blk.block is blk1 assert blk.gamma == 1.0 blk = system.Feedback(block=blk1) assert blk.block is blk1 assert blk.gamma == 1.0 blk = system.Feedback(block=blk1, gamma=4) assert blk.block is blk1 assert blk.gamma == 4 blk.write(2, 3) (yk, ) = blk.read() self.assertEqual(yk, 2 * (3 * 4 - 2)) gn = system.Gain(gain=150) blk.set(block=gn) self.assertTrue(blk.block is gn) blk.set(gamma=10) self.assertEqual(blk.gamma, 10) # Feedback with transfer-function # # G(z) = -.5/(z - .5) # TODO: CHECK DIFFERENT SIZES NUM/DEN blk1 = system.System(model=tf.zDTTF([-.5, 0], [-.5, 1])) blktf = system.Feedback(block=blk1) self.assertTrue(blktf.block is blk1) # A = .5, B = 1, C = -.5, D = 0 # # u = C x + D (- y + r) # x = A x + B (- y + r) A = np.array([[.5]]) B = np.array([[-1, 1]]) C = np.array([[-.5]]) D = np.array([[0, 0]]) blkss = system.System(model=ss.DTSS(A, B, C, D)) blktf.write(1, 3) yk1 = list(blktf.read()) blkss.write([1, 3]) yk2, = blkss.read() self.assertTrue(np.all(np.array(yk1) == yk2)) blktf.write(-1, 3) yk1 = list(blktf.read()) blkss.write([-1, 3]) yk2, = blkss.read() self.assertTrue(np.all(np.array(yk1) == yk2)) blktf.write(-1, 3) yk1 = list(blktf.read()) blkss.write([-1, 3]) yk2, = blkss.read() self.assertTrue(np.all(np.array(yk1) == yk2)) # Reset feedback self.assertTrue( np.array_equal(blktf.block.model.state, np.array((6.5, )))) blktf.reset() self.assertTrue( np.array_equal(blktf.block.model.state, np.array((0, ))))
def test4(self): num1 = np.array([2, 1]) den1 = np.array([-1, 1]) sys = tf.zDTTF(num1, den1) # G(z) = (z + 2)/(z - 1) = (z - 1 + 3)/(z-1) = 1 + 3/(z-1) sysss = sys.as_DTSS() A = np.array([[1]]) B = np.array([[1]]) C = np.array([[3]]) D = np.array([[1]]) self.assertTrue(np.array_equal(A, sysss.A)) self.assertTrue(np.array_equal(B, sysss.B)) self.assertTrue(np.array_equal(C, sysss.C)) self.assertTrue(np.array_equal(D, sysss.D)) y1 = sys.update(1) y2 = sysss.update(1) self.assertEqual(y1, y2[0]) y1 = sys.update(-1) y2 = sysss.update(-1) self.assertEqual(y1, y2[0]) # print(y1, y2) y1 = sys.update(3) y2 = sysss.update(3) self.assertEqual(y1, y2[0]) # print(y1, y2) y1 = sys.update(0) y2 = sysss.update(0) self.assertEqual(y1, y2[0]) # print(y1, y2) # G(z) = z/(z - 1) = 1 / (1 - q) num1 = np.array([0, 1]) den1 = np.array([-1, 1]) sys = tf.zDTTF(num1, den1) num2 = np.array([1, 0]) den2 = np.array([1, -1]) self.assertTrue(np.array_equal(sys.num, num2)) self.assertTrue(np.array_equal(sys.den, den2)) # G(z) = z/(z - 1) = (z - 1 + 1)/(z-1) = 1 + 1/(z-1) sysss = sys.as_DTSS() A = np.array([[1]]) B = np.array([[1]]) C = np.array([[1]]) D = np.array([[1]]) self.assertTrue(np.array_equal(A, sysss.A)) self.assertTrue(np.array_equal(B, sysss.B)) self.assertTrue(np.array_equal(C, sysss.C)) self.assertTrue(np.array_equal(D, sysss.D)) y1 = sys.update(1) y2 = sysss.update(1) self.assertEqual(y1, y2[0]) # print(y1, y2[0]) y1 = sys.update(-1) y2 = sysss.update(-1) self.assertEqual(y1, y2[0]) # print(y1, y2[0]) y1 = sys.update(3) y2 = sysss.update(3) self.assertEqual(y1, y2[0]) # print(y1, y2[0]) y1 = sys.update(0) y2 = sysss.update(0) self.assertEqual(y1, y2[0]) # print(y1, y2) # G(z) = 2/(z - 1) num1 = np.array([2, 0]) den1 = np.array([-1, 1]) sys = tf.zDTTF(num1, den1) num2 = np.array([0, 2]) den2 = np.array([1, -1]) self.assertTrue(np.array_equal(sys.num, num2)) self.assertTrue(np.array_equal(sys.den, den2)) sysss = sys.as_DTSS() A = np.array([[1]]) B = np.array([[1]]) C = np.array([[2]]) D = np.array([[0]]) self.assertTrue(np.array_equal(A, sysss.A)) self.assertTrue(np.array_equal(B, sysss.B)) self.assertTrue(np.array_equal(C, sysss.C)) self.assertTrue(np.array_equal(D, sysss.D)) # print(sysss.A, sysss.B, sysss.C, sysss.D) y1 = sys.update(1) y2 = sysss.update(1) self.assertEqual(y1, y2[0]) # print(y1, y2[0]) y1 = sys.update(-1) y2 = sysss.update(-1) self.assertEqual(y1, y2[0]) # print(y1, y2[0]) y1 = sys.update(3) y2 = sysss.update(3) self.assertEqual(y1, y2[0]) # print(y1, y2[0]) y1 = sys.update(0) y2 = sysss.update(0) self.assertEqual(y1, y2[0]) # print(y1, y2[0]) # G(z) = z^2/(z - 1) = 1 / (1 - q) num1 = np.array([1, 0, 0]) den1 = np.array([-1, 1]) with self.assertRaises(system.SystemException): sys = tf.zDTTF(num1, den1) # G(z) = (z + 3)/(z^2 + 2 z - 1) = (q + 3 q^2)/(1 + 2 q - q^2) num1 = np.array([3, 1, 0]) den1 = np.array([-1, 2, 1]) sys = tf.zDTTF(num1, den1) num2 = np.array([0, 1, 3]) den2 = np.array([1, 2, -1]) self.assertTrue(np.array_equal(sys.num, num2)) self.assertTrue(np.array_equal(sys.den, den2)) sysss = sys.as_DTSS() A = np.array([[0, 1], [1, -2]]) B = np.array([[0], [1]]) C = np.array([[3, 1]]) D = np.array([[0]]) self.assertTrue(np.array_equal(A, sysss.A)) self.assertTrue(np.array_equal(B, sysss.B)) self.assertTrue(np.array_equal(C, sysss.C)) self.assertTrue(np.array_equal(D, sysss.D)) # print('A =\n{}\nB =\n{}\nC =\n{}\nD =\n{}'.format(sysss.A, sysss.B, sysss.C, sysss.D)) # yk = -2 yk-1 + yk-2 + uk-1 + 3 uk-2 # u1 = 1 => y1 = 0 # u1 = 1 => y1 = [3 1] [0; 0] = 0 # x2 = [0 1; 1 -2] [0; 0] + [0; 1] 1 = [0; 1] y1 = sys.update(1) y2 = sysss.update(np.array([1])) self.assertEqual(y1, 0) self.assertTrue(np.array_equal(sysss.state, np.array([0, 1]))) self.assertEqual(y1, y2[0]) # u2 = -1 => y2 = -2 y1 + u1 = 1 # u2 = -1 => y2 = [3 1] [0; 1] = 1 # x3 = [0 1; 1 -2] [0; 1] + [0; 1] -1 # = [1; -2] + [0; -1] = [1; -3] y1 = sys.update(-1) y2 = sysss.update(np.array([-1])) # print(y1, y2) self.assertEqual(y1, 1) self.assertTrue(np.array_equal(sysss.state, np.array([1, -3]))) self.assertEqual(y1, y2[0]) # u3 = 3 => y3 = -2 y2 + y1 + u2 + 3 u1 = -2 + 0 + -1 + 3 = 0 # u3 = 3 => y3 = [3 1] [1; -3] = 0 # x4 = [0 1; 1 -2] [1; -3] + [0; 1] 3 # = [-3; 7] + [0; 3] = [-3; 10] y1 = sys.update(3) y2 = sysss.update(np.array([3])) # print(y1, y2) self.assertEqual(y1, 0) self.assertTrue(np.array_equal(sysss.state, np.array([-3, 10]))) self.assertEqual(y1, y2[0]) # u4 = 0 => y4 = -2 y3 + y2 + u3 + 3 u2 = 0 + 1 + 3 - 3 = 1 # u4 = 0 => y4 = [3 1] [-3; 10] = 1 # x5 = [0 1; 1 -2] [-3; 10] + [0; 1] 0 # = [10; -23] y1 = sys.update(0) y2 = sysss.update(np.array([0])) # print(y1, y2) self.assertEqual(y1, 1) self.assertTrue(np.array_equal(sysss.state, np.array([10, -23]))) self.assertEqual(y1, y2[0]) # G(z) = z^2/(z^2 + 2 z - 1) = 1 + (1 - 2 z)/(z^2 + 2 z - 1) num1 = np.array([0, 0, 1]) den1 = np.array([-1, 2, 1]) sys = tf.zDTTF(num1, den1) num2 = np.array([1, 0, 0]) den2 = np.array([1, 2, -1]) self.assertTrue(np.array_equal(sys.num, num2)) self.assertTrue(np.array_equal(sys.den, den2)) sysss = sys.as_DTSS() A = np.array([[0, 1], [1, -2]]) B = np.array([[0], [1]]) C = np.array([[1, -2]]) D = np.array([[1]]) self.assertTrue(np.array_equal(A, sysss.A)) self.assertTrue(np.array_equal(B, sysss.B)) self.assertTrue(np.array_equal(C, sysss.C)) self.assertTrue(np.array_equal(D, sysss.D)) # print('A =\n{}\nB =\n{}\nC =\n{}\nD =\n{}'.format(sysss.A, sysss.B, sysss.C, sysss.D)) # yk = -2 yk-1 + yk-2 + uk # u1 = 1 => y1 = 1 # u1 = 1 => y1 = [1 -2] [0; 0] + [1] 1 = 1 # x2 = [0 1; 1 -2] [0; 0] + [0; 1] 1 = [0; 1] y1 = sys.update(1) y2 = sysss.update(np.array([1])) # print(y1, y2) self.assertEqual(y1, 1) self.assertTrue(np.array_equal(sysss.state, np.array([0, 1]))) self.assertEqual(y1, y2[0]) # u2 = -1 => y2 = -2 y1 + u2 = -2 - 1 = -3 # u2 = -1 => y2 = [1 -2] [0; 1] + [1] -1 = -2 -1 = -3 # x3 = [0 1; 1 -2] [0; 1] + [0; 1] -1 # = [1; -2] + [0; -1] = [1; -3] y1 = sys.update(-1) y2 = sysss.update(np.array([-1])) # print(y1, y2) self.assertEqual(y1, -3) self.assertTrue(np.array_equal(sysss.state, np.array([1, -3]))) self.assertEqual(y1, y2[0]) # u3 = 3 => y3 = -2 y2 + y1 + u3 = 6 + 1 + 3 = 10 # u3 = 3 => y3 = [1 -2] [1; -3] + [1] 3 = 1 + 6 + 3 = 10 # x4 = [0 1; 1 -2] [1; -3] + [0; 1] 3 # = [-3; 7] + [0; 3] = [-3; 10] y1 = sys.update(3) y2 = sysss.update(np.array([3])) # print(y1, y2) self.assertEqual(y1, 10) self.assertTrue(np.array_equal(sysss.state, np.array([-3, 10]))) self.assertEqual(y1, y2[0]) # u4 = 0 => y4 = -2 y3 + y2 + u4 = - 20 - 3 + 0 = -23 # u4 = 0 => y4 = [1 -2] [-3; 10] + [1] 0 = -3 -20 = -23 # x5 = [0 1; 1 -2] [-3; 10] + [0; 1] 0 # = [10; -23] y1 = sys.update(0) y2 = sysss.update(np.array([0])) # print(y1, y2) self.assertEqual(y1, -23) self.assertTrue(np.array_equal(sysss.state, np.array([10, -23]))) self.assertEqual(y1, y2[0]) # vector input/output sysss = sys.as_DTSS() A = np.array([[0, 1], [1, -2]]) B = np.array([[0], [1]]) C = np.array([[1, -2]]) D = np.array([[1]]) self.assertTrue(np.array_equal(A, sysss.A)) self.assertTrue(np.array_equal(B, sysss.B)) self.assertTrue(np.array_equal(C, sysss.C)) self.assertTrue(np.array_equal(D, sysss.D)) # print('A =\n{}\nB =\n{}\nC =\n{}\nD =\n{}'.format(sysss.A, sysss.B, sysss.C, sysss.D)) # u1 = 1 => y1 = 1 y2 = sysss.update(np.array([1])) self.assertTrue(isinstance(y2, np.ndarray) and y2[0] == 1) # u2 = -1 => y2 = -2 y1 + u2 = -2 - 1 = -3 y2 = sysss.update(np.array([-1])) self.assertTrue(isinstance(y2, np.ndarray) and y2[0] == -3) # u3 = 3 => y3 = -2 y2 + y1 + u3 = 6 + 1 + 3 = 10 y2 = sysss.update(np.array([3])) self.assertTrue(isinstance(y2, np.ndarray) and y2[0] == 10) # u4 = 0 => y4 = -2 y3 + y2 + u4 = - 20 - 3 + 0 = -23 y2 = sysss.update(np.array([0])) self.assertTrue(isinstance(y2, np.ndarray) and y2[0] == -23) # SIMO A = np.array([[0, 1], [1, -2]]) B = np.array([[0], [1]]) C = np.array([[1, -2], [0, 1]]) D = np.array([[1], [0]]) sys = ss.DTSS(A, B, C, D) self.assertTrue(np.array_equal(sys.A, A)) self.assertTrue(np.array_equal(sys.B, B)) self.assertTrue(np.array_equal(sys.C, C)) self.assertTrue(np.array_equal(sys.D, D)) self.assertTrue(np.array_equal(sys.state, np.zeros(2))) # u1 = 1 => y1 = 1 y2 = sys.update(np.array([1])) self.assertTrue( isinstance(y2, np.ndarray) and np.array_equal(y2, np.array([1, 0]))) # u2 = -1 => y2 = -2 y1 + u2 = -2 - 1 = -3 y2 = sys.update(np.array([-1])) self.assertTrue( isinstance(y2, np.ndarray) and np.array_equal(y2, np.array([-3, 1]))) # u3 = 3 => y3 = -2 y2 + y1 + u3 = 6 + 1 + 3 = 10 y2 = sys.update(np.array([3])) self.assertTrue( isinstance(y2, np.ndarray) and np.array_equal(y2, np.array([10, -3]))) # u4 = 0 => y4 = -2 y3 + y2 + u4 = - 20 - 3 + 0 = -23 y2 = sys.update(np.array([0])) self.assertTrue( isinstance(y2, np.ndarray) and np.array_equal(y2, np.array([-23, 10]))) # MIMO A = np.array([[0, 1], [1, -2]]) B = np.array([[1, -1], [1, 0]]) C = np.array([[1, -2], [0, 1]]) D = np.array([[1, 0], [-1, 1]]) sys = ss.DTSS(A, B, C, D) self.assertTrue(np.array_equal(sys.A, A)) self.assertTrue(np.array_equal(sys.B, B)) self.assertTrue(np.array_equal(sys.C, C)) self.assertTrue(np.array_equal(sys.D, D)) self.assertTrue(np.array_equal(sys.state, np.zeros(2))) # u1 = 1 => y1 = 1 y2 = sys.update(np.array([1, 1])) self.assertTrue(np.array_equal(sys.state, np.array([0, 1]))) self.assertTrue( isinstance(y2, np.ndarray) and np.array_equal(y2, np.array([1, 0]))) # u2 = -1 => y2 = -2 y1 + u2 = -2 - 1 = -3 y2 = sys.update(np.array([-1, 0])) self.assertTrue(np.array_equal(sys.state, np.array([0, -3]))) self.assertTrue( isinstance(y2, np.ndarray) and np.array_equal(y2, np.array([-3, 2]))) # u3 = 3 => y3 = -2 y2 + y1 + u3 = 6 + 1 + 3 = 10 y2 = sys.update(np.array([3, -1])) self.assertTrue(np.array_equal(sys.state, np.array([1, 9]))) self.assertTrue( isinstance(y2, np.ndarray) and np.array_equal(y2, np.array([9, -7]))) # u4 = 0 => y4 = -2 y3 + y2 + u4 = - 20 - 3 + 0 = -23 y2 = sys.update(np.array([2, 1])) self.assertTrue(np.array_equal(sys.state, np.array([10, -15]))) self.assertTrue( isinstance(y2, np.ndarray) and np.array_equal(y2, np.array([-15, 8])))
def test_System(self): signals = {'clock': 1, 'encoder1': 2, 'test': 3} labels = ['clock', 'encoder1'] # Transfer-function num = np.array([1, 1, 3]) den = np.array([1, -1]) sys = tf.DTTF(num, den) self.assertTrue(np.array_equal(sys.num, num)) den = np.array([1, -1, 0]) self.assertTrue(np.array_equal(sys.den, den)) self.assertTrue(np.array_equal(sys.state, np.zeros(2))) blk = system.System(model=sys) self.assertTrue(blk.model is sys) with self.assertRaises(block.BlockException): blk = system.System(modelo=sys) with self.assertRaises(block.BlockException): blk = system.System(model=1) with self.assertRaises(block.BlockException): blk = system.System(model=sys, mux=False) blk.write([1]) (yk, ) = blk.read() state = np.array([1, 0]) self.assertTrue(np.array_equal(sys.state, state)) assert yk == 1 blk.write([-1]) (yk, ) = blk.read() state = np.array([0, 1]) self.assertTrue(np.array_equal(sys.state, state)) assert yk == 1 blk.write([2]) (yk, ) = blk.read() state = np.array([2, 0]) self.assertTrue(np.array_equal(sys.state, state)) assert yk == 5 blk.write([1]) (yk, ) = blk.read() state = np.array([3, 2]) self.assertTrue(np.array_equal(sys.state, state)) assert yk == 5 blk.reset() yk = sys.update(0) assert yk == 0 num = np.array([1, 1]) den = np.array([1, -1]) sys2 = tf.DTTF(num, den) blk = system.System(model=sys) blk.set(model=sys2) assert blk.model is sys2 with self.assertRaises(block.BlockException): blk.set(model=1) # State space A = np.array([[0, 1], [1, -2]]) B = np.array([[0], [1]]) C = np.array([[1, -2], [0, 1]]) D = np.array([[1], [0]]) sys = ss.DTSS(A, B, C, D) self.assertTrue(np.array_equal(sys.A, A)) self.assertTrue(np.array_equal(sys.B, B)) self.assertTrue(np.array_equal(sys.C, C)) self.assertTrue(np.array_equal(sys.D, D)) self.assertTrue(np.array_equal(sys.state, np.zeros(2))) blk = system.System(model=sys) assert blk.model is sys with self.assertRaises(block.BlockException): blk = system.System(modelo=sys) blk.write([1]) yk, = blk.read() state = np.array([0, 1]) self.assertTrue(np.array_equal(sys.state, state)) self.assertTrue(np.array_equal(yk, np.array([1, 0]))) blk.write([-1]) yk, = blk.read() state = np.array([1, -3]) self.assertTrue(np.array_equal(sys.state, state)) self.assertTrue(np.array_equal(yk, np.array([-3, 1]))) blk.write([3]) yk, = blk.read() state = np.array([-3, 10]) self.assertTrue(np.array_equal(sys.state, state)) self.assertTrue(np.array_equal(yk, np.array([10, -3]))) blk.write([0]) yk, = blk.read() state = np.array([10, -23]) self.assertTrue(np.array_equal(sys.state, state)) self.assertTrue(np.array_equal(yk, np.array([-23, 10]))) blk.reset() self.assertTrue(np.array_equal(sys.state, np.array([0, 0]))) # SIMO A = np.array([[0, 1], [1, -2]]) B = np.array([[0], [1]]) C = np.array([[1, -2], [0, 1]]) D = np.array([[1], [0]]) sys = ss.DTSS(A, B, C, D) self.assertTrue(np.array_equal(sys.A, A)) self.assertTrue(np.array_equal(sys.B, B)) self.assertTrue(np.array_equal(sys.C, C)) self.assertTrue(np.array_equal(sys.D, D)) self.assertTrue(np.array_equal(sys.state, np.zeros(2))) blk = system.System(model=sys) assert blk.model is sys with self.assertRaises(block.BlockException): blk = system.System(modelo=sys) blk.write(1) yk, = blk.read() state = np.array([0, 1]) # print(sys.state) self.assertTrue(np.array_equal(sys.state, state)) self.assertTrue(np.array_equal(yk, np.array([1, 0]))) blk.write([-1]) yk, = blk.read() state = np.array([1, -3]) self.assertTrue(np.array_equal(sys.state, state)) self.assertTrue(np.array_equal(yk, np.array([-3, 1]))) blk.write(3) yk, = blk.read() state = np.array([-3, 10]) self.assertTrue(np.array_equal(sys.state, state)) self.assertTrue(np.array_equal(yk, np.array([10, -3]))) blk.write([0]) yk, = blk.read() state = np.array([10, -23]) self.assertTrue(np.array_equal(sys.state, state)) self.assertTrue(np.array_equal(yk, np.array([-23, 10]))) blk.reset() self.assertTrue(np.array_equal(sys.state, np.array([0, 0]))) # System A = np.array([[0, 1], [1, -2]]) B = np.array([[1, -1], [1, 0]]) C = np.array([[1, -2], [0, 1]]) D = np.array([[1, 0], [-1, 1]]) sys = ss.DTSS(A, B, C, D) self.assertTrue(np.array_equal(sys.A, A)) self.assertTrue(np.array_equal(sys.B, B)) self.assertTrue(np.array_equal(sys.C, C)) self.assertTrue(np.array_equal(sys.D, D)) self.assertTrue(np.array_equal(sys.state, np.zeros(2))) blk = system.System(model=sys) assert blk.model is sys # u1 = 1 => y1 = 1 blk.write([1, 1]) y2, = blk.read() self.assertTrue(np.array_equal(sys.state, np.array([0, 1]))) self.assertTrue(np.array_equal(y2, np.array([1, 0]))) # u2 = -1 => y2 = -2 y1 + u2 = -2 - 1 = -3 blk.write([-1, 0]) y2, = blk.read() self.assertTrue(np.array_equal(sys.state, np.array([0, -3]))) self.assertTrue(np.array_equal(y2, np.array([-3, 2]))) # u3 = 3 => y3 = -2 y2 + y1 + u3 = 6 + 1 + 3 = 10 blk.write([3, -1]) y2, = blk.read() self.assertTrue(np.array_equal(sys.state, np.array([1, 9]))) self.assertTrue(np.array_equal(y2, np.array([9, -7]))) # u4 = 0 => y4 = -2 y3 + y2 + u4 = - 20 - 3 + 0 = -23 blk.write([2, 1]) y2, = blk.read() self.assertTrue(np.array_equal(sys.state, np.array([10, -15]))) self.assertTrue(np.array_equal(y2, np.array([-15, 8]))) # Test to work with multiple signals # reset state blk.reset() # u1 = 1 => y1 = 1 blk.write(1, 1) y2, = blk.read() self.assertTrue(np.array_equal(sys.state, np.array([0, 1]))) self.assertTrue(np.array_equal(y2, np.array([1, 0]))) # u2 = -1 => y2 = -2 y1 + u2 = -2 - 1 = -3 blk.write(-1, 0) y2, = blk.read() self.assertTrue(np.array_equal(sys.state, np.array([0, -3]))) self.assertTrue(np.array_equal(y2, np.array([-3, 2]))) # u3 = 3 => y3 = -2 y2 + y1 + u3 = 6 + 1 + 3 = 10 blk.write(3, -1) y2, = blk.read() self.assertTrue(np.array_equal(sys.state, np.array([1, 9]))) self.assertTrue(np.array_equal(y2, np.array([9, -7]))) # u4 = 0 => y4 = -2 y3 + y2 + u4 = - 20 - 3 + 0 = -23 blk.write(2, 1) y2, = blk.read() self.assertTrue(np.array_equal(sys.state, np.array([10, -15]))) self.assertTrue(np.array_equal(y2, np.array([-15, 8])))
def test3(): # different size num < den # G(z) = 2 / (z - 1) = 2 q /(1 - q) num1 = np.array([2]) den1 = np.array([-1, 1]) sys = tf.zDTTF(num1, den1) num2 = np.array([0, 2]) den2 = np.array([1, -1]) assert np.all(sys.num == num2) assert np.all(sys.den == den2) assert sys.state.size == 1 # inproper # G(z) = z^2 / (z - 1) = 1 /(q - q^2) num1 = np.array([0, 0, 1]) den1 = np.array([-1, 1]) with pytest.raises(system.SystemException): sys = tf.zDTTF(num1, den1) # G(z) = z^2 / (z - 1) = 1 /(q - q^2) num1 = np.array([0, 0, 1]) den1 = np.array([-1, 1, 0]) with pytest.raises(system.SystemException): sys = tf.zDTTF(num1, den1) # G(z) = (z + 2)/(z - 1) = (1 + 2 q) / (1 - q) num1 = np.array([2, 1]) den1 = np.array([-1, 1]) sys = tf.zDTTF(num1, den1) num2 = np.array([1, 2]) den2 = np.array([1, -1]) assert np.all(sys.num == num2) assert np.all(sys.den == den2) assert sys.state.size == 1 # G(z) = (z + 2)/(z - 1) = (z - 1 + 3)/(z-1) = 1 + 3/(z-1) sysss = sys.as_DTSS() A = np.array([1]) B = np.array([1]) C = np.array([3]) D = np.array([1]) assert np.all(A == sysss.A) assert np.all(B == sysss.B) assert np.all(C == sysss.C) assert np.all(D == sysss.D) y1 = sys.update(1) y2 = sysss.update(1) assert y1 == y2 #print(y1, y2) y1 = sys.update(-1) y2 = sysss.update(-1) assert y1 == y2 #print(y1, y2) y1 = sys.update(3) y2 = sysss.update(3) assert y1 == y2 #print(y1, y2) y1 = sys.update(0) y2 = sysss.update(0) assert y1 == y2 #print(y1, y2) # G(z) = z/(z - 1) = 1 / (1 - q) num1 = np.array([0, 1]) den1 = np.array([-1, 1]) sys = tf.zDTTF(num1, den1) num2 = np.array([1, 0]) den2 = np.array([1, -1]) assert np.all(sys.num == num2) assert np.all(sys.den == den2) # G(z) = z/(z - 1) = (z - 1 + 1)/(z-1) = 1 + 1/(z-1) sysss = sys.as_DTSS() A = np.array([1]) B = np.array([1]) C = np.array([1]) D = np.array([1]) assert np.all(A == sysss.A) assert np.all(B == sysss.B) assert np.all(C == sysss.C) assert np.all(D == sysss.D) y1 = sys.update(1) y2 = sysss.update(1) assert y1 == y2 #print(y1, y2) y1 = sys.update(-1) y2 = sysss.update(-1) assert y1 == y2 #print(y1, y2) y1 = sys.update(3) y2 = sysss.update(3) assert y1 == y2 #print(y1, y2) y1 = sys.update(0) y2 = sysss.update(0) assert y1 == y2 #print(y1, y2) # G(z) = 2/(z - 1) num1 = np.array([2, 0]) den1 = np.array([-1, 1]) sys = tf.zDTTF(num1, den1) num2 = np.array([0, 2]) den2 = np.array([1, -1]) assert np.all(sys.num == num2) assert np.all(sys.den == den2) sysss = sys.as_DTSS() A = np.array([1]) B = np.array([1]) C = np.array([2]) D = np.array([0]) assert np.all(A == sysss.A) assert np.all(B == sysss.B) assert np.all(C == sysss.C) assert np.all(D == sysss.D) #print(sysss.A, sysss.B, sysss.C, sysss.D) y1 = sys.update(1) y2 = sysss.update(1) assert y1 == y2 #print(y1, y2) y1 = sys.update(-1) y2 = sysss.update(-1) assert y1 == y2 #print(y1, y2) y1 = sys.update(3) y2 = sysss.update(3) assert y1 == y2 #print(y1, y2) y1 = sys.update(0) y2 = sysss.update(0) assert y1 == y2 #print(y1, y2) # G(z) = z^2/(z - 1) = 1 / (1 - q) num1 = np.array([1, 0, 0]) den1 = np.array([-1, 1]) with pytest.raises(system.SystemException): sys = tf.zDTTF(num1, den1) # G(z) = (z + 3)/(z^2 + 2 z - 1) = (q + 3 q^2)/(1 + 2 q - q^2) num1 = np.array([3, 1, 0]) den1 = np.array([-1, 2, 1]) sys = tf.zDTTF(num1, den1) num2 = np.array([0, 1, 3]) den2 = np.array([1, 2, -1]) assert np.all(sys.num == num2) assert np.all(sys.den == den2) sysss = sys.as_DTSS() A = np.array([[0, 1], [1, -2]]) B = np.array([[0], [1]]) C = np.array([[3, 1]]) D = np.array([[0]]) assert np.all(A == sysss.A) assert np.all(B == sysss.B) assert np.all(C == sysss.C) assert np.all(D == sysss.D) #print('A =\n{}\nB =\n{}\nC =\n{}\nD =\n{}'.format(sysss.A, sysss.B, sysss.C, sysss.D)) # yk = -2 yk-1 + yk-2 + uk-1 + 3 uk-2 # u1 = 1 => y1 = 0 # u1 = 1 => y1 = [3 1] [0; 0] = 0 # x2 = [0 1; 1 -2] [0; 0] + [0; 1] 1 = [0; 1] y1 = sys.update(1) y2 = sysss.update(np.array([1])) #print(y1, y2) assert y1 == 0 assert np.all(sysss.state == np.array([0, 1])) assert y1 == y2 # u2 = -1 => y2 = -2 y1 + u1 = 1 # u2 = -1 => y2 = [3 1] [0; 1] = 1 # x3 = [0 1; 1 -2] [0; 1] + [0; 1] -1 # = [1; -2] + [0; -1] = [1; -3] y1 = sys.update(-1) y2 = sysss.update(np.array([-1])) #print(y1, y2) assert y1 == 1 assert np.all(sysss.state == np.array([1, -3])) assert y1 == y2 # u3 = 3 => y3 = -2 y2 + y1 + u2 + 3 u1 = -2 + 0 + -1 + 3 = 0 # u3 = 3 => y3 = [3 1] [1; -3] = 0 # x4 = [0 1; 1 -2] [1; -3] + [0; 1] 3 # = [-3; 7] + [0; 3] = [-3; 10] y1 = sys.update(3) y2 = sysss.update(np.array([3])) #print(y1, y2) assert y1 == 0 assert np.all(sysss.state == np.array([-3, 10])) assert y1 == y2 # u4 = 0 => y4 = -2 y3 + y2 + u3 + 3 u2 = 0 + 1 + 3 - 3 = 1 # u4 = 0 => y4 = [3 1] [-3; 10] = 1 # x5 = [0 1; 1 -2] [-3; 10] + [0; 1] 0 # = [10; -23] y1 = sys.update(0) y2 = sysss.update(np.array([0])) #print(y1, y2) assert y1 == 1 assert np.all(sysss.state == np.array([10, -23])) assert y1 == y2 # G(z) = z^2/(z^2 + 2 z - 1) = 1 + (1 - 2 z)/(z^2 + 2 z - 1) num1 = np.array([0, 0, 1]) den1 = np.array([-1, 2, 1]) sys = tf.zDTTF(num1, den1) num2 = np.array([1, 0, 0]) den2 = np.array([1, 2, -1]) assert np.all(sys.num == num2) assert np.all(sys.den == den2) sysss = sys.as_DTSS() A = np.array([[0, 1], [1, -2]]) B = np.array([[0], [1]]) C = np.array([[1, -2]]) D = np.array([[1]]) assert np.all(A == sysss.A) assert np.all(B == sysss.B) assert np.all(C == sysss.C) assert np.all(D == sysss.D) #print('A =\n{}\nB =\n{}\nC =\n{}\nD =\n{}'.format(sysss.A, sysss.B, sysss.C, sysss.D)) # yk = -2 yk-1 + yk-2 + uk # u1 = 1 => y1 = 1 # u1 = 1 => y1 = [1 -2] [0; 0] + [1] 1 = 1 # x2 = [0 1; 1 -2] [0; 0] + [0; 1] 1 = [0; 1] y1 = sys.update(1) y2 = sysss.update(np.array([1])) #print(y1, y2) assert y1 == 1 assert np.all(sysss.state == np.array([0, 1])) assert y1 == y2 # u2 = -1 => y2 = -2 y1 + u2 = -2 - 1 = -3 # u2 = -1 => y2 = [1 -2] [0; 1] + [1] -1 = -2 -1 = -3 # x3 = [0 1; 1 -2] [0; 1] + [0; 1] -1 # = [1; -2] + [0; -1] = [1; -3] y1 = sys.update(-1) y2 = sysss.update(np.array([-1])) #print(y1, y2) assert y1 == -3 assert np.all(sysss.state == np.array([1, -3])) assert y1 == y2 # u3 = 3 => y3 = -2 y2 + y1 + u3 = 6 + 1 + 3 = 10 # u3 = 3 => y3 = [1 -2] [1; -3] + [1] 3 = 1 + 6 + 3 = 10 # x4 = [0 1; 1 -2] [1; -3] + [0; 1] 3 # = [-3; 7] + [0; 3] = [-3; 10] y1 = sys.update(3) y2 = sysss.update([3]) #print(y1, y2) assert y1 == 10 assert np.all(sysss.state == np.array([-3, 10])) assert y1 == y2 # u4 = 0 => y4 = -2 y3 + y2 + u4 = - 20 - 3 + 0 = -23 # u4 = 0 => y4 = [1 -2] [-3; 10] + [1] 0 = -3 -20 = -23 # x5 = [0 1; 1 -2] [-3; 10] + [0; 1] 0 # = [10; -23] y1 = sys.update(0) y2 = sysss.update([0]) #print(y1, y2) assert y1 == -23 assert np.all(sysss.state == np.array([10, -23])) assert y1 == y2 # vector input/output sysss = sys.as_DTSS() A = np.array([[0, 1], [1, -2]]) B = np.array([[0], [1]]) C = np.array([[1, -2]]) D = np.array([[1]]) assert np.all(A == sysss.A) assert np.all(B == sysss.B) assert np.all(C == sysss.C) assert np.all(D == sysss.D) #print('A =\n{}\nB =\n{}\nC =\n{}\nD =\n{}'.format(sysss.A, sysss.B, sysss.C, sysss.D)) # u1 = 1 => y1 = 1 y2 = sysss.update(np.array([1])) assert isinstance(y2, np.ndarray) and y2 == 1 # u2 = -1 => y2 = -2 y1 + u2 = -2 - 1 = -3 y2 = sysss.update([-1]) assert isinstance(y2, np.ndarray) and y2 == -3 # u3 = 3 => y3 = -2 y2 + y1 + u3 = 6 + 1 + 3 = 10 y2 = sysss.update(np.array([3])) assert isinstance(y2, np.ndarray) and y2 == 10 # u4 = 0 => y4 = -2 y3 + y2 + u4 = - 20 - 3 + 0 = -23 y2 = sysss.update([0]) assert isinstance(y2, np.ndarray) and y2 == -23 # SIMO A = np.array([[0, 1], [1, -2]]) B = np.array([[0], [1]]) C = np.array([[1, -2], [0, 1]]) D = np.array([[1], [0]]) sys = ss.DTSS(A, B, C, D) assert np.all(sys.A == A) assert np.all(sys.B == B) assert np.all(sys.C == C) assert np.all(sys.D == D) assert np.all(sys.state == np.zeros(2)) # u1 = 1 => y1 = 1 y2 = sys.update(np.array([1])) assert isinstance(y2, np.ndarray) and np.all(y2 == np.array([1, 0])) # u2 = -1 => y2 = -2 y1 + u2 = -2 - 1 = -3 y2 = sys.update([-1]) assert isinstance(y2, np.ndarray) and np.all(y2 == np.array([-3, 1])) # u3 = 3 => y3 = -2 y2 + y1 + u3 = 6 + 1 + 3 = 10 y2 = sys.update(np.array([3])) assert isinstance(y2, np.ndarray) and np.all(y2 == np.array([10, -3])) # u4 = 0 => y4 = -2 y3 + y2 + u4 = - 20 - 3 + 0 = -23 y2 = sys.update([0]) assert isinstance(y2, np.ndarray) and np.all(y2 == np.array([-23, 10])) # MIMO A = np.array([[0, 1], [1, -2]]) B = np.array([[1, -1], [1, 0]]) C = np.array([[1, -2], [0, 1]]) D = np.array([[1, 0], [-1, 1]]) sys = ss.DTSS(A, B, C, D) assert np.all(sys.A == A) assert np.all(sys.B == B) assert np.all(sys.C == C) assert np.all(sys.D == D) assert np.all(sys.state == np.zeros(2)) # u1 = 1 => y1 = 1 y2 = sys.update(np.array([1, 1])) assert np.all(sys.state == np.array([0, 1])) assert isinstance(y2, np.ndarray) and np.all(y2 == np.array([1, 0])) # u2 = -1 => y2 = -2 y1 + u2 = -2 - 1 = -3 y2 = sys.update(np.array([-1, 0])) assert np.all(sys.state == np.array([0, -3])) assert isinstance(y2, np.ndarray) and np.all(y2 == np.array([-3, 2])) # u3 = 3 => y3 = -2 y2 + y1 + u3 = 6 + 1 + 3 = 10 y2 = sys.update(np.array([3, -1])) assert np.all(sys.state == np.array([1, 9])) assert isinstance(y2, np.ndarray) and np.all(y2 == np.array([9, -7])) # u4 = 0 => y4 = -2 y3 + y2 + u4 = - 20 - 3 + 0 = -23 y2 = sys.update(np.array([2, 1])) assert np.all(sys.state == np.array([10, -15])) assert isinstance(y2, np.ndarray) and np.all(y2 == np.array([-15, 8]))