Exp7

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 63

Department of Electrical and Electronic Engineering

Name of the Experiment:


Design and implementation of FIR and IIR filters

Course No : EEE 366


Course Title : Digital Signal Processing Sessional
Experiment No : 07
Date of Experiment : 12.01.2023
Date of Submission : 19.01.2023

Remarks

Name : Osama Haramine Sinan


ID No : 1802015
Level : 3
Term : 2
Section : A
Group : A1
Experiment No.: 07

Experiment Name: Design and Implementation of FIR and IIR filters.

Objectives:

1. To be able to remove noises from a single tone sinusoidal signal using FIR and IIR lowpass
filters.
2. To be able to remove noises from the single tone sinusoidal signal using the Filter Designer
tool (FDATool).
3. To be able to design Low Pass Filter (LPF), High Pass Filter (HPF), Band Pass Filter (BP).

Necessary Software:

1. MATLAB
Task 01:
Write a MATLAB code to remove noises from a single tone sinusoidal signal using FIR and IIR
lowpass filters. Original signal frequency is 15Hz (last 2 digits of student ID = 1802015) and noise
frequency is (15+50) or 65Hz.

Generating a single tone sinusoidal signal:


clc
A=10;
f=15;
fs=1000;
t=5;
n=0:(1/fs):t;
x=A*sin(2*pi*f*n);
plot(n,x)

title('Original Signal')
xlabel('time')
ylabel('Amplitude')

plot(abs(fft(x,1028)))
xlim([0 100])

title('Frequency Response of Original Signal')


xlabel('Frequency')
ylabel('Amplitude')

grid on
Generating the noise signal:
An=5;
fn=65;
y=An*sin(2*pi*fn*n);
plot(n,y)

title('Noise Signal')
xlabel('time')
ylabel('Amplitude')

plot(abs(fft(y,1028)))
xlim([0 100])

title('Frequency Response of Noise Signal')


xlabel('Freqeuncy')
ylabel('Amplitude')

grid on
Output of noisy signal:
z=x+y;
plot(n,z)

title('Main signal + Noise signal')


xlabel('Time')
ylabel('Amplitude')
plot(abs(fft(z,1028)))
xlim([0 100])

grid on

title('Frequency Response of Main signal + Noise signal')


xlabel('Freqeuncy')
ylabel('Amplitude')
Filtering using IIR Filter(Order - 10):
Step 1: Filter Design Parameters
O_IIR = 10;
Wc=2*pi*f/fs;

Step 2: Designing the Filter


[b,a]=butter(O_IIR, Wc, 'low');

Step 3: Filter
x_f_iir=filter(b,a,z);
plot(n,x_f_iir)

title('IIR Filter Output for Order 10')


xlabel('Time')
ylabel('Amplitude')

plot(abs(fft(x_f_iir,1028)))
xlim([0 100])

grid on

title('Frequency Response of IIR Filtered Output')


xlabel('Freqeuncy')
ylabel('Amplitude')
IIR Low Pass Filter(For Order - 1):
O_IIR = 1;
Wc=2*pi*f/fs;
[b,a]=butter(O_IIR, Wc, 'low');
x_f_iir_0=filter(b,a,z);
plot(n,x_f_iir_0)

title('IIR Filter Output for Order 1')


xlabel('Time')
ylabel('Amplitude')

plot(abs(fft(x_f_iir_0,1028)))
xlim([0 100])

grid on
title('Frequency Response for Order 1')
xlabel('Frequency')
ylabel('Amplitude')
IIR Low Pass Filter(For Order - 15):
O_IIR = 15;
Wc=2*pi*f/fs;
[b,a]=butter(O_IIR, Wc, 'low');
x_f_iir_0=filter(b,a,z);
plot(n,x_f_iir_0)

title('IIR Filter Output for Order 15')


xlabel('TIme')
ylabel('Amplitude')

plot(abs(fft(x_f_iir_0,1028)))
xlim([0 100])

title('Frequency Response of IIR Filtered Output')


xlabel('Frequency')
ylabel('Amplitude')
IIR Low Pass Filter(For Order - 20):
O_IIR = 20;
Wc=2*pi*f/fs;
[b,a]=butter(O_IIR, Wc, 'low');
x_f_iir_0=filter(b,a,z);
plot(n,x_f_iir_0)

title('IIR Filter Output for Order 20')


xlabel('Time')
ylabel('Amplitude')

