Digital Signal Processing Lab Manual

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

www.rejinpaul.

com
EC 6511 DIGITAL SIGNAL PROCESSING LAB MANUAL

INTRODUCTION

MATLAB stands for MATrix LABoratory. It is a technical computing environment for high
performance numeric computation and visualisation. It integrates numerical analysis, matrix
computation, signal processing and graphics in an easy-to-use environment, where problems and
solutions are expressed just as they are written mathematically, without traditional programming.
MATLAB allows us to express the entire algorithm in a few dozen lines, to compute the solution
with great accuracy in a few minutes on a computer, and to readily manipulate a three-
dimensional display of the result in colour.
MATLAB is an interactive system whose basic data element is a matrix that does not require
dimensioning. It enables us to solve many numerical problems in a fraction of the time that it
would take to write a program and execute in a language such as FORTRAN, BASIC, or C. It
also features a family of application specific solutions, called toolboxes. Areas in which
toolboxes are available include signal processing, image processing, control systems design,
dynamic systems simulation, systems identification, neural networks, wavelength
communication and others. It can handle linear, non-linear, continuous-time, discrete-time,
multivariable and multirate systems. This chapter gives simple programs to solve specific
problems that are included in the previous chapters. All these MATLAB programs have been
tested under version 7.1 of MATLAB and version 6.12 of the signal processing toolbox.

MATLAB COMMANDS:

S.SUMATHI , AP/ECE Page 2

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com
EC 6511 DIGITAL SIGNAL PROCESSING LAB MANUAL

GENERATION OF SIGNALS
1.A. CONTINUOUS TIME SIGNAL

Aim

To Generate a continuous sinusoidal time signals Using MATLAB.

Requirements

Matlab 2007 SOFTWARE

Procedure

1. OPEN MATLAB
2. File New Script.
a. Type the program in untitled window
3. File Save type filename.m in matlab workspace path
4. Debug Run. Wave will displayed at Figure dialog box.

Theory
Common Periodic Waveforms

The toolbox provides functions for generating widely used periodic waveforms:sawtooth
generates a sawtooth wave with peaks at ±1 and a period of 2π. An optional width parameter
specifies a fractional multiple of 2π at which the signal's maximum occurs. square generates a
square wave with a period of 2π. An optional parameter specifies duty cycle, the percent of the
period for which the signal is positive.

Common Aperiodic Waveforms

The toolbox also provides functions for generating several widely used aperiodic waveforms:
gauspuls generates a Gaussian-modulated sinusoidal pulse with a specified time, center
frequency, and fractional bandwidth. Optional parameters return in-phase and Quadrature pulses,
the RF signal envelope, and the cutoff time for the trailing pulse envelope. chirp generates a
linear, log, or quadratic swept-frequency cosine signal. An optional parameter specifies
alternative sweep methods. An optional parameter phi allows initial phase to be specified in
degrees.
Program %

S.SUMATHI , AP/ECE Page 3

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com
EC 6511 DIGITAL SIGNAL PROCESSING LAB MANUAL

% Assuming The Sampling frequency is 5 Mhz

clc; clear all;


clear all; Finput = 1000;
t = 0:0.0005:1; Fsampling = 5000000;
a = 10 Tsampling = 1 / Fsampling;
f = 13; Nsample = Fsampling/ Finput;
xa = a*sin(2*pi*f*t); N = 0:5*Nsample-1;
subplot(2,1,1) x=sin(2 * pi * Finput * Tsampling * N);
plot(t,xa);grid plot(x); title('Sine Wave Generation');
xlabel('Time, msec'); xlabel('Time -- >');
ylabel('Amplitude'); ylabel('Amplitude-- >');
title('Continuous-time signal x_{a}(t)'); grid on;
axis([0 1 -10.2 10.2])

Result

Thus the Continuous Time Signal was generated using MATLAB.

S.SUMATHI , AP/ECE Page 4

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com
EC 6511 DIGITAL SIGNAL PROCESSING LAB MANUAL

1.B. DISCRETE TIME SIGNAL


Aim

To Generate a Discrete time Exponential signals Using MATLAB.

Requirements

Matlab 2007

Personal computer

Procedure

1. OPEN MATLAB

2. File  New  Script.


a. Type the program in untitled window
3. File  Save  type filename.m in matlab workspace path

