Lab 3

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 38

PART A: DESIGN IIR

Exercise 1: Estimation of order of IIR filter


Q1. Determine the lowest order of a digital IIR lowpass filter of Butterworth, Chebyshev Type 1 and
Chebyshev Type 2. The specifications are as follows:
Sampling rate = 40kHz, Passband ripple = 0.5 dB
Passband edge frequency = 4kHz, Minimum stopband attenuation = 40 dB.
Stopband edge frequency = 8kHz
Describe your results. (4m)
(a) Butterworth filter
Ans :
%Part A Q1 Butterworth
close all; clear all; clc;
format long % set the output format to the long fixed decimal format and display
value of pi
rp = input ('enter passband ripple :'); %refers to ripple of passband
rs = input ('enter stopband ripple :'); %refers to ripple of stopband
wp = input ('enter passband frequency :'); %refers to frequency of passband, w
indicates omega in digital
ws = input ('enter stopband frequency :'); %refers to frequency of stopband, w
indicates omega in digital
fs = input ('enter frequency sampling :');

w1 = 2*wp/fs ; %angular frequency passband. The normalized passband edge angular


frequency wp is wp = (2piFp)/Ft
w2 = 2*ws/fs ; %angular frequency stopband. The normalized stopband edge angular
frequency ws is wp = (2piFs)/Ft

[n, wn] = buttord(w1, w2, rp, rs ); %'s' indicates analog domain %butter calculates
the minimum order of a digital, n indicates the filter order
%or analog Butterworth filter required to meet a set of filter design specifications
[z,p,k] = butter(n,wn); %Butterworth filter design
[b,a] = zp2tf(z,p,k); % zp2tf means converts zero-pole-gain filter parameters to
transfer function form
[b,a] = butter(n, wn); %butterworth transfer function

w = 0 : 0.01 :pi ; %interval


[h, om] = freqz(b,a,w); %normalized frequency
m = 20*log10(abs(h)); %changing to decibels
an = angle (h);
figure (1);
subplot(211);
plot (om/pi,m);
ylabel('Gain in dB');
xlabel('Normalized frequency');

1
subplot (212);
plot (om/pi,an);
ylabel('Phase in radian');
xlabel('Normalized frequency');

The lowest order of a digital IIR lowpass filter of Butterworth is 8th order meanwhile the cutoff
frequency, wn is 0.247. This can be confirmed by entering ‘n’ and ‘wn’ in the Command Window of the
Matlab, respectively.

(b) Chebyshev Type 1 filter

Ans :

%Part A Q1 Chebyshev type 1 filter


close all; clear all; clc;
format long % set the output format to the long fixed decimal format and display
value of pi
rp = input ('enter passband ripple :'); %refers to ripple of passband
rs = input ('enter stopband ripple :'); %refers to ripple of stopband
wp = input ('enter passband frequency :'); %refers to frequency of passband, w
indicates omega in digital
ws = input ('enter stopband frequency :'); %refers to frequency of stopband, w
indicates omega in digital
fs = input ('enter frequency sampling :');

w1 = 2*wp/fs ; %angular frequency passband. The normalized passband edge angular


frequency wp is wp = (2piFp)/Ft

2
w2 = 2*ws/fs ; %angular frequency stopband. The normalized stopband edge angular
frequency ws is wp = (2piFs)/Ft

[n, wn] = cheb1ord(w1, w2, rp, rs); %cheb1ord calculates the minimum order of a
digital
%or analog Chebyshev type 1 filter required to meet a set of filter design
specifications, n indicates the filter order
[b,a] = cheby1(n, rp, wn ); %Chebysev Type 1 transfer function
w = 0 : 0.01 :pi ; %interval
[h, om] = freqz(b,a,w); %normalized frequency
m = 20*log10(abs(h)); %changing to decibels
an = angle (h);

figure (1);
subplot(211);
plot (om/pi,m);
ylabel('Gain in dB');
xlabel('Normalized frequency');

subplot (212);
plot (om/pi,an);
ylabel('Phase in radian');
xlabel('Normalized frequency');

The lowest order of a digital IIR lowpass filter of Chebyshev Type 1 is 5 th order meanwhile the cutoff
frequency, wn is 0.2. This can be confirmed by entering ‘n’ and ‘wn’ in the Command Window of the
Matlab, respectively.

3
(c) Chebysev Type 2

Ans :

%Part A Q1 Chebyshev type 2 filter


close all; clear all; clc;
format long % set the output format to the long fixed decimal format and display
value of pi
rp = input ('enter passband ripple :'); %refers to ripple of passband
rs = input ('enter stopband ripple :'); %refers to ripple of stopband
wp = input ('enter passband frequency :'); %refers to frequency of passband, w
indicates omega in digital
ws = input ('enter stopband frequency :'); %refers to frequency of stopband, w
indicates omega in digital
fs = input ('enter frequency sampling :');

