0% found this document useful (0 votes)
36 views7 pages

Expt 9 B IIR Using Python For Notch Filter and LPF

Uploaded by

gowri thumbur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views7 pages

Expt 9 B IIR Using Python For Notch Filter and LPF

Uploaded by

gowri thumbur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

66

Exp.No:7 Infinite Impulse Response Filter design and its analysis.


Aim: To study the behavior of IIR Butterworth and Chebyshev
filters and its digital implementation using Bilinear
Transformation Method.
Software: PYTHON

NOISE REMOVAL OF A SIGNAL USING IIR NOTCH FILTER:


#import the required libraries

from scipy import signal


import matplotlib.pyplot as plt
import numpy as np

# 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

# Design a notch filter using signal.iirnotch


b_notch,a_notch = signal.iirnotch(notch_freq, quality_factor,
samp_freq)

# Compute magnitude response of the designed filter


freq, h = signal.freqz(b_notch, a_notch, fs=samp_freq)
fig = plt.figure(figsize=(8, 6))

# Plot magnitude response of the filter


plt.plot(freq*samp_freq/(2*np.pi), 20 * np.log10(abs(h)),
'r', label='Bandpass filter', linewidth='2')
plt.xlabel('Frequency [Hz]', fontsize=20)
plt.ylabel('Magnitude [dB]', fontsize=20)
plt.title('Notch Filter', fontsize=20)
plt.grid()

# Create and view signal that is a mixture of two different


frequencies
f1 = 15 # Frequency of 1st signal in Hz
f2 = 50 # Frequency of 2nd signal in Hz

# Set time vector


n = np.linspace(0, 1, 1000) # Generate 1000 sample sequence in 1
sec

# Generate the signal containing f1 and f2


67

noisySignal = np.sin(2*np.pi*15*n) + np.sin(2*np.pi*50*n) + \


np.random.normal(0, .1, 1000)*0.03

# Plotting the response


fig = plt.figure(figsize=(8, 6))
plt.subplot(211)
plt.plot(n, noisySignal, color='r', linewidth=2)
plt.xlabel('Time', fontsize=20)
plt.ylabel('Magnitude', fontsize=18)
plt.title('Noisy Signal', fontsize=20)

# Apply notch filter to the noisy signal using signal.filtfilt


outputSignal = signal.filtfilt(b_notch, a_notch, noisySignal)

# Plot notch-filtered version of signal


plt.subplot(212)

# Plot output signal of notch filter


plt.plot(n, outputSignal)
plt.xlabel('Time', fontsize=20)
plt.ylabel('Magnitude', fontsize=18)
plt.title('Filtered Signal', fontsize=20)
plt.subplots_adjust(hspace=0.5)
fig.tight_layout()
plt.show()

Observation:
68

DIGITAL HIGHPASS BUTTERWORTH FILTER:


# importing the modules

import numpy as np
import matplotlib.pyplot as plt
from scipy import signal
import math

# Specifications of the Filter

# sampling frequency
f_sample = 3700

# pass band frequency


f_pass = 1060

# stop band frequency


f_stop = 650

# pass band ripple


fs = 0.5
69

# pass band freq in radian


wp = f_pass/(f_sample/2)

# stop band freq in radian


ws = f_stop/(f_sample/2)

# Sampling Time
Td = 1

# pass band ripple


g_pass = 1

# stop band attenuation


g_stop = 30

# Conversion to prewrapped analog frequency


omega_p = (2/Td)*np.tan(wp/2)
omega_s = (2/Td)*np.tan(ws/2)

# Design of Filter using signal.buttord function


N,Wn = signal.buttord(omega_p,omega_s,g_pass,g_stop, analog=True)

# Printing the values of order & cut-off frequency


print("Order of the Filter=", N) # N is the order
# Wn is the cut-off freq of the filter
print("Cut-off frequency= {:.3f} rad/s ".format(Wn))

# 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)

# w is the freq in z-domain & h is the magnitude in z-domain


w, h = signal.freqz(z, p, 512)

# 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

DIGITAL LOWPASS BUTTERWORTH FILTER:


import numpy as np
import matplotlib.pyplot as plt
from scipy import signal

# Specifications of Filter
# sampling frequency
f_sample = 41000

# pass band frequency


f_pass = 4100

# stop band frequency


f_stop = 8100

# pass band ripple


fs = 0.5

# pass band freq in radian


wp = f_pass/(f_sample/2)

# stop band freq in radian

You might also like