- Can I use
filtfilt
function in realtime filter.filtfilt
first filters a piece of data forward and then filters it backward. I want to collect a piece of data first and then output a piece of data after each input. Is this possible? - Can you introduce me some methods in filtering, the filter delay is no more than the sample frequency 3-5 times.
2 Answers
Can I use filtfilt function in realtime filter
No. filtfilt()
is a method to create zero phase IIR filters. These are infinitely non-causal, so you can't implement them in real time. You can truncate an IIR filter to a finite length, but than it becomes a regular FIR filter and filtfilt()
doesn't make sense anymore.
Can you introduce me some methods in filtering, the filter delay is no more than the sample frequency 3-5 times.
Most control systems use minimum phase filters (IIR or FIR) to minimize the latency. For any given transfer function magnitude. this class of filters minimizes the group delay between input and output. However, the delay is not constant and varies by frequency.
The alternative would be linear phase filters (FIR only). These have the same delay at every frequency, but the latency is half the filter length, so you would be constrained to very short filters and a very limited frequency resolution.
-
$\begingroup$ Why do you think it doesn't make sense for FIR filters? I use filtfilt for FIR filters as well for post-processing applications (although of course we also in most cases when not fractional delay just chop of the group delay at the beginning). For an FIR filter it is simply passing it through the filter forward and backward so twice the magnitude response of the original filter and zero phase. $\endgroup$ Commented Apr 21, 2023 at 23:27
-
$\begingroup$ @DanBoschen I suspect it's the "truncating the IIR" is the bit that doesn't make sense anymore, not the implied "using
filtfilt
with an FIR". $\endgroup$– Peter K. ♦Commented Apr 22, 2023 at 20:25 -
$\begingroup$ @DanBoschen: I was assuming that if you do want to do something like
filtfilt()
it would be a lot easier just to cascade the impulse response with it's time reversed self and than simply implement the cascade as a single FIR (with the most efficient and suitable algorithm for you platform and application that you already have). Time reversing the signal feels needlessly complicated and it's certainly tricky in a real-time application. $\endgroup$– HilmarCommented Apr 23, 2023 at 12:16 -
$\begingroup$ @Hilmar Thanks for the reply, and I agree with you.
filtfilt()
has no meaning for implementation, it is strictly for post-processing analysis. I just thought your first sentence might be misleading asfiltfilt()
is also a method to create zero phase FIR filters. Your answer would be improved in my opinion if you removed "IIR" and the sentence thatfiltfilt
doesn't make sense anymore. (as Peter said, truncating the IIR doesn't make sense, but that doesn't affect the use of filtfilt with FIR coefficients, which I do find convenient for certain post processing operations) $\endgroup$ Commented Apr 23, 2023 at 17:49 -
$\begingroup$ I guess I did not phrase this well. My point here is that for FIR filters
filtfilt(b,1,x)
is pretty much the same asfilter(conv(b,flip(b)),1,x)
and the operation can be implemented with any "normal" filter function. While you CAN usefiltfilt
you don't need to. That's different for IIR filters.filtfilt()
doesn't work for real time processing but regular filtering does. $\endgroup$– HilmarCommented Apr 24, 2023 at 15:20
The only filter that should be used in a control system is the Loop Filter, and this is typically designed using control loop theory together with the transfer function of the rest of the loop rather than a traditional FIR or IIR filer design (and certainly not an application for filtfilt
). The loop filter can be developed through frequency domain analysis techniques (which is very intuitive once used) using the open loop and closed loop gain with a stability metrics such as phase and gain margin, or in the time domain using state-space matrix equations. Higher order loops are more challenging to control but offer tighter rejection in the frequency domain.
Ultimately design the loop filter according to standard practices for an assumed loop order and type. Once designed, be very careful of temptations to to reduce noise in the error signal by adding additional filters- let the loop filter remove the noise! Additional filtering can be added only when the poles of the additional filtering are significantly higher than the poles in the primary loop as given by the open loop transfer function of all functional blocks in the loop (which end up being equivalent to minimum delay).
Please see this other post detailing example "loop filters" typically used in phase lock loop applications where an oscillator is phase-locked to a reference. Note there is only one delay element in the loop filter itself. What is shown is a "Proportional-Integral Loop Filter", which isn't the solution for all applications. The actual loop filter to use depends on the element being controlled and it's characteristics and the requirements for the closed loop design.
-
$\begingroup$ i am very confused about how to realize the realtime filter with 3-5ms delays $\endgroup$ Commented Apr 23, 2023 at 6:07
-
$\begingroup$ @halfrapper I added a link showing a typical Proportional - Integral Loop Filter that is used in 2nd order Type 2 Phase Lock Loops. "Type" refers to how many pure integrators (accumulators in discrete time) are in the loop path-- the accumulator shown here forms one "integrator" and the VCO itself is another when we are locking phase. This is typical for a Phase Lock Loop locking an analog VCO or digital NCO. The actual loop filter you need depends on what exactly it is you are controlling. $\endgroup$ Commented Apr 23, 2023 at 11:29
filtfilt
? An IIR filter, forward and backward? This has been done before with Powell/Chau linear phase IIR filters doingfiltfilt
in blocks. This can be perfectly done with truncated IIR filters (which are really FIR). $\endgroup$filtfilt
, which most definetely was meant to be an offline filtering method. as robert bristow-johnson says, you can truncate any filter to a finite length, approximating it. But: if you are latency bound, how are you planning to do the backwards filtering? That just seems self-contradictory: you want low delay, but you also want to wait and get enough samples to do be able to do a backward pass. So now, you need to logically wait your filter's length $\endgroup$filtfilt
specifically? $\endgroup$