def setUp(self):
        """Set up some systems for testing out MATLAB functions"""
        A = np.matrix("1. -2.; 3. -4.")
        B = np.matrix("5.; 7.")
        C = np.matrix("6. 8.")
        D = np.matrix("9.")
        self.siso_ss1 = StateSpace(A, B, C, D)

        # Create some transfer functions
        self.siso_tf1 = TransferFunction([1], [1, 2, 1])
        self.siso_tf2 = _convert_to_transfer_function(self.siso_ss1)

        # Create MIMO system, contains ``siso_ss1`` twice
        A = np.matrix("1. -2. 0.  0.;"
                      "3. -4. 0.  0.;"
                      "0.  0. 1. -2.;"
                      "0.  0. 3. -4. ")
        B = np.matrix("5. 0.;" "7. 0.;" "0. 5.;" "0. 7. ")
        C = np.matrix("6. 8. 0. 0.;" "0. 0. 6. 8. ")
        D = np.matrix("9. 0.;" "0. 9. ")
        self.mimo_ss1 = StateSpace(A, B, C, D)

        # Create discrete time systems
        self.siso_dtf1 = TransferFunction([1], [1, 1, 0.25], True)
        self.siso_dtf2 = TransferFunction([1], [1, 1, 0.25], 0.2)
        self.siso_dss1 = tf2ss(self.siso_dtf1)
        self.siso_dss2 = tf2ss(self.siso_dtf2)
        self.mimo_dss1 = StateSpace(A, B, C, D, True)
        self.mimo_dss2 = c2d(self.mimo_ss1, 0.2)
    def setUp(self):
        """Set up some systems for testing out MATLAB functions"""
        A = np.matrix("1. -2.; 3. -4.")
        B = np.matrix("5.; 7.")
        C = np.matrix("6. 8.")
        D = np.matrix("9.")
        self.siso_ss1 = StateSpace(A, B, C, D)

        # Create some transfer functions
        self.siso_tf1 = TransferFunction([1], [1, 2, 1])
        self.siso_tf2 = _convert_to_transfer_function(self.siso_ss1)

        # Create MIMO system, contains ``siso_ss1`` twice
        A = np.matrix("1. -2. 0.  0.;"
                      "3. -4. 0.  0.;"
                      "0.  0. 1. -2.;"
                      "0.  0. 3. -4. ")
        B = np.matrix("5. 0.;"
                      "7. 0.;"
                      "0. 5.;"
                      "0. 7. ")
        C = np.matrix("6. 8. 0. 0.;"
                      "0. 0. 6. 8. ")
        D = np.matrix("9. 0.;"
                      "0. 9. ")
        self.mimo_ss1 = StateSpace(A, B, C, D)

        # Create discrete time systems
        self.siso_dtf1 = TransferFunction([1], [1, 1, 0.25], True)
        self.siso_dtf2 = TransferFunction([1], [1, 1, 0.25], 0.2)
        self.siso_dss1 = tf2ss(self.siso_dtf1)
        self.siso_dss2 = tf2ss(self.siso_dtf2)
        self.mimo_dss1 = StateSpace(A, B, C, D, True)
        self.mimo_dss2 = c2d(self.mimo_ss1, 0.2)
    def test_convert_to_transfer_function(self):
        """Test for correct state space to transfer function conversion."""
        A = [[1., -2.], [-3., 4.]]
        B = [[6., 5.], [4., 3.]]
        C = [[1., -2.], [3., -4.], [5., -6.]]
        D = [[1., 0.], [0., 1.], [1., 0.]]
        sys = StateSpace(A, B, C, D)

        tfsys = _convert_to_transfer_function(sys)

        num = [[np.array([1., -7., 10.]),
                np.array([-1., 10.])],
               [np.array([2., -8.]),
                np.array([1., -2., -8.])],
               [np.array([1., 1., -30.]),
                np.array([7., -22.])]]
        den = [[np.array([1., -5., -2.]) for _ in range(sys.ninputs)]
               for _ in range(sys.noutputs)]

        for i in range(sys.noutputs):
            for j in range(sys.ninputs):
                np.testing.assert_array_almost_equal(tfsys.num[i][j],
                                                     num[i][j])
                np.testing.assert_array_almost_equal(tfsys.den[i][j],
                                                     den[i][j])
 def test_state_space_conversion_mimo(self):
     """Test conversion of a single input, two-output state-space
     system against the same TF"""
     s = TransferFunction([1, 0], [1])
     b0 = 0.2
     b1 = 0.1
     b2 = 0.5
     a0 = 2.3
     a1 = 6.3
     a2 = 3.6
     a3 = 1.0
     h = (b0 + b1*s + b2*s**2)/(a0 + a1*s + a2*s**2 + a3*s**3)
     H = TransferFunction([[h.num[0][0]], [(h*s).num[0][0]]],
                          [[h.den[0][0]], [h.den[0][0]]])
     sys = _convert_to_statespace(H)
     H2 = _convert_to_transfer_function(sys)
     np.testing.assert_array_almost_equal(H.num[0][0], H2.num[0][0])
     np.testing.assert_array_almost_equal(H.den[0][0], H2.den[0][0])
     np.testing.assert_array_almost_equal(H.num[1][0], H2.num[1][0])
     np.testing.assert_array_almost_equal(H.den[1][0], H2.den[1][0])
 def test_state_space_conversion_mimo(self):
     """Test conversion of a single input, two-output state-space
     system against the same TF"""
     s = TransferFunction([1, 0], [1])
     b0 = 0.2
     b1 = 0.1
     b2 = 0.5
     a0 = 2.3
     a1 = 6.3
     a2 = 3.6
     a3 = 1.0
     h = (b0 + b1*s + b2*s**2)/(a0 + a1*s + a2*s**2 + a3*s**3)
     H = TransferFunction([[h.num[0][0]], [(h*s).num[0][0]]],
                          [[h.den[0][0]], [h.den[0][0]]])
     sys = _convertToStateSpace(H)
     H2 = _convert_to_transfer_function(sys)
     np.testing.assert_array_almost_equal(H.num[0][0], H2.num[0][0])
     np.testing.assert_array_almost_equal(H.den[0][0], H2.den[0][0])
     np.testing.assert_array_almost_equal(H.num[1][0], H2.num[1][0])
     np.testing.assert_array_almost_equal(H.den[1][0], H2.den[1][0])
    def test_convert_to_transfer_function(self):
        """Test for correct state space to transfer function conversion."""

        A = [[1., -2.], [-3., 4.]]
        B = [[6., 5.], [4., 3.]]
        C = [[1., -2.], [3., -4.], [5., -6.]]
        D = [[1., 0.], [0., 1.], [1., 0.]]
        sys = StateSpace(A, B, C, D)

        tfsys = _convert_to_transfer_function(sys)

        num = [[np.array([1., -7., 10.]), np.array([-1., 10.])],
               [np.array([2., -8.]), np.array([1., -2., -8.])],
               [np.array([1., 1., -30.]), np.array([7., -22.])]]
        den = [[np.array([1., -5., -2.]) for _ in range(sys.inputs)]
               for _ in range(sys.outputs)]

        for i in range(sys.outputs):
            for j in range(sys.inputs):
                np.testing.assert_array_almost_equal(tfsys.num[i][j], num[i][j])
                np.testing.assert_array_almost_equal(tfsys.den[i][j], den[i][j])