plot(abs(fft(x_f_iir_0,1028)))
xlim([0 100])
grid on;
title('Frequency Response for Order 20')
xlabel('Frequency')
ylabel('Amplitude')
Comments:
First of all, a sinusoidal signal was generated and later a noise signal was added to generate a noisy
signal. To remove noise, I have used IIR Butterworth Low Pass Filter for different orders. Among
them, order 15 worked best. If order is lowered to much the noise signal returns and again if the
order is increased the 19 the noise signal was significant which also hinders the magnitude.
Filtering using FIR Filter(Order - 40):
Step 1: Filter Design Parameters
O_FIR = 40;
Wc=2*pi*f/fs;

Step 2: Designing the Filter


b = fir1(O_FIR,Wc,'low');

Step 3: Filter
x_f_fir=filter(b,1,z);
plot(n,x_f_fir)

title('FIR Filter Output for Order 40')


xlabel('TIme')
ylabel('Amplitude')

plot(abs(fft(x_f_fir,1028)))
xlim([0 100])

grid on

title('Frequency Response of FIR FIlter Output')


xlabel('Frequency')
ylabel('Amplitude')
FIR Low Pass Filter(For Order - 1):
O_FIR = 1;
b = fir1(O_FIR,Wc,'low');
x_f_fir=filter(b,1,z);
plot(n,x_f_fir)

title('FIR Filter Output for Order 1')


xlabel('Time')
ylabel('Amplitude')

plot(abs(fft(x_f_fir,1028)))
xlim([0 100])

grid on
title('Frequency Response for Order 1')
xlabel('Frequency')
ylabel('Amplitude')
FIR Low Pass Filter(For Order - 100):
O_FIR = 100;
b = fir1(O_FIR,Wc,'low');
x_f_fir=filter(b,1,z);
plot(n,x_f_fir)

title('FIR Filter Output for Order 100')


xlabel('Time')
ylabel('Amplitude')

plot(abs(fft(x_f_fir,1028)))
xlim([0 100])

grid on
title('Frequency Response of FIR Filter Output')
xlabel('Frequency')
ylabel('Amplitude')
FIR Low Pass Filter(For Order - 230):
O_FIR = 230;
b = fir1(O_FIR,Wc,'low');
x_f_fir=filter(b,1,z);
plot(n,x_f_fir)

title('FIR Filter Output for Order 230')


xlabel('Time')
ylabel('Amplitude')

plot(abs(fft(x_f_fir,1028)))
xlim([0 100])

grid on
title('Frequency Response for Order 230')
xlabel('Frequency')
ylabel('Amplitude')
Comments:
Again here to remove noise, I have used FIR Butterworth Low Pass Filter for different orders. Higher
order values from 100 workes good till 230. If order is lowered to much the noise signal returns and
again noise signal was significant which also hinders the amplitude.
Task 02:
1. Generate white noise using rand function
2. Write a MATLAB code to remove white noise from the single tone sinusoidal signal of the
previous task using the filterDesigner tool (FDATool) by designing LPF, HPF and BF filters
(both FIR and IIR).

Generation of white noise:


s=size(n);
yw=5*rand(s) %Generated random values shown below

yw = 1×5001
4.4887 3.7435 4.7739 4.7826 4.4430 4.9600 1.4727 ⋯

plot(n,yw)

title('White Noise')
xlabel('Time')
ylabel('Amplitude')

plot(abs(fft(yw,1028)))
xlim([0 100])

grid on
title('Frequency Response of White Noinse')
xlabel('Frequency')
ylabel('Amplitude')

Generation of noisy signal with white noise:


zw=x+yw;
plot(n,zw)

title('Main signal + white noise')


xlabel('Time')
ylabel('Amplitude')
plot(abs(fft(zw,1028)))
xlim([0 100])

grid on
title('Frequency Response of Noisy Signal')
xlabel('Frequency')
ylabel('Amplitude')
Comments:
White noise is being generated using rand() function. To detect the effect of noise signal the rand()
function is multiplied by 5 which is then added with the sinusoidal signal to create a noisy signal.

Filter design using filterDesigner tool (FDATool):

Comments:
Filter Designer is a Graphical User Interface (GUI) that allows user to create filter function and
design or import, and analyze digital FIR and IIR filters by selecting desired filter parameters..
Designing Low Pass Filter(IIR):
Butterworth, Order 10:

filterpara=lpf_15hz_iir;
x1_f_iir=filter(filterpara,zw);
plot(n,x1_f_iir)
title('Butterworth, Order 10')
xlabel('Time')
ylabel('Amplitude')
plot(abs(fft(x1_f_iir,1028)))
xlim([0 100])
grid on
title('Frequency Resonse for Order 10')
xlabel('Frequency')
ylabel('Amplitude')
Butterworth, Order 50:

filterpara=lpf_15hz_iir_50;
x1_f_iir_50=filter(filterpara,zw);
plot(n,x1_f_iir_50)

