# Bode plot of DSP filter?

**URL:** https://discourse.julialang.org/t/bode-plot-of-dsp-filter/122845
**Category:** Signal and Image Processing
**Tags:** question, package, dsp
**Created:** [November 20, 2024, 10:55am UTC](https://discourse.julialang.org/t/bode-plot-of-dsp-filter/122845 "2024-11-20T10:55:32Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [November 20, 2024, 10:55am UTC](https://discourse.julialang.org/t/bode-plot-of-dsp-filter/122845/1 "2024-11-20T10:55:32Z")

</div>

I designed a discrete Butterworth filter using the DSP package. How can I create a Bode plot of the transfer function?

```julia
using DSP

function create_filter(cut_off_freq; order=4, type=:Butter, dt)
    if type == :Butter
        return Filters.digitalfilter(Filters.Lowpass(cut_off_freq; fs=1/dt), Filters.Butterworth(order))
    elseif type == :Cheby1
        return Filters.digitalfilter(Filters.Lowpass(cut_off_freq; fs=1/dt), Filters.Chebyshev1(order, 0.01))
    end
end

dt = 0.05
cut_off_freq = 2.0 # Hz
butter = create_filter(cut_off_freq; order=4, dt)

```

---

<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: [November 20, 2024, 11:17am UTC](https://discourse.julialang.org/t/bode-plot-of-dsp-filter/122845/2 "2024-11-20T11:17:12Z")

</div>

```julia
using ControlSystemsBase, Plots
bodeplot(tf(butter))

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/0/6/0627f1ea18ba1d032aa08e9d022196d523fa86d8.png)

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [November 24, 2024, 12:17am UTC](https://discourse.julialang.org/t/bode-plot-of-dsp-filter/122845/3 "2024-11-24T00:17:22Z")

</div>

Using only DSP.jl, you could do:

```julia
using DSP, Plots; gr()
default(fontfamily="Computer Modern", framestyle=:box, dpi=600)

w = range(1e-9, pi, 1024)
h = freqz(butter, w)

ws = (w/pi)*(0.5/dt)
p1 = plot(ws, 20*log10.(abs.(h)), xlims=(1e-2, 10), ylims=(-60, 6), xscale=:log10, xlabel="", ylabel="Magnitude (db)", label=false, title="2Hz Lowpass 4th-order Butterworth", lw=2, c=:blue, minorgrid=true)
p2 = plot(ws, 180/pi*unwrap(angle.(h)), xlims=(1e-2, 10), ylims=(-360, 6), xscale=:log10, xlabel="Frequency (Hz)",ylabel="Phase (deg)", label=false, lw=2, c=:red, minorgrid=true)
plot(p1, p2, layout=(2,1), size=(800, 800))

```

which produces:

 ![Plots_Bode_loglog_DSP_Butterworth_filter_response](https://global.discourse-cdn.com/julialang/original/3X/e/2/e2ccc492456ca33d1230f3cb86d1d9c018fb364b.png)

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [November 24, 2024, 4:35am UTC](https://discourse.julialang.org/t/bode-plot-of-dsp-filter/122845/4 "2024-11-24T04:35:26Z")

</div>

Thanks! But there is a small mistake in your code,it must be:

```julia
tf(butter, dt)

```

with `dt` being the sampling time, otherwise the result is wrong.  
I used this [script](https://github.com/aenarete/DiscreteFilters.jl/blob/main/examples/test_butter.jl) to create this plot:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/9/d954a96244162a7ed346c7e8cf2afd4b3d7ce960.jpeg)

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [November 24, 2024, 4:40am UTC](https://discourse.julialang.org/t/bode-plot-of-dsp-filter/122845/5 "2024-11-24T04:40:18Z")

</div>

Thanks! Always good to have less package dependencies.

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [November 25, 2024, 7:48pm UTC](https://discourse.julialang.org/t/bode-plot-of-dsp-filter/122845/6 "2024-11-25T19:48:00Z")

</div>

Added `minorgrid=true`, which was an important omission for this type of logarithmic scale plots.
