How to real-time filter with scipy and lfilter?
<p><em>Disclaimer</em>: I am probably not as good at DSP as I should be and therefore have more issues than I should have getting this code to work.</p>
<p>I need to filter incoming signals as they happen. I tried to make this code to work, but I have not been able to so far.
Referencing <a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.signal.lfilter.html" rel="nofollow noreferrer">scipy.signal.lfilter doc</a></p>
<pre><code>import numpy as np
import scipy.signal
import matplotlib.pyplot as plt
from lib import fnlib

samples = 100
x = np.linspace(0, 7, samples)
y = [] # Unfiltered output
y_filt1 = [] # Real-time filtered

nyq = 0.5 * samples
f1_norm = 0.1 / nyq
f2_norm = 2 / nyq
b, a = scipy.signal.butter(2, [f1_norm, f2_norm], 'band', analog=False)
zi = scipy.signal.lfilter_zi(b,a)
zi = zi*(np.sin(0) + 0.1*np.sin(15*0))
</code></pre>
<p>This sets <code>zi</code> as <code>zi*y[0 ]</code> initially, which in this case is 0. I have got it from the example code in the <code>lfilter</code> documentation, but I am not sure if this is correct at all.</p>
<p>Then it comes to the point where I am not sure what to do with the few initial samples.
The coefficients <code>a</code> and <code>b</code> are <code>len(a) = 5</code> here.
As <code>lfilter</code> takes input values from now to n-4, do I pad it with zeroes, or do I need to wait until 5 samples have gone by and take them as a single bloc, then continuously sample each next step in the same way?</p>
<pre><code>for i in range(0, len(a)-1): # Append 0 as initial values, wrong?
    y.append(0)

step = 0
for i in xrange(0, samples): #x:
    tmp = np.sin(x[i]) + 0.1*np.sin(15*x[i])
    y.append(tmp)

    # What to do with the inital filterings until len(y) ==  len(a) ?

    if (step&gt; len(a)):
        y_filt, zi = scipy.signal.lfilter(b, a, y[-len(a):], axis=-1, zi=zi)
        y_filt1.append(y_filt[4])

print(len(y))
y = y[4:]
print(len(y))
y_filt2 = scipy.signal.lfilter(b, a, y) # Offline filtered

plt.plot(x, y, x, y_filt1, x, y_filt2)
plt.show()
</code></pre>