python - How to real-time filter with scipy and lfilter? -
disclaimer: not dsp should , therfore have more issues getting code work should have.
i need able filter incoming signals happen. i've tried make code work, can not life of me work. referencing scipy.signal.lfilter doc
import numpy np import scipy.signal import matplotlib.pyplot plt 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, = 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))
this sets zi zi*y[0 ]initially, in case 0. taken example code in lfilter doc. not sure if correct @ all.
then comes point i'm not sure intial few samples. , b coefficients len(a) = 5 here. lfilter needs input values n-4, pad zeroes, or need wait until 5 samples has gone by, sample block, continually sample each next step?
for in range(0, len(a)-1): # append 0 initial values, wrong? y.append(0) step = 0 in xrange(0, samples): #x: tmp = np.sin(x[i]) + 0.1*np.sin(15*x[i]) y.append(tmp) # inital filterings until len(y) == len(a) ? if (step> 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()
Comments
Post a Comment