w1 = 2*wp/fs ; %angular frequency passband. The normalized passband edge angular


frequency wp is wp = (2piFp)/Ft
w2 = 2*ws/fs ; %angular frequency stopband. The normalized stopband edge angular
frequency ws is wp = (2piFs)/Ft

[n, wn] = cheb2ord(w1, w2, rp, rs); %cheb2ord calculates the minimum order of a
digital
%or analog Chebyshev type 2 filter required to meet a set of filter design
specifications, n indicates the filter order
[b,a] = cheby2(n, rs, wn ); %Chebysev Type 2 transfer function

w = 0 : 0.01 :pi ; %interval


[h, om] = freqz(b,a,w); %normalized frequency
m = 20*log10(abs(h)); %changing to decibels
an = angle (h);

figure (1);
subplot(211);
plot (om/pi,m);
ylabel('Gain in dB');
xlabel('Normalized frequency');

subplot (212);
plot (om/pi,an);
ylabel('Phase in radian');
xlabel('Normalized frequency');

4
The lowest order of a digital IIR lowpass filter of Chebyshev Type 2 is 5 th order meanwhile the cutoff
frequency, wn is 0.4. This can be confirmed by entering ‘n’ and ‘wn’ in the Command Window of the
Matlab, respectively.

5
Q2. Determine the lowest order of a digital IIR bandpass filter of Butterworth, Chebyshev Type 1 and
Chebyshev Type 2. The specifications are as follows:
Sampling rate = 7kHz Passband ripple = 0.4 dB
Passband edge frequency = 1.4kHz and 2.1kHz Minimum stopband attenuation = 45 dB.
Stopband edge frequency = 1.05kHz and 2.45kHz
Describe your results. (4m)
(a) Butterworth filter
Ans :
%Part A Q2 Butterworth
close all; clear all; clc;
format long % set the output format to the long fixed decimal format and display
value of pi
fs = 7000;
wp = [1400 2100];
ws = [1050 2450];
rp = 0.4;
rs = 45;

w1 = 2*wp/fs ; %angular frequency passband. The normalized passband edge angular


frequency wp is wp = (2piFp)/Ft
w2 = 2*ws/fs ; %angular frequency stopband. The normalized stopband edge angular
frequency ws is wp = (2piFs)/Ft

[n, wn] = buttord(w1, w2, rp, rs); %'s' indicates analog domain %butter calculates
the minimum order of a digital
%or analog Butterworth filter required to meet a set of filter design, normalized
frequency band edges Wn
%specifications, n indicates the filter order
[z,p,k] = butter(n,wn); %Butterworth filter design
[b,a] = zp2tf(z,p,k); % zp2tf means converts zero-pole-gain filter parameters to
transfer function form
[b,a] = butter(n, wn); %butterworth transfer function

w = 0 : 0.01 :pi ; %interval


[h, om] = freqz(b,a,w); %normalized frequency,‘f’ is vector of band edges that
involves parameters of passband edge
%and stopband edge, ‘a’ is vector specifying the desired amplitude on the bands
defined by f,
%typical values are 0 and 1. %‘dev’is a vector of maximum ripples in dB.
m = 20*log10(abs(h)); %changing to decibels
an = angle (h);

figure (1);
subplot(211);
plot (om/pi,m);
ylabel('Gain in dB');
xlabel('Normalized frequency');

6
subplot (212);
plot (om/pi,an);
ylabel('Phase in radian');
xlabel('Normalized frequency');

The lowest order of a digital IIR lowpass filter of Butterworth is 8th order meanwhile the cutoff
frequency, wn is 0.384 and 0.616. This can be confirmed by entering ‘n’ and ‘wn’ in the Command
Window of the Matlab, respectively.

7
(b) Chebyshev Type 1

Ans :

%Part A Q2 Chebyshev type 1 filter


close all; clear all; clc;
format long % set the output format to the long fixed decimal format and display
value of pi
fs = 7000;
wp = [1400 2100];
ws = [1050 2450];
rp = 0.4;
rs = 45;

w1 = 2*wp/fs ; %angular frequency passband. The normalized passband edge angular


frequency wp is wp = (2piFp)/Ft
w2 = 2*ws/fs ; %angular frequency stopband. The normalized stopband edge angular
frequency ws is wp = (2piFs)/Ft

[n, wn] = cheb1ord(w1, w2, rp, rs); %cheb1ord calculates the minimum order of a
digital, normalized frequency band edges Wn
%or analog Chebyshev type 1 filter required to meet a set of filter design
specifications, n indicates the filter order
[b,a] = cheby1(n, rp, wn ); %Chebysev type 1 transfer function

w = 0 : 0.01 :pi ; %interval


[h, om] = freqz(b,a,w); %normalized frequency, ‘f’ is vector of band edges that
involves parameters of passband edge
%and stopband edge, ‘a’ is vector specifying the desired amplitude on the bands
defined by f,
%typical values are 0 and 1. %‘dev’is a vector of maximum ripples in dB.
m = 20*log10(abs(h)); %changing to decibels
an = angle (h);

