コード例 #1
0
ファイル: minreal_test.py プロジェクト: VivekMahtani/MCURE
    def testMinrealBrute(self):
        for n, m, p in permutations(range(1, 6), 3):
            s = matlab.rss(n, p, m)
            sr = s.minreal()
            if s.states > sr.states:
                self.nreductions += 1
            else:
                # Check to make sure that poles and zeros match

                # For poles, just look at eigenvalues of A
                np.testing.assert_array_almost_equal(np.sort(eigvals(s.A)),
                                                     np.sort(eigvals(sr.A)))

                # For zeros, need to extract SISO systems
                for i in range(m):
                    for j in range(p):
                        # Extract SISO dynamixs from input i to output j
                        s1 = matlab.ss(s.A, s.B[:, i], s.C[j, :], s.D[j, i])
                        s2 = matlab.ss(sr.A, sr.B[:, i], sr.C[j, :], sr.D[j,
                                                                          i])

                        # Check that the zeros match
                        # Note: sorting doesn't work => have to do the hard way
                        z1 = matlab.zero(s1)
                        z2 = matlab.zero(s2)

                        # Start by making sure we have the same # of zeros
                        self.assertEqual(len(z1), len(z2))

                        # Make sure all zeros in s1 are in s2
                        for zero in z1:
                            # Find the closest zero
                            self.assertAlmostEqual(min(abs(z2 - zero)), 0.)

                        # Make sure all zeros in s2 are in s1
                        for zero in z2:
                            # Find the closest zero
                            self.assertAlmostEqual(min(abs(z1 - zero)), 0.)

        # Make sure that the number of systems reduced is as expected
        # (Need to update this number if you change the seed at top of file)
        self.assertEqual(self.nreductions, 2)
コード例 #2
0
    def testMinrealBrute(self):
        for n, m, p in permutations(range(1,6), 3):
            s = matlab.rss(n, p, m)
            sr = s.minreal()
            if s.states > sr.states:
                self.nreductions += 1
            else:
                # Check to make sure that poles and zeros match

                # For poles, just look at eigenvalues of A
                np.testing.assert_array_almost_equal(
                    np.sort(eigvals(s.A)), np.sort(eigvals(sr.A)))

                # For zeros, need to extract SISO systems
                for i in range(m):
                    for j in range(p):
                        # Extract SISO dynamixs from input i to output j
                        s1 = matlab.ss(s.A, s.B[:,i], s.C[j,:], s.D[j,i])
                        s2 = matlab.ss(sr.A, sr.B[:,i], sr.C[j,:], sr.D[j,i])

                        # Check that the zeros match
                        # Note: sorting doesn't work => have to do the hard way
                        z1 = matlab.zero(s1)
                        z2 = matlab.zero(s2)

                        # Start by making sure we have the same # of zeros
                        self.assertEqual(len(z1), len(z2))

                        # Make sure all zeros in s1 are in s2
                        for zero in z1:
                            # Find the closest zero
                            self.assertAlmostEqual(min(abs(z2 - zero)), 0.)

                        # Make sure all zeros in s2 are in s1
                        for zero in z2:
                            # Find the closest zero
                            self.assertAlmostEqual(min(abs(z1 - zero)), 0.)

        # Make sure that the number of systems reduced is as expected
        # (Need to update this number if you change the seed at top of file)
        self.assertEqual(self.nreductions, 2)
コード例 #3
0
 def testPoleZero(self, siso):
     """Call pole() and zero()"""
     pole(siso.ss1)
     pole(siso.tf1)
     pole(siso.tf2)
     zero(siso.ss1)
     zero(siso.tf1)
     zero(siso.tf2)
#
GHp1 = Hp1 * Gp
GHp2 = Hp2 * Gp
GHp3 = Hp3 * Gp
print('\nFUNCAO DE TRANSFERENCIA DE MALHA ABERTA')
print('GHp1 = ', GHp1)
print('GHp2 = ', GHp2)
print('GHp3 = ', GHp3)
#
# Polos e zeros de malha aberta
#
print('\nPOLOS E ZEROS DE MALHA ABERTA')
print('-------------')
print('POLOS E ZEROS - GHp1')
print('Polos = ', co.pole(GHp1))
print('Zeros = ', co.zero(GHp1))
print('-------------')
print('POLOS E ZEROS - GHp2')
print('Polos = ', co.pole(GHp2))
print('Zeros = ', co.zero(GHp2))
print('-------------')
print('POLOS E ZEROS - GHp3')
print('Polos = ', co.pole(GHp3))
print('Zeros = ', co.zero(GHp3))
#
# definicao da malha fechada
# realimentacao unitaria
#
# Funcao de transferencia em malha fechada
# pode ser definida em Matlab utilizando-se
# O comando feedback(S1,S2)
コード例 #5
0
def get_PZ(TF):
    poles = mt.pole(TF)
    zeros = mt.zero(TF)
    return zeros, poles
