Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions lib/ControlSystemsBase/src/delay_systems.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,34 @@ function freqresp!(R::Array{T,3}, sys::DelayLtiSystem, ω::AbstractVector{W}; ba
return R
end

# A delay τ adds the phase -ωτ, so on a sparse frequency grid the phase may change by more than 180° between
# neighbouring frequencies, and unwrapping on the grid itself then loses multiples of 360°. The phase is therefore
# unwrapped on a finer grid, on which the delays change the phase by at most 90° between neighbouring frequencies,
# and read off at the frequencies in `w`. If this would need more than `maxextra` additional frequencies, the
# phase is unwrapped on `w` itself.
function _unwrapped_phase(sys::DelayLtiSystem, w, resp; balance=true, maxextra=100_000)
τ = sum(abs, sys.Tau; init=0.0)
n = length(w)
(τ == 0 || n < 2) && return unwrap!(angle.(resp), 3)
nextra = [max(0, ceil(Int, min(abs(w[i+1] - w[i]) * τ / (π/2), maxextra + 1.0)) - 1) for i in 1:n-1]
(all(iszero, nextra) || sum(nextra) > maxextra) && return unwrap!(angle.(resp), 3)
wf = Vector{float(eltype(w))}(undef, n + sum(nextra))
inds = Vector{Int}(undef, n)
k = 1
for i in 1:n
wf[k] = w[i]
inds[i] = k
k += 1
i == n && break
for j in 1:nextra[i]
wf[k] = w[i] + j * (w[i+1] - w[i]) / (nextra[i] + 1)
k += 1
end
end
phase = unwrap!(angle.(freqresp(sys, wf; balance)), 3)
return phase[:, :, inds]
end

function evalfr(sys::DelayLtiSystem, s)
(ny, nu) = size(sys)

Expand Down
7 changes: 5 additions & 2 deletions lib/ControlSystemsBase/src/freqresp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -354,11 +354,14 @@ mag, phase, w = bode(sys)
"""
@autovec (1, 2) function bode(sys::LTISystem, w::AbstractVector; unwrap=true, balance=true)
resp = freqresp(sys, w; balance)
angles = angle.(resp)
unwrap && unwrap!(angles,3)
angles = unwrap ? _unwrapped_phase(sys, w, resp; balance) : angle.(resp)
@. angles = rad2deg(angles)
return abs.(resp), angles, w
end

# Phase (radians) of the frequency response `resp` of `sys` at `w`, unwrapped along the frequency dimension.
# Systems with delays have a method in delay_systems.jl.
_unwrapped_phase(sys::LTISystem, w, resp; balance=true) = unwrap!(angle.(resp), 3)
@autovec (1, 2) bode(sys::LTISystem; unwrap=true, balance=true) = bode(sys, _default_freq_vector(sys, Val{:bode}()); unwrap, balance)

# Performance difference between bode and bodemag for tf. Note how expensive the phase unwrapping is.
Expand Down
3 changes: 1 addition & 2 deletions lib/ControlSystemsBase/src/plotting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ _span(vec) = -(reverse(extrema(vec))...)
if plotphase && adjust_phase_start && isrational(s)
intexcess = integrator_excess(s)
end
mag, phase = bode(s, w; unwrap=false, balance)
mag, phase = bode(s, w; unwrap, balance)
if _PlotScale == "dB" # Set by setPlotScale(str) globally
mag = 20*log10.(mag)
elseif 0 ∈ mag
Expand Down Expand Up @@ -360,7 +360,6 @@ _span(vec) = -(reverse(extrema(vec))...)
xguide --> (hz ? "Frequency [Hz]" : "Frequency [rad/s]")
label --> ""
group --> group_ind
phasedata = unwrap ? ControlSystemsBase.unwrap(phasedata.*(pi/180)).*(180/pi) : phasedata
if adaptive && eltype(phasedata) <: AbstractFloat # To guard agains https://github.com/JuliaPlots/Plots.jl/issues/5092 when using uncertain number systems
downsample(ws, phasedata, _span(phasedata)/500)[1:2]
elseif adaptive
Expand Down
12 changes: 12 additions & 0 deletions lib/ControlSystemsBase/test/test_delayed_systems.jl
Original file line number Diff line number Diff line change
Expand Up @@ -272,5 +272,17 @@ for τ in (1e-3, 1e3, 1e5)
@test w[end] <= max(1e3, 1e3/τ)
end

# The phase of a system with delay is unwrapped correctly also where the delay changes the phase by more than
# 180° between neighbouring frequencies (#757)
let sys = delay(2) * tf(1, [1, 1])
w = exp10.(range(-2, 2, length=200))
mag, phase, _ = bode(sys, w)
@test vec(phase) ≈ rad2deg.(-atan.(w) .- 2w)
mag, phase, w = bode(sys)
@test vec(phase) ≈ rad2deg.(-atan.(w) .- 2w)
mag, phase, _ = bode(delay(1) * delay(0.5) * tf(1, [1, 1]), w)
@test vec(phase) ≈ rad2deg.(-atan.(w) .- 1.5w)
end


end