# ControlSystems / Frequency in Hz for bodeplot

Hello,

I would like to know how to change the xaxis unit (rad/s) to frequency for bodeplot.

Patrick.

Iβm no expert, but a clarifying questionβ¦:

• is it purely the xaxis text you want to change, or is it both the text and the scaling (so that the abcissa actually is the frequecy in Hz)?

One of my frustrations with MATLABβs Control TB is that it defaults to βsβ (step, impulse) and βrad/sβ for Bodeplotsβ¦ I often consider systems where βminβ, βhβ, βdayβ, βweekβ β or βmsβ for that matter - is the time unit. Iβm sure it is possible to override this in MATLAB, but it is not intuitive how to do this β at least not the way my brain works.

Hopefully, this is simpler in ControlSystems of Julia. If it is only to change the text, my first attempt would be just specify `xlabel = "Hz"`. If you need to scale the axis, a cumbersome way is to scale the time constants, etc with 2*pi.

Hi BLI,

I want also to change the scale. I am an electronic designer and we are dealing with frequency in Hz, not the rad/s.

Patrick.

You can always do

``````mag, phase, w = bode(sys)
plot(w./(2pi), mag)
``````

and if on master branch, I think you can do

``````mag, phase, w = bode(sys)
plot(w./(2pi), mag, seriestype=:bodemag)
``````

(not entierly sure about the keyword `seriestype` though)

Also you could check out the `SampledSignals` package, which is designed to represent regularly-sampled data and is samplerate-aware. the `SampleBuf` type holds time-series data, and when you take the `fft` of it you get a `SpectrumBuf`. the `domain` function will give you the x-axis values, either in seconds or Hz, respectively. So plotting the spectrum is just

``````using Plots
using SampledSignals

julia> buf = SampleBuf(rand(1024) .* sin.(linspace(0,2Ο,1024)), 48000)
1024-frame, 1-channel SampleBuf{Float64, 1}
0.021333333333333333s sampled at 48000.0Hz
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ

julia> spec = fft(buf)
1024-frame, 1-channel SpectrumBuf{Complex{Float64}, 1}
48000.0Hz sampled at 0.021333333333333333s
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ

julia> plot(domain(spec), abs.(spec))
``````

There are still some rough edges, but there are a couple of us working to smooth them out. Iβve also been pretty slow to get 0.7 compatibility working, but that should be coming in the next couple weeks at the latest.

This looks like an ok solution. I assume there is a similar `:bodephase` seriestype?

Is it ok to just set the `xlabel`? (Iβm away from laptop, snd cannot run Julia on my smartphoneβ¦ ).

Yes, all plot commands works just as in Plots.jl

You have the code and can inspect the available options here. Itβs all based on PlotRecipes.

Testing plotting of ControlSystems systemsβ¦ I have used `julia> using ControlSystems`. Then

``````julia> sys = tfg("2*(1-3*s)/(1+0.1*s)/(1+10*s)");
#
julia> bodeplot(sys,xlabel=["" "Frequency (Hz)"],linecolor = [:red :blue])
``````

The result is as follows:

β¦ which looks fine, except that the specified `linecolor` is not adhered to. [The reason for specifying individual `xlabel` for magnitude and phase, is that I donβt want to waste space by inserting `xlabel` for the uppermost plotβ¦]

Next, I tried to use the `bode` command in combination with standard `plot`.

``````mag,phase,w = bode(sys);
``````

Turns out that `mag` and `phase` are not standard vectors, but 3D arrays:

``````julia> mag
400Γ1Γ1 Array{Float64,3}:
[:, :, 1] =
1.99097
1.99055
...
``````

Thus, to plot the magnitudeβ¦

``````julia> plot(w,20*log10.(mag[:][:][:]),xscale=:log10)
``````

So, two questions:

1. Why doesnβt it work to specify different colors for magnitude and phase?
2. Why are `mag` and `phase` returned as 3D arrays? [This slightly complicates plottingβ¦]

PS: with a little bit of fine tuning, it is possible to get decent Bode diagrams:

``````julia> plot(w,[20*log10.(mag[:][:][:]),phase[:][:][:]],xscale=:log10,linecolor=[:red :blue],layout=(2,1))
julia> plot!(title=["Bode plot" ""],xlabel=["" "Frequency [rad/s]"])
julia> plot!(ylabel=["Magnitude (dB)" "Phase (deg)"])
``````

We have been rewriting a lot of the functionality in ControlSystems.jl, some of the changes are on the βmasterβ branch of the package and not in a tagged version. It seems to me that the color works as expected on the βmasterβ branch. You could try

``````Pkg.checkout("ControlSystems", "master")
``````

and see if it works. There are other major updates on its way on the βautodiffβ branch, but they are not completely stable yet.

For the question regarding the `400Γ1Γ1 Array`:
This is the result of the system being a 1-input 1-output system, so to access the response from input `j` to output `i` on a general system you do `mag[:,i,j]` and get a simple vector.

There have been discussions on automatically reduce this to a vector in some SISO cases, but there is no consensus yet, and it has some implications on the type system, input is welcome.

I would also advice against using the `tfg` method, this will probably be removed soon. It was intended only for cases where the system is not a simple finite order system, as in `sys = tfg("e^(-s)/s")` and offers limited functionality. The easiest way to create a TransferFunction is to first create the system βsβ using the special constructor `tf("s")`:

``````s = tf("s") # Equivalent to tf([1, 0], [1])
sys = 2*(1-3*s)/(1+0.1*s)/(1+10*s)
``````

or if you know the coefficients

``````sys = tf([-6.0, 2], [1, 10.1, 1]) # (-6s+2)*(1s^2+10.1s+1)
``````

or as a zero-pole-gain system

``````sys = zpk([1/3], [-10, -0.1], -6) # -6*(s-1/3)/((s+10)*(s+0.1))
``````

A last note: `mag[:]` will βvectorizeβ the array, so there is no need to do `mag[:][:][:]`, they are equivalent.

Edit: this command should generated the plot you want on the βmasterβ branch

``````bodeplot(sys, xlabel=["" "Frequency (Hz)"], linecolor=[:red :blue], title=["Bode plot" ""])
``````
1 Like