4. Debug  Run. Wave will displayed at Figure dialog box.

Theory:

Program

clear all;
a = 10;
f = 13;
T = 0.01;
n = 0:T:1;
xs = a*sin(2*pi*f*n);
k = 0:length(n)-1;
stem(k,xs);
grid
xlabel('Time index n');
ylabel('Amplitude');
title('Discrete-time signal x[n]');
axis([0 (length(n)-1) -10.2 10.2])

S.SUMATHI , AP/ECE Page 5

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com
EC 6511 DIGITAL SIGNAL PROCESSING LAB MANUAL

Expected Graph:

Result

Thus the Discrete Time Signal was generated using MATLAB.

S.SUMATHI , AP/ECE Page 6

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com
EC 6511 DIGITAL SIGNAL PROCESSING LAB MANUAL

GENERATIONS OF ELEMENTRY SEQUENCES


Aim : To develop elementary signal function modules (m-files) for unit sample, unit step, exponential
and unit ramp sequences.

Apparatus :

PC having MATLAB software.

Program :

% program for generation of unit sample

clc;clear all;close all;

t = -3:1:3;

y = [zeros(1,3),ones(1,1),zeros(1,3)];

subplot(2,2,1);stem(t,y);

ylabel('Amplitude------>');

xlabel('(a)n ------>');

title('Unit Impulse Signal');

% program for genration of unit step of sequence [u(n)- u(n)-N]

t = -4:1:4;

y1 = ones(1,9);

subplot(2,2,2);stem(t,y1);

ylabel('Amplitude------>');

xlabel('(b)n ------>');

title('Unit step');

% program for generation of ramp signal

n1 = input('Enter the value for end of the seqeuence '); %n1 = <any value>7 %

x = 0:n1;

subplot(2,2,3);stem(x,x);

ylabel('Amplitude------>');

S.SUMATHI , AP/ECE Page 7

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com
EC 6511 DIGITAL SIGNAL PROCESSING LAB MANUAL

xlabel('(c)n ------>');

title('Ramp sequence');

% program for generation of exponential signal

n2 = input('Enter the length of exponential seqeuence '); %n2 = <any value>7 %

t = 0:n2;

a = input('Enter the Amplitude'); %a=1%

y2 = exp(a*t);

subplot(2,2,4);stem(t,y2);

ylabel('Amplitude------>');

xlabel('(d)n ------>');

title('Exponential sequence');

disp('Unit impulse signal');y

disp('Unit step signal');y1

disp('Unit Ramp signal');x

disp('Exponential signal');x

Output :

Enter the value for end of the seqeuence 6

Enter the length of exponential seqeuence 4

Enter the Amplitude1

Unit impulse signal y = 0 0 0 1 0 0 0

Unit step signal y1 = 1 1 1 1 1 1 1 1 1

Unit Ramp signal x = 0 1 2 3 4 5 6

Exponential signal x = 0 1 2 3 4 5 6

S.SUMATHI , AP/ECE Page 8

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com
EC 6511 DIGITAL SIGNAL PROCESSING LAB MANUAL

Graph:

Unit Impulse Signal Unit step


1 1
Amplitude------>

Amplitude------>
0.5 0.5

0 0
-4 -2 0 2 4 -4 -2 0 2 4
(a)n ------> (b)n ------>
Ramp sequence Exponential sequence
6 60
Amplitude------>

Amplitude------>
4 40

2 20

0 0
0 2 4 6 0 1 2 3 4
(c)n ------> (d)n ------>

Result

Thus the different sequences were generated using MATLAB.

S.SUMATHI , AP/ECE Page 9

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com
EC 6511 DIGITAL SIGNAL PROCESSING LAB MANUAL

BASIC OPERATIONS ON SIGNALS


Aim :

To develop program for some basic operations like addition, subtraction, shifting and folding on signal.

Apparatus :

PC having MATLAB software.

Theory:

Basic Operations
 Signal Adding:

This is a sample-by-sample addition given by and the length of x1(n) and x2(n) must be the same

n)}={x1 n)}

 Signal Multiplication:

This is a sample-by-sample multiplication (or “dot” multiplication) given by

PROGRAM:

S.SUMATHI , AP/ECE Page 10

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com
EC 6511 DIGITAL SIGNAL PROCESSING LAB MANUAL

