Ejemplo n.º 1
0
    def test_subspace_det_algo1_mimo(self):
        """
        Subspace deterministic algorithm (MIMO).
        """
        ss2 = sysid.StateSpaceDiscreteLinear(A=np.array([[0, 0.1, 0.2],
                                                         [0.2, 0.3, 0.4],
                                                         [0.4, 0.3, 0.2]]),
                                             B=np.array([[1, 0], [0, 1],
                                                         [0, -1]]),
                                             C=np.array([[1, 0, 0], [0, 1,
                                                                     0]]),
                                             D=np.array([[0, 0], [0, 0]]),
                                             Q=np.diag([0.01, 0.01, 0.01]),
                                             R=np.diag([0.01, 0.01]),
                                             dt=0.1)
        np.random.seed(1234)
        prbs1 = sysid.prbs(1000)
        prbs2 = sysid.prbs(1000)

        def f_prbs_2d(t, x, i):
            "input function"
            #pylint: disable=unused-argument
            i = i % 1000
            return 2 * np.array([[prbs1[i] - 0.5], [prbs2[i] - 0.5]])

        tf = 8
        data = ss2.simulate(f_u=f_prbs_2d, x0=np.array([[0, 0, 0]]).T, tf=tf)
        ss2_id = sysid.subspace_det_algo1(y=data.y,
                                          u=data.u,
                                          f=5,
                                          p=5,
                                          s_tol=0.1,
                                          dt=ss2.dt)
        data_id = ss2_id.simulate(
            f_u=f_prbs_2d,
            # x0=np.array(np.matrix(np.zeros(ss2_id.A.shape[0])).T),
            x0=np.array([np.zeros(ss2_id.A.shape[0])]).T,
            tf=tf)

        nrms = sysid.nrms(data_id.y, data.y)
        self.assertGreater(nrms, 0.9)

        if ENABLE_PLOTTING:
            for i in range(2):
                plt.figure()
                plt.plot(data_id.t.T,
                         data_id.y[i, :].T,
                         label='$y_{:d}$ true'.format(i))
                plt.plot(data.t.T,
                         data.y[i, :].T,
                         label='$y_{:d}$ id'.format(i))
                plt.legend()
                plt.grid()
Ejemplo n.º 2
0
    def test_subspace_det_algo1_siso(self):
        """
        Subspace deterministic algorithm (SISO).
        """
        ss1 = sysid.StateSpaceDiscreteLinear(
            A=0.9, B=0.5, C=1, D=0, Q=0.01, R=0.01, dt=0.1)

        pl.seed(1234)
        prbs1 = sysid.prbs(1000)
        def f_prbs(t, x, i):
            "input function"
            #pylint: disable=unused-argument, unused-variable
            return prbs1[i]

        tf = 10
        data = ss1.simulate(f_u=f_prbs, x0=pl.matrix(0), tf=tf)
        ss1_id = sysid.subspace_det_algo1(
            y=data.y, u=data.u,
            f=5, p=5, s_tol=1e-1, dt=ss1.dt)
        data_id = ss1_id.simulate(f_u=f_prbs, x0=0, tf=tf)
        nrms = sysid.subspace.nrms(data_id.y, data.y)
        self.assertGreater(nrms, 0.9)

        if ENABLE_PLOTTING:
            pl.plot(data_id.t.T, data_id.x.T, label='id')
            pl.plot(data.t.T, data.x.T, label='true')
            pl.legend()
            pl.grid()
