MATLAB's filtfilt() Algorithm
algorithm, filtering, matlab
Solution
The filtfilt algorithm matches the initial conditions on the filter to minimise start and end transients (from the `doc filtfilt`). If you type `edit filtfilt` you can see the code - there is a function `getCoeffsAndInitialConditions(b,a,Npts)` that will show you the details of this.
Problem
I'm trying to reproduce a lengthy piece of MATLAB code in another language which has no built-in equivalent of the phaseless filter, `filtfilt()`. I'm trying to re-cast the function in terms of simple filtering (or convolution) operations so I can easily reproduce it. I understand that this filtering operation is equivalent to forward filtering, followed by reverse filtering, but I'm seeing small differences at the edges of the data. Specifically: ``` data = [1 1 1 2 2 3 5 7 1 1 1 1 1]; ker = [2 1 1]; a = filtfilt(ker,1,data) b = fliplr( filter( ker, 1, fliplr( filter(ker, 1, data) ) ) ) % a = % % 16 18 21 29 39 57 66 68 42 28 16 16 16 % % b = % % 11 16 21 29 39 57 66 68 42 28 16 12 8 ``` I've tried padding the data with zeros at one or both ends, before one or both of the filtering operations. I think I'm likely missing something obvious, but can't spot it.