4

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

ADA
  • 239
  • 3
  • 11

3 Answers3

3

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

f_s0c
  • 31
  • 2
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/31674880) – Stephen Ostermiller May 08 '22 at 15:03
0

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.

Pascal Getreuer
  • 2,906
  • 1
  • 5
  • 14
0

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

pratikpc
  • 432
  • 4
  • 12