-
Notifications
You must be signed in to change notification settings - Fork 0
/
balreal.py
149 lines (108 loc) · 3.91 KB
/
balreal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Balanced realisaton of a stable LTI state-space system.
@author: Arnfinn Eielsen
@date: 06.03.2024
@license: BSD 3-Clause
"""
import numpy as np
from scipy import linalg
def balreal(A, B, C, D):
"""
Straight forward implementation of a Gramian-based balanced realisation
using SciPy linear algebra library.
This is for discrete time systems.
[1] A. Laub, M. Heath, C. Paige, and R. Ward,
‘Computation of System Balancing Transformations and Other Applications of Simultaneous Diagonalization Algorithms’,
IEEE Transactions on Automatic Control, vol. AC-32, no. 2, pp. 115–122, Feb. 1987.
"""
Wr = linalg.solve_discrete_lyapunov(A, [email protected])
Wo = linalg.solve_discrete_lyapunov(A.T, C.T@C)#, method=None)
Lr = linalg.cholesky(Wr, lower=True)
Lo = linalg.cholesky(Wo, lower=True)
U, s, Vh = linalg.svd(Lo.T@Lr)
S = linalg.diagsvd(s, A.shape[0], A.shape[1])
T = [email protected]@linalg.sqrtm(linalg.inv(S))
A_ = linalg.inv(T)@A@T
B_ = linalg.inv(T)@B
C_ = C@T
D_ = D
return A_, B_, C_, D_
def balreal_ct(A, B, C, D):
"""
Straight forward implementation of a Gramian-based balanced realisation
using SciPy linear algebra library.
This is for continuous time systems.
[1] A. Laub, M. Heath, C. Paige, and R. Ward,
‘Computation of System Balancing Transformations and Other Applications of Simultaneous Diagonalization Algorithms’,
IEEE Transactions on Automatic Control, vol. AC-32, no. 2, pp. 115–122, Feb. 1987.
"""
Wr = linalg.solve_continuous_lyapunov(A, [email protected])
Wo = linalg.solve_continuous_lyapunov(A.T, -C.T@C)
Lr = linalg.cholesky(Wr, lower=True)
Lo = linalg.cholesky(Wo, lower=True)
U, s, Vh = linalg.svd(Lo.T@Lr)
S = linalg.diagsvd(s, A.shape[0], A.shape[1])
T = [email protected]@linalg.sqrtm(linalg.inv(S))
A_ = linalg.inv(T)@A@T
B_ = linalg.inv(T)@B
C_ = C@T
D_ = D
return A_, B_, C_, D_
def main():
"""
Test the method.
"""
from scipy import signal
import control as ct
from matplotlib import pyplot as plt
match 2:
case 1:
if False:
sys = ct.drss(4, outputs=1, inputs=1) # random, stable LTI system
else:
b, a = signal.butter(3, 0.25, 'lowpass', analog=False)
Wlp = signal.dlti(b, a, dt=1) # filter LTI system instance
sys = Wlp.to_ss()
A = sys.A
B = sys.B
C = sys.C
D = sys.D
A_, B_, C_, D_ = balreal(A,B,C,D)
sys_init = signal.dlti(A, B, C, D, dt=1)
sys_bal = signal.dlti(A_, B_, C_, D_, dt=1)
ti, yi = signal.dlti.step(sys_init)
tb, yb = signal.dlti.step(sys_bal)
yi = yi[0]
yb = yb[0]
case 2:
if False:
sys = ct.rss(4, outputs=1, inputs=1) # random, stable LTI system
A = sys.A
B = sys.B
C = sys.C
D = sys.D
else:
Wn = 2*np.pi*1000e3
b, a = signal.butter(3, 1, 'lowpass', analog=True)
Wlp = signal.lti(b, a) # filter LTI system instance
sys = Wlp.to_ss()
A = sys.A
B = sys.B
C = sys.C
D = sys.D
A = Wn*A # scale to get correct cut-off
B = Wn*B
A_, B_, C_, D_ = balreal_ct(A,B,C,D)
sys_init = signal.lti(A, B, C, D)
sys_bal = signal.lti(A_, B_, C_, D_)
ti, yi = signal.lti.step(sys_init)
tb, yb = signal.lti.step(sys_bal)
plt.plot(ti, yi)
plt.plot(tb, yb)
plt.xlabel('Time [s]')
plt.ylabel('Amplitude')
plt.title('Compare realisations')
plt.grid()
if __name__ == "__main__":
main()