forked from forgi86/sysid-transformers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlti.py
More file actions
149 lines (128 loc) · 5.06 KB
/
lti.py
File metadata and controls
149 lines (128 loc) · 5.06 KB
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
import math
from numpy import zeros, empty, cos, sin, any, copy
from numpy.linalg import solve, LinAlgError
from numpy.random import rand, randn, uniform, default_rng
from numba import float32, float64, jit, NumbaPerformanceWarning
import warnings
warnings.simplefilter('ignore', category=NumbaPerformanceWarning)
def drss_matrices(
states, inputs, outputs, strictly_proper=False, mag_range=(0.5, 0.97), phase_range=(0, math.pi / 2),
dtype="float64", rng=None):
"""Generate a random state space.
This does the actual random state space generation expected from rss and
drss. cdtype is 'c' for continuous systems and 'd' for discrete systems.
"""
if rng is None:
rng = default_rng(None)
# Probability of repeating a previous root.
pRepeat = 0.05
# Probability of choosing a real root. Note that when choosing a complex
# root, the conjugate gets chosen as well. So the expected proportion of
# real roots is pReal / (pReal + 2 * (1 - pReal)).
pReal = 0.6
# Probability that an element in B or C will not be masked out.
pBCmask = 0.8
# Probability that an element in D will not be masked out.
pDmask = 0.3
# Probability that D = 0.
pDzero = 0.5
# Check for valid input arguments.
if states < 1 or states % 1:
raise ValueError("states must be a positive integer. states = %g." %
states)
if inputs < 1 or inputs % 1:
raise ValueError("inputs must be a positive integer. inputs = %g." %
inputs)
if outputs < 1 or outputs % 1:
raise ValueError("outputs must be a positive integer. outputs = %g." %
outputs)
# Make some poles for A. Preallocate a complex array.
poles = zeros(states) + zeros(states) * 0.j
i = 0
while i < states:
if rng.random() < pRepeat and i != 0 and i != states - 1:
# Small chance of copying poles, if we're not at the first or last
# element.
if poles[i - 1].imag == 0:
# Copy previous real pole.
poles[i] = poles[i - 1]
i += 1
else:
# Copy previous complex conjugate pair of poles.
poles[i:i + 2] = poles[i - 2:i]
i += 2
elif rng.random() < pReal or i == states - 1:
# No-oscillation pole.
#
poles[i] = rng.uniform(mag_range[0], mag_range[1], 1)
i += 1
else:
mag = rng.uniform(mag_range[0], mag_range[1], 1)
phase = rng.uniform(phase_range[0], phase_range[1], 1)
poles[i] = complex(mag * cos(phase), mag * sin(phase))
poles[i + 1] = complex(poles[i].real, -poles[i].imag)
i += 2
# Now put the poles in A as real blocks on the diagonal.
A = zeros((states, states))
i = 0
while i < states:
if poles[i].imag == 0:
A[i, i] = poles[i].real
i += 1
else:
A[i, i] = A[i + 1, i + 1] = poles[i].real
A[i, i + 1] = poles[i].imag
A[i + 1, i] = -poles[i].imag
i += 2
# Finally, apply a transformation so that A is not block-diagonal.
while True:
T = rng.normal(size=(states, states))
try:
A = solve(T, A) @ T # A = T \ A @ T
break
except LinAlgError:
# In the unlikely event that T is rank-deficient, iterate again.
pass
# Make the remaining matrices.
B = rng.normal(size=(states, inputs))
C = rng.normal(size=(outputs, states))
D = rng.normal(size=(outputs, inputs))
# Make masks to zero out some of the elements.
while True:
Bmask = rng.random(size=(states, inputs)) < pBCmask
if any(Bmask): # Retry if we get all zeros.
break
while True:
Cmask = rng.random(size=(outputs, states)) < pBCmask
if any(Cmask): # Retry if we get all zeros.
break
if rng.random() < pDzero:
Dmask = zeros((outputs, inputs))
else:
Dmask = rng.random(size=(outputs, inputs)) < pDmask
# Apply masks.
B = B * Bmask
C = C * Cmask
D = D * Dmask if not strictly_proper else zeros(D.shape)
return A.astype(dtype), B.astype(dtype), C.astype(dtype), D.astype(dtype)
signatures = [
float32[:, :](float32[:, :], float32[:, :], float32[:, :], float32[:, :], float32[:, :], float32[:]),
float64[:, :](float64[:, :], float64[:, :], float64[:, :], float64[:, :], float64[:, :], float64[:])
]
@jit(signatures, nopython=True, cache=True)
def _dlsim(A, B, C, D, u, x0):
seq_len = u.shape[0]
nx, nu = B.shape
ny, _ = C.shape
y = empty(shape=(seq_len, ny), dtype=u.dtype)
x_step = copy(x0) # x_step = zeros((nx,), dtype=u.dtype)
for idx in range(seq_len):
u_step = u[idx]
y[idx] = C.dot(x_step) + D.dot(u_step)
x_step = A.dot(x_step) + B.dot(u_step)
return y
def dlsim(A, B, C, D, u, x0=None):
if x0 is None:
nx = A.shape[0]
x0 = zeros((nx,), dtype=u.dtype)
return _dlsim(A, B, C, D, u, x0)