The logic used by you is perfectly correct and works well.
Cause of issue:
whose 3db frequency is 0.25 Hz. The [b,a] coefficients of the filter were evaluated as shown in the below code:
I obtained the following outputs.
It is seen that your filter is becoming unstable.
SOLUTION:
Please note that in the transfer function of a Butterworth filter the coefficient ( a(1) ) is not equal to 1. However, the user-defined function "Sequential_filter" that you provided requires ( a(1) = 1 ) to comply with the "direct form 2" syntax. Therefore, it is necessary to normalize the coefficients ([b, a]) so that ( a(1) = 1 ), as demonstrated below:
This resolves the issue you are facing.
I am attaching the code below:
sos = [2 4 2 6 0 2;3 3 0 6 0 0];
[b,a] = sos2tf(sos,1)
b =
6 18 18 6
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
a =
36 0 12 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
x_in=sin(2*pi*0.25*(0:0.001:4));
y_out1 = filter(b,a,x_in);
y_out2 = zeros(1,length(x_in));
[y_out2(n),w_buff] = Sequential_filter(b,a,x_in(n),w_buff);
function [yn,w_buff] = Sequential_filter(b,a,xn,w_buff)
w_buff = circshift(w_buff,1,1);
wn = xn - a(1,2:end)*w_buff(2:end,1);
I hope this helps !