Ejemplo n.º 3
0
    def test_subspace_det_algo1_mimo(self):
        """
        Subspace deterministic algorithm (MIMO).
        """
        ss2 = sysid.StateSpaceDiscreteLinear(
            A=pl.matrix([[0, 0.1, 0.2],
                         [0.2, 0.3, 0.4],
                         [0.4, 0.3, 0.2]]),
            B=pl.matrix([[1, 0],
                         [0, 1],
                         [0, -1]]),
            C=pl.matrix([[1, 0, 0],
                         [0, 1, 0]]),
            D=pl.matrix([[0, 0],
                         [0, 0]]),
            Q=pl.diag([0.01, 0.01, 0.01]), R=pl.diag([0.01, 0.01]), dt=0.1)
        pl.seed(1234)
        prbs1 = sysid.prbs(1000)
        prbs2 = sysid.prbs(1000)
        def f_prbs_2d(t, x, i):
            "input function"
            #pylint: disable=unused-argument
            i = i%1000
            return 2*pl.matrix([prbs1[i]-0.5, prbs2[i]-0.5]).T
        tf = 8
        data = ss2.simulate(
            f_u=f_prbs_2d, x0=pl.matrix([0, 0, 0]).T, tf=tf)
        ss2_id = sysid.subspace_det_algo1(
            y=data.y, u=data.u,
            f=5, p=5, s_tol=0.1, dt=ss2.dt)
        data_id = ss2_id.simulate(
            f_u=f_prbs_2d,
            x0=pl.matrix(pl.zeros(ss2_id.A.shape[0])).T, tf=tf)
        nrms = sysid.nrms(data_id.y, data.y)
        self.assertGreater(nrms, 0.9)

        if ENABLE_PLOTTING:
            for i in range(2):
                pl.figure()
                pl.plot(data_id.t.T, data_id.y[i, :].T,
                        label='$y_{:d}$ true'.format(i))
                pl.plot(data.t.T, data.y[i, :].T,
                        label='$y_{:d}$ id'.format(i))
                pl.legend()
                pl.grid()
Ejemplo n.º 4
0
    def test_subspace_det_algo1_siso(self):
        """
        Subspace deterministic algorithm (SISO).
        """
        ss1 = sysid.StateSpaceDiscreteLinear(A=0.9,
                                             B=0.5,
                                             C=1,
                                             D=0,
                                             Q=0.01,
                                             R=0.01,
                                             dt=0.1)

        np.random.seed(1234)
        prbs1 = sysid.prbs(1000)

        def f_prbs(t, x, i):
            "input function"
            # pylint: disable=unused-argument, unused-variable
            return prbs1[i]

        tf = 10
        data = ss1.simulate(f_u=f_prbs, x0=np.matrix(0), tf=tf)
        ss1_id = sysid.subspace_det_algo1(y=data.y,
                                          u=data.u,
                                          f=5,
                                          p=5,
                                          s_tol=1e-1,
                                          dt=ss1.dt)
        data_id = ss1_id.simulate(f_u=f_prbs, x0=0, tf=tf)
        nrms = sysid.subspace.nrms(data_id.y, data.y)
        self.assertGreater(nrms, 0.9)

        if ENABLE_PLOTTING:
            plt.plot(data_id.t.T, data_id.x.T, label='id')
            plt.plot(data.t.T, data.x.T, label='true')
            plt.legend()
            plt.grid()
simulation_example = False

if simulation_example:
    # 4 internal states MIMO [2 IN, 3 OUT]
    ss3 = sysid.StateSpaceDiscreteLinear(
        A=np.array([[0, 0.01, 0.2, 0.4], [0.1, 0.2, 0.2, 0.3], [0.11, 0.12, 0.21, 0.13], [0.11, 0.12, 0.21, 0.13]]),  # X x X
        B=np.array([[1, 0], [0, 1], [1, 0], [0, 1]]),  # X x u
        C=np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 1, 0, 0]]),  # y x x
        D=np.array([[0, 0], [0, 0], [0, 0]]),  # y x u
        Q=np.diag([0.2, 0.1, 0.1, 0.1]),  #  X x X
        R=np.diag([0.04, 0.04, 0.04]),  # R? y x y
        dt=0.1)

    np.random.seed(1234)
    prbs1 = sysid.prbs(1000)
    prbs2 = sysid.prbs(1000)
    prbs3 = sysid.prbs(1000)
    def f_prbs_3d(t, x, i):
        i = i%1000
        return 2 * np.array([[prbs1[i]-0.5], [prbs2[i]-0.5]])

    tf = 10
    data3 = ss3.simulate(
        f_u=f_prbs_3d,
        x0=np.array([[0, 0, 0, 0]]).T,
        tf=tf)

    print(data3.u.shape, data3.y.shape, "shapes")

    ss3_id = sysid.subspace_det_algo1(y=data3.y, u=data3.u,