You are on page 1of 49

List of Experiments for DSP

No. Practical
1. Introduction of MATLAB
2. Generation of various discrete-time sequences:
(a)unit sample (d)Exponential
(b)unit step (e) Sinusoidal
(c)ramp (f) Square
3. Convolution and Deconvolution
4. System response:
(a) Impulse response (c) Frequency response
(b) Step response (d) Phase response
5. Z-Transform
Inverse Z-Transform
6. Sampling:
(a)Up sampling
(b) Down sampling
(c) Re-sampling
7. Discrete Fourier Transform Or FFT and IFFT:
(a)4-point
(b)8-point
8. Simulink
System response
Convolution/Deconvolution
Up/Down Sampling
9. DSP Application
Waveread, Wavwrite, Imread, Imwrite, Imshow
10. Design Digital IIR Butterworth
a) Low-pass
b) High-pass
c) Band-pass
d) Band-stop
11. Analog-to-Digital Filter Transformation by
a) Impulse invariance
b) Bilinear Transformation
12. FIR filter design using windowing

EC-402 DSP LAB MANUAL Prepared by: P.M.Shah

PDF created with pdfFactory Pro trial version www.pdffactory.com


Lab 1: Introduction of MATLAB
Introduction to MATLAB
MATLAB is a high – performance language for technical computing. It integrates computation,
visualization and programming in an easy – to – use environment where problems and solutions
are expressed in familiar mathematical notations. It includes…

• Math and Computation


• Algorithm development
• Data acquisition
• Modeling, simulation and prototyping
• Scientific and Engineering graphics
• Application development, including GUI building.

The MATLAB system consists of five main parts:

Development Environment:
This is the set of tools and facilities that help you use MATLAB functions and files. Many of these
tools are GUIs. It includes MATLAB desktop and Command Window, a command history, an
editor and debugger, and browsers for viewing help, the workspace, files, and the search path.

The MATLAB Mathematical Function Library:


This is a vast collection of computational algorithms ranging from elementary functions like sum,
sin, cosine, and complex arithmetic, to more sophisticated functions like matrix inverse, matrix
eigenvalues, Bessel Functions and fast Fourier Transforms.

The MATLAB Language:


This is high – level matrix array language with control flow statements, functions, data structures,
input/output, and object – orientated programming features. It allows both “programming in the
small” to rapidly create quick and dirty throw – away programs, and “programming in the large” to
create complete large and complex application programs.

Graphics:
MATLAB has extensive facilities for displaying vectors and matrices as graphs, as well as
annotating and printing these graphs. It includes high – level functions for two dimensional and
three dimensional data visualization, image processing, animation and presentation graphics. It
also includes low level functions that allow you to fully customize the appearance of graphics as
well as to build complete graphical user interface on your MATLAB applications.

The MATLAB Application Program Interface(API):


This is a library that allows you to write C and Fortran programs that interact with MATLAB. It
includes facilities for calling routines from MATLAB, calling MATLAB as a computational
engine, and for reading and writing MAT- files.

EC-402 DSP LAB MANUAL Prepared by: P.M.Shah

PDF created with pdfFactory Pro trial version www.pdffactory.com


Tutorial 1

1. From the command line, evaluate these expressions (a-d) using the following values, t=3.25,
x=4.5,y=6.97,z=1.03
a.) M = 4 x 2 + 3 y + 10
b.) N = e log10 y + 2 z x
1 1
c.)O = +
( x + y ) (t + z )
π
−  x 
d .) P = 4e 2
− sin  
π y 
Ans:
echo on
t=3.25
x=4.5
y=6.97
z=1.03
M=4*x^2+3*y+10
N=exp(log10(y))+2*z^x
O=sqrt(1/(x+y)+1/(t+z));
P=4*exp(-pi/2)-abs(sin(x/(pi*y)));

2.
a. Create a vector X with a starting value of 0.0 and an ending value of 50, with increments of 0.1
between values
b. Create a vector Y with a starting value of 0.1 and an ending value of 50 and with the same
number of elements as in X. The elements must be evenly spaced.

