-
Notifications
You must be signed in to change notification settings - Fork 85
/
Lti.jl
140 lines (104 loc) · 4.76 KB
/
Lti.jl
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
abstract type LTISystem{TE<:TimeEvolution} <: AbstractSystem end
+(sys1::LTISystem, sys2::LTISystem) = +(promote(sys1, sys2)...)
-(sys1::LTISystem, sys2::LTISystem) = -(promote(sys1, sys2)...)
*(sys1::LTISystem, sys2::LTISystem) = *(promote(sys1, sys2)...)
/(sys1::LTISystem, sys2::LTISystem) = /(promote(sys1, sys2)...)
# Fallback number
+(sys1::LTISystem, sys2::Number) = +(promote(sys1, sys2)...)
-(sys1::LTISystem, sys2::Number) = -(promote(sys1, sys2)...)
*(sys1::LTISystem, sys2::Number) = *(promote(sys1, sys2)...)
/(sys1::LTISystem, sys2::Number) = /(promote(sys1, sys2)...)
+(sys1::Number, sys2::LTISystem) = +(promote(sys1, sys2)...)
-(sys1::Number, sys2::LTISystem) = -(promote(sys1, sys2)...)
*(sys1::Number, sys2::LTISystem) = *(promote(sys1, sys2)...)
/(sys1::Number, sys2::LTISystem) = /(promote(sys1, sys2)...)
# Fallback Matrix
+(sys1::LTISystem, sys2::AbstractMatrix) = +(promote(sys1, sys2)...)
-(sys1::LTISystem, sys2::AbstractMatrix) = -(promote(sys1, sys2)...)
*(sys1::LTISystem, sys2::AbstractMatrix) = *(promote(sys1, sys2)...)
/(sys1::LTISystem, sys2::AbstractMatrix) = /(promote(sys1, sys2)...)
+(sys1::AbstractMatrix, sys2::LTISystem) = +(promote(sys1, sys2)...)
-(sys1::AbstractMatrix, sys2::LTISystem) = -(promote(sys1, sys2)...)
*(sys1::AbstractMatrix, sys2::LTISystem) = *(promote(sys1, sys2)...)
/(sys1::AbstractMatrix, sys2::LTISystem) = /(promote(sys1, sys2)...)
Base.:^(sys::LTISystem, p::Integer) = Base.power_by_squaring(sys, p)
# TODO We should have proper fallbacks for matrices too
feedback(sys1::Union{LTISystem,Number,AbstractMatrix{<:Number}},
sys2::Union{LTISystem,Number,AbstractMatrix{<:Number}}; kwargs...) = feedback(promote(sys1, sys2)...; kwargs...)
Base.inv(G::LTISystem) = 1/G
"""`issiso(sys)`
Returns `true` if `sys` is SISO, else returns `false`."""
function issiso(sys::LTISystem)
return ninputs(sys) == 1 && noutputs(sys) == 1
end
"""`timeevol(sys::LTISystem)`
Get the timeevol::TimeEvolution from system `sys`, usually sys.timeevol """
timeevol(sys::LTISystem) = sys.timeevol
"""`iscontinuous(sys)`
Returns `true` for a continuous-time system `sys`, else returns `false`."""
iscontinuous(sys::LTISystem) = timeevol(sys) isa Continuous
"""`isdiscrete(sys)`
Returns `true` for a discrete-time system `sys`, else returns `false`."""
isdiscrete(sys::LTISystem) = timeevol(sys) isa Discrete
function Base.getproperty(sys::LTISystem, s::Symbol)
if s === :Ts
# if !isdiscrete(sys) # NOTE this line seems to be breaking inference of isdiscrete (is there a test for this?)
if isdiscrete(sys)
return timeevol(sys).Ts
else
@warn "Getting time 0.0 for non-discrete systems is deprecated. Check `isdiscrete` before trying to access time."
return 0.0
end
else
return getfield(sys, s)
end
end
Base.propertynames(sys::LTISystem, private::Bool=false) =
(fieldnames(typeof(sys))..., :nu, :ny, (isdiscrete(sys) ? (:Ts,) : ())...)
common_timeevol(systems::LTISystem...) = common_timeevol(timeevol(sys) for sys in systems)
"""
isstable(sys)
Returns `true` if `sys` is stable, else returns `false`."""
isstable(sys::LTISystem{Continuous}) = all(real.(poles(sys)) .< 0)
isstable(sys::LTISystem{<:Discrete}) = all(abs.(poles(sys)) .< 1)
# Fallback since LTISystem not AbstractArray
Base.size(sys::LTISystem, i::Integer) = size(sys)[i]
## Names =======================================================================
"""
system_name(nothing::LTISystem)
Return the name of the system. If the system does not have a name, an empty string is returned.
"""
system_name(::LTISystem) = ""
"""
input_names(P)
input_names(P, i)
Get a vector of strings with the names of the inputs of `P`, or the `i`:th name if an index is given.
"""
input_names(P::LTISystem; kwargs...) = [input_names(P, i; kwargs...) for i in 1:ninputs(P)]
function input_names(P::LTISystem, i; default = "u")
i <= ninputs(P) || throw(BoundsError(P, i))
ninputs(P) == 1 && (return default)
"u($i)"
end
"""
output_names(P)
output_names(P, i)
Get a vector of strings with the names of the outputs of `P`, or the `i`:th name if an index is given.
"""
output_names(P::LTISystem; kwargs...) = [output_names(P, i; kwargs...) for i in 1:noutputs(P)]
function output_names(P::LTISystem, i; default = "y")
i <= noutputs(P) || throw(BoundsError(P, i))
noutputs(P) == 1 && (return default)
"y($i)"
end
"""
state_names(P)
state_names(P, i)
Get a vector of strings with the names of the states of `P`, or the `i`:th name if an index is given.
"""
state_names(P::LTISystem; kwargs...) = [state_names(P, i; kwargs...) for i in 1:nstates(P)]
function state_names(P::LTISystem, i; default = "x")
i <= nstates(P) || throw(BoundsError(P, i))
nstates(P) == 1 && (return default)
"x($i)"
end