figure (1);
subplot(211);
plot (om/pi,m);
ylabel('Gain in dB');
xlabel('Normalized frequency');

subplot (212);
plot (om/pi,an);
ylabel('Phase in radian');
xlabel('Normalized frequency');

8
The lowest order of a digital IIR lowpass filter of Chebyshev Type 1 is 5 th order meanwhile the cutoff
frequency, wn is 0.4 and 0.6. This can be confirmed by entering ‘n’ and ‘wn’ in the Command Window
of the Matlab, respectively.

9
(c) Chebyshev Type 2

Ans :

%Part A Q1 Chebyshev type 2 filter


close all; clear all; clc;
format long % set the output format to the long fixed decimal format and display
value of pi
fs = 7000;
wp = [1400 2100];
ws = [1050 2450];
rp = 0.4;
rs = 45;

w1 = 2*wp/fs ; %angular frequency passband. The normalized passband edge angular


frequency wp is wp = (2piFp)/Ft
w2 = 2*ws/fs ; %angular frequency stopband. The normalized stopband edge angular
frequency ws is wp = (2piFs)/Ft

[n, wn] = cheb2ord(w1, w2, rp, rs); %cheb2ord calculates the minimum order of a
digital, normalized frequency band edges Wn
%or analog Chebyshev type 2 filter required to meet a set of filter design
specifications, n indicates the filter order
[b,a] = cheby2(n, rs, wn ); %Chebysev Type 2 transfer function, rs bukan rp

w = 0 : 0.01 :pi ; %interval


[h, om] = freqz(b,a,w); %normalized frequency, ‘f’ is vector of band edges that
involves parameters of passband edge
%and stopband edge, ‘a’ is vector specifying the desired amplitude on the bands
defined by f,
%typical values are 0 and 1. %‘dev’is a vector of maximum ripples in dB.
m = 20*log10(abs(h)); %changing to decibels
an = angle (h);

figure (1);
subplot(211);
plot (om/pi,m);
ylabel('Gain in dB');
xlabel('Normalized frequency');

subplot (212);
plot (om/pi,an);
ylabel('Phase in radian');
xlabel('Normalized frequency');

10
The lowest order of a digital IIR lowpass filter of Chebyshev Type 2 is 5 th order meanwhile the
cutoff frequency, wn is 0.5 and 0.7. This can be confirmed by entering ‘n’ and ‘wn’ in the
Command Window of the Matlab, respectively

11
Exercise 2 : IIR Filter Design
Q1. Complete the magnitude response/gain response computation program below based on transfer
function of numerator and denominator in decibel (dB). (2m)
function [g,w]=gain(num,den)
%compute of magnitude response/ gain function in dB of a transfer function at 256 equally spaced
points
%on the top half of the unit circle
%numerator = num; %denominator = den
%frequency values are returned in vector w
%gain values are returned in vector g
w=0:pi/255:pi;
h=freqz(num,den,w);
g=

Ans :
function [g,w]=gain(num,den)
%compute of magnitude response/ gain function in dB of a transfer function at 256
equally spaced points
%on the top half of the unit circle
%numerator = num; %denominator = den
%frequency values are returned in vector w
%gain values are returned in vector g
w=0:pi/255:pi;
h=freqz(num,den,w);
g=20*log10(abs(h));

figure (1);
subplot(111);
plot (om/pi,m);
ylabel('Gain in dB');
xlabel('Normalized frequency');

12
Q2. Design the Butterworth bandstop filter based on the steps below with the given specifications.
Shows all the transfer function coefficients and write down the exact expression for the transfer function
generated. Does your design MEET the specifications?
The Steps:
Initialize specifications
Estimate the filter order
Design the filter
Display the transfer function
Compute the gain response

Plot the gain response for x-axis is 𝝎/𝝅 and y-axis is Gain in dB
The specifications:
Passband edge frequency = 0.2 and 0.8
Stopband edge frequency = 0.4 and 0.6
Passband ripple = 0.4 dB
Stopband ripple = 50 dB
Sampling frequency = 2kHz
(10 Marks)
Ans :
close all; clear all; clc;
% Design of a Butterworth Bandstop Digital Filter

% Initialize specifications
fs = 2000;
ws = [0.4 0.6];
wp = [0.2 0.8];
rp = 0.4;
rs = 50;

% Estimate Filter Order


[n,wn] = buttord(wp,ws,rp,rs);

% Design the Filter


[num,den] = butter(n,wn,'stop');

% Display the transfer function


disp('Numerator Coefficients are ');

13
disp(num);
disp('Denominator Coefficients are ');
disp(den);
t = 0 :0.01 :pi;
[g,w] = freqz(num,den,t);

% Plot the gain response


plot(w/pi,20*log(abs(g)));
axis([0 1 -60 5]);
xlabel('\omega /\pi');
ylabel('Gain in dB');
title('Gain Response of a Butterworth Bandstop Filter');