Ans:
X=0:0.1:50;
Y=linspace(.1,50,length(X));

3.
a. Using MATLAB commands create an array, labeled A, consisting of the first 10 prime numbers
listed in increasing order.
b. Using MATLAB commands on the array A to create the matrix B
13 17 19 23 29
B= 
13 11 7 5 3 
Ans:
A=primes(30)
B=[A(6:10);A(6:-1:2)]

EC-402 DSP LAB MANUAL Prepared by: P.M.Shah

PDF created with pdfFactory Pro trial version www.pdffactory.com


4.
a. Use MATLAB commands to find the transpose of B, which you name C
b. Use MATLAB to Compute B*C and C*B.

Ans:
C=B'
BC=B*C
CB=C*B

5. Use a MATLAB command to create directly from matrix B, above, the matrices
19 
D= 
7
E = [19 7 ]

Ans:
D=B(:,3);
E=B(:,3)';

6. Verify Woodberry's Identity: ( A + DE ) = A


−1 −1

( A DEA ) for A = 5
−1 −1
2
3 1 
(1 + EA D )
−1

Ans:
A=[5 2;3 1];
A1=inv(A+D*E);
A2=inv(A)-(inv(A)*D*E*inv(A))*inv(1+E*inv(A)*D);

7. Write a MATLAB code to complete the magic square (not using the command magic) so that the
x1 x2 x3
sum from all sides is 15, x4 x5 x6 . Hint : Use solution to system of equations
4 x7 2
Ans:
A=[1 1 1 0 0 0 0 15;
0 0 0 1 1 1 0 15;
0 0 0 0 0 0 1 9;
1 0 0 1 0 0 0 11;
0 1 0 0 1 0 1 15;
0 0 1 0 0 1 0 13;
1 0 0 0 1 0 0 13;
0 0 1 0 1 0 0 11];
X=A(:,1:7)\A(:,8);

Magic_Sq=[X(1:3)';
X(4:6)';
4 X(7) 2]

EC-402 DSP LAB MANUAL Prepared by: P.M.Shah

PDF created with pdfFactory Pro trial version www.pdffactory.com


Lab 2: Generation of various discrete-time sequences: (Tutorial 2)

1. Generate row and column vector of n zero's.


Ans: a=zeros(100,1);
a1=zeros(1,100);

2. Generate row and column vector of n one's.


Ans: b=ones(100,1);
b1=ones(1,100);

3. Generate matrix of 10 * 10 and fill up matrix with 1 to 100 numbers.


Ans: a(10,10)=0;
a(1:100)=1:100;
a'

4. Generate and plot the unit impulse sequence.


Ans: a(1:10)=0;
a(1,1)=1;
stem(a);

5. Generate and the unit step sequence.


Ans: a(1:10)=1;
stem(a);

6. Generate and the ramp sequence.


Ans: a(1:10)=1:10;
stem(a);

EC-402 DSP LAB MANUAL Prepared by: P.M.Shah

PDF created with pdfFactory Pro trial version www.pdffactory.com


7. Generate and the exponential decaying sequence.
Ans: t = 0:10;
d = 0.5
c(1,10) = 0;
c = d.^t
Stem(t,c);
8. Generate and the exponential growing sequence.
Ans: t = 0:10;
e = 2;
f(1,10) = 0;
f = e.^t
Stem(t,f);

9. Generate and plot sinusoidal sequence.


Ans: t = 0:10;
g(1,10) = 0;
g = sin(2*pi*50*t/180);
stem(t,g);
10. Generate and plot cosine sequence.
Ans: t = 0:10;
g(1,10) = 0;
g = cos(2*pi*50*t/180);
stem(t,g);
11. Generate and plot square sequence.
Ans: t=0:0.0001:0.0625;
y=square(2*pi*30*t);
plot(t,y);

EC-402 DSP LAB MANUAL Prepared by: P.M.Shah

PDF created with pdfFactory Pro trial version www.pdffactory.com


MATLAB PROGRAM:
clc;clear all;close all;
n=input('enter the the number of samples n=')
t=0:n-1;

