You are on page 1of 62

BECE101P DIGITAL SIGNAL

PROCESING LABORATORY
WINTER-2022

BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 1
FACULTY PROFILE
Dr.S.KALAIVANI, B.E.,M.E.,Ph.D.,
Professor,
Department of Communication Engineering,
School of Electronics Engineering (SENSE),
Vellore Institute of Technology, Vellore.

Mail ID: Kalaivani.s@vit.ac.in


Mobile: 7904147611,
WhatsApp: 9994958857

Cabin: Technology Tower Annex 102 B

BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 2
ASSESSMENT METHODOLOGY
TASK 1
TASK 2
TASK 3
TASK 4
TASK 5
MID LAB FAT (TASK 6)

BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 3
INTRODUCTION TO MATLAB
WINTER_ 2023

BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 4
MATLAB
The name MATLAB stands for;
MATrix LABoratory.
• MATLAB is a powerful, general-purpose
system or environment for doing
mathematics, scientific and engineering
calculations.

BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 5
Contd.,
• MATLAB is a "High-Performance
Numeric Computation and Visualization
Software" package
• MATLAB is an interactive system whose
basic data is a matrix that does not
require dimensioning
• MATLAB has a number of add-on
software modules, called toolbox , that
perform more specialized computations
BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 6
MATAB INTRODUCTION
In Windows systems
MATLAB is started
by double-clicking
the mouse on the
appropriate icon.

BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 7
Matlab Screen
Command Window
 type commands

Current Directory
 View folders and m-files

Workspace
 View program variables
 Double click on a variable
to see it in the Array Editor

Command History
 view past commands
 save a whole session
using diary

BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 8
MATLAB Screen
• Desktop Menus
• File Menu
– New
• Create new MATLAB
program file (m-file)
– Open existing m-file
– Import data
– Set Path
– Open recent m-files

BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 9
MATLAB Screen
Edit Menu
 Copy, cut, paste
 Find and replace
phrases
 Clear command
history, workspace
Desktop Menu
 Change
appearance of
desktop
 Select windows to
display BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 10
Use of M-File
Click to create
a new M-File

• Extension “.m”
• A text file containing script or function or program to run
BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 11
Use of M-File
Save file as Denem430.m

If you include “;” at the


end of each statement,
result will not be shown
immediately

BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 12
Starting MATLAB
MATLAB displays >> prompt when
ready for a command
 Will have no >> prompt when processing
commands
 Newer versions also say “Ready” or “Busy”
in lower left corner of GUI
 Can use arrow keys to work through
command history and modify commands

BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 13
Interactive Commands
Enter commands at >> prompt

Variable ‘x’ automatically allocated


 MATLAB does not require declaration of
variables
 Nice, but can BECE301P_WINTER_2022-23
get you in trouble so be careful
13-12-2022 Dr S KALAIVANI 14
Interactive Commands
MATLAB is case sensitive
 Variable ‘ans’ will take
value of result of command
if no equal sign specified
 Holds most recent result
only
 Semicolon at end of line
will suppress output, it is
not required like in C
 Useful in script files and
debugging

BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 15
Interactive Commands
Format of output
 Defaults to 4 decimal places
 Can change using format statement
 format long changes output to 15
decimal places

BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 16
Operators (arithmetic)
+ addition
- subtraction
* multiplication
/ division
^ power
‘ complex conjugate transpose

BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 17
Order of Operations
• Parentheses
• Exponentiation
• Multiplication and division have equal
precedence
• Addition and subtraction have equal
precedence
• Evaluation occurs from left to right

BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 18
Logical Operators

BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 19
Variables
MATLAB is case sensitive
 X is not equal to x
Variable names must start
with a letter
 You’ll get an error if this
doesn’t happen
 After that, can be any
combination of letters,
numbers and underscores
 No special characters though
BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 20
Variables
Variable types
 Numeric
 Logical
 Character and string
 Cell and Structure
 Function handle

BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 21
Variables
Don’t name your variables the same as
functions
 min, max, sqrt, cos, sin, tan, mean,
median, etc
 Funny things happen when you do this
MATLAB reserved words don’t work
either
 i, j, eps, nargin, end, pi, date, etc
 i, j are reserved as complex numbers
initially
13-12-2022
BECE301P_WINTER_2022-23
Dr S KALAIVANI 22
Array, Matrix
a vector x = [1 2 5 1]

x =
1 2 5 1

a matrix A = [1 2 3; 5 1 4; 3 2 -1]