ADDITION:
x=input(„ENTER THE FIRST SEQUENCE:‟);
subplot(3,1,1);
stem(x);
title('X');
y=input(„ENTER THE SECOND SEQUENCE:‟);
subplot(3,1,2);
stem(y);
title('Y');
z=x+y;
disp(z)
subplot(3,1,3);
stem(z);
title('Z=X+Y');
OUTPUT:

ENTER THE FIRST SEQUENCE:[2 3 1 4 5]


ENTER THE SECOND SEQUENCE:[1 -1 0 1 -1]
3 2 1 5 4

EXPECTED GRAPH:

S.SUMATHI , AP/ECE Page 11

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com
EC 6511 DIGITAL SIGNAL PROCESSING LAB MANUAL

SUBTRACTION
clc;
clear all;
close all;
n1=-2:1;
x=input('ENTER THE FIRST SEQUENCE:');
n2=0:3;
y=input('ENTER THE SECOND SEQUENCE:');
subplot(3,1,1);
stem(n1,x);
xlabel ('time')
ylabel ('amplitude')
title('FIRST SEQUENCE') ;
axis([-4 4 -5 5]);
subplot(3,1,2);
stem(n2,y);
xlabel ('time')
ylabel ('amplitude')
title('SECOND SEQUENCE');
axis([-4 4 -5 5]);
n3 =min (min(n1) ,min( n2 ) ) : max ( max ( n1 ) , max ( n2 ) ); % finding the duration of output
signal
s1 =zeros(1,length (n3) );
s2 =s1;
s1 (find ( ( n3>=min( n1 ) ) & ( n3 <=max ( n1 ) )==1 ) )=x;
% signal x with the duration of output signal 'sub'
s2 (find ( ( n3>=min ( n2 ) ) & ( n3 <=max ( n2 ))==1) )=y;
% signal y with the duration of output signal 'sub'
sub=s1 - s2; % subtraction
disp('subtracted sequence')
disp(sub)
subplot(3,1,3)
stem(n3,sub)
xlabel ('time')
ylabel ('amplitude')

OUTPUT:

ENTER THE FIRST SEQUENCE:[2 4 6 8]


ENTER THE SECOND SEQUENCE:[1 3 5 7]
subtracted sequence
2 4 5 5 -5 -7

S.SUMATHI , AP/ECE Page 12

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com
EC 6511 DIGITAL SIGNAL PROCESSING LAB MANUAL

EXPECTED GRAPH:

MULTIPLICATION
PROGRAM:
clc;
clear all;
close all;
n1=-2:1;
x=input('ENTER THE FIRST SEQUENCE:');
n2=0:3;
y=input('ENTER THE SECOND SEQUENCE:');
subplot(3,1,1);
stem(n1,x);
xlabel ('time')
ylabel ('amplitude')
title('FIRST SEQUENCE') ;
axis([-4 4 -5 5]);
subplot(3,1,2);
stem(n2,y);
xlabel ('time')
ylabel ('amplitude')
title('SECOND SEQUENCE');
axis([-4 4 -5 5]);
n3 =min (min(n1) ,min( n2 ) ) : max ( max ( n1 ) , max ( n2 ) ); % finding the duration of output
signal (out)

S.SUMATHI , AP/ECE Page 13

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com
EC 6511 DIGITAL SIGNAL PROCESSING LAB MANUAL

s1 =zeros(1,length (n3) );
s2 =s1;
s1 (find ( ( n3>=min( n1 ) ) & ( n3 <=max ( n1 ) )==1 ) )=x;
% signal x with the duration of output signal 'mul'
s2 (find ( ( n3>=min ( n2 ) ) & ( n3 <=max ( n2 ))==1) )=y;
% signal y with the duration of output signal 'mul'
mul=s1 .* s2; % multiplication
disp('MULTIPLIED SEQUENCE')
disp(mul)
subplot(3,1,3)
stem(n3,mul)
xlabel ('time')
ylabel ('amplitude')

OUTPUT:

ENTER THE FIRST SEQUENCE:[2 4 6 8]


ENTER THE SECOND SEQUENCE:[2 3 4 5]
MULTIPLIED SEQUENCE
0 0 12 24 0 0

EXPECTED GRAPH:

S.SUMATHI , AP/ECE Page 14

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com
EC 6511 DIGITAL SIGNAL PROCESSING LAB MANUAL

SHIFTING