コード例 #6
0
    def do_sys_id(self):
        num_poles = 2
        num_zeros = 1
        if not self._use_subspace:
            method = 'ARMAX'
            #sys_id = system_identification(self._y.T, self._u[0], method, IC='BIC', na_ord=[0, 5], nb_ord=[1, 5], nc_ord=[0, 5], delays=[0, 5], ARMAX_max_iterations=300, tsample=self._Ts, centering='MeanVal')
            sys_id = system_identification(self._y.T,
                                           self._u[0],
                                           method,
                                           ARMAX_orders=[num_poles, 1, 1, 0],
                                           ARMAX_max_iterations=300,
                                           tsample=self._Ts,
                                           centering='MeanVal')

            print(sys_id.G)

            if self._verbose:
                print(sys_id.G)
                print("System poles of discrete G: ", cnt.pole(sys_id.G))

            # Convert to continuous tf
            G = harold.Transfer(sys_id.NUMERATOR,
                                sys_id.DENOMINATOR,
                                dt=self._Ts)
            G_cont = harold.undiscretize(G, method='zoh')
            self._sys_tf = G_cont
            self._A, self._B, self._C, self._D = harold.transfer_to_state(
                G_cont, output='matrices')

            if self._verbose:
                print("Continuous tf:", G_cont)

            # Convert to state space, because ARMAX gives transfer function
            ss_roll = cnt.tf2ss(sys_id.G)
            A = np.asarray(ss_roll.A)
            B = np.asarray(ss_roll.B)
            C = np.asarray(ss_roll.C)
            D = np.asarray(ss_roll.D)
            if self._verbose:
                print(ss_roll)

            # simulate identified system using input from data
            xid, yid = fsetSIM.SS_lsim_process_form(A, B, C, D, self._u)
            y_error = self._y - yid
            self._fitness = 1 - (y_error.var() / self._y.var())**2
            if self._verbose:
                print("Fittness %", self._fitness * 100)

            if self._plot:
                plt.figure(1)
                plt.plot(self._t[0], self._y[0])
                plt.plot(self._t[0], yid[0])
                plt.xlabel("Time")
                plt.title("Time response Y(t)=U*G(t)")
                plt.legend([
                    self._y_name, self._y_name + '_identified: ' +
                    '{:.3f} fitness'.format(self._fitness)
                ])
                plt.grid()
                plt.show()

        else:
            sys_id = system_identification(self._y,
                                           self._u,
                                           self._subspace_method,
                                           SS_fixed_order=num_poles,
                                           SS_p=self._subspace_p,
                                           SS_f=50,
                                           tsample=self._Ts,
                                           SS_A_stability=True,
                                           centering='MeanVal')
            #sys_id = system_identification(self._y, self._u, self._subspace_method, SS_orders=[1,10], SS_p=self._subspace_p, SS_f=50, tsample=self._Ts, SS_A_stability=True, centering='MeanVal')
            if self._verbose:
                print("x0", sys_id.x0)
                print("A", sys_id.A)
                print("B", sys_id.B)
                print("C", sys_id.C)
                print("D", sys_id.D)

            A = sys_id.A
            B = sys_id.B
            C = sys_id.C
            D = sys_id.D

            # Get discrete transfer function from state space
            sys_tf = cnt.ss2tf(A, B, C, D)
            if self._verbose:
                print("TF ***in z domain***", sys_tf)

            # Get numerator and denominator
            (num, den) = cnt.tfdata(sys_tf)

            # Convert to continuous tf
            G = harold.Transfer(num, den, dt=self._Ts)
            if self._verbose:
                print(G)
            G_cont = harold.undiscretize(G, method='zoh')
            self._sys_tf = G_cont
            self._A, self._B, self._C, self._D = harold.transfer_to_state(
                G_cont, output='matrices')
            if self._verbose:
                print("Continuous tf:", G_cont)

            # get zeros
            tmp_tf = cnt.ss2tf(self._A, self._B, self._C, self._D)
            self._zeros = cnt.zero(tmp_tf)

            # simulate identified system using discrete system
            xid, yid = fsetSIM.SS_lsim_process_form(A, B, C, D, self._u,
                                                    sys_id.x0)
            y_error = self._y - yid
            self._fitness = 1 - (y_error.var() / self._y.var())**2
            if self._verbose:
                print("Fittness %", self._fitness * 100)

            if self._plot:
                plt.figure(1)
                plt.plot(self._t[0], self._y[0])
                plt.plot(self._t[0], yid[0])
                plt.xlabel("Time")
                plt.title("Time response Y(t)=U*G(t)")
                plt.legend([
                    self._y_name, self._y_name + '_identified: ' +
                    '{:.3f} fitness'.format(self._fitness)
                ])
                plt.grid()
                plt.show()