A =
1 2 3
5 1 4
3 2 -1

transpose y = x’
y =
1
2
5
1
BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 23
Long Array, Matrix
=1:10
t
t =
1 2 3 4 5 6 7 8 9 10
T=1:2:10
T = 1 3 5 7 9
k =2:-0.5:-1

k =
2 1.5 1 0.5 0 -0.5 -1

B = [1:4; 5:8]
x =
1 2 3 4
5 6 7 8
BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 24
Generating Vectors from
functions
zeros(M,N) MxN matrix of zeros x = zeros(1,3)
x =
0 0 0

ones(M,N) MxN matrix of ones x = ones(1,3)


x =
1 1 1

rand(M,N) MxN matrix of


uniformly distributed random
x = rand(1,3)
numbers on (0,1) x =
0.9501 0.2311 0.6068

BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 25
Matrix Index
The matrix indices begin from 1 (not 0 (as in C))
The matrix indices must be positive integer
Given: A(8)=?
A(3,2)=?
A(2,:)=?

BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 26
Concatenation of Matrices
x = [1 2], y = [4 5], z=[ 0 0]

A = [ x y]

1 2 4 5

B = [x ; y]

1 2
4 5

BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 27
Matrix Operations

A*B A.*B

ans = ans = Find


3 10 6 1. X=A+B,
22 27 45
55 66 102 20 10 48 2. Y=A-B,
88 105 159 21 48 81 3. Z=A*B,
13-12-2022
BECE301P_WINTER_2022-23
Dr S KALAIVANI
4. T=A’ 28
Matrices Operations

Given A and B:

Addition Subtraction Product Transpose

BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 29
Operators (Element by
Element)

.* - element-by-element multiplication
./ - element-by-element division
.^ - element-by-element power

BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 30
BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 31
Plots in MATLAB
MATLAB has remarkable graphics
capacities:
2-D and 3-D plots,
Line plotting,
3-D surface plot,
Splash screen plot,
Plot of complex functions and ...

BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 32
EXAMPLE OF A 2-D PLOT Legend
Plot title
Light Intensity as a Function of Distance
1200
Theory
y axis Experiment

label 1000

Text
Tick-mark
800
INTENSITY (lux)

Comparison between theory and experiment.

600

400
Data symbol

200

0
8 10 12 14 16 18 20 22 24
DISTANCE (cm)
x axis Tick-mark label
label BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 33
BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 34
TWO-DIMENSIONAL plot() COMMAND

The basic 2-D plot command is:

plot(x,y)
where x is a vector (one dimensional array), and y is a vector.
Both vectors must have the same number of elements.

 The plot command creates a single curve with the x values on


the abscissa (horizontal axis) and the y values on the ordinate
(vertical axis).

 The curve is made from segments of lines that connect the


points that are defined by the x and y coordinates of the
elements in the two vectors.
BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 35
PLOT OF GIVEN DATA

Given data:

x 1 2 3 5 7 7.5 8 10

y 2 6.5 7 7 5.5 4 6 8

A plot can be created by the commands shown below. This can be done
in the Command Window, or by writing and then running a script file.

>> x=[1 2 3 5 7 7.5 8 10];


>> y=[2 6.5 7 7 5.5 4 6 8];
>> plot(x,y)

Once the plot command is executed, the Figure Window opens with the
following plot.
BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 36
PLOT OF GIVEN DATA

BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 37
LINE SPECIFIERS IN THE plot() COMMAND

Line specifiers can be added in the plot command to:


 Specify the style of the line.
 Specify the color of the line.
 Specify the type of the markers (if markers are desired).

plot(x,y,’line specifiers’)

BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 38
LINE SPECIFIERS IN THE plot() COMMAND

plot(x,y,‘line specifiers’)

Line Specifier Line Specifier Marker Specifier


Style Color Type

Solid - red r plus sign +


dotted : green g circle o
dashed -- blue b asterisk *
dash-dot -. Cyan c point .
magenta m square s
yellow y diamond d
black k
BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 39
LINE SPECIFIERS IN THE plot() COMMAND

EXAMPLES:
plot(x,y) A solid blue line connects the points with no markers.

plot(x,y,’r’) A solid red line connects the points with no markers.

plot(x,y,’--y’) A yellow dashed line connects the points.


plot(x,y,’*’) The points are marked with * (no line between the
points.)
plot(x,y,’g:d’) A green dotted line connects the points which are
marked with diamond markers.

BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 40
PLOT OF GIVEN DATA USING LINE
SPECIFIERS IN THE plot()
COMMAND
Year 1988 1989 1990 1991 1992 1993 1994

