Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement on Bode plot phase #312

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
12 changes: 11 additions & 1 deletion src/freqresp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,17 @@ at frequencies `w`
`mag` and `phase` has size `(length(w), ny, nu)`"""
function bode(sys::LTISystem, w::AbstractVector)
resp = freqresp(sys, w)
return abs.(resp), rad2deg.(unwrap!(angle.(resp),1)), w
# use the number of integrators in the system to estimate initial phase
count_unit_circle = x -> count(abs.(abs.(x) .- 1.0) .< 1e-5)
tallakt marked this conversation as resolved.
Show resolved Hide resolved
count_zeros = x -> count(abs.(x) .< 1e-8)

phase0 = if sys.Ts == 0.0
pi/2 * (count_zeros(zpk(sys).matrix[1].z) - count_zeros(zpk(sys).matrix[1].p))
tallakt marked this conversation as resolved.
Show resolved Hide resolved
else
pi/2 * (count_unit_circle(zpk(sys).matrix[1].z) - count_unit_circle(zpk(sys).matrix[1].p))
end
phase = rad2deg.(unwrap!(vcat(phase0, angle.(resp)),1))[2:end]
tallakt marked this conversation as resolved.
Show resolved Hide resolved
return abs.(resp), phase, w
end
bode(sys::LTISystem) = bode(sys, _default_freq_vector(sys, Val{:bode}()))

Expand Down