% Unit Impulse sequence


y=[zeros(1,n)]
y(1,1)=1
subplot(3,3,1);
stem(t,y,'m');
ylabel('Amlitude')
xlabel('time')
title('Unit Impulse')

%Unit step sequence


y1=[ones(1,n)]
subplot(3,3,2)
stem(t,y1,'v')
ylabel('Amlitude')
xlabel('time')
title('Unit step')

% Ramp sequence
subplot(3,3,3)
stem(t,t,'r')
ylabel('Amlitude')
xlabel('time')
title('ramp')

% Exponential sequence
a=0.5
y2=a.^t
subplot(3,3,4)
stem(t,y2,'y')
ylabel('Amlitude')
xlabel('time')
title('Exp decaying')

a1=1.5
y2=a1.^t
subplot(3,3,5)
stem(t,y2,'g')
ylabel('Amlitude')
xlabel('time')
title('Exp Growing')

EC-402 DSP LAB MANUAL Prepared by: P.M.Shah

PDF created with pdfFactory Pro trial version www.pdffactory.com


% Sine sequence
y3=sin(t)
subplot(3,3,6)
stem(t,y3,'p')
ylabel('Amlitude')
xlabel('time')
title('Sine')

% Cosine sequence
y4=cos(t)
subplot(3,3,7)
stem(t,y4,'m')
ylabel('Amlitude')
xlabel('time')
title('Cosine')

% Square sequence
y5=square(t)
subplot(3,3,8)
stem(t,y5,'r')
ylabel('Amlitude')
xlabel('time')
title('Square')

OUTPUT ON COMMAND WINDOW:


enter the the number of samples n=20
n=
20
y=
Columns 1 through 13
0 0 0 0 0 0 0 0 0 0 0 0 0
Columns 14 through 20
0 0 0 0 0 0 0
y=
Columns 1 through 13
1 0 0 0 0 0 0 0 0 0 0 0 0
Columns 14 through 20
0 0 0 0 0 0 0
y1 =
Columns 1 through 13
1 1 1 1 1 1 1 1 1 1 1 1 1
Columns 14 through 20
1 1 1 1 1 1 1
a=
0.5000

EC-402 DSP LAB MANUAL Prepared by: P.M.Shah

PDF created with pdfFactory Pro trial version www.pdffactory.com


y2 =
Columns 1 through 8
1.0000 0.5000 0.2500 0.1250 0.0625 0.0313 0.0156 0.0078
Columns 9 through 16
0.0039 0.0020 0.0010 0.0005 0.0002 0.0001 0.0001 0.0000
Columns 17 through 20
0.0000 0.0000 0.0000 0.0000
a1 =
1.5000
y2 =
1.0e+003 *
Columns 1 through 8
0.0010 0.0015 0.0022 0.0034 0.0051 0.0076 0.0114 0.0171
Columns 9 through 16
0.0256 0.0384 0.0577 0.0865 0.1297 0.1946 0.2919 0.4379
Columns 17 through 20
0.6568 0.9853 1.4779 2.2168
y3 =
Columns 1 through 8
0 0.8415 0.9093 0.1411 -0.7568 -0.9589 -0.2794 0.6570
Columns 9 through 16
0.9894 0.4121 -0.5440 -1.0000 -0.5366 0.4202 0.9906 0.6503
Columns 17 through 20
-0.2879 -0.9614 -0.7510 0.1499
y4 =
Columns 1 through 8
1.0000 0.5403 -0.4161 -0.9900 -0.6536 0.2837 0.9602 0.7539
Columns 9 through 16
-0.1455 -0.9111 -0.8391 0.0044 0.8439 0.9074 0.1367 -0.7597
Columns 17 through 20
-0.9577 -0.2752 0.6603 0.9887
y5 =
Columns 1 through 13
1 1 1 1 -1 -1 -1 1 1 1 -1 -1 -1
Columns 14 through 20
1 1 1 -1 -1 -1 1

EC-402 DSP LAB MANUAL Prepared by: P.M.Shah

