# Reproducing MATLAB's \`filter\` behavior

**URL:** <https://discourse.julialang.org/t/reproducing-matlabs-filter-behavior/107619>\
**Category:** Specific Domains\
**Tags:** question, matlab, dsp\
**Created:** [December 14, 2023, 4:21pm UTC](https://discourse.julialang.org/t/reproducing-matlabs-filter-behavior/107619 "2023-12-14T16:21:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Abdulrhmn\_Ghanem](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abdulrhmn_ghanem/32/205482_2.png) [@Abdulrhmn\_Ghanem](https://discourse.julialang.org/u/Abdulrhmn_Ghanem)\
**Post date:** [December 14, 2023, 4:21pm UTC](https://discourse.julialang.org/t/reproducing-matlabs-filter-behavior/107619/1 "2023-12-14T16:21:12Z")

</div>

Hi  
I am trying to reproduce the following matlab code using julia.

```mat
NEst = 1000 % length estimation record

[b,a] = cheby1(3, 0.5, 2*[0.15 0.3]); % the coefficients of the true IIR system

% generate the test data with random input
u = randn(1, NEst); % exact input
y0 = filter(b,a,u); % exact output

```

It generates a vector of uniformally distributed data. Then it passes it through a bandpass chebyshev filter.

To remove the randomness element form the comparsion I exported `u` from matlab and used it directly with the julia `filt`.

Here is the Julia implementation 👇

```jl
import Pkg
Pkg.add(["DSP", "HDF5", "FileIO"])
using DSP, HDF5, FileIO

data = h5open(download("https://github.com/AbdulrhmnGhanem/PracticalMATLABModelingWithSimulink.jl/raw/main/src/SysID.jl/e11.h5"))
u_mat = read(data["u/value"])
a_mat = read(data["a/value"])
b_mat = read(data["b/value"])
y0_mat = read(data["y0/value"])

n = 3
r = 0.5
wp = 2*[0.15, 0.3]

h = digitalfilter(Bandpass(wp...), Chebyshev1(n, r))
tf = convert(PolynomialRatio, h)
b, a = coefb(tf), coefa(tf)

# both Julia's and MATLAB's filters have the same coffiecnets
@assert b' ≈ b_mat  
@assert a' ≈ a_mat

y0_filt = filt(b, a, u_mat)
y0_filtfilt = filtfilt(b, a, u_mat')

println(y0_filt ≈ y0_mat) # false
println(y0_filtfilt' ≈ y0_mat) # false

```

What am I doing wrong that cuauses a different result compared to matlab?

---

<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:** [December 14, 2023, 5:39pm UTC](https://discourse.julialang.org/t/reproducing-matlabs-filter-behavior/107619/2 "2023-12-14T17:39:17Z")

</div>

In Julia, `u_mat` is a `1x1000 matrix` and we should input this as a vector (or adjoint) to `filt()`:

```julia
y0_filt = filt(b, a, u_mat')

```

then:

```julia
y0_filt ≈ y0_mat' # true

```

PS:  
Otherwise, `filt()` would be applied along the first dimension of matrix `u_mat`, i.e., 1000 times, once for each entry.

---

<div class="post-metadata">

**Author:** ![Abdulrhmn\_Ghanem](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abdulrhmn_ghanem/32/205482_2.png) [@Abdulrhmn\_Ghanem](https://discourse.julialang.org/u/Abdulrhmn_Ghanem)\
**Post date:** [December 14, 2023, 5:50pm UTC](https://discourse.julialang.org/t/reproducing-matlabs-filter-behavior/107619/3 "2023-12-14T17:50:08Z")

</div>

Ah, thank you for your quick reply!  
I can’t believe that the matlab to julia dimension conversion still gets the best of me 😂