title('Butterworth, Order 50')


xlabel('Frequency')
ylabel('Amplitude')
plot(abs(fft(x1_f_iir_50,1028)))
xlim([0 100])

grid on
title('Frequency Response for Order 50')
xlabel('Frequency')
ylabel('Amplitude')
Comments:
Butterworth filter for both order 10 and 50 is analyzed where order 10 filter outperforms the other.
The filter containing order 50 hinders original signal and have delay while the order 10 filter keeps
some noise before 15Hz because of low pass filter but does good for higher frequency noises.

Chebyshev, Order 10:

filterpara=ch_lpf_15hz_iir;
x1_f_iir_50=filter(filterpara,zw);
plot(n,x1_f_iir_50)

title('Chebyshev, Order 10')


xlabel('Frequency')
ylabel('Amplitude')
plot(abs(fft(x1_f_iir_50,1028)))
xlim([0 100])

grid on
title('Frequency Response for Order 10')
xlabel('Frequency')
ylabel('Amplitude')
Chebyshev, Order 50:

filterpara=ch_lpf_15hz_iir_50;
x1_f_iir_50=filter(filterpara,zw);
plot(n,x1_f_iir_50)

title('Chebyshev, Order 50')


xlabel('Frequency')
ylabel('Amplitude')
plot(abs(fft(x1_f_iir_50,1028)))
xlim([0 100])

grid on
title('Frequency Response for Order 50')
xlabel('Frequency')
ylabel('Amplitude')
Comments:
Here Chebyshev filter of order 10 is better than order 50 becuase order 50 filter has delay and it
returns low amount of original signal which is very much poor in terms of performance. But Order 10
filter does quite well to remove higher frequency noises.

Designing Low Pass Filter (FIR):


Window(Kaiser), Order 10:

filterpara=lpf_15hz_fir;
x1_f_fir=filter(filterpara,zw);
plot(n,x1_f_fir)

title('Window(Kaiser) Order 10')


xlabel('Time')
ylabel('Amplitude')

plot(abs(fft(x1_f_fir,1028)))
xlim([0 100])

title('Frequency Response')
xlabel('Frequency')
ylabel('Amplitude')
Window(Kaiser), Order 50:

filterpara=lpf_15hz_fir_50;
x1_f_fir_50=filter(filterpara,zw);
plot(n,x1_f_fir_50)

title('Window(Kaiser) Order 50')


xlabel('Time')
ylabel('Amplitude')
plot(abs(fft(x1_f_fir_50,1028)))
xlim([0 100])

title('Frequency Response')
xlabel('Time')
ylabel('Amplitude')
Window(Bartlett), Order 10:

filterpara=bart_lpf_15hz_fir;
x1_f_fir=filter(filterpara,zw);
plot(n,x1_f_fir)

title('Window(Bartlett), Order 10')


xlabel('Time')
ylabel('Amplitude')
plot(abs(fft(x1_f_fir,1028)))
xlim([0 100])

title('Frequency Response')
xlabel('Frequency')
ylabel('Amplitude')
Window(Bartlett), Order 50:

filterpara=bart_lpf_15hz_fir_50;
x1_f_fir=filter(filterpara,zw);
plot(n,x1_f_fir)

title('Window(Bartlett), Order 50')


xlabel('Time')
ylabel('Amplitude')
plot(abs(fft(x1_f_fir,1028)))
xlim([0 100])

title('Frequency Response')
xlabel('Frequency')
ylabel('Amplitude')
Comments:
In terms of FIR Low Pass Filter, Window-Kaiser and Window-Bartlett was used for both order 10 and
50. The conclusion that can be made from this is that higher order removes noise very good but it
sacrifices the amplitude of the signal. Moreover for same order(50) Bartlett was better than Kaiser.

Design of High Pass Filter(IIR):


Butterworth, Order 10:

filterpara=hpf_15hz_iir;
x2_f_iir = filter(filterpara, zw);
plot(n, x2_f_iir)

title('Butterworth Order 10')


xlabel('Time')
ylabel('Amplitude')
plot(abs(fft(x2_f_iir,1028)))
xlim([0 100])

title('Frequency Response')
xlabel('Frequency')
ylabel('Amplitude')
Chebyshev, Order 10:

filterpara=ch_hpf_15hz_iir;
x2_f_iir = filter(filterpara, zw);
plot(n, x2_f_iir)

title('Chebyshev Order 20')


xlabel('Time')
ylabel('Amplitude')
plot(abs(fft(x2_f_iir,1028)))
xlim([0 100])