14
Transfer function, H(z) ;
−2 −4 −6 −8 −10
0.04 9+0.2 46 z +0.49 3 z + 0.493 z +0.2 5 z +0.049 z
H(z) = 1−0.085 z −2 +0.636 z− 4−0.029 z−6 +0.056 z−8 −0.001 z−10

Hence, based on the results generated, the design does meet the specifications of the Butteworth
bandstop filter.

15
PART B: DESIGN FIR
Exercise 1: Estimation of order of FIR filter
Q1. Using appropriate function, estimate the order of a linear-phase lowpass FIR filter with the
following
specifications:
Passband edge = 2kHz Passband ripple = 0.005
Stopband edge = 2.5kHz Stopband ripple = 0.005
Sampling rate = 10kHz. (2m)
Ans :
%Specifications as in the questions
Fp = 2000;
Fs = 2500;
Rp = 0.005;
Rs = 0.005;
FT = 10000;

%Estimate order using Kaiser function


fedge = [Fp Fs];
aval = [1 0];
dev = [Rp Rs];
fs = FT;
[n wn beta ftype] = kaiserord(fedge,aval,dev,fs);

16
The order, n is 54 and the normalized frequency, wn is 0.45. The beta is 4.09 and the ftype is low which
is referring to lowpass filter.

17
Exercise 2: FIR filter design
Q1. Using the function fir1, design a linear phase FIR lowpass filter meeting with these specifications:
Passband edge = 2kHz Passband ripple = 0.005
Stopband edge = 2.5kHz Stopband ripple = 0.005
Sampling rate = 10kHz.
and plot its gain and phase responses. Show the filter coefficients in tabular form. Does your design
meet
the specifications? If it does not, adjust the filter order until the design meets the specifications. What is
the order of the filter meeting the specifications?
(10 Marks)
Ans :
close all; clear all; clc;
%Design spec as given in Exercise 2 Q1
Fp = 2000;
Fs = 2500;
Rp = 0.005;
Rs = 0.005;
FT = 10000;

%Estimate order usign Kaiser function


fedge = [Fp Fs];
aval = [1 0];
dev = [Rp Rs];
fs = FT;
[n wn beta ftype] = kaiserord(fedge,aval,dev,fs);

%Design the filter, Hamming windowm by default


h = fir1(n,wn);

%Show the numerator cofficients


disp('Numerator Coefficient are');
disp(h);

%Compute and plot the gain response


[g,w] = freqz(h,1,256); %frequency response of a digital filter

figure(1);
plot(w/pi,g);
grid;
xlabel('\omega/\pi'); ylabel('Gain in dB');
title('Gain Response');

figure(2);
an = angle(g);

18
plot(w/pi,an);grid;
xlabel('\omega/\pi'); ylabel('Phase in radian');
title('Phase Response');

MagH = abs(g);
T1 = 1.005*ones(1,length(w));
T2 = 0.995*ones(1,length(w));
T3 = 0.005*ones(1,length(w));
figure(3);
plot(w/pi,MagH,w/pi,T1,w/pi,T2,w/pi,T3);
grid;

The n is 54 and the wn is 0.45.

19
Filter coefficients in Command Window display in Matlab :

Filter coefficients in tabular form :


1 2 3 4 5 6 7 8 9 10
0.00042 -0.0008 -0.0008 0.0008 0.00159 -0.0007 -0.0028 0.0000 0.0044 0.0017
11 12 13 14 15 16 17 18 19 20
-0.0060 -0.0048 0.0069 0.0094 -0.0063 -0.0156 0.0030 0.0230 0.0043 -0.0308
21 22 23 24 25 26 27 28 29 30
-0.0177 0.0383 0.0416 -0.0445 -0.0920 0.0486 0.3136 0.4502 0.3136 0.0486
31 32 33 34 35 36 37 38 39 40
-0.0920 -0.0445 0.0416 0.0383 -0.0177 -0.0308 0.0043 0.0230 0.0030 -0.0156
41 42 43 44 45 46 47 48 49 50
-0.0063 0.0094 0.0069 -0.0048 -0.0060 0.0017 0.0044 0.0000 -0.0028 -0.0007
51 52 53 54 55

20
0.0016 0.0008 -0.0008 -0.0008 0.0004

However, the order of the filter does not meet the specification as the passband frequency, neither of the
specification of wp is at 0.4 (normalized frequency) nor stopband frequency, ws is at 0.5 (normalized
frequency). The specifications can be targeted by adding the number of order filter, n.
%Estimate order usign Kaiser function
fedge = [Fp Fs];
aval = [1 0];
dev = [Rp Rs];
fs = FT;
[n wn beta ftype] = kaiserord(fedge,aval,dev,fs);
n = n+20;

21
Filter coefficients in tabular form :

