It is not possible to execute a filter with a zero phase in real time because a filter with a zero phase requires filter coefficients that are symmetrical around zero. This means that the filter is not causal, or that the current output is dependent on future input. This, of course, is not possible in real time and can be faked, as in the case of filtfilt during subsequent processing.
What you are probably looking for is a linear phase . Do not let this name confuse you; this does not mean that the filter produces any phase distortion. This means that a time shift is applied to the output. A linear phase shift in frequency leads to a constant time shift. Thus, basically your output will delay a certain number of samples ( group delay ) from the input.
Thus, the only difference between filters with a zero phase and a linear phase is that the output of the filter of the linear phase is a delayed version of the output signal with a zero phase. This delay can be accounted for by tracking group delay if you need to keep output aligned in time with input.
Reply to comment:
FIR filters are guaranteed to be linear if their coefficients are symmetrical about the center. MATLAB can easily create these types of filters with features such as fir1 or firpm . The examples in the documentation of these functions should show you how to use them.
The group delay of the linear FIR filter is (L-1)/2 , where L is the filter length. Because of this and several other things, I usually choose an odd filter length so that the delay is aligned with the sample, and not between the samples. This basically means that the output signal will be delayed from the input using (L-1)/2 samples.
The implementation of the actual filtering process is basically a discrete convolution of input with a filter. This includes changing the filter coefficients, multiplying them by the most recent input samples L and adding these results to create one output sample. Then a new input pattern is introduced, and the whole process is performed again to create another pattern (mutiplying and summing over a sliding window). You should be able to find sample convolution code on the Internet.
This is a direct way to do FIR filtering, but for longer filters it might be more efficient to perform fast convolution using FFT.This will be much harder because if you are not talking about high sampling frequencies and long filters, I would just go with a direct approach .
source share