PDF created with pdfFactory Pro trial version www.pdffactory.com


EC-402 DSP LAB MANUAL Prepared by: P.M.Shah

PDF created with pdfFactory Pro trial version www.pdffactory.com


Lab 3: Convolution and Deconvolution

close all;
clear all;
clc;
x = input(' Enter First Seq : ');
y = input(' Enter Second Seq : ');
%x = [1 2 3 4 5];
%y = [3 4 6];
n= length(x)+length(y)-1;
yl = length(y);
xl = length(x);

x0 = zeros(1,n+xl);
x0(yl:yl+xl-1)=x;

y0 = zeros(1,n+yl);
y0(1:yl)=y(yl:-1:1);

for i = 1:n

h(i)=0;
for k = yl:yl+xl-1
h(i) = h(i)+x0(k)*y0(k);
end
for t = length(y0):-1:2
y0(t)=y0(t-1);
end
y0(1)=0;
end
disp('Without using inbuilt Command');
h
disp('With using inbuilt Command');
h0 = conv(x,y)
disp('Deconvolution')
[q,r]=deconv(h0,y);
disp(q)
disp('Deconvolution')
[q,r]=deconv(h0,x);
disp(q)

EC-402 DSP LAB MANUAL Prepared by: P.M.Shah

PDF created with pdfFactory Pro trial version www.pdffactory.com


Output:
Enter First Seq : [1 1 1 1]
Enter Second Seq : [1 1 1 1]
Without using inbuilt Command

h=

1 2 3 4 3 2 1

With using inbuilt Command

h0 =

1 2 3 4 3 2 1

Deconvolution
1 1 1 1

Deconvolution
1 1 1 1

EC-402 DSP LAB MANUAL Prepared by: P.M.Shah

PDF created with pdfFactory Pro trial version www.pdffactory.com


Lab 4: System Response
% …………………..impulse response
close all
clear all
clc
a=[1/3];
b=[1 -5/6 1/6]
x=[1 0 0 0 0 0 0 0];
figure(1)
freqz(a,b);
h=filter(a,b,x);
figure(2)
subplot(2,1,1)
stem(h)
%................................ with impz command
h=impz(a,b)

%................................ step response


y=[1 1 1 1 1 1 1 1 1]
h=filter(a,b,y);
%................................ with stepz command
s=stepz(a,b);
subplot(2,1,2);
stem(s)
fvtool(a,b)
Output:
b=
1.0000 -0.8333 0.1667
h=
0.3333
0.2778
0.1759
0.1003
0.0543
0.0285
0.0147
0.0075
0.0038
0.0019
0.0010
0.0005
0.0002
0.0001
y=
1 1 1 1 1 1 1 1 1

EC-402 DSP LAB MANUAL Prepared by: P.M.Shah

PDF created with pdfFactory Pro trial version www.pdffactory.com


Frequency Response:

EC-402 DSP LAB MANUAL Prepared by: P.M.Shah

PDF created with pdfFactory Pro trial version www.pdffactory.com


Impulse & Step
Response:

EC-402 DSP LAB MANUAL Prepared by: P.M.Shah

PDF created with pdfFactory Pro trial version www.pdffactory.com


Pole and Zero
Plots:

EC-402 DSP LAB MANUAL Prepared by: P.M.Shah

PDF created with pdfFactory Pro trial version www.pdffactory.com


Lab 5 : Z-Transform, Inverse Z-Transform

%............. z-transform & inverse z-transform


clc
close all
clear all
n=1;
d=[1 -1.5 0.5];
o=tf(n,d,'variable','z^-1')

%............. inverse z by partial fraction


[r,p,k]=residuez(n,d)
a=tf([-1],[1 -1],'variable','z^-1')
b=tf([-1],[1 -0.5],'variable','z^-1')

%............. by power series


c=impz(n,d)

%............. for stability poles


[z,p,k]=tf2zp(n,d)
zplane(z,p)

%............. for converting higher order to 2nd order


n1=1;
d1=[1 0.5 6 1];
[sos,g]=tf2sos(n1,d1)
[a,b]=sos2tf(sos)

