Expt 9 B IIR Using Python For Notch Filter and LPF
Expt 9 B IIR Using Python For Notch Filter and LPF
# Notch filter
samp_freq = 1000 # Sample frequency (Hz)
notch_freq = 50.0 # Frequency to be removed from signal (Hz)
quality_factor = 20.0 # Quality factor
Observation:
68
import numpy as np
import matplotlib.pyplot as plt
from scipy import signal
import math
# sampling frequency
f_sample = 3700
# Sampling Time
Td = 1
# Conversion in Z-domain
# b is the numerator coefficients of the filter & a is the
denominator
b, a = signal.butter(N, Wn, 'high', True)
z, p = signal.bilinear(b, a, fs)
# Magnitude Response
plt.semilogx(w, 20*np.log10(abs(h)))
plt.xscale('log')
plt.title('Butterworth filter frequency response')
plt.xlabel('Frequency [Hz]')
plt.ylabel('Amplitude [dB]')
plt.margins(0, 0.1)
70
plt.grid(which='both', axis='both')
plt.axvline(100, color='green')
plt.show()
# Impulse Response
imp = signal.unit_impulse(40)
c, d = signal.butter(N, 0.5)
response = signal.lfilter(c, d, imp)
plt.stem(np.arange(0,
40),imp,markerfmt='D',use_line_collection=True)
plt.stem(np.arange(0,40), response,use_line_collection=True)
plt.margins(0, 0.1)
plt.xlabel('Time [samples]')
plt.ylabel('Amplitude')
plt.grid(True)
plt.show()
# Phase Response
fig, ax1 = plt.subplots()
ax1.set_title('Digital filter frequency response')
ax1.set_ylabel('Angle(radians)', color='g')
ax1.set_xlabel('Frequency [Hz]')
angles = np.unwrap(np.angle(h))
ax1.plot(w/2*np.pi, angles, 'g')
ax1.grid()
ax1.axis('tight')
plt.show()
Observation:
71
72
# Specifications of Filter
# sampling frequency
f_sample = 41000