In matlab we can use filtfilt function to filter out data which implements forward and backward filtering techniques which results in zero-phase. But it's difficult to implement this filter in real time as it involves backward filtering.
I want to implement a 1st order high-pass or low pass filter with zero phase in real time. How can i achieve this?
I have search the web for days but unable to get any clue to start with it!
Thanks in advance!
It is not possible to perform a zero-phase filter in real-time because a zero-phase filter requires filter coefficients that are symmetric around zero. That means that the filter is non-causal, or that current output depends on future input. This of course is not possible in the real-time case and can be faked as in the case of filtfilt
during post-processing.
What you probably are looking for is a linear phase filter. Don't let the name confuse you; this does not mean that the filter produces any phase distortion. It only means that a time shift is applied to the output. A linear phase shift with respect to frequency results in a constant shift with respect to time. So basically your output will be delayed some constant number of samples (group delay) from the input.
So the only difference between a zero-phase and linear-phase filter is that the linear-phase filter output is a delayed version of the zero-phase output. This delay can be accounted for by keeping track of the group delay if you need to keep the output aligned in time with the input.
Response to comment:
FIR filters are guaranteed to be linear phase if their coefficients are symmetric about the center. MATLAB can easily create these types of filters with functions such as fir1 or firpm. The examples in the documentation of those functions should show you how to use them.
The group delay of a linear phase FIR filter is (L-1)/2
where L
is the length of the filter. Because of this and a few other things, I would usually choose an odd filter length so the delay is aligned to a sample and not in between samples. This basically means that the output signal will be delayed from the input by (L-1)/2
samples.
Implementing the actual filtering process is basically discrete convolution of the input with the filter. This involves reversing the filter coefficients, multiplying them by the most recent L
input samples, and adding those results to produce a single output sample. Then a new input sample is brought in and the whole process is done again to produce another sample (mutiplying and summing over a sliding window). You should be able to find some sample code for convolution on the web.
This is the direct way to perform FIR filtering, but for longer filters, it may be more efficient to perform fast convolution with an FFT. This will be a lot more difficult to get right, so unless you are talking about high sample rates and long filters, I would just go with the direct approach.
A "non-causal" zero-phase filter plus a sufficient amount of added delay can be approximated by a causal linear phase FIR filter. This assumes that adding some delay fits your system requirements.
In your case, you could take the impulse response of your forward+backward asymmetric (or non-linear phase) filtering process, window that impulse response to make the response finite in length, delay the finite length kernel so that it doesn't require "future" samples, and use that as a FIR filter kernel. You will have to check the results to see if your chosen window was appropriate in length and shape. There may be a trade-off in choosing a delay versus quality of the filter due to the finite length windowing required for that delay.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With