0% found this document useful (0 votes)
20 views26 pages

MATLAB programs (1)

Uploaded by

kelbesashibo429
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
20 views26 pages

MATLAB programs (1)

Uploaded by

kelbesashibo429
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 26

DIRE DAWA UNIVERSITY

INSTITUTE OF TECHNOLOGY
SCHOOL OF ELECTRICAL AND COMPUTER
ENGINEERING

DIGITAL SIGNAL PROCESSING


LABORATORY MANAUAL
(3RD YEAR)

Prepared By
RAMESH HULIKAL RANGASWAMY
LECTURER
MATLAB is a high-performance language for technical computing. It integrates computation,
visualization, and programming environment.
MATLAB has many advantages compared to conventional computer languages (e.g. C,
FORTRAN) for solving technical problems.
MATLAB is an interactive system whose basic data element is an array that does not require
dimensioning. It has powerful built-in routines that enable a very wide variety of computations.
It also has easy to use graphics commands that make the visualization of results immediately
available. Specific applications are collected in packages referred to as toolbox.
There are toolboxes for signal processing, symbolic computation, control theory, simulation,
optimization, and several other fields of applied science and engineering.

The Graphical Interface of the MATLAB workspace


The Command Window
The Command History
The Workspace
The Current Directory
The Help Browser
The Start button
Creating Variables in MATLAB
MATLAB variables are created with an assignment statement:
Variable name= a value(or an expression)
The expression can be:
manual entry
built-in functions
user-defined functions
Try Using the MATLAB as a calculator
Type in the command prompt:>>1+2*3
Repeat the above command but place a semicolon ; at the end of the command
Type ans in the command prompt (ans is the default variable for your answer)
Try putting the answer in a variable other than ans:>>x=1+2*3
Type this:>>4*x
Controlling the appearance of floating point number
MATLAB does numerical calculations in double precision, which is 15 digits.
The command format controls how the results of computations are displayed

Managing the Workspace


The contents of the workspace persist between the executions of separate commands
>> clear
Therefore, it is possible for the results of one problem to have an effect on the next
one. To avoid this possibility, it is a good idea to issue a clear command at the start
of each new inde-pendent calculation.
In order to display a list of the variables currently in the memory, type
>>whos
Multiple Statements per line, Type this:
>>a=7; b=cos(a);c=cosh(a);
b=0.753902254343305
c=5.483170351552120e+02
Getting help from the MATLAB documentation
MATLAB offers a very powerful help which you can get any help for any command
in the MATLAB
Type in the command prompt:
>>doc format
Matrices in MATLAB
Matrices are fundamental to MATLAB

So far, we have been dealing with scalars(1x1 matrix)

We will deal with matrices of size mxn

We can also deal with row vectors of size 1xn

We can also deal with column vectors of size mx1

Creating a row vector :

>>v= [1 4 7 10 13]

>>w=[1;4;7;10;13]

Creating a column vector:

Type: >> z=v+w

Type: >> z=w , v=w (automatic resize)

Type: >> z=v+w’ (transpose operator)

To access individual elements of the vector:


v(1) is the first element of vector v, v(2) its second element, and so forth.
To access blocks of elements of the vector, we use MATLAB's colon notation
(:)[don’t use for loops]
Type:>>v(1:3)
To access all elements from the third through the last elements of a vector:
Type: >>v(3,end)
Entering a matrix in MATLAB

To enter a matrix A, such as: A =

1 2 3

4 5 6

7 8 9

Type >> A=[1 2 3;4 5 6;7 8 9]


To view a particular element of the matrix:
Type: >>A(2,1)
Matrix Indexing: Type: >>A(1,3)=3
Colon Operator
The colon operator will prove very useful and understanding how it works is the key
to efficient and convenient usage of MATLAB
For example, suppose we want to enter a vector x consisting of points (0; 0.1; 0.2;
0.3; ….. ; 5). We can use the command:(This is a row vector of 51 elements)
>>x=0:0.5:5;
Linear Spacing
Generates a row vector y of n points linearly spaced between and including a and b:
Y=linspace(a,b,n);
Try this:>>theta=linspace(0,2*pi,101)
This divides the interval [0; 2Π ] into 100 equal subintervals, then creating a vector
of 101 elements
Matrix or Vector Dimension
To get the Dimensions of a matrix or a vector, we use the command size:
>>size(A)
To get a length of a vector:
>> length(A)
>>[m,n]=size(A)
List of Elementary Matrices