title('Frequency Response')
xlabel('Frequency')
ylabel('Amplitude')
Comments:
As high pass filter is used here, high frequency noise is remained. Two types of filter, i.e.,
Butterworth and Chebyshev is used for same order was used to remove noise where Butterworth
does decent job than Chebyshev. The spikes of noise signal is more significant for chebyshev filter
than butterworth filter.

Design of High Pass Filter(FIR):


Window(Kaiser), Order 10:

filterpara=hpf_15hz_fir;
x2_f_fir = filter(filterpara, zw);
plot(n, x2_f_fir)

title('Window(Kaiser) Order 10')


xlabel('Time')
ylabel('Amplitude')

plot(abs(fft(x2_f_fir,1028)))
xlim([0 100])

title('Frequency Response')
xlabel('Frequency')
ylabel('Amplitude')
Window(Kaiser), Order 80:

filterpara=hpf_15hz_fir_80;
x2_f_fir_80 = filter(filterpara, zw);
plot(n, x2_f_fir_80)

title('Window(Kaiser) Order 10')


xlabel('Time')
ylabel('Amplitude')
plot(abs(fft(x2_f_fir_80,1028)))
xlim([0 100])

title('Frequency Response')
xlabel('Frequency')
ylabel('Amplitude')
Window(Kaiser), Order 60, Cutoff Frequency = 10Hz:

filterpara=hpf_15hz_fir_60; %cutoff frequency = 10Hz


x3_f_fir = filter(filterpara, zw);
plot(n, x3_f_fir)

title('Window(Kaiser) Order 60 at Fc=10Hz')


xlabel('Time')
ylabel('Amplitude')
plot(abs(fft(x3_f_fir,1028)))
xlim([0 100])
title('Frequency Response')
xlabel('Frequency')
ylabel('Amplitude')
Comments:
High pass fir filter is used here to remove noise signal. Different orders such 10, 60, 80 is tried to
remove the noise at lower frequency. Order 60 at 10Hz cutoff frequency succesfully removes the
noise signal's spike at the beginning.

Design of Band Pass Filter(IIR):


Butterworth, Order 10:

filterpara = bpf_10_20hz_iir;
x4_f_iir = filter(filterpara, zw);
plot(n, x4_f_iir)
title('Butterworth Order 10')
xlabel('Time')
ylabel('Amplitude')
plot(abs(fft(x4_f_iir,1028)))
xlim([0 100])
title('Frequency Response')
xlabel('Frequency')
ylabel('Amplitude')
Chebyshev, Order 10:

filterpara = ch_bpf_10_20hz_iir;
x4_f_iir = filter(filterpara, zw);
plot(n, x4_f_iir)
title('Chebyshev Order 10')
xlabel('Time')
ylabel('Amplitude')
plot(abs(fft(x4_f_iir,1028)))
xlim([0 100])
title('Frequency Response')
xlabel('Frequency')
ylabel('Amplitude')
Comments:
Two types of IIR Band pass filter were used to remove noise here. Butterworth filter is the clear
winner here as chebyshev filter completely deteriorates the signal which does not output any desired
signal.

Design of Band Pass Filter(FIR):


Window(Kaiser), Order 10:

filterpara = bpf_10_20hz_fir;
x4_f_fir = filter(filterpara, zw);
plot(n, x4_f_fir)
title('Window(Kaiser), Order 10')
xlabel('Time')
ylabel('Amplitude')
plot(abs(fft(x4_f_fir,1028)))
xlim([0 100])
title('Frequency Response')
xlabel('Frequency')
ylabel('Amplitude')
Window(Kaiser), Order 100:

filterpara = bpf_10_20hz_fir_100;
x4_f_fir_2 = filter(filterpara, zw);
plot(n, x4_f_fir_2)
title('Window(Kaiser), Order 100')
xlabel('Time')
ylabel('Amplitude')
plot(abs(fft(x4_f_fir_2,1028)))
xlim([0 100])

grid on
title('Frequency Response')
xlabel('Frequency')
ylabel('Amplitude')
Comments:
For FIR band pass fitler, window-kaiser had trials for different orders. For lower order, such as 10,
leaves some noise signal spikes, But for higher order, for 100 show above, except for the beginning
spike noise signal is removed quite succesfully.
Discussion:
In this experiment, different types of FIR, IIR low pass, high pass & band pass filter was used to
remove noise signals. First part of the experiment, with help of 'code' filter function was generated
using required filter parameters. Later, rand() function was used to generate white noise which is
distributed all over the frequency domain. The it was added to the original signal. Here, Filter
Designer Tool of MATLAB was used to generate various suitable low pass, high pass, band pass
filters for IIR and FIR to compare and come to a conclusion which type of filter works better for
individual cases.

You might also like