I have to apply a downsampling filter on a realtime data stream (signal) and I want to do it in python. The data is sampled at 16 kHz and loaded in a loop in chunks of 1 second. I have to downsample the data with the following:
- Forward-backward filter for downsampling to preserve phase (zero-phase filter).
- Downsample in "real-time" (i.e. one second at a time) from 16 kHz to 2 kHz.
I've tried to use
Scipy.signal.butter to output b and a and pass them to
Scipy.signal.lfilter. I'm having issues at the edges between two 1 second chunks, in the form of a transient.
Scipy.signal.filtfilt does not solve the issue. I suspect the problem is with the initial conditions of the backward filter zi_back, but I am not sure. I have tried initializing them at rest and then passing zi_back and zi_forw this way:
firstpass, zi_forw = signal.lfilter(b, a, signal, zi_forw)
signal_downsampled, zi_back = signal.lfilter(b, a, firstpass[::-1], zi_back)[::-1]
I don't mind the transient on the first chunk (~0sec), but the ones at 1sec, 2sec, 3sec, 4sec and so on are an issue.
How can I implement this near real time forward-backward filter without encountering this transient?