# Implementation of the matlab function filtfilt in C language

72 views (last 30 days)
afef on 18 Aug 2017
Edited: Oybek Amonov on 5 Nov 2020
I used in Matlab the function filtfilt for my Low pass fir filter :
d1=designfilt('lowpassfir','PassbandFrequency',0.45,'StopbandFrequency',0.5,'Pass bandRipple',3,'StopbandAttenuation',60,'DesignMethod','equiripple'); a = filtfilt(d1,deriv1);
And I tried to implement this function in C language I don't get the same output that I get with Matlab and I don't understand the problem. Can anyone help me please?
This is the code:
#define ORDER 65
#define NP 2560
filter(int ord, float *a, float *b, int np, float *x, float *y) {
int i,j;
y=b*x;
for (i=1;i<ord+1;i++) {
y[i]=0.0;
for (j=0;j<i+1;j++)
y[i]=y[i]+b[j]*x[i-j];
for (j=0;j<i;j++)
y[i]=y[i]-a[j+1]*y[i-j-1];
}
for (i=ord+1;i<np+1;i++) {
y[i]=0.0;
for (j=0;j<ord+1;j++)
y[i]=y[i]+b[j]*x[i-j];
for (j=0;j<ord;j++)
y[i]=y[i]-a[j+1]*y[i-j-1];
}
}
main()
{
float x[NP]= { -84.786,0.000,0.000,0.000,0.000,-0.781....};
float y[NP],a[ORDER+1],b[ORDER+1];
int i,j;
for (i=0;i<NP;i++)
printf("%f\n",x[i]);
/* test coef from
[b,a]=butter(3,30/500); in MATLAB
*/
b=-0.0056;
b=-0.0202;
b=-0.0341;
b=-0.0303;
b=-0.0058;
b= 0.0157;
b=0.0117;
b=-0.0081;
b=-0.0126;
b=0.0038;
b=0.0133;
b=-0.000;
b=-0.0139;
b=-0.0027;
b=0.0147;
b=0.0065;
b=-0.0150;
b=-0.0110;
b=0.0147;
b=0.0163;
b=-0.0134;
b=-0.0228;
b=0.0107;
b=0.0308;
b=-0.0057;
b=-0.0412;
b=-0.0030;
b=0.0561;
b=0.0197;
b=-0.0833;
b=-0.0617;
b=0.1726;
b=0.4244;
b=0.4244;
b=0.1726;
b=-0.0617;
b=-0.0833;
b=0.0197;
b=0.0561;
b=-0.0030;
b=-0.0412;
b=-0.0057;
b=0.0308;
b=0.0107;
b=-0.0228;
b=-0.0134;
b=0.0163;
b=0.0147;
b=-0.0110;
b=-0.0150;
b=0.0065;
b=0.0147;
b=-0.0027;
b=-0.0139;
b=-0.0005;
b=0.0133;
b=0.0038;
b=-0.0126;
b=-0.0081;
b=0.0117;
b=0.0157;
b=-0.0058;
b=-0.0303;
b=-0.0341;
b=-0.0202;
b=-0.0056;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
a=0;
filter(ORDER,a,b,NP,x,y);
for (i=0;i<NP;i++)
{ x[i]=y[NP-i-1];}
filter(ORDER,a,b,NP,x,y);
for (i=0;i<NP;i++)
{ x[i]=y[NP-i-1];}
for (i=0;i<NP;i++)
{ y[i]=x[i];}
for (i=0;i<NP;i++)
printf("%f\n",y[i]);
}

Michele Giugliano on 20 Mar 2018
Not sure it (still) helps you: the MATLAB filtfilt.m implementation has an internal method to reduce edge effects, while initialising the internal forward and backward filtering. Have a look at filtfilt.m and at the paper cited therein:
https://pdfs.semanticscholar.org/f98b/09d9ef5b8cedfc7d1de0138f9d13b9b87b58.pdf

Jan on 21 Mar 2018
Edited: Jan on 21 Mar 2018
See https://www.mathworks.com/matlabcentral/fileexchange/32261-filterm for an implementation of FILTFILT. Here the treatment of the initial and final sequence is process in Matlab and only the filtering in implemented in C.
This is not correct:
for (j=0;j<i+1;j++)
y[i]=y[i]+b[j]*x[i-j];
for (j=0;j<i;j++)
y[i]=y[i]-a[j+1]*y[i-j-1];
a and b must be applied simultaneously and you need to store the intermediate states. See this M-version of the code:
function [Y, z] = emulateFILTER(b, a, X, z)
b = b ./ a(1);
a = a ./ a(1);
n = length(a);
z(n) = 0;
Y = zeros(size(X));
for m = 1:length(Y)
Xm = X(m);
Y(m) = b(1) * Xm + z(1);
Ym = Y(m);
for i = 2:n
z(i - 1) = b(i) * Xm + z(i) - a(i) * Ym;
end
end
z = z(1:n - 1);
Your hard-coded filter parameters have 3 significant digits only. This will cause inaccurate results in addition.
Oybek Amonov on 5 Nov 2020
Is z the actual result here? I do not understand what z is here. As I understand it, Y(m) is the result of forward filtering, so I though z would be the actual zero-phase filtering result.