EEVblog Electronics Community Forum

General => General Technical Chat => Topic started by: joeqsmith on September 22, 2022, 12:41:25 pm

Title: Matlab Zero-Phase Digital Filtering Question
Post by: joeqsmith on September 22, 2022, 12:41:25 pm
I am attempting to replicate MatLab's filtfilt function.  I'm curious if we have any MatLab users that may be able to explain the details of how they handle the start-up and ending.

Quote
filtfilt minimizes start-up and ending transients by matching initial conditions. Do not use filtfilt with differentiator and Hilbert FIR filters, because the operation of these filters depends heavily on their phase response.

https://www.mathworks.com/help/signal/ref/filtfilt.html (https://www.mathworks.com/help/signal/ref/filtfilt.html)
Title: Re: Matlab Zero-Phase Digital Filtering Question
Post by: ejeffrey on September 22, 2022, 02:44:52 pm
I'm not sure exactly how filtfilt does it but I think you start with the filtic function:
  https://www.mathworks.com/help/signal/ref/filtic.html (https://www.mathworks.com/help/signal/ref/filtic.html)

Calling filtic with x and y values corresponding to the first value in the data (assuming a low pass filter with DC gain 1) will eliminate the effective discontinuity at the start.  Depending on the order of the base filter you could set an initial value that matches more of the derivatives of the signal.
Title: Re: Matlab Zero-Phase Digital Filtering Question
Post by: radar_macgyver on September 22, 2022, 02:59:21 pm
You can find an implementation in the octave-signal package. The comments to that implementation refer to "Hardware-efficient technique for minimizing startup transients in Direct Form II digital filters" (https://www.researchgate.net/publication/245318435_Hardware-efficient_technique_for_minimizing_startup_transients_in_Direct_Form_II_digital_filters/link/5d80cdee299bf10c1ab29b1d/download) by Likhterov and Kopeika.
Title: Re: Matlab Zero-Phase Digital Filtering Question
Post by: joeqsmith on September 22, 2022, 04:00:04 pm
Quote
For example, the function filtfilt (in MATLAB 2001b) pads the input sequence with its mirrored part, which is three times longer than the filter order.

I don't believe that would be close.

***
I think I miss understood the term mirrored.  I was thinking just the along the vertical axis but I suspect they mean both.   
***
Coding the 2 axis mirror like shown using a window 3X the filter order, its slightly worse than  my previous attempt.   

***
Bug in code.  Very close now.  Plot comparing MatLab filtfilt with using the above.    Basically feeding a series of noise into each filter. 

Thanks for the help.