Operations on Vectors

Note: to get the square of each element we use: X.^2 NOT X^2
Where the dot operator(.) means element by element operation
Vector Multiplication
What is the difference?
Z = X*Y -> (the inner dimension of the 2 vectors must agree: one row and the other
column)
Z = X.*Y ->vector! (X and Y dimensions must agree)
Define 2 vectors and multiply them with and without the dot operator
Plotting 2 vectors
Note: minimizing the step size will lead to very smooth curve for the relationship
and vice versa.
Simply we use the plot order to plot the vectors against each other.
Plot (indep_var , depen_var)

Exercise: plot the average temperature of the months of the year to their order
according to the following table

Solution:
>> Month=[1:1:12];
>> Temperature=[ 17 19 22 24 27 30 34 35 31 26 23 20];
>> plot(Month, Temperature)
The result is a continuous curve(Interpolation), but do we need that really in this
application?
Use stem(Month,Temperature) for discrete sample plotting.
Practical 01:
Aim: Write a MATLAB program to generate the following elementary signals:
i) Unit step sequence ii) Sine signal iii) Unit step signal
iv) Cosine signal v) Unit ramp vi) Exponential signal
(i) MATLAB program to generate Unit Step sequence.
clc;
clear all;
close all;

n=input ('Enter the length of the step sequence N='); % Get the length of the require
sequence from the user
t=0:n-1; % defines the time axis
y=ones(1,n); % defines an 1 x n matrix which is filled with ones
stem(t,y); %displays the data as lines
ylabel ('Amplitude'); % name the Y axis
xlabel ('Time '); %Name the x axis
title ('Unit Step Signal'); % Giving the title for the plot
(ii) MATLAB program to generate Unit Step signal.
clc;
clear all;
close all;

n=input ('Enter the length of the step sequence N='); % Get the length of the require
sequence from the user
t=0: n-1; % defines the time axis
y=ones(1,n); % defines an 1 x n matrix which is filled with one
plot(t,y); % Plot the graph
ylabel ('Amplitude'); % name the Y axis
xlabel ('Time '); %Name the x axis
title ('Unit Step Signal'); % Giving the title for the plot
(iii) MATLAB program to generate Unit ramp Signal.
clc;
clear all;
close all;

n=input ('Enter the length of the sequence N= ');


