Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

FiltFilt() convertion into ANSI C code

1,102 views
Skip to first unread message

andreasem Seminatore

unread,
Nov 23, 2010, 5:43:17 AM11/23/10
to
Hi All,

I'm trying to implement a C program that emulate the filtfilt() function.
I wrote a code for filter() function as you can look in the bottom code sequence.
After filtering in the forward direction, the filtered sequence is then reversed and run back through the filter (filter twice).

//IIF Filter 3° Order
static void iir3f(double *input,double *output, int size, double *A, double *B,double gain) {
printf("\nLPF3 F %d\n",sizeof(double)*size);
int i;
for(i=0;i<size;i++)
{

in[0] = in[1];
in[1] = in[2];
in[2] = input[i];
// Shift previous outputs and calculate new output
out[0] = out[1];
out[1] = out[2];
out[2] = B[0] * in[2] + B[1] * in[1] + B[2] * in[0] - A[1] * out[1] - A[2] * out[0];
output[i]=out[2];

// Shift previous outputs and calculate new output
tmp = A[3] * previous_out + (output[i] + previous_in);
previous_out = tmp;
previous_in = output[i];
output[i]=tmp*gain;
}

}

The problem is the presence of an initail and final transient. I know that this problem can be solved using initial condition but, I do not know how this can be done.

Thank's in advance for any contibution that you'll give me

Andrea

Wayne King

unread,
Nov 23, 2010, 5:57:05 AM11/23/10
to
"andreasem Seminatore" <Antonio.S...@ansaldo-sts.com> wrote in message <icg5s5$8ci$1...@fred.mathworks.com>...

Hi Andrea, If you have the Signal Processing Toolbox, you can look at the MATLAB code for filtfilt(). That is one of the nice things about MATLAB, the implementation is usually open for you to see. That is absolutely true for filtfilt(). You can see how the transients are handled in MATLAB code and translate that to C.

Wayne

andreasem Seminatore

unread,
Nov 23, 2010, 6:18:03 AM11/23/10
to
"Wayne King" <wmki...@gmail.com> wrote in message <icg6m1$sff$1...@fred.mathworks.com>...


Thank's Wayne,

I have already looked the file filtfilt.m
The file shows you how the initial conditions are calculated, then these values are provided to Matlab filter() function.
The implementation of filter() function is hide.
Essentially I know how calculate the initial condition's but I don't know how filter use these values.
I have implemented filter function without initial conditions...

Matthew Askar

unread,
Jul 23, 2021, 4:39:01 PM7/23/21
to
Hey Andrea,

Did you ever end up solving your problem? I'm having the same problem with translating filtfilt where the inital and final transients are causing problems. Please, let me know if you found any good conversion online or translation of the code.

Best,
Matt

Ayşe Gül Gökdemir

unread,
Sep 27, 2021, 4:33:50 PM9/27/21
to
23 Temmuz 2021 Cuma tarihinde saat 23:39:01 UTC+3 itibarıyla Matthew Askar şunları yazdı:
https://blog.fearcat.in/a?ID=01550-c703f0dc-0af2-4066-a1b9-03ce13146e75
0 new messages