|
1 |
| -# Base.convert(::Type{<:SisoTf}, b::Real) = Base.convert(SisoRational, b) |
2 |
| -# Base.convert{T<:Real}(::Type{<:SisoZpk}, b::T) = SisoZpk(T[], T[], b) |
3 |
| -# Base.convert{T<:Real}(::Type{<:SisoRational}, b::T) = SisoRational([b], [one(T)]) |
4 |
| -# Base.convert{T1}(::Type{SisoRational{Vector{T1}}}, t::SisoRational) = SisoRational(Polynomial(T1.(t.num.coeffs$1),Polynomial(T1.(t.den.coeffs$1)) |
5 |
| -# Base.convert(::Type{<:StateSpace}, t::Real) = ss(t) |
6 |
| -# |
7 |
| - |
8 |
| -# |
9 |
| -# function Base.convert{T<:AbstractMatrix{<:Number}}(::Type{StateSpace{T}}, s::StateSpace) |
10 |
| -# AT = promote_type(T, arraytype(s)) |
11 |
| -# StateSpace{AT}(AT(s.A),AT(s.B),AT(s.C),AT(s.D), s.timeevol, s.statenames, s.inputnames, s.outputnames) |
12 |
| -# end |
13 |
| - |
14 | 1 | # TODO Fix these to use proper constructors
|
15 | 2 | # NOTE: no real need to convert numbers to transfer functions, have addition methods..
|
16 | 3 | # How to convert a number to either Continuous or Discrete transfer function
|
@@ -42,19 +29,7 @@ Base.convert(::Type{TransferFunction{TE,SisoZpk{T,TR}}}, d::Number) where {TE, T
|
42 | 29 |
|
43 | 30 | Base.convert(::Type{StateSpace{TE,T}}, d::Number) where {TE, T} =
|
44 | 31 | convert(StateSpace{TE,T}, fill(d, (1,1)))
|
45 |
| -# |
46 |
| -# Base.convert(::Type{TransferFunction{Continuous,<:SisoRational{T}}}, b::Number) where {T} = tf(T(b), Continuous()) |
47 |
| -# Base.convert(::Type{TransferFunction{Continuous,<:SisoZpk{T, TR}}}, b::Number) where {T, TR} = zpk(T(b), Continuous()) |
48 |
| -# Base.convert(::Type{StateSpace{Continuous,T, MT}}, b::Number) where {T, MT} = convert(StateSpace{Continuous,T, MT}, fill(b, (1,1))) |
49 |
| - |
50 |
| -# function Base.convert{T<:Real,S<:TransferFunction}(::Type{S}, b::VecOrMat{T}) |
51 |
| -# r = Matrix{S}(size(b,2),1) |
52 |
| -# for j=1:size(b,2) |
53 |
| -# r[j] = vcat(map(k->convert(S,k),b[:,j])...) |
54 |
| -# end |
55 |
| -# hcat(r...) |
56 |
| -# end |
57 |
| -# |
| 32 | + |
58 | 33 |
|
59 | 34 | function convert(::Type{TransferFunction{TE,S}}, G::TransferFunction) where {TE,S}
|
60 | 35 | Gnew_matrix = convert.(S, G.matrix)
|
@@ -92,77 +67,85 @@ struct ImproperException <: Exception end
|
92 | 67 | Base.showerror(io::IO, e::ImproperException) = print(io, "System is improper, a state-space representation is impossible")
|
93 | 68 |
|
94 | 69 | # Note: balancing is only applied by default for floating point types, integer systems are not balanced since that would change the type.
|
95 |
| -function Base.convert(::Type{StateSpace{TE,T}}, G::TransferFunction; balance=!(T <: Union{Integer, Rational, ForwardDiff.Dual})) where {TE,T<:Number} |
96 |
| - if !isproper(G) |
97 |
| - throw(ImproperException()) |
98 |
| - end |
| 70 | +function Base.convert(::Type{StateSpace{TE,T}}, G::TransferFunction; balance=!(T <: Union{Integer, Rational, ForwardDiff.Dual}), minimal=false, contr=minimal, obs=minimal, noseig=minimal, kwargs...) where {TE,T<:Number} |
99 | 71 |
|
100 |
| - ny, nu = size(G) |
101 | 72 |
|
102 |
| - # A, B, C, D matrices for each element of the transfer function matrix |
103 |
| - abcd_vec = [siso_tf_to_ss(T, g) for g in G.matrix[:]] |
| 73 | + NUM = numpoly(G) |
| 74 | + DEN = denpoly(G) |
| 75 | + (A, E, B, C, D, blkdims) = MatrixPencils.rm2ls(NUM, DEN; contr, obs, noseig, minimal, kwargs...) |
| 76 | + E == I || throw(ImproperException()) |
104 | 77 |
|
105 |
| - # Number of states for each transfer function element realization |
106 |
| - nvec = [size(abcd[1], 1) for abcd in abcd_vec] |
107 |
| - ntot = sum(nvec) |
108 | 78 |
|
109 |
| - A = zeros(T, (ntot, ntot)) |
110 |
| - B = zeros(T, (ntot, nu)) |
111 |
| - C = zeros(T, (ny, ntot)) |
112 |
| - D = zeros(T, (ny, nu)) |
| 79 | + # if !isproper(G) |
| 80 | + # throw(ImproperException()) |
| 81 | + # end |
113 | 82 |
|
114 |
| - inds = -1:0 |
115 |
| - for j=1:nu |
116 |
| - for i=1:ny |
117 |
| - k = (j-1)*ny + i |
| 83 | + # ny, nu = size(G) |
118 | 84 |
|
119 |
| - # states corresponding to the transfer function element (i,j) |
120 |
| - inds = (inds.stop+1):(inds.stop+nvec[k]) |
| 85 | + # # A, B, C, D matrices for each element of the transfer function matrix |
| 86 | + # abcd_vec = [siso_tf_to_ss(T, g) for g in G.matrix[:]] |
121 | 87 |
|
122 |
| - A[inds,inds], B[inds,j:j], C[i:i,inds], D[i:i,j:j] = abcd_vec[k] |
123 |
| - end |
124 |
| - end |
| 88 | + # # Number of states for each transfer function element realization |
| 89 | + # nvec = [size(abcd[1], 1) for abcd in abcd_vec] |
| 90 | + # ntot = sum(nvec) |
| 91 | + |
| 92 | + # A = zeros(T, (ntot, ntot)) |
| 93 | + # B = zeros(T, (ntot, nu)) |
| 94 | + # C = zeros(T, (ny, ntot)) |
| 95 | + # D = zeros(T, (ny, nu)) |
| 96 | + |
| 97 | + # inds = -1:0 |
| 98 | + # for j=1:nu |
| 99 | + # for i=1:ny |
| 100 | + # k = (j-1)*ny + i |
| 101 | + |
| 102 | + # # states corresponding to the transfer function element (i,j) |
| 103 | + # inds = (inds.stop+1):(inds.stop+nvec[k]) |
| 104 | + |
| 105 | + # A[inds,inds], B[inds,j:j], C[i:i,inds], D[i:i,j:j] = abcd_vec[k] |
| 106 | + # end |
| 107 | + # end |
125 | 108 | if balance
|
126 | 109 | A, B, C = balance_statespace(A, B, C)
|
127 | 110 | end
|
128 | 111 | return StateSpace{TE,T}(A, B, C, D, TE(G.timeevol))
|
129 | 112 | end
|
130 | 113 |
|
131 |
| -siso_tf_to_ss(T::Type, f::SisoTf) = siso_tf_to_ss(T, convert(SisoRational, f)) |
| 114 | +# siso_tf_to_ss(T::Type, f::SisoTf) = siso_tf_to_ss(T, convert(SisoRational, f)) |
132 | 115 |
|
133 | 116 | # Conversion to statespace on controllable canonical form
|
134 |
| -function siso_tf_to_ss(T::Type, f::SisoRational) |
| 117 | +# function siso_tf_to_ss(T::Type, f::SisoRational) |
135 | 118 |
|
136 |
| - num0, den0 = numvec(f), denvec(f) |
137 |
| - # Normalize the numerator and denominator to allow realization of transfer functions |
138 |
| - # that are proper, but not strictly proper |
139 |
| - num = num0 ./ den0[1] |
140 |
| - den = den0 ./ den0[1] |
| 119 | +# num0, den0 = numvec(f), denvec(f) |
| 120 | +# # Normalize the numerator and denominator to allow realization of transfer functions |
| 121 | +# # that are proper, but not strictly proper |
| 122 | +# num = num0 ./ den0[1] |
| 123 | +# den = den0 ./ den0[1] |
141 | 124 |
|
142 |
| - N = length(den) - 1 # The order of the rational function f |
| 125 | +# N = length(den) - 1 # The order of the rational function f |
143 | 126 |
|
144 |
| - # Get numerator coefficient of the same order as the denominator |
145 |
| - bN = length(num) == N+1 ? num[1] : zero(eltype(num)) |
| 127 | +# # Get numerator coefficient of the same order as the denominator |
| 128 | +# bN = length(num) == N+1 ? num[1] : zero(eltype(num)) |
146 | 129 |
|
147 |
| - @views if N == 0 #|| num == zero(Polynomial{T}) |
148 |
| - A = zeros(T, 0, 0) |
149 |
| - B = zeros(T, 0, 1) |
150 |
| - C = zeros(T, 1, 0) |
151 |
| - else |
152 |
| - A = diagm(1 => ones(T, N-1)) |
153 |
| - A[end, :] .= .-reverse(den)[1:end-1] |
| 130 | +# @views if N == 0 #|| num == zero(Polynomial{T}) |
| 131 | +# A = zeros(T, 0, 0) |
| 132 | +# B = zeros(T, 0, 1) |
| 133 | +# C = zeros(T, 1, 0) |
| 134 | +# else |
| 135 | +# A = diagm(1 => ones(T, N-1)) |
| 136 | +# A[end, :] .= .-reverse(den)[1:end-1] |
154 | 137 |
|
155 |
| - B = zeros(T, N, 1) |
156 |
| - B[end] = one(T) |
| 138 | +# B = zeros(T, N, 1) |
| 139 | +# B[end] = one(T) |
157 | 140 |
|
158 |
| - C = zeros(T, 1, N) |
159 |
| - C[1:min(N, length(num))] = reverse(num)[1:min(N, length(num))] |
160 |
| - C[:] .-= bN .* reverse(den)[1:end-1] # Can index into polynomials at greater inddices than their length |
161 |
| - end |
162 |
| - D = fill(bN, 1, 1) |
| 141 | +# C = zeros(T, 1, N) |
| 142 | +# C[1:min(N, length(num))] = reverse(num)[1:min(N, length(num))] |
| 143 | +# C[:] .-= bN .* reverse(den)[1:end-1] # Can index into polynomials at greater inddices than their length |
| 144 | +# end |
| 145 | +# D = fill(bN, 1, 1) |
163 | 146 |
|
164 |
| - return A, B, C, D |
165 |
| -end |
| 147 | +# return A, B, C, D |
| 148 | +# end |
166 | 149 |
|
167 | 150 | """
|
168 | 151 | A, B, C, T = balance_statespace{S}(A::Matrix{S}, B::Matrix{S}, C::Matrix{S}, perm::Bool=false)
|
|
0 commit comments