Communications Lab._Lec. 1_Introduction to MATLAB and Signals in MATLAB
Communications Lab._Lec. 1_Introduction to MATLAB and Signals in MATLAB
Objectives
To familiarize the students with MATLAB and the basic concept of signals in the MATLAB. Following are the
main features of the experiment.
ü Introduction to MATLAB
ü Plotting of Continuous and Discrete time signals on MATLAB.
M-Files
In order to write many commands that are executed all together, the program must be written in a
text editor. In this editor, one can type all the needed commands to form a program, save the
program, and execute it any time he or she wants. The text files are called M-files due to their suffice
*.m.
There are two categories of M-files: the Scripts and the Functions.
Scripts
Sripts are the M-files with MATLAB commands. Their name must have a .m suffix. Scripts are suitable for
solving the problems that require many commands. The advantage of the scripts is that they are
implemented very easily.
Lab Task
Write a script file and execute.
Solution
% Program to understand the use of script file
% Muhammad Usman Iqbal
% EEE223 Signals and Systems
clc
close all
clear all
% All commands that are
% needed are typed in the
% .m File. Everything written
% at the right of symbol %
% is a comment and is not
% taken into account
t=-5:0.1:5;
f=t.*cos(2*pi*t);
plot(t,f);
Functions
Function are also M-files, That is, are files with extension .m and must be saved in the current
Directory of MATLAB. The difference between functions and scripts is that a function accepts one or
more input arguments and returns one or more output arguments. To declare that an M-file is a
function the first line of the m file must contain the syntax definition. More specifically, the first line
of the M-file must be of the form function[y1,y2,y3,…yn]=name{x1,x2,x3… xm}. The variable
y1,y2,…yn are the outputs of the function while x1,x2,…xm are the input arguments. In case there is
only one output, the square brackets are not necessary. The “name” specifies the name of the
function. In order to execute a function, first the M-File is saved in Current Directory.
Lab Task
Write a function file and execute. Function should accepts as input two matrices and returns their sum and
product.
Solution
function [sm,pro]=oper(A,B)
sm=A+B;
pro=A*B;
clc
close all
clear all
Useful Commands
Here we will learn and practice useful (when workig with vectors and matries) commands. As already
discussed, the command sum returns the sum of the elements of a vector. The command cumsum returns a
vector whose elements are the comulative sum of the previous elements, the command prod is the product
of the vector elements, while the command diff returns a vector in which each element is given by its
subtraction with the previous element. The command max and min return the largest and smallest
elements of the vector,respectively, as well as their index. The command sort sorts the vector elements in
ascending(by default) or descending order. The command mean computes the mean value, while the
command median returns the median value. All these commands are suitable also for matrices by slightly
changing their syntax.
Commands Results/Comments
a = [4 2 7 0 6] a = 4 2 7 0 6
%Definition of vector .
s = sum(a) s = 19
%Sum the elements of .
c = 4 6 13 13 19
c = cumsum(a) %Cumilative sum.the result is obtained as[4,4+2,4+2+7,
4+2+7+0, 4+2+7+0+6]
p = 0
p = prod(a)
%Product of all elements.
d = -2 5 -7 6
d = diff(a) %Difference between two consecutive elements i.e.,
d(1)=! (2)-! (1),etc.
[m,i] = max(a) m = 7
i = 4
%The largest value is assigned to variable ",and its
index is aasigned to variable #.
[m,i] = min(a) m = 0
i = 3
%The smaller value is assigned to variable ",and its
index is aasigned to variable #.
max(a) ans = 7
%If no output variable is specified,only the largest value
mean(a) is returned.
ans = 3.8000
median(a)
%Mean value of the elements.
sort(a) ans = 4
%Median value of the vector.
ans = 0 2 4 6 7
sort(a,’descend’) %Sorting in ascending order.
ans = 7 6 4 2 0
%Sorting in descending order.
Commands Results/Comments
ones(2,3) ans = 1 1 1
1 1 1
%Matrix of size 2x3 with ones.
zeros(1,4) ans = 0 0 0 0
%Matrix of size 1x4(or vector of length)with zeros.
eye(4,2) ans = 1 0
0 1
0 0
0 0
%Magic of size 4x2 with ones in the main diagonal and
zeros elsewhere.
Ans = 1 0 0
eye(3) 0 1 0
0 0 1
%The identity matrix 1 of size 3x3.
A = 8 1 6
A = magic(3) 3 5 7
4 9 2
%Magic matrix
Symbolic Variables
In MATLAB, a variable type is the symbolic variable (or object). A symbolic variable is defined by the
command sym and syms. The use of symbolic variables allows the computation of limits, integrals,
derivatives etc.
Part II- Plotting Signals in MATLAB
MATLAB is a very reliable and power full tool for plotting. A graph is constructed as a set of points
in two or three dimensions. These points are typically connected with a solid line. Commonly a
graph of a function is required. However in MATLAB a plot is done using the vectors and matrices
not functions.
Lab Task
Plot the function $(") = " % ,&&&&&& ' 2 ! " ! 2
Solution
clc
close all
clear all
x=
-2 -1 0 1 2
ans =
y=
4 1 0 1 4
ans =
5
Write down modified MATLAB code to plot exactly like below
Solution
clc
clear all
close all
In the previous examples, the functions were plotted with predefined colors and line type (solid). It is
possible to create a graph using colors, symbols used to draw the points, and type of line that connects the
points of your choice. This is achieved by applying one more input argument to the command Plot The
new argument is a series of special character given in single quotes. The available special characters are
presented in the table below.
Formatting a Figure
The command grid on adds lines to the graph, while the command grid off removes the grid
lines. Simply typing grid is switch between the two modes. Text besides the x-axis and y-axis can
be added using the commands xlabel and ylabel, respectively. A graph title is inserted by the
Command title.
clc
clear all
close all
x = linspace(0,2*pi,150);
plot(x,cos(x),'r*',x,sin(x),'k')
grid
xlabel('Time Axis')
ylabel('Amplitude')
title('Graph of cos(x) & sin(x)')
legend('cos','sine')
Lab Task
Plot the functions that were plotted in last lab task in the following two ways.
Lab Task
Plot the discrete function (!) = !" #, $%&'&# * 2 + ! + 2.
Solution
clc
clear all
close all
n = -3:3
f= n.^2
stem(n,f)
xlabel('Time Axis')
ylabel('Amplitude')
title('Graph of f(n)')
n = -3:3
f= n.^2
stem(n,f)
xlabel('Time Axis')
ylabel('Amplitude')
title('Graph of f(n)')
Lab Task
Plot the following function
1,############################ * 2 + - + 2#
(-) = . 0#################################2 < - < 5
- sin(4/-) #################5 + - + 8
Solution
clc
clear all
close all
t1=-2:.1:2;
t2=2.1:.1:4.9;
t3=5:.1:8;
f1=ones(size(t1));
f2=zeros(size(t2));
f3=t3.*sin(4*pi*t3);
t=[t1 t2 t3];
f=[f1 f2 f3];
plot(t,f)
title('Multi-part function f(t)')
xlabel( 'time')
ylabel( 'Amplitude')