Butterworth Filter compared to Butter in Matlab

Hello,

I am not yet fully familiar with Julia and am currently struggling with the DSP / Butterworth filter. My original Matlab-Code is

x = linspace(0, 10, 100);
y1 = sin(x);
[b,a] = butter(3,0.1,'high')
y2 = filter(b,a,y1)

plot(x, y1);
hold on;
plot(x, y2);
hold off;
grid;

I got the following filter parameter and the following result of the Butterworth/Highpass filter (here as a plot, blue: input signal, red: output signal)

a = [1,-2.374094743709352,1.929355669091215,-0.532075368312092]
b = [0.729440722639082,-2.188322167917247,2.188322167917247,-0.729440722639082]

I tried to rewrite the code as seen below, but I got significant different results: Corresponding to a and b of the former result, I got

ZeroPoleGain{:z, ComplexF64, ComplexF64, Float64}(
ComplexF64[1.0 + 0.0im, 1.0 + 0.0im, 1.0 + 0.0im],
ComplexF64[0.8237761078519955 - 0.2318012972462002im, 0.8237761078519955 + 0.2318012972462002im, 0.726542528005361 - 0.0im], 0.7294407226390823)

which now includes complex data (the real part is also different) and with the plot

filter_julia

using Plots;
using DSP;

function highpassfilter(signals, cutoff, order)
    filth = digitalfilter(Highpass(cutoff), Butterworth(order));
    println(filth);
    filtfilt(filth, signals);
end

x  = vcat(range(0,10,100));
y1 = sin.(x);
y2 = highpassfilter(y1,0.1,3);
println(y2);

img = plot(x, [y1,y2], gridlinewidth=1, xticks = 0:1:10, yticks = -1:0.2:1, xlims = (0,10), ylims = (-1,1))

Did I use the functions correctly or did I use the wrong functions?

Maybe someone has an idea what is wrong here.

Best wishes,

Martin

1 Like

You are calling filtfilt in julia but not in matlab. filtfilt is a zero-phase filter that applies the filter twice, once forward and once backwards.

3 Likes

Yes, that makes sense.

Thank you for the hint.

Kind regards,

Martin