Use python to implement digital filter (FIR)
import scipy.signal as signalimport numpy as npimport pylab as plimport matplotlib.pyplot as pltimport matplotlib
Set filter parameters and create Signals
# A = np. array ([1.0,-1.947463016918843, 0.9555873701383931]) B = np. array ([0.9833716591860479,-1.947463016918843, 0.9722157109523452]) #44.1 kHz, 1 second frequency scanning wave t = np. arange (0, 0.5, 1/44100. 0) x = signal. chirp (t, f0 = 10, t1 = 0.5, f1 = 1000.0) # output of the directly one-time computing filter y = signal. lfilter (B, a, x) plt. plot (x) plt. show ()
# Divide the input signal into 50 data sets x2 = x. reshape (-) # The initial state of the filter is 0, and the length is the length of the filter coefficient-1z = np. zeros (max (len (a), len (B)-1, dtype = np. float) y2 = [] # Save the output list for tx in x2: # filter each signal segment and update the filter status z ty, z = signal. lfilter (B, a, tx, zi = z) # Add the output to the output List B y2.append (ty) # convert the output y2 to a one-dimensional array y2 = np. array (y2) y2 = y2.reshape (-1,) # print np. sum (y-y2) ** 2) # plot pl. plot (t, y, t, y2) pl. show ()