コード例 #7
0
import control.matlab as ctl
import matplotlib.pyplot as plt
import numpy as np

num = np.array([1, 1])
den = np.array([3, 9, 0, 0])

GH = ctl.tf(num, den)
print(GH)
print("Zeros: ", ctl.zero(GH))
print("Plos: ", ctl.pole(GH))

rlist, klist = ctl.rlocus(GH, PrintGain=True, grid=True)
plt.grid()
print(plt.show())
コード例 #8
0
import numpy as np
import control.matlab as ml
import matplotlib.pyplot as plt

# If using termux
import subprocess
import shlex
#end if

# num is the numerator of the trasfer function which is (2s)
# dem is the denominator of the transfer function which is (0.25s + 1)(0.25s + 1)(0.5s + 1)
num = np.array([2, 0])
den = np.polymul(np.array([0.5, 1]), np.array([0.25, 1]))
den = np.polymul(den, np.array([0.25, 1]))

# Generating the transfer function
g = ml.tf(num, den)
print("The transfer function is: ", g)
print("The poles of the above function are ", ml.pole(g))
print("The zeros of the above function are ", ml.zero(g))

# Generating the bode plot as well as plotting it
mag, phase, w = ml.bode(g)

# If using termux
plt.savefig("./figs/ep18btech11016_plot.pdf")
plt.savefig("./figs/ep18btech11016_plot.eps")
subprocess.run(shlex.split("termux-open ./figs/ep18btech11016_plot.pdf"))
# else
plt.show()
コード例 #9
0
else:
    H3 = Kp3 * (1 + (1 / (Ti3 * s)) + (Td3 * s / ((Td3 / N3) * (s) + 1)))
Hw3 = Ktac * H3
#%
#% Definicao da malha aberta
#% Sao definidos 3 sistemas distintos
#%
GHw1 = Hw1 * Gw
GHw2 = Hw2 * Gw
GHw3 = Hw3 * Gw
#%
#% Polos e zeros de malha aberta
#%
print('Polos e zeros - GHw1')
print(co.pole(GHw1))
print(co.zero(GHw1))
print('\nPolos e zeros - GHw2')
print(co.pole(GHw2))
print(co.zero(GHw2))
print('\nPolos e zeros - GHw3')
print(co.pole(GHw3))
print(co.zero(GHw3))
#%
#% Plot com os polos e zeros de malha aberta
#%

co.pzmap(GHw1, title="GHw1")
co.pzmap(GHw2, title="GHw2")
co.pzmap(GHw3, title="GHw3")

#plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=0.45)
コード例 #10
0
    H3 = Kp3*(1+(1/(Ti3*s))+(Td3*s/((Td3/N3)*(s)+1)))
Hp3 = Kpot*H3

#%
#% Definicao da malha aberta
#% Sao definidos 3 sistemas distintos
#%
GHp1 = Hp1*Gp
GHp2 = Hp2*Gp
GHp3 = Hp3*Gp
#%
#% Polos e zeros de maplha aberta
#%
print('Polos e zeros - GHp1')
print(co.pole(GHp1))
print(co.zero(GHp1))
print('\nPolos e zeros - GHp2')
print(co.pole(GHp2))
print(co.zero(GHp2))
print('\nPolos e zeros - GHp3')
print(co.pole(GHp3))
print(co.zero(GHp3))
#%
#% Plot com os polos e zeros de malha aberta
#%

co.pzmap(GHp1, title = "GHp1")
co.pzmap(GHp2,title = "GHp2")
co.pzmap(GHp3, title = "GHp3")

#figure(1);
コード例 #11
0
def zero(G):
    return co.zero(G)