1 2 3 4 5 6 7 8 9 10
0.0006 0.0004 -0.0006 -0.0007 0.0004 0.0011 -0.0002 -0.0017 -0.0003 0.0022
11 12 13 14 15 16 17 18 19 20
0.0013 -0.0026 -0.0027 0.0026 0.0045 -0.0018 -0.0066 0.0000 0.0086 0.0031
21 22 23 24 25 26 27 28 29 30
-0.0100 -0.0074 0.0101 0.0131 -0.0083 -0.0197 0.0037 0.0268 0.0048 -0.0339
31 32 33 34 35 36 37 38 39 40
-0.0190 0.0404 0.0431 -0.0455 -0.0930 0.0488 0.3136 0.4496 0.3136 0.0488
41 42 43 44 45 46 47 48 49 50
-0.0930 -0.0455 0.04314 0.0404 -0.0190 0.0339 0.0048 0.0268 0.00367 -0.0197
51 52 53 54 55 56 57 58 59 60
-0.0083 0.0131 0.0101 -0.0074 -0.0100 0.0030 0.0086 0.0000 -0.0066 -0.0018
61 62 63 64 65 66 67 68 69 70
0.0045 0.0026 -0.0027 -0.0026 0.0013 0.0022 -0.0003 -0.0017 -0.0002 0.0011
71 72 73 74 75
0.0005 -0.0007 -0.0006 0.0004 0.0006

The new design is meeting the specification. The order filter, n = 74 and wn is 0.45. The specification is
met when the n is 74.

22
Q2. Repeat Q1 using following windows: Hanning and Blackman. (8 Marks)
(a) Hanning Window
Ans :
% Hanning
clear all;clc;close all;

%Design specs as given in Ex2 Q1


Wp = 2000;
Ws = 2500;
Rp = 0.005;
Rs = 0.005;
fs = 10000;
wp = 2*Wp/fs;
ws = 2*Ws/fs;

%Estimate order using Kaiser function


f = [Wp Ws]; a = [1 0]; dev = [Rp Rs];
[n wn beta ftype] = kaiserord(f,a,dev,fs);

%Design filter Hanning


Wn = wp+(ws-wp)/2;
wdw = hann(n+3);
wdw = wdw(2:n+2);
b = fir1(n,Wn,wdw); %b is the numerator

%Compute and plot the gain response


[h,w] = freqz(b,1,256);
m = 20*log10(abs(h));

figure(1);
plot(w/pi,m);grid;
xlabel('\omega/\pi'); ylabel('Gain in dB');
title('Gain Response');
an = angle(h);

figure(2);
plot(w/pi,an);grid;
xlabel('\omega/\pi');
ylabel('Phase in radian');
title('Phase Response');

MagH = abs(h);
T1 = 1.005*ones(1,length(w));
T2 = 0.995*ones(1,length(w));
T3 = 0.005*ones(1,length(w));
figure(3);
plot(w/pi,MagH,w/pi,T1,w/pi,T2,w/pi,T3);
grid;

23
24
The filter coefficients in tabular form :
1 2 3 4 5 6 7 8 9 10
0.00002 -0.0001 -0.0003 0.0004 0.0009 -0.0005 -0.0022 0.0000 0.0039 0.0015
11 12 13 14 15 16 17 18 19 20
-0.0056 -0.0045 0.0066 0.0092 -0.0061 -0.0154 0.0030 0.0228 0.0042 -0.0307
21 22 23 24 25 26 27 28 29 30
-0.0176 0.0383 0.0416 -0.0445 -0.0919 0.0485 0.3134 0.4500 0.3134 0.0486
31 32 33 34 35 36 37 38 39 40
-0.0918 -0.0445 0.0416 0.0382 -0.0176 -0.0307 0.0042 0.0228 0.0030 -0.0154
41 42 43 44 45 46 47 48 49 50
-0.0062 0.0092 0.0067 -0.0045 -0.0056 0.0015 0.0039 0.0000 -0.0022 -0.0005
51 52 53 54 55
0.0009 0.0004 -0.0003 -0.0001 0.00002

The order of the filter, n is 54 and the wn is 0.45. Based on the design, it does not meet the specification.
Neither the passband frequency, wp is 0.4 (normalized frequency) nor stopband frequency ws is 0.5
(normalized frequency) are met. To meet the specification, the order of the filter needs to be increased.
%Estimate order using Kaiser function
f = [Wp Ws]; a = [1 0]; dev = [Rp Rs];
[n wn beta ftype] = kaiserord(f,a,dev,fs);
n = n+20;

The filter coefficients in tabular form :