t=0: n;
y=t;
plot(t,y);
ylabel ('Amplitude');
xlabel ('Time Index');
title ('Ramp Signal');
(iv) MATLAB program to generate Sine Wave Signal.
f= input('enter the frequency in hertz of the sine wave');
t=0:.0001:5;
y=sin(2*pi*f*t);
plot(t,y);
ylabel ('Amplitude');
xlabel ('Time ');
title('Sine wave');
(v) MATLAB program to generate Cosine Wave Signal.
f= input('enter the frequency in hertz of the sine wave');
t=0:.0001:5;
y=cos(2*pi*f*t);
plot(t,y);
ylabel ('Amplitude');
xlabel ('Time ');
title('cosine wave');
(vi) MATLAB program to generate Exponential signal Signal.
n=input('Enter the duration of the signal N = ');
a=input ('Enter the scaling factor a = ');
t=0:.1:n-1;
y=exp(a*t);
plot(t,y);
ylabel ('Amplitude');
xlabel ('Time ');
title('Exponential Signal');
Practical No:02
Aim: Write a MATLAB program to find the linear convolution of two
sequences.
a) Without using MATLAB convolution function.
b) Using MATLAB convolution function.
MATLAB Program.
% Matlab program <linear_convolution.m>
% This program performs convolution of two positive sequences.
clc; %Clear the window
close all; ` %Close all files
clear all; %Clear the screen
x=input('Enter the 1st sequence x:'); %Enter the 1st sequence
subplot(3,1,1);
stem(x);
title('1st sequence x');
xlabel('n-->');
ylabel('x(n)-->');
h=input('Enter the 2nd sequence h:'); %Enter the 2nd sequence
subplot(3,1,2);
stem(h);
title('2nd sequence h');
xlabel('n-->');
ylabel('h(n)-->');
x1=length(x); %Finds the length of x
h1=length(h); %Finds the length of h
n1=length(x)+length(h)-1; %Gives the max. Range of n
for n=1:n1 %MATLAB starts any array
sum(n)=0;
for k=1:x1 %Specifies summation limit
m=n+1-k; %m=n-k
if m<=h1 %To keep m below h1
if k<n+1
sum(n)=sum(n)+(x(k)*h(m)); %Convolution
end
else
end
end
y(n)=sum(n);
end
disp ('The convolution of x(n)& h(n)is y(n)= x(n)*h(n)');
y %Convolved output
subplot(3,1,3);
stem(y);
title('Convolved sequence y');
xlabel('n-->');
ylabel('y(n)-->');
COMMAND WINDOW
Enter the sequence x:[3 2 1 2]
Enter the sequence h:[1 2 1 2]
The convolution of x(n)& h(n)is y(n)= x(n)*h(n)
y = 3 8 8 12 9 4 4
MATLAB Program.
% Matlab program <linear_convolution.m>
% This program performs convolution of two positive sequences by MATLAB
function.
clc; %Clear the window
close all; ` %Close all files
clear all; %Clear the screen
x=input('Enter the 1st sequence x:'); %Enter the 1st sequence
subplot(3,1,1);
stem(x);
title('1st sequence x');
xlabel('n-->');
ylabel('x(n)-->');
h=input('Enter the 2nd sequence h:'); %Enter the 2nd sequence
subplot(3,1,2);
stem(h);
title('2nd sequence h');
xlabel('n-->');
ylabel('h(n)-->');
y=conv(x,h); %convolution function
disp ('The convolution of x(n)& h(n) is y(n) = x(n)*h(n) ')
y
subplot(3,1,3);
stem(y);
title('Convolved sequence y');
xlabel('n-->');
ylabel('y(n)-->');
COMMAND WINDOW
Enter the sequence x:[3 2 1 2]
Enter the sequence h:[1 2 1 2]
The convolution of x(n)& h(n) is y(n) = x(n)*h(n)
y = 3 8 8 12 9 4 4
Practical No:03
Aim : Write a MATLAB program for following:
a) To obtain partial fraction expansion of rational Z-transform.
b) To obtain Z-transform from partial fraction expansion.
c) To obtain power series expansion of Z-transform.
MATLAB Program.
% Matlab program <PFE_of_Ztrans.m>
% This program gives partial fraction expansion of Z-transform.

clc; %Clear the window


