Skip to content

Commit 1d8ed79

Browse files
committed
Improved TransferFunction -> StateSpace conversion
1 parent 638e93a commit 1d8ed79

File tree

1 file changed

+23
-27
lines changed

1 file changed

+23
-27
lines changed

src/types/conversion.jl

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -84,38 +84,34 @@ function Base.convert(::Type{StateSpace}, G::TransferFunction{TE,<:SisoTf{T0}})
8484
convert(StateSpace{TE,T}, G)
8585
end
8686

87-
8887
function Base.convert(::Type{StateSpace{TE,T}}, G::TransferFunction) where {TE,T<:Number}
8988
if !isproper(G)
9089
error("System is improper, a state-space representation is impossible")
9190
end
9291

93-
# TODO : These are added due to scoped for blocks, but is a hack. This
94-
# could be much cleaner.
95-
#T = Base.promote_op(/, T0, T0)
96-
97-
Ac = Bc = Cc = Dc = A = B = C = D = Matrix{T}(undef, 0, 0)
98-
for i=1:ninputs(G)
99-
for j=1:noutputs(G)
100-
a, b, c, d = siso_tf_to_ss(T, G.matrix[j, i])
101-
if j > 1
102-
# vcat
103-
Ac = blockdiag(Ac, a)
104-
Bc = vcat(Bc, b)
105-
Cc = blockdiag(Cc, c)
106-
Dc = vcat(Dc, d)
107-
else
108-
Ac, Bc, Cc, Dc = a, b, c, d
109-
end
110-
end
111-
if i > 1
112-
# hcat
113-
A = blockdiag(A, Ac)
114-
B = blockdiag(B, Bc)
115-
C = hcat(C, Cc)
116-
D = hcat(D, Dc)
117-
else
118-
A, B, C, D = Ac, Bc, Cc, Dc
92+
ny, nu = size(G)
93+
94+
# A, B, C, D matrices for each element of the transfer function matrix
95+
abcd_vec = [siso_tf_to_ss(T, g) for g in G.matrix[:]]
96+
97+
# Number of states for each transfer function element realization
98+
nvec = [size(abcd[1], 1) for abcd in abcd_vec]
99+
ntot = sum(nvec)
100+
101+
A = zeros(T, (ntot, ntot))
102+
B = zeros(T, (ntot, nu))
103+
C = zeros(T, (ny, ntot))
104+
D = zeros(T, (ny, nu))
105+
106+
inds = -1:0
107+
for j=1:nu
108+
for i=1:ny
109+
k = (j-1)*ny + i
110+
111+
# states correpsonding to the transfer function element (i,j)
112+
inds = (inds.stop+1):(inds.stop+nvec[k])
113+
114+
A[inds,inds], B[inds,j:j], C[i:i,inds], D[i:i,j:j] = abcd_vec[k]
119115
end
120116
end
121117
# A, B, C = balance_statespace(A, B, C)[1:3] NOTE: Use balance?

0 commit comments

Comments
 (0)