1 2 3 4 5 6 7 8 9 10
0.00001 0.0004 -0.0001 -0.0002 0.0002 0.0006 -0.0001 -0.0011 -0.0002 0.0017
11 12 13 14 15 16 17 18 19 20
0.0010 -0.0022 -0.0024 0.0023 0.0042 -0.0017 -0.0062 0.0000 0.0083 0.0030
21 22 23 24 25 26 27 28 29 30
-0.0097 -0.0073 0.0099 0.0129 -0.0082 -0.0195 0.0037 0.0267 0.0048 -0.0339
31 32 33 34 35 36 37 38 39 40
-0.0190 0.0403 0.0431 -0.0455 -0.0930 0.0488 0.3139 0.4500 0.3139 0.0488
41 42 43 44 45 46 47 48 49 50
-0.0931 -0.0455 0.0431 0.0403 -0.0190 -0.0339 0.0048 0.0267 0.0037 -0.0195
51 52 53 54 55 56 57 58 59 60
-0.0082 0.0129 0.0099 -0.0073 -0.0097 0.0030 0.00827 0.0000 -0.0063 -0.0017
61 62 63 64 65 66 67 68 69 70
0.0042 0.0023 -0.0024 -0.0022 0.0010 0.0017 -0.0002 -0.0012 -0.0001 0.0006
71 72 73 74 75
0.0002 -0.0002 -0.0001 0.00003 0.00002

25
The new order filter of the new design is 74. The new design meets the specification.

26
(b) Blackman Window
% Blackman
clear all;clc;close all;

%Design specs as in Ex2 Q1


Wp = 2000;
Ws = 2500;
Rp = 0.005;
Rs = 0.005;
fs = 10000;
wp = 2*Wp/fs;
ws = 2*Ws/fs;

%Estimare order using Kaiser function


f = [Wp Ws];
a = [1 0];
dev = [Rp Rs];
[n wn beta ftype] = kaiserord(f,a,dev,fs);
%Design filter
Wn = wp+(ws-wp)/2;
wdw = blackman(n+3);
wdw = wdw(2:n+2);
b = fir1(n,Wn,wdw);

%Ciompute and plot the gain response


[h,w] = freqz(b,1,256);
m = 20*log10(abs(h));
figure(1);
plot(w/pi,m);grid;
xlabel('\omega/\pi'); ylabel('Gain in dB');
title('Gain Response');
an = angle(h);
figure(2);
plot(w/pi,an);grid;
xlabel('\omega/\pi'); ylabel('Phase in radian');
title('Phase Response');

MagH = abs(h);
T1 = 1.005*ones(1,length(w));
T2 = 0.995*ones(1,length(w));
T3 = 0.005*ones(1,length(w));
figure(3);
plot(w/pi,MagH,w/pi,T1,w/pi,T2,w/pi,T3);grid;

27
The order filter, n of this design is 45 and the wn is 0.45. From the design, it does not meet the
specification as neither of the passband frequency, wp is 0.4 (normalized frequency) nor the stopband
frequency, ws is 0.5 (normalized frequency).

28
The filter coefficients in tabular form :
1 2 3 4 5 6 7 8 9 10
0.00001 0.0000 -0.0001 0.0002 0.0004 -0.0002 -0.0009 0.0000 0.0020 0.0008
11 12 13 14 15 16 17 18 19 20
-0.0032 -0.0028 0.0043 0.0063 -0.0044 -0.0115 0.0024 0.0187 0.0036 -0.027
21 22 23 24 25 26 27 28 29 30
0.0160 0.0357 0.0395 -0.0430 -0.0902 0.0482 0.3128 0.4500 0.3128 0.04818
31 32 33 34 35 36 37 38 39 40
-0.0902 -0.0430 0.0395 0.0356 -0.0160 -0.0270 0.0036 0.0187 0.0024 -0.0116
41 42 43 44 45 46 47 48 49 50
-0.0044 0.0063 0.0043 -0.0028 -0.0032 0.0008 0.0020 0.0000 -0.0010 -0.0002
51 52 53 54 55
0.0004 0.0002 -0.0001 0.0000 0.00001

In order for the design to meet the specifications, the order of the filter can be increased.
%Estimate order using Kaiser function
f = [Wp Ws];
a = [1 0];
dev = [Rp Rs];
[n wn beta ftype] = kaiserord(f,a,dev,fs);
n = n+50;

29
The order of the modified design is 104 and the wn is 0.45. The design is now meet with the
specification.