close all; ` %Close all files
clear all; %Clear the screen
num = input('Enter the numerator coefficients: '); % num=(1+(1/z))
den = input('Enter the denominator coefficients: ');
%den=(1-(5/6)(1/z)+(1/6)(1/z^2))
[r,p,k] = residuez(num,den);
disp('Residues');
disp(r')
disp('Poles');
disp(p')
disp('Constants');
disp(k)
COMMAND WINDOW
Enter the numerator coefficients: [1 1]
Enter the denominator coefficients: [1 -5/6 1/6]
Residues
9.0000 -8.0000
Poles
0.5000 0.3333
Constants []
MATLAB Program.
% Matlab program <Ztrans_from_PFE.m>
% This program gives Z-transformer from partial fraction expansion.
clc; %Clear the window
close all; %Close all files
clear all; %Clear the screen
r = input('Enter the residues: ');
p = input('Enter the poles: ');
k = input('Enter the constants: ');
[num, den] = residuez(r,p,k);
disp('Numerator polynomial coefficients');
disp(num)
disp('Denominator polynomial coefficients');
disp(den)
COMMAND WINDOW
Enter the residues: [9 -8]
Enter the poles: [.5 .3333]
Enter the constants: []
Numerator polynomial coefficients
1.0000 1.0003
Denominator polynomial coefficients
1.0000 -0.8333 0.1666
% Matlab program <PSE_of_ztrans.m>
% This program gives power series expansion of Z-transform.
clc; %Clear the window
close all; ` %Close all files
clear all; %Clear the screen
N = input('Enter the length of output vector: ');
num = input('Enter the numerator coefficients: '); % 1/(1-2(1/z))
den = input('Enter the denominator coefficients: ');
x = [1 zeros(1, N-1)]; % Compute the desired number of inverse transform coefficients
y = filter(num, den, x);
disp('Coefficients of the power series expansion');
disp(y)
COMMAND WINDOW
Enter the length of output vector: 4
Enter the numerator coefficients: [1]
Enter the denominator coefficients: [1 -2]
Coefficients of the power series expansion
1248
Practical No:04
MATLAB PROGRAMS ON DISCRETE FOURIER TRANSFORM
Aim : Write a MATLAB program to:
a) Obtain N-point DFT of a sequence.
b) Obtain N-point IDFT of a sequence.
c) Compute linear convolution using DFT.
MATLAB Program.
% Matlab program <DFT.m>
% This program performs DFT of the entered sequence.
clc; %Clear the window
close all; %Close all files
clear all; %Clear the screen
N=input('Enter the N-point to be calculated for DFT: ');
x=input('Enter the sequence: ');
for k=2:1:N+1
sum(k-1)=0;
for n=1:1:N
y(n)=x(n)*exp(-(j*2*pi*(n-1)*(k-2)/N));
sum(k-1)=sum(k-1)+y(n);
end
end
z=sum;
COMMAND WINDOW
Enter the N-point to be calculated for DFT: 4
Enter the sequence: [0 1 2 3]
The 4 point DFT of the given input sequence is
z=
6.0000 -2.0000 + 2.0000i -2.0000 - 0.0000i -2.0000 - 2.0000iMATLAB Program.
% Matlab program <IDFT.m>
% This program performs IDFT of the entered sequence.
clc; %Clear the window
close all; ` %Close all files
clear all; %Clear the screen
N=input('Enter the N-point to be calculated for DFT: ');
x=input('Enter the sequence: ');
for k=2:1:N+1
sum(k-1)=0;
for n=1:1:N
y(n)=x(n)*exp((j*2*pi*(n-1)*(k-2)/N));
sum(k-1)=sum(k-1)+y(n);
end
end
out=(1/N)*sum;
z=out;
fprintf ('The %d point DFT of the given input sequence is ', N)
COMMAND WINDOW
Enter the N-point to be calculated for DFT: 4
Enter the sequence: [2 1+i 0 1-i]
The 4 point DFT of the given input sequence is
z=
1.0000 -0.0000 + 0.0000i 0.0000 + 0.0000i 1.0000 - 0.0000i
MATLAB Program.
% Matlab program <linear_convolution_DFT.m>
% This program performs linear convolution of the entered sequence using DFT.
clc; %Clear the window
close all; ` %Close all files
clear all; %Clear the screen
x=input('Enter the sequence x: ');
h=input('Enter the sequence h: ');
xa=[x zeros(1,length(h)-1)];
ha=[h zeros(1,length(x)-1)];
N=length(xa);
for k1=2:1:(N+1)
sum1(k1-1)=0;
for n1=1:1:N
y1(n1)=xa(n1)*exp(-(j*2*pi*(n1-1)*(k1-2)/N));
sum1(k1-1)=sum1(k1-1)+y1(n1);
end
end
Xk=sum1;
for k2=2:1:(N+1)
sum2(k2-1)=0;
for n2=1:1:N
y2(n2)=ha(n2)*exp(-(j*2*pi*(n2-1)*(k2-2)/N));
sum2(k2-1)=sum2(k2-1)+y2(n2);
end
end
Hk=sum2;
Yk= Xk.*Hk;
M=length(Yk);
for k=2:1:(M+1)
sum(k-1)=0;
for n=1:1:M
y(n)=Yk(n)*exp((j*2*pi*(n-1)*(k-2)/M));
sum(k-1)=sum(k-1)+y(n);
end
end
Y=(1/M)*sum;
disp('The Linear Convolution by DFT of given sequences is:' )
Y
COMMAND WINDOW
Enter the sequence x: [1 2 2 1]
Enter the sequence h: [1 2 3]
The Linear Convolution by DFT of given sequences is:
Y = 1.0000 - 0.0000i 4.0000 - 0.0000i 9.0000 - 0.0000i 11.0000 - 0.0000i
8.0000 + 0.0000i 3.0000 - 0.0000i
Practical No:05
Verification of sampling theorem
clc; % clear screen
clear all; % clear work space
close all; % close all figure windows
tfinal = 0.05; % define final value of time vector
t= 0:0.00005: tfinal; % define time vector for analog signal
fd= input('enter the analog frequency'); % enter the analog frequency
xt = sin(2*pi*fd*t); % define analog signal
fs1 = 1.3*fd; % simulate condition for under sampling
n1= 0: 1/fs1: tfinal; % define time vector for discrete signal
xn = sin(2*pi*n1*fd); % to generate under sampled
signal subplot(3,1,1);
plot(t,xt,'b',n1,xn,'r*-'); % plot the analog and sampled signal
title('under sampling');
fs2= 2*fd; % simulate condition for nyquist rate
n2= 0:1/fs2:tfinal; % define time vector for discrete signal
xn = sin(2*pi*n2*fd); % to generate under sampled signal
subplot(3,1,2);
plot(t,xt,'b',n2,xn,'r*-'); % plot the analog and sampled signal
title('nyquist rate');
fs3 = 2.5*fd; % simulate condition for over sampling
n3= 0:1/fs3:tfinal; % define time vector for disrete
signal xn = sin(2*pi*n3*fd); %generate over sampling
signal subplot(3,1,3);
plot(t,xt,'b',n3,xn,'r*-'); % plot the analog and sampled signal
title('over sampling');
OUTPUT:
enter the analog wave frequency = 500