PROGRAM:
clc;
clear all;
close all;
n1=input('Enter the amount to be delayed');
n2=input('Enter the amount to be advanced');
n=-2:2;
x=input('ENTER THE SEQUENCE');
subplot(3,1,1);
stem(n,x);
title('Signal x(n)');
m=n+n1;
y=x;
subplot(3,1,2);
stem(m,y);
title('Delayed signal x(n-n1)');
t=n-n2;
z=x;
subplot(3,1,3);
stem(t,z);
title('Advanced signal x(n+n2)');

OUTPUT:
Enter the amount to be delayed 3
Enter the amount to be advanced4
ENTER THE SEQUENCE[1 2 3 4 5]

EXPECTED GRAPH:

S.SUMATHI , AP/ECE Page 15

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com
EC 6511 DIGITAL SIGNAL PROCESSING LAB MANUAL

FOLDING or REVERSING:

PROGRAM:

clc;
clear all;
close all;
n=-1:2;
x=input('ENTER THE SEQUENCE');
subplot(2,1,1)
stem(n,x);
axis([-3 3 -5 5]);
title('Signal x(n)');
c=fliplr(x);
y=fliplr(-n);
disp('FOLDED SEQUENCE')
disp(c)
subplot(2,1,2);
stem(y,c);
axis([-3 3 -5 5]);
title('Reversed Signal x(-n)') ;

OUTPUT:

ENTER THE SEQUENCE[1 -1 2 -3]

FOLDED SEQUENCE
-3 2 -1 1

S.SUMATHI , AP/ECE Page 16

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com
EC 6511 DIGITAL SIGNAL PROCESSING LAB MANUAL

EXPECTED GRAPH

Result

Thus the different operations on sequences were verified using MATLAB.

S.SUMATHI , AP/ECE Page 17

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com
EC 6511 DIGITAL SIGNAL PROCESSING LAB MANUAL

CORRELATION
Aim :

To develop program for correlation.

Apparatus :

PC having MATLAB software.

Program :

CROSS CORRELATION
% Program for computing cross-correlation of the sequences x5[1, 2, 3, 4] and h5[4, 3, 2, 1]
clc;
clear all;
close all;
x=input(„enter the 1st sequence‟);
h=input(„enter the 2nd sequence‟);
y=crosscorr(x,h);
figure;
subplot(3,1,1);
stem(x);
ylabel(„Amplitude --.‟);
xlabel(„(a) n --.‟);
title(„input sequence‟);
subplot(3,1,2);
stem(h);
ylabel(„Amplitude --.‟);
xlabel(„(b) n --.‟);
title(„impulse sequence‟);
subplot(3,1,3);
stem(fliplr(y));
ylabel(„Amplitude --.‟);
xlabel(„(c) n --.‟);
title(„Cross correlated sequence‟);
disp(„The resultant signal is‟);
fliplr(y)

OUTPUT:

enter the 1st sequence [1 2 3 4]


enter the 2nd sequence [4 3 2 1]
The resultant signal is
Y=1.0000 4.0000 10.0000 20.↑0000 25.0000 24.0000 16.0000

S.SUMATHI , AP/ECE Page 18

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com
EC 6511 DIGITAL SIGNAL PROCESSING LAB MANUAL

EXPECTED GRAPH:

AUTO CORRELATION

% Program for computing autocorrelation function


x=input(„enter the sequence‟);
y=crosscorr(x,x);
figure;subplot(2,1,1);
stem(x);ylabel(„Amplitude --.‟);
xlabel(„(a) n --.‟);
title(„original signal‟);
subplot(2,1,2);
stem(fliplr(y));ylabel(„Amplitude --.‟);
xlabel(„(a) n --.‟);
title („Auto correlated sequence‟);
disp(„The resultant signal is‟);
fliplr(y)

S.SUMATHI , AP/ECE Page 19

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com
EC 6511 DIGITAL SIGNAL PROCESSING LAB MANUAL

OUTPUT:

enter the sequence [1 2 3 4]


The resultant signal is
Y=4 11 20 ↑30 20 11 4

EXPECTED GRAPH:

RESULT:

Thus correlation of two signals ACHIEVED using MATLAB.

S.SUMATHI , AP/ECE Page 20

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com
EC 6511 DIGITAL SIGNAL PROCESSING LAB MANUAL

