'Implementation of scipy.signla.filtfilt in c++? [closed]

I am trying to implement scipy.signal.filtfilt function in c++ and I am wondering if there is already an implementation available of this?



Solution 1:[1]

filtfilt applies an IIR filter twice, once going forward, once going backward. The nontrivial part is how to initialize the IIR filter at the boundaries.

As a starting point, look at how scipy.signal.filtfilt does it. Here is the code: https://github.com/scipy/scipy/blob/master/scipy/signal/signaltools.py#L3870

You might also find it useful to look at the source code for Octave's filtfilt (M code): https://sourceforge.net/p/octave/signal/ci/default/tree/inst/filtfilt.m

To reproduce filtfilt in C++, you need a C++ implementation of IIR filtering to take the role of scipy's lfilter plus some boundary handling logic. I don't know about an existing C++ implementation of filtfilt, but at least for the default method='pad' logic, the core computation seems simple enough to consider porting directly.

Solution 2:[2]

Scipy's filtfilt is similar to Matlab's filtfilt.

A question for MATLAB's filtfilt was previously asked

An implementation for the same was previously shared on Stackoverflow by @darien-pardinas

Do note I say similar because as mentioned by @paco-wong

The difference is in the default padding length. In matlab's filtfilt, it is 3*(max(len(a), len(b)) - 1), and in scipy's filtfilt, it is 3*max(len(a), len(b)).

So you will have to account for that

Solution 3:[3]

I know its a long time. but maybe you find this repository useful: FiltFilt in C++

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Pascal Getreuer
Solution 2 pratikpc
Solution 3 f_s0c