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
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
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...