Output:
c=
1.0000
1.5000
1.7500
1.8750
1.9375
1.9688
1.9844
1.9922
1.9961
1.9980
1.9990
1.9995
1.9998
1.9999

EC-402 DSP LAB MANUAL Prepared by: P.M.Shah

PDF created with pdfFactory Pro trial version www.pdffactory.com


z=
Empty matrix: 0-by-1

p=
1.0000
0.5000

k=
1

sos =
1.0000 0 0 1.0000 0.1682 0
1.0000 0 0 1.0000 0.3318 5.9442

g=
1

a=
1 0 0 0

b=
1.0000 0.5000 6.0000 1.0000

EC-402 DSP LAB MANUAL Prepared by: P.M.Shah

PDF created with pdfFactory Pro trial version www.pdffactory.com


Z-PLANE:

EC-402 DSP LAB MANUAL Prepared by: P.M.Shah

PDF created with pdfFactory Pro trial version www.pdffactory.com


Lab 6: Sampling (a) Up (b) Down (c) Re-sampling

clear all;
close all;
%x= [1 3 2 5 7 8 4 2];
fc = .3*pi;
n= 0:40;
x = sin(2*pi*fc*n);

figure(1);
subplot(2,1,1);
stem(x);
title('original signal');

y = upsample(x,2);
subplot(2,1,2);
stem(y);
title('upsampled signal *2');

figure(2);
subplot(2,1,1);
z = downsample(x,2);
stem(z);
title('downsampled signal /2');

w = resample(x,2,3);
subplot(2,1,2);
stem(w);
title('re sampled signal(2/3)');

figure(3);
%upsampling
subplot(2,1,1)
x3 = interp(x,2);
stem(x3);
N = 2;
len = length(x);
len1 = N*len;
x1 = zeros(1,len1);
x1(1:2:len1) = x;
subplot(2,1,2);
stem(x1);

EC-402 DSP LAB MANUAL Prepared by: P.M.Shah

PDF created with pdfFactory Pro trial version www.pdffactory.com


figure(4);
%downsampling
M = 2;
len = length(x);
len1 = len/M;
x1 = x(1:2:len);
stem(x1);

figure(5);
subplot(2,1,1);
stem(x1);
subplot(2,1,2);
x2 = decimate(x,2);
stem(x2);

EC-402 DSP LAB MANUAL Prepared by: P.M.Shah

PDF created with pdfFactory Pro trial version www.pdffactory.com


EC-402 DSP LAB MANUAL Prepared by: P.M.Shah

PDF created with pdfFactory Pro trial version www.pdffactory.com


EC-402 DSP LAB MANUAL Prepared by: P.M.Shah

PDF created with pdfFactory Pro trial version www.pdffactory.com


EC-402 DSP LAB MANUAL Prepared by: P.M.Shah

PDF created with pdfFactory Pro trial version www.pdffactory.com


EC-402 DSP LAB MANUAL Prepared by: P.M.Shah

PDF created with pdfFactory Pro trial version www.pdffactory.com


EC-402 DSP LAB MANUAL Prepared by: P.M.Shah

PDF created with pdfFactory Pro trial version www.pdffactory.com


Lab 7: Discrete Fourier Transform Or FFT and IFFT: (a) 4-point (b) 8-point

Part – A

function y = twiddle_f(n,k,N);

y = exp(-2*pi*j*n*k/N);

Part – B

clear all;
close all;
%Decimation in Time FFT algorithm
% implementation

x = [1 2 3 4 5 6 7 8];

xo = x(1:2:length(x));
xe = x(2:2:length(x));

xoo = xo(1:2:length(xo));
xeo = xo(2:2:length(xo));

xoe = xe(1:2:length(xe));
xee = xe(2:2:length(xe));

for i = 1:2
A(i) = 0;
B(i) = 0;
C(i) = 0;
D(i) = 0;
for j = 1:2
A(i) = A(i)+ xoo(j)*twiddle_f(j-1,i-1,2);
B(i) = B(i)+ xeo(j)*twiddle_f(j-1,i-1,2);
C(i) = C(i)+ xoe(j)*twiddle_f(j-1,i-1,2);
D(i) = D(i)+ xee(j)*twiddle_f(j-1,i-1,2);
end
end

