# ControlSystems / Frequency in Hz for bodeplot

**URL:** <https://discourse.julialang.org/t/controlsystems-frequency-in-hz-for-bodeplot/12683>\
**Category:** Modelling & Simulations\
**Tags:** package, control\
**Created:** [July 27, 2018, 11:47am UTC](https://discourse.julialang.org/t/controlsystems-frequency-in-hz-for-bodeplot/12683 "2018-07-27T11:47:09Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Patrick\_Dubus](https://avatars.discourse-cdn.com/v4/letter/p/c77e96/32.png) [@Patrick\_Dubus](https://discourse.julialang.org/u/Patrick_Dubus)\
**Post date:** [July 27, 2018, 11:47am UTC](https://discourse.julialang.org/t/controlsystems-frequency-in-hz-for-bodeplot/12683/1 "2018-07-27T11:47:09Z")

</div>

Hello,

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

Thanks in advance.  
Patrick.

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [July 27, 2018, 1:04pm UTC](https://discourse.julialang.org/t/controlsystems-frequency-in-hz-for-bodeplot/12683/2 "2018-07-27T13:04:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Patrick\_Dubus](https://avatars.discourse-cdn.com/v4/letter/p/c77e96/32.png) [@Patrick\_Dubus](https://discourse.julialang.org/u/Patrick_Dubus)\
**Post date:** [July 27, 2018, 2:06pm UTC](https://discourse.julialang.org/t/controlsystems-frequency-in-hz-for-bodeplot/12683/3 "2018-07-27T14:06:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [July 27, 2018, 2:19pm UTC](https://discourse.julialang.org/t/controlsystems-frequency-in-hz-for-bodeplot/12683/4 "2018-07-27T14:19:33Z")

</div>

You can always do

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

```

and if on master branch, I think you can do

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

```

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

---

<div class="post-metadata">

**Author:** ![ssfrr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ssfrr/32/3736_2.png) [@ssfrr](https://discourse.julialang.org/u/ssfrr)\
**Post date:** [July 27, 2018, 4:34pm UTC](https://discourse.julialang.org/t/controlsystems-frequency-in-hz-for-bodeplot/12683/5 "2018-07-27T16:34:18Z")

</div>

Also you could check out the [`SampledSignals`](https://github.com/JuliaAudio/SampledSignals.jl) 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

```julia
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.

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [July 27, 2018, 5:20pm UTC](https://discourse.julialang.org/t/controlsystems-frequency-in-hz-for-bodeplot/12683/6 "2018-07-27T17:20:48Z")

</div>

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… 🙂 ).

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [July 27, 2018, 5:40pm UTC](https://discourse.julialang.org/t/controlsystems-frequency-in-hz-for-bodeplot/12683/7 "2018-07-27T17:40:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [July 28, 2018, 7:14am UTC](https://discourse.julialang.org/t/controlsystems-frequency-in-hz-for-bodeplot/12683/8 "2018-07-28T07:14:03Z")

</div>

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

> <https://github.com/JuliaControl/ControlSystems.jl/blob/master/src/plotting.jl#L299>

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [July 30, 2018, 8:49am UTC](https://discourse.julialang.org/t/controlsystems-frequency-in-hz-for-bodeplot/12683/9 "2018-07-30T08:49:28Z")

</div>

Testing plotting of ControlSystems systems… I have used `julia> using ControlSystems`. Then

```julia
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:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/c/7/c72a5ac0ead23d10735a795f31e541ed284e5528.png)

… 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`.

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

```

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

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

```

Thus, to plot the magnitude…

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

```

leads to:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/e/7e65d32c008fed648ad0de6a6115108c9ea1e150.png)

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
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)"])

```

leads to:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/a/9/a9c441a7338d28da4a17cb6cfd0b661e9548d34a.png)

---

<div class="post-metadata">

**Author:** ![mfalt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mfalt/32/12467_2.png) [@mfalt](https://discourse.julialang.org/u/mfalt)\
**Post date:** [July 30, 2018, 3:31pm UTC](https://discourse.julialang.org/t/controlsystems-frequency-in-hz-for-bodeplot/12683/10 "2018-07-30T15:31:50Z")

</div>

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

```julia
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")`:

```julia
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

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

```

or as a zero-pole-gain system

```julia
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

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

```
