Skip to content
Closed
Changes from 2 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
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)
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))
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]
return abs.(resp), phase, w
end
bode(sys::LTISystem) = bode(sys, _default_freq_vector(sys, Val{:bode}()))

Expand Down