# FIR Filter Implementation in Julia

**URL:** <https://discourse.julialang.org/t/fir-filter-implementation-in-julia/81331>\
**Category:** Signal and Image Processing\
**Tags:** dsp, digital-filters\
**Created:** [May 19, 2022, 8:21pm UTC](https://discourse.julialang.org/t/fir-filter-implementation-in-julia/81331 "2022-05-19T20:21:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![duodenum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/duodenum/32/36258_2.png) [@duodenum](https://discourse.julialang.org/u/duodenum)\
**Post date:** [May 19, 2022, 8:21pm UTC](https://discourse.julialang.org/t/fir-filter-implementation-in-julia/81331/1 "2022-05-19T20:21:13Z")

</div>

Hello all,

I’m trying to replicate this MATLAB code in Julia for zero-phase filtering:

```julia
d = designfilt('bandpassfir', 'StopbandFrequency1', 1e-9, ...
    'PassbandFrequency1', 35, 'PassbandFrequency2', 45, ...
    'StopbandFrequency2', 50, 'StopbandAttenuation1', 120, ...
    'PassbandRipple', 1, 'StopbandAttenuation2', 120, 'SampleRate', 100);

```

Which results in a filter with the following properties:

```julia
d = 

 digitalFilter with properties:

            Coefficients: [1×371 double]

   Specifications:
       FrequencyResponse: 'bandpass'
         ImpulseResponse: 'fir'
              SampleRate: 100
    StopbandAttenuation1: 120
      PassbandFrequency1: 1
      StopbandFrequency2: 50
          PassbandRipple: 1
      PassbandFrequency2: 5
    StopbandAttenuation2: 120
      StopbandFrequency1: 1.0000e-09
            DesignMethod: 'equiripple'

```

And I use this in filtfilt:

```julia
filteredData = filtfilt(d, data)

```

However, I’m having difficulty with the DSP package to replicate this. The FIR method asks for a window and I couldn’t find the equiripple design method for FIR in the documentation. Can anybody help me out?

Thanks,  
Yasir

---

<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:** [May 19, 2022, 9:41pm UTC](https://discourse.julialang.org/t/fir-filter-implementation-in-julia/81331/2 "2022-05-19T21:41:32Z")

</div>

I don’t think DSP.jl supports as many design techniques as Matlab does. You can approximate the same filter response using `remez`:

```julia
fil = remez(371, [0, 34, 35, 45, 46, 50], [0, 1, 0], Hz=100, maxiter=100);

```

Alternatively, just go with a FIR window design:

```julia
fil = digitalfilter(Bandpass(35, 45, fs=100), FIRWindow(blackman(371)));

```

which actually produces better stopband rejection. I don’t think any of these two methods reaches 120 dB of rejection, though.

---

<div class="post-metadata">

**Author:** ![duodenum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/duodenum/32/36258_2.png) [@duodenum](https://discourse.julialang.org/u/duodenum)\
**Post date:** [May 20, 2022, 5:26am UTC](https://discourse.julialang.org/t/fir-filter-implementation-in-julia/81331/3 "2022-05-20T05:26:25Z")

</div>

Thanks a lot! I’ll use the FIR window then, 120 dB was a bit overkill so something around 60 would do the trick just as well.