for i = 1:4
if i<=2
G(i) = A(i) + twiddle_f(i-1,1,4)*B(i);
H(i) = C(i) + twiddle_f(i-1,1,4)*D(i);
else

EC-402 DSP LAB MANUAL Prepared by: P.M.Shah

PDF created with pdfFactory Pro trial version www.pdffactory.com


G(i) = A(i-2) + twiddle_f(i-1,1,4)*B(i-2);
H(i) = C(i-2) + twiddle_f(i-1,1,4)*D(i-2);
end
end

for i = 1:8;
if i<=4
X(i) = G(i) + twiddle_f(i-1,1,8)*H(i);
else
X(i) = G(i-4) + twiddle_f(i-1,1,8)*H(i-4);
end
end

X1 =fft(x,8);

subplot(2,1,1);
stem(abs(X));
title('FFT using algorithm');

subplot(2,1,2);
stem(abs(X1));
title('FFT using in-built function');

EC-402 DSP LAB MANUAL Prepared by: P.M.Shah

PDF created with pdfFactory Pro trial version www.pdffactory.com


EC-402 DSP LAB MANUAL Prepared by: P.M.Shah

PDF created with pdfFactory Pro trial version www.pdffactory.com


Lab 8: Simulink

Drag and Drop in Simulink Windows:


In a Simulink model, if you select and drag from one window to another, the
selection will appear in the destination window but you will not see it while it is being
dragged outside a Simulink window. This is also true in any other drag and drop situation
between windows. Simulink Model Menus When working with Simulink models, the
OroborOSX menus appear at the top of the desktop.

Simulink Library:

EC-402 DSP LAB MANUAL Prepared by: P.M.Shah

PDF created with pdfFactory Pro trial version www.pdffactory.com


Output On Scope:

EC-402 DSP LAB MANUAL Prepared by: P.M.Shah

PDF created with pdfFactory Pro trial version www.pdffactory.com


Output On Scope:

EC-402 DSP LAB MANUAL Prepared by: P.M.Shah

PDF created with pdfFactory Pro trial version www.pdffactory.com


Lab 9 : DSP Application
close all;
clear all;
clc;
a=imread('rice.tif');
b=imnoise(a,'speckle');
c=imnoise(a,'gaussian');
d=imnoise(a,'salt & pepper');
d1=medfilt2(d);
subplot(2,3,1);
imshow(a);
subplot(2,3,2);
imshow(b);
subplot(2,3,3);
imshow(c);
subplot(2,3,4);
imshow(d);
subplot(2,3,5);
imshow(d1);

EC-402 DSP LAB MANUAL Prepared by: P.M.Shah

PDF created with pdfFactory Pro trial version www.pdffactory.com


EC-402 DSP LAB MANUAL Prepared by: P.M.Shah

PDF created with pdfFactory Pro trial version www.pdffactory.com


Lab 10: Design Digital IIR Butterworth

clc;
clear all;
close all;

%LOW PASS FILTER

rp=input('Enter Passband Ripple : ');


rs=input('Enter Stopband Ripple : ');
fs=input('Enter Sampling Freq : ');
disp(' ');
disp('FOR LOW PASS FILTER');

wp=input('Enter Passband Freq : ');


ws=input('Enter Stopband Freq : ');

w1=2*wp/fs;
w2=2*ws/fs;

[n,wn]=buttord(w1,w2,rp,rs);
[b,a]=butter(n,wn);

w=0:0.01:pi;
[h,om]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
figure(1);
subplot(2,1,1);
plot(om/pi,m);
xlabel('Normalised Freq');
ylabel('Gain in dB');
%plot(om/pi,m);
subplot(2,1,2);
plot(om/pi,an);
xlabel('Normalised Freq');
ylabel('Phase in Radians');

%HIGH PASS FILTER