30
The filter coefficients in tabular form :
1 2 3 4 5 6 7 8 9 10
0.0000 0.0000 0.0000 0.0000 0.0001 0.0000 0.0001 0.0001 -0.0001 -0.0002
11 12 13 14 15 16 17 18 19 20
0.0001 0.0004 0.0000 -0.0006 -0.0002 0.0008 0.0006 -0.0009 -0.0012 0.0008
21 22 23 24 25 26 27 28 29 30
0.0019 -0.0004 -0.0026 -0.0005 0.0032 0.0017 -0.0035 -0.0035 0.0033 0.0055
31 32 33 34 35 36 37 38 39 40
-0.0022 -0.0077 0.0000 0.0097 0.0034 -0.0109 -0.0080 0.0108 0.0138 -0.0087
41 42 43 44 45 46 47 48 49 50
-0.0204 0.0038 0.0275 0.0049 -0.0345 -0.0192 0.0407 0.0434 -0.0457 -0.0933
51 52 53 54 55 56 57 58 59 60
0.0489 0.3139 0.4500 0.3139 0.0489 -0.0933 -0.0457 0.0434 0.0407 -0.0192
61 62 63 64 65 66 67 68 69 70
-0.0345 0.0049 0.0275 0.0038 -0.0204 -0.0087 0.0138 0.0108 -0.0080 -0.0109
71 72 73 74 75 76 77 78 79 80
0.0034 0.0097 0.0000 -0.0077 -0.0022 0.0055 0.0033 -0.0035 -0.0035 0.0017
81 82 83 84 85 86 87 88 89 90
0.0032 -0.0005 -0.0026 -0.0004 0.0019 0.0008 -0.0012 -0.0009 0.0006 0.0008
91 92 93 94 95 96 97 98 99 100
-0.0002 -0.0006 0.0000 0.0004 0.0001 -0.0002 -0.0001 0.0001 0.0001 0.0000
101 102 103 104 105
0.0001 0.0000 0.0000 0.0000 0.0000

31
Exercise 3:
Write a MATLAB program to design FIR bandpass filters
Length of the Filter = 51. http://kdd.ics.uci.edu/databases/eeg/eeg.full.html
This data arises from a large study to examine EEG correlates of genetic predisposition to alcoholism. It
contains measurements from 64 electrodes placed on the scalp sampled at 256 Hz. Download EEG
signal
of subject 1(alcoholic) and subject1 (Normal). Consider the following 5 different frequency bands, delta
(0-4Hz), theta (4-8Hz), alpha (8-16Hz), beta (16- 32Hz) and gamma (32¨C 64Hz). Compute and tabulate
the mean, minimum, maximum, average, standard deviation, variance, energy and power of the filtered
signal of Normal and alcoholic subjects.
(15 Marks)
Ans :
(a) Alcoholic
N = 51;
a = co2a0000364; %load EEG for alcoholic

%Delta
b1 = fir1(51,4/128,'low');
c1 = conv(a,b1,'same');
x1 = conv(b1,a,'same');
figure(1);
subplot(511);
plot(c1);
title('Alcoholic-Delta');
xlabel('Sample');
ylabel('Amplitude(mV)');
Mean = mean(x1)
Min = min(x1)
Max = max(x1)
Std = std(x1)
Var = var(x1)
energy_delta = sum(x1.^2)
power_delta = (sum(x1.^2))/length(x1)

%Theta
b2 = fir1(51,[4 8]/128,'bandpass');
c2 = conv(a,b2,'same');
x2 = conv(b2,a,'same');
subplot(512);
plot(c2);
title('Alcoholic-Theta');
xlabel('Sample'); ylabel('Amplitude(mV)');
Mean = mean(x2)

32
Min = min(x2)
Max = max(x2)
Std = std(x2)
Var = var(x2)
energy_theta = sum(x2.^2)
power_theta = (sum(x2.^2))/length(x2)

%Alpha
b3 = fir1(51,[8 16]/128,'bandpass');
c3 = conv(a,b3,'same');
x3 = conv(b3,a,'same');
subplot(513);
plot(c3);
title('Alcoholic-Alpha');
xlabel('Sample'); ylabel('Amplitude(mV)');
Mean = mean(x3)
Min = min(x3)
Max = max(x3)
Std = std(x3)
Var = var(x3)
energy_alpha = sum(x3.^2)
power_alpha = (sum(x3.^2))/length(x3)

%Beta
b4 = fir1(51,[16 32]/128,'bandpass');
c4 = conv(a,b4,'same');
x4 = conv(b4,a,'same');
subplot(514);
plot(c4);
title('Alcoholic-Beta');
xlabel('Sample'); ylabel('Amplitude(mV)');
Mean = mean(x4)
Min = min(x4)
Max = max(x4)
Std = std(x4)
Var = var(x4)
energy_beta = sum(x4.^2)
power_beta = (sum(x4.^2))/length(x4)

%Gamma
b5 = fir1(51,[32 64]/128,'bandpass');
c5 = conv(a,b5,'same');
x5 = conv(b5,a,'same');
subplot(515);
plot(c5);
title('Alcoholic-Gamma');
xlabel('Sample'); ylabel('Amplitude(mV)');
Mean = mean(x5)
Min = min(x5)
Max = max(x5)
Std = std(x5)
Var = var(x5)
energy_gamma = sum(x5.^2)
power_gamma = (sum(x5.^2))/length(x5)

33
(b) Normal
% Normal
N = 51;
a = co2c0000364; %load EEG for normal

%Delta
b1 = fir1(51,4/128,'low');
c1 = conv(a,b1,'same');
x1 = conv(b1,a,'same');
figure(1);
subplot(511);
plot(c1);
title('Normal-Delta');
xlabel('Sample'); ylabel('Amplitude(mV)');
Mean = mean(x1)
Min = min(x1)
Max = max(x1)
Std = std(x1)
Var = var(x1)
energy_delta = sum(x1.^2)
power_delta = (sum(x1.^2))/length(x1)

