-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathfreqresp.jl
More file actions
536 lines (466 loc) · 17.7 KB
/
freqresp.jl
File metadata and controls
536 lines (466 loc) · 17.7 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
struct BodemagWorkspace{T}
R::Array{Complex{T}, 3}
mag::Array{T, 3}
end
function BodemagWorkspace{T}(ny::Int, nu::Int, N::Int) where T <: Real
R = Array{Complex{T},3}(undef, ny, nu, N)
mag = Array{T,3}(undef, ny, nu, N)
BodemagWorkspace{T}(R, mag)
end
"""
BodemagWorkspace(sys::LTISystem, N::Int)
BodemagWorkspace(sys::LTISystem, ω::AbstractVector)
BodemagWorkspace(R::Array{Complex{T}, 3}, mag::Array{T, 3})
BodemagWorkspace{T}(ny, nu, N)
Generate a workspace object for use with the in-place function [`bodemag!`](@ref).
`N` is the number of frequency points, alternatively, the input `ω` can be provided instead of `N`.
Note: for threaded applications, create one workspace object per thread.
# Arguments:
- `mag`: The output array ∈ 𝐑(ny, nu, nω)
- `R`: Frequency-response array ∈ 𝐂(ny, nu, nω)
"""
function BodemagWorkspace(sys::LTISystem, N::Int)
T = float(numeric_type(sys))
R = Array{Complex{T},3}(undef, sys.ny, sys.nu, N)
mag = Array{T,3}(undef, sys.ny, sys.nu, N)
BodemagWorkspace(R, mag)
end
BodemagWorkspace(sys::LTISystem, ω::AbstractVector) = BodemagWorkspace(sys, length(ω))
function freqresp(sys::LTISystem, w::Real)
# Create imaginary freq vector s
if iscontinuous(sys)
s = im*w
else
s = cis(w*sys.Ts)
end
evalfr(sys, s)
end
freqresp(G::Union{UniformScaling, AbstractMatrix, Number}, w::Real) = G
"""
sys_fr = freqresp(sys, w)
Evaluate the frequency response of a linear system.
For continuous systems, computes `G(jω) = C(jωI - A)^{-1}B + D`
For discrete systems, computes `G(e^{jωT}) = C(e^{jωT}I - A)^{-1}B + D`
# Arguments
- `sys::LTISystem`: The system to analyze
- `w::AbstractVector{<:Real}`: Frequency vector (rad/s)
# Returns
- `sys_fr`: Complex frequency response array of size `(ny, nu, length(w))`
See also [`freqresp!`](@ref), [`evalfr`](@ref), [`bode`](@ref), [`nyquist`](@ref).
# Examples
```julia
using ControlSystemsBase
sys = ss([-1 0; 0 -2], [1; 1], [1 1], 0)
w = exp10.(LinRange(-2, 2, 200))
resp = freqresp(sys, w)
```
"""
@autovec () function freqresp(sys::LTISystem, w_vec::AbstractVector{W}) where W <: Real
te = timeevol(sys)
ny,nu = noutputs(sys), ninputs(sys)
T = promote_type(Complex{real(numeric_type(sys))}, Complex{W})
R = Array{T, 3}(undef, ny, nu, length(w_vec))
freqresp!(R, sys, w_vec)
end
"""
freqresp!(R::Array{T, 3}, sys::LTISystem, w_vec::AbstractVector{<:Real})
In-place version of [`freqresp`](@ref) that takes a pre-allocated array `R` of size (ny, nu, nw)`
"""
function freqresp!(R::Array{T,3}, sys::LTISystem, w_vec::AbstractVector{<:Real}) where T
te = sys.timeevol
ny,nu = noutputs(sys), ninputs(sys)
@boundscheck size(R) == (ny,nu,length(w_vec))
@inbounds for wi = eachindex(w_vec), ui = 1:nu, yi = 1:ny
R[yi,ui,wi] = evalfr(sys[yi,ui], _freq(w_vec[wi], te))[]
end
R
end
function freqresp!(R::Array{T,3}, sys::TransferFunction, w_vec::AbstractVector{<:Real}) where T
te = sys.timeevol
ny,nu = noutputs(sys), ninputs(sys)
@boundscheck size(R) == (ny,nu,length(w_vec))
@inbounds for wi = eachindex(w_vec), ui = 1:nu, yi = 1:ny
R[yi,ui,wi] = evalfr(sys.matrix[yi,ui], _freq(w_vec[wi], te))
end
R
end
@autovec () function freqresp(G::AbstractMatrix, w_vec::AbstractVector{<:Real})
repeat(G, 1, 1, length(w_vec))
end
@autovec () function freqresp(G::Number, w_vec::AbstractVector{<:Real})
fill(G, 1, 1, length(w_vec))
end
_freq(w, ::Continuous) = complex(0, w)
_freq(w, te::Discrete) = cis(w*te.Ts)
@autovec () function freqresp!(R::Array{T,3}, sys::AbstractStateSpace, w_vec::AbstractVector{W}) where {T, W <: Real}
ny, nu = size(sys)
@boundscheck size(R) == (ny,nu,length(w_vec))
if sys.nx == 0 # Only D-matrix
@inbounds for i in eachindex(w_vec)
R[:,:,i] .= sys.D
end
return R
end
local F, Q
try
F = hessenberg(sys.A)
Q = Matrix(F.Q)
catch e
# For matrix types that do not have a hessenberg implementation, we call the standard version of freqresp.
(e isa @static VERSION < v"1.12" ? Union{MethodError, ErrorException} : Union{MethodError, ErrorException, FieldError}) && return freqresp_nohess!(R, sys, w_vec)
# ErrorException appears if we try to access Q on a type which does not have Q as a field or property, notably HessenbergFactorization from GenericLinearAlgebra, on julia v1.12, this is instead a FieldError
rethrow()
end
A = F.H
C = complex.(sys.C*Q) # We make C complex in order to not incur allocations in mul! below
B = Q\sys.B
D = sys.D
te = sys.timeevol
Bc = similar(B, T) # for storage
w = -_freq(w_vec[1], te)
u = Vector{typeof(zero(eltype(A.data))+w)}(undef, sys.nx)
cs = Vector{Tuple{real(eltype(u)),eltype(u)}}(undef, length(u)) # store Givens rotations
@inbounds for i in eachindex(w_vec)
Ri = @view(R[:, :, i])
copyto!(Ri,D) # start with the D-matrix
isinf(w_vec[i]) && continue
copyto!(Bc,B) # initialize storage to B
w = -_freq(w_vec[i], te)
ldiv2!(u, cs, A, Bc, shift = w) # B += (A - w*I)\B # solve (A-wI)X = B, storing result in B
mul!(Ri, C, Bc, -1, 1) # use of 5-arg mul to subtract from D already in Ri. - rather than + since (A - w*I) instead of (w*I - A)
end
R
end
#=
Custom implementation of hessenberg ldiv to allow reuse of u and cs
With this method, the following benchmark goes from
(100017 allocations: 11.48 MiB) # before
to
(17 allocations: 35.45 KiB) # after
w = exp10.(LinRange(-2, 2, 50000))
G = ssrand(2,2,3)
R = freqresp(G, w);
@btime ControlSystemsBase.freqresp!($R, $G, $w);
=#
function ldiv2!(u, cs, F::UpperHessenberg, B::AbstractVecOrMat; shift::Number=false)
LinearAlgebra.checksquare(F)
m = size(F,1)
m != size(B,1) && throw(DimensionMismatch("wrong right-hand-side # rows != $m"))
LinearAlgebra.require_one_based_indexing(B)
n = size(B,2)
H = F.data
μ = shift
copyto!(u, 1, H, m*(m-1)+1, m) # u .= H[:,m]
u[m] += μ
X = B # not a copy, just rename to match paper
@inbounds for k = m:-1:2
c, s, ρ = LinearAlgebra.givensAlgorithm(u[k], H[k,k-1])
cs[k] = (c, s)
for i = 1:n
X[k,i] /= ρ
t₁ = s * X[k,i]; t₂ = c * X[k,i]
@simd for j = 1:k-2
X[j,i] -= u[j]*t₂ + H[j,k-1]*t₁
end
X[k-1,i] -= u[k-1]*t₂ + (H[k-1,k-1] + μ) * t₁
end
@simd for j = 1:k-2
u[j] = H[j,k-1]*c - u[j]*s'
end
u[k-1] = (H[k-1,k-1] + μ) * c - u[k-1]*s'
end
for i = 1:n
τ₁ = X[1,i] / u[1]
@inbounds for j = 2:m
τ₂ = X[j,i]
c, s = cs[j]
X[j-1,i] = c*τ₁ + s*τ₂
τ₁ = c*τ₂ - s'τ₁
end
X[m,i] = τ₁
end
return X
end
function freqresp_nohess(sys::AbstractStateSpace, w_vec::AbstractVector{W}) where W <: Real
ny, nu = size(sys)
T = promote_type(Complex{real(eltype(sys.A))}, Complex{W})
R = Array{T, 3}(undef, ny, nu, length(w_vec))
freqresp_nohess!(R, sys, w_vec)
end
"""
freqresp_nohess(sys::AbstractStateSpace, w_vec::AbstractVector{<:Real})
Compute the frequency response of `sys` without forming a Hessenberg factorization.
This function is called automatically if the Hessenberg factorization fails.
"""
freqresp_nohess
@autovec () function freqresp_nohess!(R::Array{T,3}, sys::AbstractStateSpace, w_vec::AbstractVector{W}) where {T, W <: Real}
ny, nu = size(sys)
@boundscheck size(R) == (ny,nu,length(w_vec))
nx = sys.nx
if nx == 0 # Only D-matrix
@inbounds for i in eachindex(w_vec)
R[:,:,i] .= sys.D
end
return R
end
A,B,C0,D = ssdata(sys)
C = complex.(C0) # We make C complex in order to not incur allocations in mul! below
te = sys.timeevol
Ac = (A+one(T)*I) # for storage
Adiag = diagind(A)
@inbounds for i in eachindex(w_vec)
Ri = @views R[:,:,i]
copyto!(Ri,D) # start with the D-matrix
isinf(w_vec[i]) && continue
w = _freq(w_vec[i], te)
# @views copyto!(Ac[Adiag],A[Adiag]) # reset storage to A
for j in Adiag # Loop slightly faster
Ac[j] = A[j] # reset storage to A
end
@views Ac[Adiag] .-= w # Ac = A - w*I
Bc = Ac \ B # Bc = (A - w*I)\B # avoid inplace to handle sparse matrices etc.
mul!(Ri, C, Bc, -1, 1) # use of 5-arg mul to subtract from D already in Ri. - rather than + since (A - w*I) instead of (w*I - A)
end
R
end
function _evalfr_return_type(sys::AbstractStateSpace, s::Number)
T0 = numeric_type(sys)
temp_product = one(T0)*one(typeof(s))
typeof(temp_product/temp_product)
end
"""
evalfr(sys, x)
Evaluate the transfer function of the LTI system sys
at the complex number s=x (continuous-time) or z=x (discrete-time).
For many values of `x`, use `freqresp` instead.
"""
function evalfr(sys::AbstractStateSpace, s::Number)
T = _evalfr_return_type(sys, s)
try
R = s*I - sys.A
sys.D + sys.C*((R\sys.B))
catch e
@warn "Got exception $e, returning Inf" maxlog=1
fill(convert(T, Inf), size(sys))
end
end
function evalfr(G::TransferFunction{<:TimeEvolution,<:SisoTf}, s::Number)
map(m -> evalfr(m,s), G.matrix)
end
evalfr(G::Union{UniformScaling, AbstractMatrix, Number}, s) = G
"""
`F(s)`, `F(omega, true)`, `F(z, false)`
Notation for frequency response evaluation.
- F(s) evaluates the continuous-time transfer function F at s.
- F(omega,true) evaluates the discrete-time transfer function F at exp(im*Ts*omega)
- F(z,false) evaluates the discrete-time transfer function F at z
"""
function (sys::TransferFunction)(s)
evalfr(sys,s)
end
function (sys::TransferFunction)(z_or_omega::Number, map_to_unit_circle::Bool)
isdiscrete(sys) || throw(ArgumentError("It only makes no sense to call this function with discrete systems"))
if map_to_unit_circle
isreal(z_or_omega) ? evalfr(sys,exp(im*z_or_omega.*sys.Ts)) : error("To map to the unit circle, omega should be real")
else
evalfr(sys,z_or_omega)
end
end
function (sys::TransferFunction)(z_or_omegas::AbstractVector, map_to_unit_circle::Bool)
isdiscrete(sys) || throw(ArgumentError("It only makes no sense to call this function with discrete systems"))
vals = sys.(z_or_omegas, map_to_unit_circle)# evalfr.(sys,exp.(evalpoints))
# Reshape from vector of evalfr matrizes, to (in,out,freq) Array
nu,ny = size(vals[1])
[v[i,j] for i in 1:nu, j in 1:ny, v in vals]
end
"""
mag, phase, w = bode(sys[, w]; unwrap=true)
Compute the magnitude and phase parts of the frequency response of system `sys`
at frequencies `w`. The frequency response is evaluated as `G(jω)` for continuous
systems and `G(e^{jωT})` for discrete systems.
# Arguments
- `sys::LTISystem`: The system to analyze
- `w::AbstractVector`: Frequency vector (rad/s). If omitted, a default frequency range is used.
- `unwrap::Bool`: If true (default), apply phase unwrapping to avoid discontinuities
# Returns
- `mag`: Magnitude of frequency response, size `(ny, nu, length(w))`
- `phase`: Phase in degrees, size `(ny, nu, length(w))`
- `w`: Frequency vector used
Note: Phase unwrapping is computationally expensive and can be disabled for better performance. For higher performance, see the function [`bodemag!`](@ref) that computes the magnitude only.
See also [`bodeplot`](@ref), [`bodemag!`](@ref), [`freqresp`](@ref).
# Examples
```julia
using ControlSystemsBase
sys = tf(1, [1, 1])
mag, phase, w = bode(sys)
```
"""
@autovec (1, 2) function bode(sys::LTISystem, w::AbstractVector; unwrap=true)
resp = freqresp(sys, w)
angles = angle.(resp)
unwrap && unwrap!(angles,3)
@. angles = rad2deg(angles)
return abs.(resp), angles, w
end
@autovec (1, 2) bode(sys::LTISystem) = bode(sys, _default_freq_vector(sys, Val{:bode}()))
# Performance difference between bode and bodemag for tf. Note how expensive the phase unwrapping is.
# using ControlSystemsBase
# G = tf(ssrand(2,2,5))
# w = exp10.(LinRange(-2, 2, 20000))
# @btime bode($G, $w);
# # 55.120 ms (517957 allocations: 24.42 MiB)
# @btime bode($G, $w, unwrap=false);
# # 3.624 ms (7 allocations: 2.44 MiB)
# ws = ControlSystemsBase.BodemagWorkspace(G, w)
# @btime bodemag!($ws, $G, $w);
# # 2.991 ms (1 allocation: 64 bytes)
"""
mag = bodemag!(ws::BodemagWorkspace, sys::LTISystem, w::AbstractVector)
Compute the Bode magnitude operating in-place on an instance of [`BodemagWorkspace`](@ref).
# Arguments
- `ws::BodemagWorkspace`: Pre-allocated workspace created with [`BodemagWorkspace`](@ref)
- `sys::LTISystem`: The system to analyze
- `w::AbstractVector`: Frequency vector (rad/s)
# Returns
- `mag`: Magnitude of frequency response, size `(ny, nu, length(w))`.
Note: The returned array is aliased with `ws.mag`.
# Performance
This function provides significant performance benefits for repeated magnitude calculations:
- Avoids memory allocation by reusing workspace arrays
- Optimized for systems with many frequency points
For thread-safe applications, create one workspace per thread.
See also [`BodemagWorkspace`](@ref), [`bode`](@ref), [`freqresp!`](@ref).
# Examples
```julia
using ControlSystemsBase
sys = tf(1, [1, 1])
w = exp10.(LinRange(-2, 2, 200))
ws = BodemagWorkspace(sys, w)
mag = bodemag!(ws, sys, w)
```
"""
function bodemag!(ws::BodemagWorkspace, sys::LTISystem, w::AbstractVector)
freqresp!(ws.R, sys, w)
@. ws.mag = abs(ws.R)
ws.mag
end
function bodemag_nohess!(ws::BodemagWorkspace, sys::LTISystem, w::AbstractVector)
freqresp_nohess!(ws.R, sys, w)
@. ws.mag = abs(ws.R)
ws.mag
end
"""
re, img, w = nyquist(sys[, w])
Compute the real and imaginary parts of the frequency response of system `sys`
at frequencies `w`. The frequency response is evaluated as `G(jω)` for continuous
systems and `G(e^{jωT})` for discrete systems.
# Arguments
- `sys::LTISystem`: The system to analyze
- `w::AbstractVector`: Frequency vector (rad/s). If omitted, a default frequency range is used.
# Returns
- `re`: Real part of frequency response, size `(ny, nu, length(w))`
- `img`: Imaginary part of frequency response, size `(ny, nu, length(w))`
- `w`: Frequency vector used
See also [`nyquistplot`](@ref), [`freqresp`](@ref), [`bode`](@ref).
# Examples
```julia
using ControlSystems
sys = tf(1, [1, 1])
w = logspace(-2, 2, 100)
re, img, w = nyquist(sys, w)
```
"""
@autovec (1, 2) function nyquist(sys::LTISystem, w::AbstractVector)
resp = freqresp(sys, w)
return real(resp), imag(resp), w
end
@autovec (1, 2) nyquist(sys::LTISystem) = nyquist(sys, _default_freq_vector(sys, Val{:nyquist}()))
"""
sv, w = sigma(sys[, w])
Compute the singular values of the frequency response of system `sys` at
frequencies `w`. The frequency response is evaluated as `G(jω)` for continuous
systems and `G(e^{jωT})` for discrete systems.
# Arguments
- `sys::LTISystem`: The system to analyze
- `w::AbstractVector`: Frequency vector (rad/s). If omitted, a default frequency range is used.
# Returns
- `sv`: Singular values of frequency response, size `(min(ny, nu), length(w))`
- `w`: Frequency vector used
The singular values provide information about the system's gain characteristics
across different frequencies and input/output directions.
See also [`sigmaplot`](@ref), [`freqresp`](@ref), [`bode`](@ref).
# Examples
```julia
using ControlSystems
sys = ss([-1 0; 0 -2], [1 0; 0 1], [1 1; 0 1], 0)
sv, w = sigma(sys)
```
"""
@autovec (1) function sigma(sys::LTISystem, w::AbstractVector)
resp = freqresp(sys, w)
ny, nu = size(sys)
if ny == 1 || nu == 1 # Shortcut available
sv = Matrix{real(eltype(resp))}(undef, 1, length(w))
for i = eachindex(w)
@views sv[1, i] = norm(resp[:,:,i])
end
else
sv = dropdims(mapslices(svdvals, resp, dims=(1,2)),dims=2)::Matrix{real(eltype(resp))}
end
return sv, w
end
@autovec (1) sigma(sys::LTISystem) = sigma(sys, _default_freq_vector(sys, Val{:sigma}()))
function _default_freq_vector(systems::Vector{<:LTISystem}, plot; adaptive=false)
if adaptive
min_pt_per_dec = 100
min_pt_total = 1000
else
min_pt_per_dec = 60
min_pt_total = 200
end
bounds = map(sys -> _bounds_and_features(sys, plot)[1], systems)
w1 = minimum(minimum, bounds)
w2 = maximum(maximum, bounds)
nw = round(Int, max(min_pt_total, min_pt_per_dec*(w2 - w1)))
w = exp10.(range(w1, stop=w2, length=nw))
if length(systems) == 1 && isdiscrete(systems[1])
w[end] = π/systems[1].Ts # To account for numerical rounding problems from exp(log())
end
w
end
_default_freq_vector(sys::LTISystem, plot; kwargs...) = _default_freq_vector(
[sys], plot; kwargs...)
function _bounds_and_features(sys::LTISystem, plot::Val)
# Get zeros and poles for each channel
if !isa(plot, Val{:sigma})
zs, ps = zpkdata(sys)
# Compose vector of all zs, ps, positive conjugates only.
zpType = promote_type(eltype(eltype(zs)), eltype(eltype(ps)))
zp = vcat(zpType[], zs..., ps...) # Emty vector to avoid type unstable vcat()
zp = zp[imag(zp) .>= 0.0]
else
# For sigma plots, use the MIMO poles and zeros
zp = [tzeros(sys); poles(sys)]
end
# Get the frequencies of the features, ignoring low frequency dynamics
fzp = log10.(abs.(zp))
fzp = fzp[fzp .> -4]
fzp = sort!(fzp)
# Determine the bounds on the frequency vector
if !isempty(fzp)
w1 = floor(fzp[1] - 1.2)
w2 = ceil(fzp[end] + 1.2)
# Expand the range for nyquist plots
if plot isa Val{:nyquist}
w1 -= 0.0
w2 += 1.0
end
else
w1 = 0.0
w2 = 2.0
end
if isdiscrete(sys)
w2 = log10(π/sys.Ts) # Draw up to Nyquist frequency for discrete systems
end
return [w1, w2], zp
end