From 4743be28a34f21c79abb288b507d60f79fa95007 Mon Sep 17 00:00:00 2001 From: Syed Inayatullah <44152603+inayatku@users.noreply.github.com> Date: Sun, 25 Oct 2026 21:24:11 +0500 Subject: [PATCH] Unwrap the Bode phase of delay systems on a finer grid (#757) A delay adds the phase -w*tau, which on a sparse frequency grid changes by more than 180 degrees between neighbouring frequencies, so unwrapping on the grid itself lost multiples of 360 degrees. For a DelayLtiSystem, the phase is now unwrapped on a grid on which the delays change the phase by at most 90 degrees, and read off at the given frequencies. bodeplot uses this phase instead of unwrapping again on the plotted grid. --- lib/ControlSystemsBase/src/delay_systems.jl | 28 +++++++++++++++++++ lib/ControlSystemsBase/src/freqresp.jl | 7 +++-- lib/ControlSystemsBase/src/plotting.jl | 3 +- .../test/test_delayed_systems.jl | 12 ++++++++ 4 files changed, 46 insertions(+), 4 deletions(-) diff --git a/lib/ControlSystemsBase/src/delay_systems.jl b/lib/ControlSystemsBase/src/delay_systems.jl index 42cb16393..22598fd3a 100644 --- a/lib/ControlSystemsBase/src/delay_systems.jl +++ b/lib/ControlSystemsBase/src/delay_systems.jl @@ -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) diff --git a/lib/ControlSystemsBase/src/freqresp.jl b/lib/ControlSystemsBase/src/freqresp.jl index ff53fb5a0..9eab739a2 100644 --- a/lib/ControlSystemsBase/src/freqresp.jl +++ b/lib/ControlSystemsBase/src/freqresp.jl @@ -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. diff --git a/lib/ControlSystemsBase/src/plotting.jl b/lib/ControlSystemsBase/src/plotting.jl index e6acd6e43..c904b6698 100644 --- a/lib/ControlSystemsBase/src/plotting.jl +++ b/lib/ControlSystemsBase/src/plotting.jl @@ -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 @@ -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 diff --git a/lib/ControlSystemsBase/test/test_delayed_systems.jl b/lib/ControlSystemsBase/test/test_delayed_systems.jl index 1997c0649..2b3e1246d 100644 --- a/lib/ControlSystemsBase/test/test_delayed_systems.jl +++ b/lib/ControlSystemsBase/test/test_delayed_systems.jl @@ -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