%Theta
b2 = fir1(51,[4 8]/128,'bandpass');
c2 = conv(a,b2,'same');
x2 = conv(b2,a,'same');
subplot(512);
plot(c2);
title('Normal-Theta');
xlabel('Sample'); ylabel('Amplitude(mV)');
Mean = mean(x2)
Min = min(x2)

34
Max = max(x2)
Std = std(x2)
Var = var(x2)
energy_theta = sum(x2.^2)
power_theta = (sum(x2.^2))/length(x2)

%Alpha
b3 = fir1(51,[8 16]/128,'bandpass');
c3 = conv(a,b3,'same');
x3 = conv(b3,a,'same');
subplot(513);
plot(c3);
title('Normal-Alpha');
xlabel('Sample'); ylabel('Amplitude(mV)');
Mean = mean(x3)
Min = min(x3)
Max = max(x3)
Std = std(x3)
Var = var(x3)
energy_alpha = sum(x3.^2)
power_alpha = (sum(x3.^2))/length(x3)

%Beta
b4 = fir1(51,[16 32]/128,'bandpass');
c4 = conv(a,b4,'same');
x4 = conv(b4,a,'same');
subplot(514);
plot(c4);
title('Normal-Beta');
xlabel('Sample'); ylabel('Amplitude(mV)');
Mean = mean(x4)
Min = min(x4)
Max = max(x4)
Std = std(x4)
Var = var(x4)
energy_beta = sum(x4.^2)
power_beta = (sum(x4.^2))/length(x4)

%Gamma
b5 = fir1(51,[32 64]/128,'bandpass');
c5 = conv(a,b5,'same');
x5 = conv(b5,a,'same');
subplot(515);
plot(c5);
title('Normal-Gamma');
xlabel('Sample'); ylabel('Amplitude(mV)');
Mean = mean(x5)
Min = min(x5)
Max = max(x5)
Std = std(x5)
Var = var(x5)
energy_gamma = sum(x5.^2)
power_gamma = (sum(x5.^2))/length(x5)

35
36
Tabulation of the mean, minimum, maximum, average, standard deviation, variance, energy and power
of the filtered signal of normal and alcoholic subjects :

Theta
Parameters Alcoholic Normal
Mean 6.7225 1.6146
Minimum -1.0577 -6.7878
Maximum 11.8906 9.5176
Standard 5.0368 5.7371
Deviation
Variance 25.3692 32.9149
Energy 3.6438e+03 1.8142e+03
Power 70.0731 34.8889
Alpha
Parameters Alcoholic Normal
Mean 0.1862 -0.1202
Minimum -4.4736 -3.0357
Maximum 2.8096 4.1327
Standard 1.9009 2.2033
Deviation
Variance 3.6135 4.8546
Energy 186.0899 248.3357
Power 3.5787 4.7757
Beta
Parameters Alcoholic Normal
Mean -0.0515 0.0971
Minimum -8.9825 -4.6622
Maximum 8.9547 5.0180
Standard 3.9201 2.3220
Deviation
Variance 15.3671 5.3919
Energy 783.8576 275.4759
Power 15.0742 5.2976
Gamma
Parameters Alcoholic Normal
Mean -0.0244 0.0219
Minimum -15.1740 -6.3486
Maximum 13.3657 6.9501
Standard 4.7747 2.1193

37
Deviation
Variance 22.7976 4.4914
Energy 1.1627e+03 229.0868
Power 22.3597 4.4055

CONCLUSION
By doing this lab, the objectives are achieved as I am able to design and evaluate the efficacy of FIR and
IIR filters for analyzing biomedical signals EEG using matlab software.

For PART A, we learnt how to design filter of Butterworth, Chebyshev type I and Chebyshev type II.
The butterworth bandstop filter is designed to reject the discrete frequency between the stopband edge.

For PART B, we learnt using a variety of window to design FIR filter. For example, Hanning window,
Hanning window and Blackman window. If the design of FIR filter does not meet the specification, the
order of filter need to be increased in order to meet the specification. This is because by increasing the
order of filter, the stopband become closer to the cutoff frequency, the output will be more accurate.

FIR bandpass filters with order, N=51 is design to evaluate the efficacy of FIR filters for analyzing
biomedical signals EEG correlates of genetic predisposition toalcoholism. 5 frequency bands which are
delta, theta, alpha, beta and gamma of filtered signals was plot and the mean, minimum, maximum,
average, standard deviation, variance, energy and power of the filtered signal of normal and alcoholic
subjects are computed and compared between both subjects. From the graph, we can observe that the
waveform of alcoholism is more inconsistent than the normal person. Therefore, it can be said that
consumption of alcohol effect the brain signal

38

You might also like