Reproducing MATLAB's `filter` behavior

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

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 :point_down:

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?

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

y0_filt = filt(b, a, u_mat')

then:

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.

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