filtfilt
Hi sam_manchali,
Imagine you have a sequence x[n] with Z-transform X(z). Let us pass it by a filter with impulse response h[n], but taking x[n] in reversed order, say x[-n], whose Z-transform is X(z^-1). The obtained sequence has Z-transform X(z^-1)*H(z). If you take this output in reversed order, the result is Y(z)=X(z)*H(z^-1). Otherwise stated, it is the result of filtering the original sequence with a filter with transfer function H(z^-1), or impulse response h[-n], that is, h[n] reversed in time.
If the filter h[n] is causal, h[-n] is anticausal. The amplitudes of H(z^-1) and H(z) are the same, but the phases are opposite. If H(z) has a pole (or zero) at Zp (say inside the unit circle), H(z^-1) has a pole (or zero) at Zp^-1 (outside the unit circle).
Now, take Y(z) and apply to it the filter H(z) again. You obtain X(z)*H(z^-1)*H(z). You get the original sequence X(z) filtered by a filter H(z^-1)*H(z), that has zero-phase becuase the phases of H(z) and H(z^-1) compensate. The resulting impulse response is the convolution of h[n] with h[-n], that is symmetrical in time (as expected for a funcion with zero phase). The resulting transfer funcion is |H(z)|^2 (with zero phase).
Clearly, the two stages (filtering forward and backward) can be performed in any order.
This is the kind of processing filtfilt() performs.
Pay attention that if you use an IIR filter, the sequence obtained after the first filtering (y[-n]) extends to infinite in time, and when you take it in reverse order you have to truncate it in time, and an error arises if you don’t compensate the initial conditions. The filtfilt() function takes into account this effect.
The problem is that this technique can be applied only to finite sequences, and not in real-time, because you need to be able to take a sequence in both orders (forward and reversed). This is a serious limitation for many applications.
I hope this is useful.
Regards
Z