Practical No: 06
IMPULSE RESPONSE of a given system
a. PROGRAM:IMPLULSE RESPONSE USING DECONV FUNCTION
clc; % clear screen
close all % close all figure windows
clear all; % clear work space
y = input('Output sequence y(n) of the system = '); % enter the output sequence
x = input('Input sequence x(n) of the system = ');% enter the input sequence
[h,r] = deconv(y,x); % deconvolute output and input to get the impulse response
disp('Impulse response of the system is = ');
disp(h); % display result
N= length(h); % find the length of h
n=0:1:N-1; % define time axis
stem(n,h); % plot the impulse response
xlabel('n'); % label x axis
ylabel('h(n)'); % label y axis
title('Impulse Response of the system'); % graph title
% verification
yv = conv(x,h)+ r;% verification of result using convolution of input and impulse
to get output
disp('Verified output sequence y(n) is');
disp(yv); % display the value of output
OUTPUT:
Output sequence y(n) of the system = [1 -3/4]
Input sequence x (n) of the system = [1 -3/4 1/8]
Impulse response of the system = [1 0 –0.125 -0.0937 -0.0546]

PROGRAM: IMPLULSE RESPONSE USING IMPZ FUNCTION


clc; % clear screen
close all; % close all figure windows
clear all; % clear workspace
y = input('Output sequence y(n) of the system = ');
x = input('Input sequence x(n) of the system = ');
N=input('Enter the length of impulse response =');
h = impz(y,x,N);
disp('Impulse Response of system h(n)');
disp(h);
n = 0:1:N-1;
stem(n,h);
xlabel('n');
ylabel('h(n)');
title('Impulse Response');
% enter the coefficients of y terms
% enter the coefficient of x terms
% define the length of the response
% calculate the impulse response
% display the values of impulse response
%graphical plot
% define x axis
% plot the impulse response
% label x axis
% label y axis
% graph title
OUTPUT:
Output sequence y(n) of the system = [1 -1/3]
Input sequence x(n) of the system = [1 -3/4 -1/8]
Enter the length of impulse response =5
Impulse Response of system h(n) =1.0000 0 -0.125 -0.0937 -0.0546
PROGRAM: IMPLULSE RESPONSE USING FILTER FUNCTION
clc; % clear screen
close all; % close all the figure windows
clear all; % clear the work space
y = input('Enter the co-efficient of y = '); % define the coefficient of y terms
x = input('Enter the co-efficient of x = '); % define the coefficient of x terms
N = input('length of impulse sequence = '); % define the length of the response
xi = [1,zeros(1,N-1)]; % define the input signal
h = filter(x,y,xi); % calculate the impulse response
disp( 'Impulse Response of the system is = ');
disp(h); % display the response
% graphical display
n = 0:1:N-1; % define the time axis
stem(n,h); % plot the impulse response
xlabel('n'); % label x axis
ylabel('h(n)'); % label y axis
title('Impulse Response of the system'); % graph title
OUTPUT:
Output sequence y(n) of the system = [1 -1/3]
Input sequence x(n) of the system = [1 -3/4 -1/8]
Enter the length of impulse response = 5
Impulse Response of the system is = 1.0000 0 -0.125 -0.0937 -0.0546

You might also like