# DSP.jl: analog filter design

**URL:** <https://discourse.julialang.org/t/dsp-jl-analog-filter-design/33646>\
**Category:** General Usage\
**Tags:** dsp\
**Created:** [January 22, 2020, 1:22am UTC](https://discourse.julialang.org/t/dsp-jl-analog-filter-design/33646 "2020-01-22T01:22:13Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![kumarbalachandran](https://avatars.discourse-cdn.com/v4/letter/k/9f8e36/32.png) [@kumarbalachandran](https://discourse.julialang.org/u/kumarbalachandran)\
**Post date:** [January 22, 2020, 1:22am UTC](https://discourse.julialang.org/t/dsp-jl-analog-filter-design/33646/1 "2020-01-22T01:22:14Z")

</div>

I am using the following on Julia 1.3.1:

```julia
using DSP
    fs=4*5000e6
    responsetype = Highpass(4000e6; fs=fs)
    designmethod = Elliptic(8, 1.0, 40.0)
    fil=analogfilter(responsetype, designmethod)
    rng=range(3100e6, stop=4200e6, length=500)
    resp=freqs(fil, rng, fs)

    using Plots
    plot(collect(rng)/1e9, (abs.(resp)))

```

and I get the plot

 ![image](https://global.discourse-cdn.com/julialang/original/2X/9/98716c992aa6cbb48db400e54ad978b6e9c1635a.png)

This is obviously not a high pass response. Can I get some guidance on what I am doing wrong? I am able to get the equivalent Matlab design working, so I know the design is feasible. My conclusion is that I have not understood the documentation.

Kumar

---

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [January 22, 2020, 1:40am UTC](https://discourse.julialang.org/t/dsp-jl-analog-filter-design/33646/2 "2020-01-22T01:40:34Z")

</div>

> [@kumarbalachandran](#):
>
> using DSP fs=4\*5000e6 responsetype = Highpass(4000e6; fs=fs) designmethod = Elliptic(8, 1.0, 40.0) fil=analogfilter(responsetype, designmethod) rng=range(3100e6, stop=4200e6, length=500) resp=freqs(fil, rng, fs) using Plots plot(collect(rng)/1e9, (abs.(resp)))

First tell us what you are trying to do

Second put in all the units in comments

```julia
using DSP
    fs=4*5000e6 # what unit? Hz or rad/sec or cycles/sec
    # 4000e6 what unit? Hz or rad/sec or cycles/sec
    responsetype = Highpass(4000e6; fs=fs)
    designmethod = Elliptic(8, 1.0, 40.0)
    fil=analogfilter(responsetype, designmethod)
    # what unit? Hz or rad/sec or cycles/sec
    rng=range(3100e6, stop=4200e6, length=500)
    resp=freqs(fil, rng, fs)

    using Plots
    plot(collect(rng)/1e9, (abs.(resp)))

```

---

<div class="post-metadata">

**Author:** ![kumarbalachandran](https://avatars.discourse-cdn.com/v4/letter/k/9f8e36/32.png) [@kumarbalachandran](https://discourse.julialang.org/u/kumarbalachandran)\
**Post date:** [January 22, 2020, 2:27am UTC](https://discourse.julialang.org/t/dsp-jl-analog-filter-design/33646/3 "2020-01-22T02:27:54Z")

</div>

All units are in Hz. Final plotting is in GHz

```julia
using DSP
    fs=4*5000e6 # Hz
    # 4000e6 Hz 
    responsetype = Highpass(4000e6; fs=fs)
    designmethod = Elliptic(8, 1.0, 40.0)
    fil=analogfilter(responsetype, designmethod)
    # what unit? Hz 
    rng=range(3100e6, stop=4200e6, length=500)
    resp=freqs(fil, rng, fs)

    using Plots
    plot(collect(rng)/1e9, (abs.(resp)))

```

---

<div class="post-metadata">

**Author:** ![mbaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbaz/32/17295_2.png) [@mbaz](https://discourse.julialang.org/u/mbaz)\
**Post date:** [January 22, 2020, 2:45am UTC](https://discourse.julialang.org/t/dsp-jl-analog-filter-design/33646/4 "2020-01-22T02:45:39Z")

</div>

If you plot a larger range you’ll see an HPF response – I plotted from 100 MHz to 5 GHz. Still, the cutoff frequency seems to be wrong (around 1.2 GHz), and the passband ripple seems a little high.

---

<div class="post-metadata">

**Author:** ![kumarbalachandran](https://avatars.discourse-cdn.com/v4/letter/k/9f8e36/32.png) [@kumarbalachandran](https://discourse.julialang.org/u/kumarbalachandran)\
**Post date:** [January 22, 2020, 2:05pm UTC](https://discourse.julialang.org/t/dsp-jl-analog-filter-design/33646/5 "2020-01-22T14:05:01Z")

</div>

Thanks. Here is an experiment and more detailed comments in the code.

```julia

# Test script to design an analog highpass elliptic filter with 8 poles and a cutoff 
# frequency of 4000 Hz. Various pressing questions arise:
# 1. What is the scaling of parameters for cutoff frequencies?
# 2. Why does an analog filter need a sampling frequency at all?
# 3. Why does the freqs function need a sampling frequency at all? I can
# understand this for freqz, but 's'?

using DSP
using Plots

fs=4*5000e6 #Hz
# 4 GHz cutoff and a 20 GHz sampling freqency! Why 'fs" at all?
# Should there be a HighpassAnalog function?
responsetype = Highpass(4000e6; fs=fs)
# This at least is okay
designmethod = Elliptic(8, 1.0, 40.0)
# We want ANALOG, not digital
fil=analogfilter(responsetype, designmethod)

# Lets try to plot the response
rng=range(100e6, stop=4200e6, length=500)
resp=freqs(fil, rng, fs)
# The x-axis is in GHz
plot(collect(rng)/1e9, 20*log10.((abs.(resp))))

```

This gives the following plot

 ![image](https://global.discourse-cdn.com/julialang/original/2X/9/98716c992aa6cbb48db400e54ad978b6e9c1635a.png)

while if I scale the x-axis as

```julia
# The x-axis is in GHz scaled by π
plot(collect(rng)/1e9 * π, 20*log10.((abs.(resp))))

```

I get (voila!)

 ![ellip2](https://global.discourse-cdn.com/julialang/original/3X/f/1/f1e02e9de6b7b9bf9f49ca7b2e95ca9e361ddc88.png)

which does not make sense at all. However, there is clearly some factor of π missing in the DSP documentation as the cutoff is right where I want it to be in the plot. However I have no idea yet whether the filter is designed correctly.

I think the whole analogfilter design is broken. Please see the comments at the top of the code.

---

<div class="post-metadata">

**Author:** ![mbaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbaz/32/17295_2.png) [@mbaz](https://discourse.julialang.org/u/mbaz)\
**Post date:** [January 22, 2020, 10:21pm UTC](https://discourse.julialang.org/t/dsp-jl-analog-filter-design/33646/6 "2020-01-22T22:21:23Z")

</div>

If you use `digitalfilter`, the resulting cutoff frequency is correct at 4 GHz. My opinion is that this is a bug; you should file an issue.

---

<div class="post-metadata">

**Author:** ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)\
**Post date:** [January 23, 2020, 12:22am UTC](https://discourse.julialang.org/t/dsp-jl-analog-filter-design/33646/7 "2020-01-23T00:22:17Z")

</div>

You say

> [@kumarbalachandran](#):
>
> However I have no idea yet whether the filter is designed correctly.

sure you do.  
you asked for 40dB stop band ripple and got 40dB stop band ripple.  
you asked for 1dB passband ripple, and that looks about like what you got.  
the frequency cutoff looks like a radian/hertz problem, but after you accound for that it’s in the right place.

> [@kumarbalachandran](#):
>
> I think the whole analogfilter design is broken.

That seems like too strong a statement. The /pi problem definitely seems buggish, and is almost certainly due to radian/hertz confusion, but otherwise it designed an elliptic filter for you and it looks like it met the design parameters.

---

<div class="post-metadata">

**Author:** ![mbaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbaz/32/17295_2.png) [@mbaz](https://discourse.julialang.org/u/mbaz)\
**Post date:** [January 23, 2020, 12:51am UTC](https://discourse.julialang.org/t/dsp-jl-analog-filter-design/33646/8 "2020-01-23T00:51:44Z")

</div>

> [@purplishrock](#):
>
> The /pi problem definitely seems buggish

Agreed – this is what I think is a bug.

---

<div class="post-metadata">

**Author:** ![kumarbalachandran](https://avatars.discourse-cdn.com/v4/letter/k/9f8e36/32.png) [@kumarbalachandran](https://discourse.julialang.org/u/kumarbalachandran)\
**Post date:** [January 23, 2020, 2:20am UTC](https://discourse.julialang.org/t/dsp-jl-analog-filter-design/33646/9 "2020-01-23T02:20:53Z")

</div>

I also think the demand for a sampling frequency for a continuous time filter is an interface design issue. Moreover, why should freqs also require an fs parameter for an analog filter?

You are correct that a filter is being designed, and it is elliptical. I wonder if its a problem with the documentation and I just don’t fathom what the parameters mean.

---

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [January 23, 2020, 5:51am UTC](https://discourse.julialang.org/t/dsp-jl-analog-filter-design/33646/10 "2020-01-23T05:51:54Z")

</div>

![reason](https://global.discourse-cdn.com/julialang/original/3X/a/e/aede45e4a33a47d808387d69cb9f8c610b98a388.png)

I think this is the reason

You asked for a cutoff at 4GHz but you got a cutoff at (4/Pi) GHz

1 Hz = 2 Pi rads/sec = 1 cycle/sec

(4 / Pi) GHz = (4 \* 2 Pi / Pi) rads/sec = (4 / Pi) Cycle /sec

So perhaps the unit of Wn is in “Cycles / sec” and not Hz

---

<div class="post-metadata">

**Author:** ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)\
**Post date:** [January 23, 2020, 4:49pm UTC](https://discourse.julialang.org/t/dsp-jl-analog-filter-design/33646/11 "2020-01-23T16:49:11Z")

</div>

I have to admit “half-cycles/sample” is not particularly clear, and of course has absolutely no meaning for a continuous time filter.

I’m not even exactly sure what it means in the context of a digital filter.

> [@kumarbalachandran](#):
>
> why should freqs also require an fs parameter for an analog filter?

A good question, why indeed.

Generally speaking the DSP library is very, very good. I did industrial strength work with it a while back and every time i thought i found a problem, the problem was me. lol.

---

<div class="post-metadata">

**Author:** ![kumarbalachandran](https://avatars.discourse-cdn.com/v4/letter/k/9f8e36/32.png) [@kumarbalachandran](https://discourse.julialang.org/u/kumarbalachandran)\
**Post date:** [January 27, 2020, 5:54am UTC](https://discourse.julialang.org/t/dsp-jl-analog-filter-design/33646/12 "2020-01-27T05:54:14Z")

</div>

This issue is a bug and the detailed discussion with a possible solution is in [analogfilter design seems to be broken · Issue #341 · JuliaDSP/DSP.jl · GitHub](https://github.com/JuliaDSP/DSP.jl/issues/341)

Thanks for the discussions that got me strated down this rabbit hole 🙂