Sales (M) 127 130 136 145 158 178 211

>> year = [1988:1:1994];


>> sales = [127, 130, 136, 145, 158, 178, 211];
>> plot(year,sales,'--r*')

Line Specifiers:
dashed red line and
asterisk markers.
BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 41
PLOT OF GIVEN DATA USING LINE
SPECIFIERS IN THE plot()
COMMAND

Dashed red line and


asterisk markers.

BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 42
CREATING A PLOT OF A FUNCTION
Consider:

y  3.50.5 x cos(6 x) for  2  x  4


A script file for plotting the function is:

% A script file that creates a plot of


% the function: 3.5^(-0.5x)*cos(6x)
x = [-2:0.01:4]; Creating a vector with spacing of 0.01.
y = 3.5.^(-0.5*x).*cos(6*x);
plot(x,y) Calculating a value of y
for each x.
Once the plot command is executed, the Figure Window opens with the
following plot.

BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 43
A PLOT OF A FUNCTION
0.5 x
y  3.5 cos(6 x) for  2  x  4

BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 44
CREATING A PLOT OF A FUNCTION
If the vector x is created with large spacing, the graph is not accurate.
Below is the previous plot with spacing of 0.3.

x = [-2:0.3:4];
y = 3.5.^(-0.5*x).*cos(6*x);
plot(x,y)

BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 45
THE fplot COMMAND
The fplot command can be used to plot a function
with the form: y = f(x)

fplot(‘function’,limits)

 The function is typed in as a string.

 The limits is a vector with the domain of x, and optionally with limits
of the y axis:

[xmin,xmax] or [xmin,xmax,ymin,ymax]
 Line specifiers can be added.

BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 46
PLOT OF A FUNCTION WITH THE fplot() COMMAND

A plot of: y  x 2  4 sin( 2 x)  1 for  3  x  3

>> fplot('x^2 + 4 * sin(2*x) - 1', [-3 3])

BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 47
PLOTTING MULTIPLE GRAPHS IN THE SAME PLOT

Plotting two (or more) graphs in one plot:

1. Using the plot command.


2. Using the hold on, hold off commands.

BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 48
USING THE plot() COMMAND TO PLOT
MULTIPLE GRAPHS IN THE SAME PLOT

plot(x,y,u,v,t,h)

Plots three graphs in the same plot:

y versus x, v versus u, and h versus t.

 By default, MATLAB makes the curves in different colors.


 Additional curves can be added.
 The curves can have a specific style by adding specifiers after
each pair, for example:

plot(x,y,’-b’,u,v,’—r’,t,h,’g:’)
BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 49
USING THE plot() COMMAND TO PLOT
MULTIPLE GRAPHS IN THE SAME PLOT

Plot of the function, y  3x 3  26 x  10 and its first and second


derivatives, for  2  x  4 , all in the same plot.

x = [-2:0.01:4]; vector x with the domain of the function.


y = 3*x.^3-26*x+6; Vector y with the function value at each x.
2 x  4
yd = 9*x.^2-26;
Vector yd with values of the first derivative.
ydd = 18*x;
Vector ydd with values of the second derivative.
2 x  4
plot(x,y,'-b',x,yd,'--r',x,ydd,':k')

Create three graphs, y vs. x (solid blue line), yd


vs. x (dashed red line), and ydd vs. x (dotted
black line) in the same figure.
BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 50
USING THE plot() COMMAND TO PLOT
MULTIPLE GRAPHS IN THE SAME PLOT

120

100

80

60

40

20

-20

-40
-2 -1 0 1 2 3 4

BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 51
USING THE hold on, hold off, COMMANDS
TO PLOT MULTIPLE GRAPHS IN THE SAME PLOT

hold on Holds the current plot and all axis properties so that
subsequent plot commands add to the existing plot.

hold off Returns to the default mode whereby plot commands


erase the previous plots and reset all axis properties
before drawing new plots.

This method is useful when all the information (vectors) used for the
plotting is not available a the same time.

BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 52
USING THE hold on, hold off, COMMANDS
TO PLOT MULTIPLE GRAPHS IN THE SAME PLOT
Plot of the function, y  3x 3  26 x  10and its first and second
derivatives, for
 2  x  4 all in the same plot.
x = [-2:0.01:4];
y = 3*x.^3-26*x+6;
yd = 9*x.^2-26;
ydd = 18*x;
plot(x,y,'-b')
First graph is created.
hold on
plot(x,yd,'--r')
plot(x,ydd,':k') Two more graphs are created.
hold off

BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 53
USING THE plot() COMMAND TO PLOT
MULTIPLE GRAPHS IN THE SAME PLOT

120

100

80

60

40

20

-20

-40
-2 -1 0 1 2 3 4

BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 54
FORMATTING PLOTS

A plot can be formatted to have a required appearance.

With formatting you can:

 Add title to the plot.


 Add labels to axes.
 Change range of the axes.
 Add legend.
 Add text blocks.
 Add grid.
BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 55
FORMATTING COMMANDS

title(‘string’)
Adds the string as a title at the top of the plot.

xlabel(‘string’)
Adds the string as a label to the x-axis.

ylabel(‘string’)
Adds the string as a label to the y-axis.

axis([xmin xmax ymin ymax])


Sets the minimum and maximum limits of the x- and y-axes.

BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 56
FORMATTING A PLOT IN THE FIGURE WINDOW

Once a figure window is open, the figure can be formatted interactively.


Use the insert menu to

Use Figure, Axes, Click here to start the plot


and Current edit mode.
Object-Properties
in the Edit menu

BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 57
Experiment 1
Generate the following Signals;
i. 𝑦(𝑡) = 2 ∗ 𝑠𝑖𝑛(2 ∗ 𝑝𝑖 ∗ 500 ∗ 𝑡) + 3 ∗ 𝑠𝑖𝑛(2 ∗ 𝑝𝑖 ∗ 1000 ∗ 𝑡).
ii. 𝑥1[𝑛] = 𝑢[𝑛] − 𝑢[𝑛 − 4]
iii. 𝑥2[𝑛] = 𝛿[𝑛] + 2𝛿[𝑛 − 1] + 4 𝛿[𝑛 − 2] − 6 𝛿[𝑛 − 3]
iv. 𝑥3[𝑛] = 𝑟[𝑛 − 2]
v. 𝑥4[𝑛] = 𝑠𝑞𝑢𝑎𝑟𝑒 𝑤𝑎𝑣𝑒 𝑤𝑖𝑡ℎ 𝑓𝑟𝑒𝑞𝑢𝑒𝑛𝑐𝑦 4𝐻𝑧, 𝑑𝑢𝑡𝑦 𝑐𝑦𝑐𝑙𝑒 50%
vi. 𝑥5[𝑛] =
𝑆𝑎𝑤𝑡𝑜𝑜𝑡ℎ 𝑤𝑎𝑣𝑒 𝑤𝑖𝑡ℎ 𝑓𝑟𝑒𝑞𝑢𝑒𝑛𝑐𝑦 3𝐻𝑧 𝑎𝑛𝑑 𝑝𝑒𝑎𝑘 𝑖𝑠 ℎ𝑎𝑙𝑓𝑤𝑎𝑦 𝑡ℎ𝑟𝑜𝑢𝑔ℎ 𝑡ℎ𝑒 𝑝𝑒𝑟𝑖𝑜𝑑

vii. Write a program to plot the given


signal

BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 58
Code
clc
clear all
close all
% GENERATION OF SINE WAVE%
t=0:0.05e-3:5e-3;
A=2*sin(2*pi*500*t)+3*sin(2*pi*1000*t);
figure, subplot(2,2,1),plot(t,A);grid on
title('SINE WAVE');
xlabel('----->t');
ylabel('Amplitude');
BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 59
%Generation of u[n]-u[n-4]%
n=-10:1:10;
Y=[zeros(1,10),ones(1,4),zeros(1,7)];
subplot(2,2,2);
stem(n,Y),grid on
title('u[n]-u[n-4]');
xlabel('----->n');
ylabel('Amplitude');

BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 60
% Generation of Unit impulse shifted
and scaled sequence%
y1=[zeros(1,10),ones(1,1),2*ones(1,1)
,4*ones(1,1),-6*ones(1,1),zeros(1,7)]
subplot(2,2,3);
stem(n,y1);grid on
title('Impulse');
xlabel('----->n');
ylabel('Amplitude');

BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 61
%Generation of Shifted Ramp Sequence%
t=[-10:10];
y2=zeros(size(n));
t2=t-2;
y2(t>=3)=t2(t>=3)
subplot(2,2,4);
stem(t,y2);grid on
title('r[n-2]');
xlabel('----->n');
ylabel('Amplitude');
BECE301P_WINTER_2022-23
13-12-2022 Dr S KALAIVANI 62

You might also like