disp(' ');
disp('FOR HIGH PASS FILTER');
%rp=input('Enter Passband Ripple : ');
%rs=input('Enter Stopband Ripple : ');
wp=input('Enter Passband Freq : ');

EC-402 DSP LAB MANUAL Prepared by: P.M.Shah

PDF created with pdfFactory Pro trial version www.pdffactory.com


ws=input('Enter Stopband Freq : ');
%fs=input('Enter Sampling Freq : ');

w1=2*wp/fs;
w2=2*ws/fs;

[n,wn]=buttord(w1,w2,rp,rs);
[b,a]=butter(n,wn,'high');

w=0:0.01:pi;
[h,om]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
figure(2);
subplot(2,1,1);
plot(om/pi,m);
xlabel('Normalised Freq');
ylabel('Gain in dB');
%plot(om/pi,m);
subplot(2,1,2);
plot(om/pi,an);
xlabel('Normalised Freq');
ylabel('Phase in Radians');

%BAND PASS FILTER


disp(' ');
disp('FOR BAND PASS FILTER');
%rp=input('Enter Passband Ripple : ');
%rs=input('Enter Stopband Ripple : ');
wp=input('Enter Passband Freq : ');
ws=input('Enter Stopband Freq : ');
%fs=input('Enter Sampling Freq : ');

w1=2*wp/fs;
w2=2*ws/fs;

[n]=buttord(w1,w2,rp,rs);
wn=[w1 w2];
[b,a]=butter(n,wn,'bandpass');

w=0:0.01:pi;
[h,om]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
figure(3);
subplot(2,1,1);

EC-402 DSP LAB MANUAL Prepared by: P.M.Shah

PDF created with pdfFactory Pro trial version www.pdffactory.com


plot(om/pi,m);
xlabel('Normalised Freq');
ylabel('Gain in dB');

subplot(2,1,2);
plot(om/pi,an);
xlabel('Normalised Freq');
ylabel('Phase in Radians');

%BAND STOP FILTER


disp(' ');
disp('FOR BAND STOP FILTER');
%rp=input('Enter Passband Ripple : ');
%rs=input('Enter Stopband Ripple : ');
wp=input('Enter Passband Freq : ');
ws=input('Enter Stopband Freq : ');
%fs=input('Enter Sampling Freq : ');

w1=2*wp/fs;
w2=2*ws/fs;

[n]=buttord(w1,w2,rp,rs);
wn=[w1 w2];
[b,a]=butter(n,wn,'stop');
w=0:0.01:pi;
[h,om]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
figure(4);
subplot(2,1,1);
plot(om/pi,m);
xlabel('Normalised Freq');
ylabel('Gain in dB');
%plot(om/pi,m);
subplot(2,1,2);
plot(om/pi,an);
xlabel('Normalised Freq');
ylabel('Phase in Radians');

EC-402 DSP LAB MANUAL Prepared by: P.M.Shah

PDF created with pdfFactory Pro trial version www.pdffactory.com


Output :
Enter Passband Ripple : 0.001
Enter Stopband Ripple : 0.1
Enter Sampling Freq : 0.5

FOR LOW PASS FILTER


Enter Passband Freq : 0.1
Enter Stopband Freq : 0.2

FOR HIGH PASS FILTER


Enter Passband Freq : 0.2
Enter Stopband Freq : 0.1
Warning: Log of zero.
> In C:\MATLAB6p5\toolbox\matlab\elfun\log10.m at line 17
In C:\MATLAB6p5\work\as.m at line 56

FOR BAND PASS FILTER


Enter Passband Freq : 0.2
Enter Stopband Freq : 0.1
Warning: Log of zero.
> In C:\MATLAB6p5\toolbox\matlab\elfun\log10.m at line 17
In C:\MATLAB6p5\work\as.m at line 87

FOR BAND STOP FILTER


Enter Passband Freq : 0.1
Enter Stopband Freq : 0.2

EC-402 DSP LAB MANUAL Prepared by: P.M.Shah

PDF created with pdfFactory Pro trial version www.pdffactory.com


LOW PASS
FILTER:

