Skip to content

Commit 70dee61

Browse files
authored
simpler margin zero freq logic (#1048)
1 parent 37e0f5b commit 70dee61

2 files changed

Lines changed: 4 additions & 7 deletions

File tree

lib/ControlSystemsBase/src/analysis.jl

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -607,10 +607,10 @@ function sisomargin(sys::LTISystem, w::AbstractVector{<:Real}; full=false, allMa
607607
end
608608
end
609609
margin(system::LTISystem; kwargs...) =
610-
margin(system, _add_eps_negative(_default_freq_vector(system, Val{:bode}())); kwargs...)
610+
margin(system, _add_zero(_default_freq_vector(system, Val{:bode}())); kwargs...)
611611
#margin(sys::LTISystem, args...) = margin(LTISystem[sys], args...)
612612

613-
_add_eps_negative(w) = [-(eps(eltype(w))); w] # The size of the negative frequency is a tradeoff, too small and the check `if abs(d) > 20` in _findCrossings may fail, but too large and we get an inaccurate interpolation.
613+
_add_zero(w) = [-(zero(eltype(w))); w] # The size of the negative frequency is a tradeoff, too small and the check `if abs(d) > 20` in _findCrossings may fail, but too large and we get an inaccurate interpolation.
614614

615615
# Interpolate the values in "list" given the floating point "index" fi
616616
function interpolate(fi, list)
@@ -639,11 +639,7 @@ function _findCrossings(w, n, res; filter_th=Inf)
639639
push!(tcross, i)
640640
elseif n[i] != n[i+1]
641641
d = res[i]-res[i+1]
642-
if abs(d) > filter_th && (sign(w[i+1]) != sign(w[i]))
643-
# This can happen when there are both negative and positive frequencies and the Nyquist contour crosses the negative real axis at -infinity
644-
# This logic is slightly flawed since if there are two frequencies, like -eps, eps that have phase values very close to each other we'll fail to reject the crossing, but to handle this case properly one would have to look at both phase and gain at the same time, which we aren't set-up to do easily at this. We should be able to reject the most common case when the default eps() negative frequency is the only one used
645-
continue
646-
end
642+
w[i] == 0 && continue
647643
#Interpolate to approximate crossing
648644
t = res[i]/d
649645
push!(tcross, i+t)

lib/ControlSystemsBase/src/utilities.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ function unwrap!(M::Array, dim=1)
122122
# d = M[i,:,:,...,:] - M[i-1,:,...,:]
123123
# M[i,:,:,...,:] -= floor((d+π) / (2π)) * 2π
124124
d = M[alldims(i)...] - M[alldims(i-1)...]
125+
any(!isfinite, d) && continue
125126
M[alldims(i)...] -= @. floor((d + π) / π2) * π2
126127
end
127128
return M

0 commit comments

Comments
 (0)