Impulse response of 2nd order IIR filter Sample-by-sample algorithm
Python implementation of the MATLAB code from the book:
DAFX - Digital Audio Effects
Edited by Udo Zölzer
ISBN: 978-0-470-66599-2
Second Edition, John Wiley & Sons, 2011
Matlab files source: http://ant-s4.unibw-hamburg.de/dafx/DAFX_Book_Page_2nd_edition/matlab.html
%matplotlib inline
import matplotlib.pyplot as plt
# Coefficients for a high-pass
a=[1, -1.28, 0.47];
b=[0.69, -1.38, 0.69];
# Initialization of state variables
xh1 = 0; xh2 = 0;
yh1 = 0; yh2 = 0;
# Input signal: unit impulse
N = 20; # length of input signal
x = [0]*N
x[0] = 1
y = []
# Sample-by-sample algorithm
for n in range(N):
y.append(b[0]*x[n] + b[1]*xh1 + b[2]*xh2 - a[1]*yh1 - a[2]*yh2)
xh2 = xh1; xh1 = x[n];
yh2 = yh1; yh1 = y[n];
# Plot results
plt.rcParams['figure.figsize'] = (10, 8)# figure size
plt.subplot(211)
plt.stem(x);plt.axis([-0.6, len(x), -0.8, 1.2]);
plt.xlabel('n');plt.ylabel('x(n)');
plt.subplot(212)
plt.stem(y);plt.axis([-0.6, len(x), -0.8, 1.2]);
plt.xlabel('n');plt.ylabel('y(n)');
plt.tight_layout()
plt.show()