EC-402 DSP LAB MANUAL Prepared by: P.M.Shah

PDF created with pdfFactory Pro trial version www.pdffactory.com


HIGH PASS FILTER:

BAND PASS FILTER:

EC-402 DSP LAB MANUAL Prepared by: P.M.Shah

PDF created with pdfFactory Pro trial version www.pdffactory.com


BAND STOP FILTER:

EC-402 DSP LAB MANUAL Prepared by: P.M.Shah

PDF created with pdfFactory Pro trial version www.pdffactory.com


EC-402 DSP LAB MANUAL Prepared by: P.M.Shah

PDF created with pdfFactory Pro trial version www.pdffactory.com


Lab 11: Analog-to-Digital Filter Transformation By

b) Impulse invariance
close all;
clear all;
clc
a=input('Enter the Numerator : ');
b=input('Enter the Denumerator : ');
f=input('Enter the Sampling Freq : ');
sys=tf(a,b);
[numd dend]=impinvar(a,b,f);
disp(numd);
disp(dend);
disp(sys);

Output:
Enter the Numerator : [1 2]
Enter the Denumerator : [1 3 4]
Enter the Sampling Freq : 0.3
3.3333 -0.0014
1.0000 0.0040 0.0000
tf object: 1-by-1

c) Bilinear Transformation
close all;
clear all;
clc
a=input('Enter The Numerator : ');
b=input('Enter The Denumerator : ');
sys=tf(a,b);
f=input('Enter The Sampling Freq : ');
[numd dend]=bilinear(a,b,f);
disp(numd);
disp(dend);

Output:
Enter The Numerator : [1 2]
Enter The Denumerator : [2 3 4]
Enter The Sampling Freq : .4
0.3646 0.5208 0.1563
1.0000 0.7083 0.3750

EC-402 DSP LAB MANUAL Prepared by: P.M.Shah

PDF created with pdfFactory Pro trial version www.pdffactory.com


Lab 12: FIR filter design using windowing
%FIR FILTER DESIGN
clc;clear all;close all;
pr=0.05;sr=0.04;
pf=1500;sf=2000;
f=9000;
wp=2*pf/f;ws=2*sf/f;
%LOW PASS FILTER
N=(-20*log10(sqrt(pr*sr))-13)/(14.6*(sf-pf)/f);
N=ceil(N);
%RECTANGULAR WINDOW
y=boxcar(N);
b=fir1(N-1,wp,y);
figure(1);
freqz(b,1,256);
title('Rectangular Window');
%BARTLETT WINDOW
y=bartlett(N);
b=fir1(N-1,wp,y);
figure(2);
freqz(b,1,256);
title('Bartlett Window');
%HAMMING WINDOW
y=hamming(N);
b=fir1(N-1,wp,y);
figure(3);
freqz(b,1,256);
title('Hamming Window');
%HANNING WINDOW
y=hanning(N);
b=fir1(N-1,wp,y);
figure(4);
freqz(b,1,256);
title('Hanning Window');
%KAISER WINDOW
beta=5.8;
y=kaiser(N,beta);
b=fir1(N-1,wp,y);
figure(5)freqz(b,1,256);title('KaiserWindow');

EC-402 DSP LAB MANUAL Prepared by: P.M.Shah

PDF created with pdfFactory Pro trial version www.pdffactory.com


RECTANGULAR
WINDOW

EC-402 DSP LAB MANUAL Prepared by: P.M.Shah

PDF created with pdfFactory Pro trial version www.pdffactory.com


EC-402 DSP LAB MANUAL Prepared by: P.M.Shah

PDF created with pdfFactory Pro trial version www.pdffactory.com


EC-402 DSP LAB MANUAL Prepared by: P.M.Shah

PDF created with pdfFactory Pro trial version www.pdffactory.com


EC-402 DSP LAB MANUAL Prepared by: P.M.Shah

PDF created with pdfFactory Pro trial version www.pdffactory.com


EC-402 DSP LAB MANUAL Prepared by: P.M.Shah

PDF created with pdfFactory Pro trial version www.pdffactory.com

You might also like