7/28/2019 in_dsp
1/33
BEIT, 6th Semester
Digital Signal Processing : Introductionto MATLAB
Ms. T. Samanta
Lecturer
Department of Information Technology
7/28/2019 in_dsp
2/33
BEIT, 6th Semester
Books:
Getting Started with MATLAB
- Rudra Pratap
Digital Signal Processing
- Sanjit K. Mitra.
Digital Signal Processing-A PractitionersApproach
- Kaluri V. Rangarao- Ranjan K. Mallik
7/28/2019 in_dsp
3/33
BEIT, 6th Semester
Introduction
MATLAB -> MATrix LABoratory
Built-in Functions
-Computations
-Graphics
-External Interface
-Optional Toolboxfor Signal Processing,
System analysis, Image Processing
7/28/2019 in_dsp
4/33
BEIT, 6th Semester
How many windows?
7/28/2019 in_dsp
5/33
BEIT, 6th Semester
MATLAB Windows
Command window: main windowcharacterized by >> command prompt.
Edit window: where programs are writtenand saved as M-files.
Graphics window: shows figures,alternately known as Figure window.
7/28/2019 in_dsp
6/33
BEIT, 6th Semester
Command Window
Figure Window
Edit Window
7/28/2019 in_dsp
7/33
BEIT, 6th Semester
Matlab Basics
7/28/2019 in_dsp
8/33
BEIT, 6th Semester
MATLAB Basics: Variables
Variables:-Variables are assigned numerical valuesby typing the expression directly
>> a = 1+2 press enter>> a =
3
>> a = 1+2; suppresses the output
7/28/2019 in_dsp
9/33
BEIT, 6th Semester
Variables
several predefined variablesi sqrt(-1)
j sqrt(-1)pi 3.1416...
>> y= 2*(1+4*j)
>> y= 2.0000 + 8.0000i
7/28/2019 in_dsp
10/33
BEIT, 6th Semester
Variables
Global Variables
Local Variables
7/28/2019 in_dsp
11/33
BEIT, 6th Semester
MATLAB Basics:
Arithmetic operators:
+ addition,
- subtraction,* multiplication,
/division,
^ power operator,' transpose
7/28/2019 in_dsp
12/33
BEIT, 6th Semester
Matrices
Matrices : Basic building block
Elements are entered row-wise
>> v = [1 3 5 7] creates a 1x4 vector
>> M = [1 2 4; 3 6 8] creates a 2x3matrix
>> M(i,j) => accesses the element of ithrow and jth column
7/28/2019 in_dsp
13/33
BEIT, 6th Semester
Basics Matrices
Special Matrices
null matrix: M = [];
nxm matrix of zeros: M =zeros(n,m);
nxm matrix of ones: M = ones(n,m);
nxn identity matrix: M = eye(n);
--Try these Matrices
[eye(2);zeros(2)],[eye(2);zeros(3)], [eye(2),ones(2,3)]
7/28/2019 in_dsp
14/33
BEIT, 6th Semester
Matrix Operations
Matrix operations:
A+B is valid if A and B are of same size
A*B is valid if As number of columns equals
Bs number of rows.A/B is valid and equals A.B-l for same size
square matrices A & B.
Element by element operations:.* , ./ , .^ etc
--a = [1 2 3], b = [2 2 4],
=>do a.*b and a*b
7/28/2019 in_dsp
15/33
BEIT, 6th Semester
Vectors
7/28/2019 in_dsp
16/33
BEIT, 6th Semester
Few methods to create vector
LINSPACE(x1, x2): generates a row vector of 100linearly equally spaced points between x1 and x2.
LINSPACE(x1, x2, N):generates N pointsbetween x1 and x2.
LOGSPACE(d1, d2):generates a row vector of 50
logarithmically equally spaced points betweendecades 10^d1 and 10^d2.
a = 0:2:10 generates a row vector of 6 equally
spaced points between 0 and 10.
7/28/2019 in_dsp
17/33
BEIT, 6th Semester
Waveform representation
7/28/2019 in_dsp
18/33
BEIT, 6th Semester
2D Plottingplot: creates linear continuous plots of
vectors and matrices;
plot(t,y): plots the vector t on the x-axisversus vector y on the y-axis.
To label your axes and give the plot atitle, type
xlabel('time (sec)')
ylabel('step response')title('My Plot')
Change scaling of the axes by using theaxis command after the plotting
command:axis([xmin xmax ymin ymax]);
7/28/2019 in_dsp
19/33
BEIT, 6th Semester
2D Plotting
stem(k,y): for discrete-time signals thiscommand is used.
To plot more than one graph on thescreen,
subplot(m,n,p): breaks the Figure windowinto an m-by-n matrix of small axes,
selects the p-th sub-window for thecurrent plot
grid: shows the underlying grid of axes
7/28/2019 in_dsp
20/33
BEIT, 6th Semester
Example: Draw sine wave
t=-2*pi:0.1:2*pi;
y=1.5*sin(t);
plot(t,y);
xlabel('------> time') ylabel('------> sin(t)')
7/28/2019 in_dsp
21/33
BEIT, 6th Semester
Matlab Plot
7/28/2019 in_dsp
22/33
BEIT, 6th Semester
Example: Discrete time signal
t=-2*pi:0.5:2*pi;
y=1.5*sin(t);
stem(t,y);
xlabel('------> time') ylabel('------> sin(t)')
7/28/2019 in_dsp
23/33
BEIT, 6th Semester
Matlab Plot
7/28/2019 in_dsp
24/33
BEIT, 6th Semester
Example: Draw unit step and delayed
unit step functions
n=input('enter value of n')
t=0:1:n-1;
y1=ones(1,n); %unit step
y2=[zeros(1,4) ones(1,n-4)]; %delayed unit step
subplot(2,1,1);
stem(t,y1,'filled');ylabel('amplitude');
xlabel('n----->');ylabel('amplitude');
subplot(2,1,2); stem(t,y2,'filled');
xlabel('n----->');ylabel('amplitude');
7/28/2019 in_dsp
25/33
BEIT, 6th Semester
Matlab Plot
7/28/2019 in_dsp
26/33
BEIT, 6th Semester
Functions
7/28/2019 in_dsp
27/33
BEIT, 6th Semester
Scripts and Functions
Script files are M-files with some validMATLAB commands.
Generally work on global variables presentin the workspace.
Results obtained after executing scriptfiles are left in the workspace.
7/28/2019 in_dsp
28/33
BEIT, 6th Semester
Scripts and Functions
Function files are also M-files but withlocal variables .
Syntax of defining a function
function [output variable] =function_name (input variable)
File name should be exactly same asfunction name.
7/28/2019 in_dsp
29/33
BEIT, 6th Semester
Example : user defined function
Assignment is the method of giving a value to avariable. You have already seen this in theinteractive mode. We write x=a to give the valueof a to the value of x. Here is a short programillustrating the use of assignment.
function r=mod(a,d)%If a and d are integers, then r is the integer remainder of a after
division by d. If a and b are integer matrices, then r is the matrixof remainders after division by corresponding entries.
r=a-d.*floor(a./d);%You should make a file named mod.m and enter this program
exactly as it is written.%Now assign some integer values for a and d writing another .m file
and Run
a =[10 10 10]; d = [3 3 3]; mod(a,d);
Another .m file to accessabove function
7/28/2019 in_dsp
30/33
BEIT, 6th Semester
Loops, flows:
for m=1:10:100num = 1/(m + 1)
end%i is incremented by 10 from 1 to 100
I = 6; j = 21if I > 5
k = I;elseif (i>1) & (j == 20)
k = 5*I + jelse
k = 1;end%if statement
7/28/2019 in_dsp
31/33
BEIT, 6th Semester
Recapitulate
7/28/2019 in_dsp
32/33
BEIT, 6th Semester
Few in-built Matlab functions
sin() sqrt()
cos() square()
log() rand()
asin() random()
acos() real(x)
exp() imag(x)
sinh() conv()
Type help, followed by the function name on theMatlab command window, press enter. This willdisplay the function definition.
Study the definition of each function.
7/28/2019 in_dsp
33/33
BEIT, 6th Semester
Practice1>Draw a straight line satisfying the equation: y = 3*x + 10
2> Create matrices withzeros, eye and ones
3>Draw a circle with radius unity. (use cos for x axis, sin for yaxis)
4>Draw a cosine wave with frequency 10kHz. Use both plotandstemfunctions to see the difference.
5>Multiply two matrices ofsizes 3x3.
6>Plot the parametric curve x(t) = t, y(t) = exp(-t/2) for 0