LINEAR CONVOLUTION
Aim

To perform a Linear Convolution Using MATLAB.

Requirements
Matlab 2007 later

Procedure
1. OPEN MATLAB

2. File  New  Script.

a. Type the program in untitled window


3. File  Save  type filename.m in matlab workspace path

4. Debug  Run. Wave will displayed at Figure dialog box.

Program
% Program for linear convolution of the sequence x5[1, 2] and h5[1, 2, 4]
clc;
clear all;
close all;
x=input('enter the 1st sequence');
h=input('enter the 2nd sequence');
y=conv(x,h);
figure;
subplot(3,1,1);
stem(x);
ylabel('Amplitude --.');
xlabel('(a) n --.');
title('first sequence');
subplot(3,1,2);
stem(h);ylabel('Amplitude --.');
xlabel('(b) n --.');
title('Second sequence');
subplot(3,1,3);
stem(y);
ylabel('Amplitude --.');
xlabel('(c) n --.');
title('Convoluted sequence');

S.SUMATHI , AP/ECE Page 21

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com
EC 6511 DIGITAL SIGNAL PROCESSING LAB MANUAL

disp('The resultant signal is');

Output:
enter the 1st sequence [1 2]
enter the 2nd sequence [1 2 4]
The resultant signal is
Y= 1 4 8 8

EXPECTED GRAPHS:

Result
Thus the Linear convolution was performed using MATLAB.

S.SUMATHI , AP/ECE Page 22

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com
EC 6511 DIGITAL SIGNAL PROCESSING LAB MANUAL

CIRCULAR CONVOLUTION
Aim

To perform a Circular Convolution Using MATLAB.

Requirements
Matlab 2007 later

Procedure
1. OPEN MATLAB

2. File  New  Script.


a. Type the program in untitled window
3. File  Save  type filename.m in matlab workspace path

4. Debug  Run. Wave will displayed at Figure dialog box.

Program

clc;
clear all;
a = input(„enter the sequence x(n) = ‟);
b = input(„enter the sequence h(n) = ‟);
n1=length(a);
n2=length(b);
N=max(n1,n2);
x = [a zeros(1,(N-n1))];
for i = 1:N
k = i;
for j = 1:n2
H(i,j)=x(k)* b(j);
k = k-1;
if (k == 0)
k = N;
end
end
end
y=zeros(1,N);
M=H‟;
for j = 1:N
for i = 1:n2
y(j)=M(i,j)+y(j);
end
end
disp(„The output sequence is y(n)= „);
disp(y);

S.SUMATHI , AP/ECE Page 23

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com
EC 6511 DIGITAL SIGNAL PROCESSING LAB MANUAL

stem(y);
title(„Circular Convolution‟);
xlabel(„n‟);
ylabel(‚y(n)„);

OUTPUT:
enter the sequence x(n) = [1 2 4]
enter the sequence h(n) = [1 2]
The output sequence is y(n)= 9 4 8

% Program for Computing Circular Convolution with zero padding


clc;
close all;
clear all;
x=input('enter the first sequence');
h=input('enter the 2nd sequence');
y=x'*h;
n1=length(x);
n2=length(h);
figure
subplot(3,1,1)
stem(x);
title('Input sequence');
xlabel('n1');
ylabel('x(n1)');
subplot(3,1,2)
stem(h);
title('Impulse sequence');
xlabel('n2');
ylabel('h(n2)');
n=n1+n2-1;
c=zeros(n);
for i=1:n1
for j=1:n2
c(i+j-1)=c(i+j-1)+y(i,j);
end
end
for i=1:n
d(i)=c(i,1);
end
disp('convoluted sequence');
disp(d);
n3=1:n;
subplot(3,1,3)
stem(n3-1,c);
title('Convoluted sequence');
xlabel('time');

S.SUMATHI , AP/ECE Page 24

Get useful study materials from www.rejinpaul.com


www.rejinpaul.com
EC 6511 DIGITAL SIGNAL PROCESSING LAB MANUAL

ylabel('Amplitude');

OUTPUT:
enter the first sequence [1 2 4]
enter the 2nd sequence [1 2]
The resultant signal is y=1 4 8 8

Result
Thus the Circular convolution was performed using MATLAB.

S.SUMATHI , AP/ECE Page 25

Get useful study materials from www.rejinpaul.com

You might also like