import scipy.signal as signalimport numpy as npimport pylab as plimport matplotlib.pyplot as pltimport matplotlib
Set filter parameters, create signals
# Parameters of a certain equalization filterA = NP. Array([1.0, -1.947463016918843,0.9555873701383931]) B = NP. Array([0.9833716591860479, -1.947463016918843,0.9722157109523452])# 44.1kHz, 1-second frequency sweep waveT = NP. Arange(0,0.5,1/44100.0)x= Signal. Chirp(T, f0=Ten, T1 =0.5, f1=1000.0)# directly compute the output of the filter at oncey= Signal. Lfilter(b, A,x) PLT. Plot(x) PLT. Show()
# Divide the input signal into 50 data setsx2 =x. Reshape((-1, -))# The initial state of the filter is 0 and the length is the filter coefficient length-1Z= NP. Zeros(Max (Len (a), Len (b))-1, DTYPE=NP. Float) y2 = []# Save the list of outputsFor TXinchX2:# Filter Each signal and update the status of the filter ZTyZ= Signal. Lfilter(b, A, TX, zi=Z)# Add the output to the output listb y2. Append(Ty)# Convert output y2 to one-dimensional arraysy2 = NP. Array(y2) y2 = y2. Reshape((-1,))# Error between output y and y2Print NP. Sum((y-Y2) * *2)# DrawingPl. Plot(T,y, T, y2) PL. Show()
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Python implements digital filter IIR FIR