Exemplo n.º 1
0
    def test_import_export(self):

        sys = ctrl.tf([1], [100, 1])
        sim_duration = 2000
        time = np.zeros(sim_duration)
        i = 0
        while (i < sim_duration):
            time[i] = i
            i += 1
        _, output = ctrl.step_response(sys, time)
        m = ps.FsrModel(output, t=time)
        # testing our model with a test signal
        test_sgnl_len = int(2500)
        u = np.zeros(test_sgnl_len)
        for i in range(test_sgnl_len):
            u[i] = 1
        time = np.zeros(test_sgnl_len)
        for i in range(test_sgnl_len):
            time[i] = i
        _, comp, _ = ctrl.forced_response(sys, time, u)
        # 'rb' and 'wb' to avoid problems under Windows!
        with open("temp", "w") as fh:
            ps.export_csv(m, fh)
        with open("temp", "r") as fh:
            m = ps.import_csv(fh)
        os.remove("temp")
        _, y = ps.forced_response(m, time, u)
        self.assertTrue(np.allclose(y, comp, rtol=1e-2), "import/export broke")
Exemplo n.º 2
0
def creation_example():
    """Example for creation of a FSR model with python-control."""

    # creating a step response for our model
    sys = ctrl.tf([1], [100, 1])
    sim_duration = 2000
    time = np.arange(sim_duration)
    _, output = ctrl.step_response(sys, time)
    # creating the model
    model = ps.FsrModel(output, t=time)
    # creating a test input signal
    u = np.ones(sim_duration)
    for i in range(len(u)):
        if i < 500:
            u[i] = 0.001 * i
    # getting response from our model
    t1, y1 = ps.forced_response(model, time, u)
    # simulating a reference with control
    t2, y2, _ = ctrl.forced_response(sys, time, u)
    # show everything
    ax = plt.subplot(111)
    plt.plot(t1, y1, label="Model response")
    plt.plot(t2, y2, label="Reference response")
    plt.plot(t1, u, label="Input signal")
    ax.legend()
    plt.title("pystrem example")
    plt.show()
Exemplo n.º 3
0
    def test_IT2_forced_response(self):

        sys = ctrl.tf([100], [50, 1000, 150, 0])  # IT2
        _, o = ctrl.step_response(sys, self.time)
        m = ps.FsrModel(o, t=self.time, optimize=False)
        _, y = ps.forced_response(m, self.t, self.u)
        _, o, _ = ctrl.forced_response(sys, self.t, self.u)
        self.assertTrue(np.allclose(y, o, rtol=1e-2), "it2 response broke")
Exemplo n.º 4
0
    def test_PT2_forced_response(self):

        sys = ctrl.tf([1], [1000, 10, 1])  # PT2 with overshooting
        _, o = ctrl.step_response(sys, self.time)
        m = ps.FsrModel(o, t=self.time)
        _, y = ps.forced_response(m, self.t, self.u)
        _, o, _ = ctrl.forced_response(sys, self.t, self.u)
        self.assertTrue(np.allclose(y, o, rtol=1e-2), "pt2 response broke")
Exemplo n.º 5
0
    def test_all_pass_forced_response(self):

        sys = ctrl.tf([-50, 1], [1000, 10, 1])  # all-pass
        _, o = ctrl.step_response(sys, self.time)
        m = ps.FsrModel(o, t=self.time)
        _, y = ps.forced_response(m, self.t, self.u)
        _, o, _ = ctrl.forced_response(sys, self.t, self.u)
        self.assertTrue(np.allclose(y, o, rtol=1e-1),
                        "all-pass response broke")
Exemplo n.º 6
0
 def test_crazy_system_forced_response(self):
     # numerator: (s+50)(s-2)((s+8)²+45)(s²+100))
     # denominator: (s+2)(s+3)(s+3)(s+4)((s+1.5)²+5)((s+3)²+2)((s+0.5)²+3)
     #              ((s+4)²+30)
     sys = ctrl.tf([1, 64, 877, 10032, 66800, 363200, -1090000], [
         1, 30, 443.5, 4140, 26677.6, 124311, 430648, 1.12577e6, 2.22475e6,
         3.28564e6, 3.49963e6, 2.45298e6, 858429
     ])
     time = np.arange(0, 60, 0.01)
     u = np.ones(len(time))
     for i in range(len(u)):
         if i < 25 / 0.01:
             u[i] = 0.5
     _, o = ctrl.step_response(sys, time)
     m = ps.FsrModel(o, time)
     _, y = ps.forced_response(m, time, u)
     _, o, _ = ctrl.forced_response(sys, time, u)
     self.assertTrue(np.allclose(y, o, rtol=1e-2), "crazy response broke")