PCS Lab-1
PCS Lab-1
PCS Lab-1
Lab-1
MATLAB Basics for Communication System Design
Objective:
• To understand the use of MATLAB for solving communication engineering problems.
• Learn the basics of MATLAB as used in Analogue Communication.
• To develop understanding of MATLAB environment, commands, and syntax.
MATLAB:
MATLAB is a powerful tool that is utilized by the engineers and others professionals in
development and testing of various projects. It is versatile software, with the help of which you
can solve and develop any sort of engineering problem. The name MATLAB stands for MATRIX
LABORAORY. All the work done in MATLAB is basically in the form of matrices. Scalars are
referred as 1-to-1 matrix and vectors are matrices having more than 1 row and column. MATLAB
is programmable and have the same logical, relational, conditional and loop structures as in other
programming languages, such as C, Java etc. It’s very easy to use MATLAB, all we need is to
practice it and become a friend of it.
Summary:
• Scalars
• Vectors
• Matrices
• Plotting
• m-files
• functions
Getting Started:
a) How to start MATLAB
Go to the start button, then programs, MATLAB and then start MATLAB. It is preferred
that you have MATLAB2015a. You can then start MATLAB by double clicking on its icon
on Desktop, if there is any.
b) The Prompt:
>>
The operator shows above is the prompt in MATLAB. MATLAB is interactive language
like C, Java etc. We can write the commands over here.
c) In MATLAB
we can see our previous commands and instructions by pressing the up key. Press the key
once to see the previous entry, twice to see the entry before that and so on. We can also edit the
text by using forward and back-word keys.
Help in MATLAB
In order to use the built-in help of the MATLAB we use the help keyword. Write it on the
prompt and see the output.
>> help sin
Also try
>> lookfor sin
>> Doc sin
Scalars
A scalar is a single number. A scalar is stored in the MATLAB as a 1 x 1 matrix. Try these
on the prompt.
>> A = 2;
>> B = 3;
>> C = A^B
>> C = A*B
Try these commands as well
>> C = A+B
>> C = A-B
>> C = A/B
>> C = A\B
Note the difference between last two instructions.
Try to implement these two relations and show the result in the provided space
a) 25 ( 31/3 ) + 2 (2+92) = _____________________________
b) 5x3 + 3x2 + 5x + 14 for x = 3 is _______________________
Vectors
Vectors are also called arrays in MATLAB. Vectors are declared in the following format.
>> X = [1 2 3 4];
>> Y = [2 5 8 9];
Try these two commands in MATLAB and see the result.
>> length (X) = __________
>> size (X) = ____________
What is the difference between these two?
______________________________________________________________________________
______________________________________________________________________________
Try these instructions and see the results.
>> X.*Y = __________________
>> X.^Y = __________________
>> X+Y = ___________________
>> X-Y = ___________________
>> X./Y = ___________________
>> X’ = _____________________
Also try some new instructions for this like and notice the outputs in each case.
>> ones (1,4)
>> ones (2,4)
>> ones (4,1)
>> zeros (1,4)
>> zeros (2,4)
There is an important operator, the colon operator (:), it is very important operator and frequently
used during these labs. Try this one.
>> X = [0:0.1:1]
Notice the result. And now type this
>> length (X)
>> size (X)
What did the first and second number represent in the output of last instruction?
______________________________________________________________________________
______________________________________________________________________________
Now try this one.
>>A= [ones(1,3), [2:2:10], zeros(1,3)]
What is the length and size of this?
>> Length = ____________________
>> Size = ______________________
A= magic(4)
H = A(2 , 3)
A(2,2) = 20
I = A(: , 2)
J = sum(A)
K = max(A)
L= sum(A,2)
M= trace(A)
A4 = trace(flip(A))
Activity: Extract the 3rd row of matrix A.
Try ‘help ones’ and ‘help zeros’ as well and note down its important features.
MATRICES
Try this and see the output.
>> A = [1 2 3;4 5 6;7 8 9]
>> B = [1,2,3;4,5,6;7,8,9]
Is there any difference between the two? Try to implement 2-to-3 matrix and 3-to-2 matrix.
Also take help on mod, rem, det, inv and eye and try to implement them. Try to use length and
size commands with these matrices as well and see the results.
Try to solve this.
1. 6x + 12y + 4z = 70
7x – 2y + 3z= 5
2x + 8y -9z = 64
PLOTTING
Plotting is very important as we have to deal with various type of waves, and we have to view
them as well.
Try these and have a look on the results.
>> x = [0:0.1:10];
>> y = sin (x);
>> z = cos (x);
>> subplot (3,1,1);
>> plot (x,y);
>> grid on;
>> subplot (3,1,2);
>> plot (x,z);
>> grid on;
>>hold on;
>> subplot (3,1,3);
>> stem (x,z);
>> grid on;
>> hold on; >>
subplot (3,1,3);
>> stem (x,y, ,'r');
Take help on the functions and commands that you don’t know. See the difference between the
stem and plot.
See help on plot, figure, grid, hold, subplot, stem and other features of it.
Figure 1.1
M-FILES
MATLAB can execute a sequence of statements stored in disk files. Such files are called
Mfiles because they must have the file type ‘.m’. Lot of our work will be done with creation of m-
files.
There are two types of m-files: Script and function files.
Script Files
We can use script files in order to write long programs such as one on the previous page.
A script file may contain any command that can be entered on the prompt. Script files can have
any name but they should be saved with “.m” extension. In order to excurse an m-file from the
prompt, just type its name on the prompt. You can make an m-file by typing edit on the prompt or
by clicking on the file then new and m-file. See an example of m-file.
Write it and see the results.
% This is comment
% A comment begins with a percent symbol
% The text written in the comments is ignored by the MATLAB
% comments in your m-files.
clear;
clc;
x = [0:0.1:10];
y = sin (x);
subplot (2,2,1);
plot (x,y, ,'r');
grid on;
z = cos (x);
subplot (2,2,2);
plot (x,z);
grid on;
w = 90;
yy = 2*pi*sin (x+w)
subplot (2,2,3);
plot (x,yy);
grid on;
zz = sin (x+2*w);
subplot (2,2,4);
stem (x,zz, ,'g');
hold on;
stem (x,y, ,'r');
grid on;
Figure 1.2
Function Files
MATLAB have many built-in functions including trigonometry, logarithm, calculus and
hyperbolic functions etc. In addition we can define our own functions and we can use builtin
functions in our functions files as well. The function files should be started with the function
definition and should be saved with the name of function. The general format of the function file
is
Function [output_variables] = function name (input_variables)
See the following example and implement it.
% this is a function file
% this function computes the factorial of a number function [y] = my_func (x)
y = factorial (x);
Function with Multiple Outputs
Define a function in a file named stat.m that returns the mean and standard deviation of an input
vector.
function [m,s] = stat(x)
n = length(x);
m = sum(x)/n;
s = sqrt(sum((x-m).^2/n));
end
Call the function from the command line.
>> values = [12.7, 45.4, 98.9, 26.6, 53.1];
>> [ave, stdev] = stat(values)
ave =
47.3400
stdev =
29.4124
Try to develop a function that will compute the maximum and minimum of two.
Sinusoidal Sequence:
% Generation of sinusoidal signals
% 2sin( 2πt-π/2)
t= [-5:0.01:5];
x= 2*sin((2*pi*t)-(pi/2));
plot(t, x)
grid on;
axis([-6 6 -3 3])
ylabel ('x(t)')
xlabel ('Time(sec)')
title ('Figure 2.1')