Skip to content

Commit 2c732aa

Browse files
authored
Merge pull request #1017 from JuliaControl/marginhz
marginplot to support frequency display in Hertz
2 parents f04916f + 8bcc023 commit 2c732aa

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

lib/ControlSystemsBase/src/plotting.jl

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -749,12 +749,13 @@ _to1series(y) = _to1series(1:size(y,3),y)
749749

750750
@userplot Marginplot
751751
"""
752-
fig = marginplot(sys::LTISystem [,w::AbstractVector]; balance=true, kwargs...)
753-
marginplot(sys::Vector{LTISystem}, w::AbstractVector; balance=true, kwargs...)
752+
fig = marginplot(sys::LTISystem [,w::AbstractVector]; hz=false, balance=true, kwargs...)
753+
marginplot(sys::Vector{LTISystem}, w::AbstractVector; hz=false, balance=true, kwargs...)
754754
755755
Plot all the amplitude and phase margins of the system(s) `sys`.
756756
757757
- A frequency vector `w` can be optionally provided.
758+
- `hz`: If true, the plot x-axis will be displayed in Hertz, the input frequency vector is still treated as rad/s.
758759
- `balance`: Call [`balance_statespace`](@ref) on the system before plotting.
759760
- `adjust_phase_start`: If true, the phase will be adjusted so that it starts at -90*intexcess degrees, where `intexcess` is the integrator excess of the system.
760761
@@ -763,6 +764,7 @@ Plot all the amplitude and phase margins of the system(s) `sys`.
763764
marginplot
764765
@recipe function marginplot(p::Marginplot; plotphase=true, hz=false, balance=true, adjust_phase_start=true, adaptive=true)
765766
systems, w = _processfreqplot(Val{:bode}(), p.args...; adaptive)
767+
ws = (hz ? 1/(2π) : 1) .* w
766768
ny, nu = size(systems[1])
767769
s2i(i,j) = LinearIndices((nu,(plotphase ? 2 : 1)*ny))[j,i]
768770
layout --> ((plotphase ? 2 : 1)*ny, nu)
@@ -805,7 +807,9 @@ marginplot
805807
oneLine = 1
806808
titles[j,i,1,1] *= "["*join([Printf.@sprintf("%3.2g",v) for v in gm],", ")*"] "
807809
end
808-
titles[j,i,1,2] *= "["*join([Printf.@sprintf("%3.2g",v) for v in wgm],", ")*"] "
810+
# Scale frequencies for display when hz=true
811+
wgm_display = hz ? wgm ./ (2π) : wgm
812+
titles[j,i,1,2] *= "["*join([Printf.@sprintf("%3.2g",v) for v in wgm_display],", ")*"] "
809813

810814

811815
subplot := min(s2i((plotphase ? (2i-1) : i),j), prod(plotattributes[:layout]))
@@ -822,10 +826,10 @@ marginplot
822826
m = bmag[i, j, :]
823827
if adaptive
824828
lmag = _PlotScale == "dB" ? m : log.(m)
825-
wsi, _, inds = downsample(w, lmag, _span(lmag)/500)
829+
wsi, _, inds = downsample(ws, lmag, _span(lmag)/500)
826830
wsi, m[inds]
827831
else
828-
w, m
832+
ws, m
829833
end
830834
end
831835

@@ -834,17 +838,19 @@ marginplot
834838
primary := false
835839
color --> :gray
836840
linestyle --> :dash
837-
[w[1],w[end]], [oneLine,oneLine]
841+
[ws[1],ws[end]], [oneLine,oneLine]
838842
end
839843
@series begin
840844
primary := false
841-
[wgm wgm]', [ones(length(mag)) mag]'
845+
[wgm_display wgm_display]', [ones(length(mag)) mag]'
842846
end
843847
plotphase || continue
844848

845849

846850
titles[j,i,2,1] *= "["*join([Printf.@sprintf("%3.2g°",v) for v in pm],", ")*"] "
847-
titles[j,i,2,2] *= "["*join([Printf.@sprintf("%3.2g",v) for v in wpm],", ")*"] "
851+
# Scale phase margin frequencies for display when hz=true
852+
wpm_display = hz ? wpm ./ (2π) : wpm
853+
titles[j,i,2,2] *= "["*join([Printf.@sprintf("%3.2g",v) for v in wpm_display],", ")*"] "
848854

849855
# Phase margins
850856
subplot := s2i(2i,j)
@@ -855,9 +861,9 @@ marginplot
855861
primary := true
856862
seriestype := :bodephase
857863
if adaptive
858-
downsample(w, phasedata, _span(phasedata)/500)[1:2]
864+
downsample(ws, phasedata, _span(phasedata)/500)[1:2]
859865
else
860-
w, phasedata
866+
ws, phasedata
861867
end
862868
end
863869
@series begin
@@ -869,7 +875,7 @@ marginplot
869875
end
870876
@series begin
871877
primary := false
872-
[wpm wpm]', [fullPhase fullPhase-pm]'
878+
[wpm_display wpm_display]', [fullPhase fullPhase-pm]'
873879
end
874880
end
875881
end

lib/ControlSystemsBase/test/test_plots.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,7 @@ end
6464

6565
setPlotScale("log10")
6666
funcs[8]() # test marginlpot with log10 scale
67+
68+
# Test marginplot with hz=true
69+
@test_nowarn marginplot(tf([2], [1,1])^3, 10.0 .^range(-2,stop=2,length=50); hz=true)
6770
end

0 commit comments

Comments
 (0)