You are on page 1of 5

Kulliyyah of Engineering, IIUM

Department of Electrical and Electronics Engineering

ECE3123 Digital Signal Processing

Semester I, 2016/2017 Due date: March 7, 2017

Lab. Assignment #1. Acquaintance with MATLAB

Learning Familiarize with the Matlab environment and running some basic Matlab
Objectives: commnds in Matlab

Equipment Matlab

Variables and Arrays

The functional unit of data in any Matlab program is array. An Array is a collection of data
values organized into rows and columns and known by a single name.
Arrays can be classified as either vectors or matrices. The term VECTOR is usually used to
describe an array with only one dimension. While the term MATRIX is usually used to describe
an array with two or more dimensions.
Size of array is specified by the number of rows and the number of columns in the array, with the
number of rows mentioned first. The total number of elements in the array will be the product of
the number of rows and the number of columns.
Try using these values:
[1.0 2.0 3.0]
[1.0; 2.0; 3.0]
[1, 2, 3; 4, 5, 6]
[1, 2, 3, 4, 5, 6]
Not all the elements of an array need to be defined when it is created. If a specific array
element is defined and onwe or more of the elements before it are not, then the earlier
elements are automatically created and initialized to zero. For example, if c is not previously
defined, the statement
Will produce matrix 0 0 0

Similarly the array d=[1 2]. Then the statement

will produce d=[1 2 0 4].
Initializing with shortcut expressions
Matlab provides a special shortcut notation for these circumstances using the COLON
operator. The colon operator specifies a whole series of values by specifying the first vales in
the series, the stepping increment and then the last value in the series. The general form of a
colon operator is
First: increment: last

For example
will generate x= 1 3 5 7 9
Try using angles=(0.01:0.01:1.00)*pi;
Transpose Operator():
This operator swaps the rows and columns of any array that it is applied to.
For example try using:
And F=[1:4]
Initializing with Built-in functions:
Try using these:
C=[1 2; 3 4]
Length(C) //Generate the longest dimension of the array.

Zeros can be used to create an all zero array of any desired size. If function has a single
square array; it will generate a square array using the single argument as both the number of
rows and columns. size function returns two values containing the number of rows and
columns in an array.
Initializing variables with keyboard input:
Input function displays a prompt string in the command window and then waits for the user
to type in a response. For example , consider the following statement:
My_val=input(enter an input value);
Multidimensioanl Array
Matlab allows us to create arrays as many dimensions as necessary for any given problem.
These arrays have one subscript for each dimension and an individual element in the array
will be the product of the maximum value of each subscript. For example the following two
statements create a 2X2X3 array C:
C(:,:,1)=[1 2 3; 4 5 6];
C(:,:,2)=[7 8 9; 10 11 12];
Whos C
End Function:
It is very useful for creating array subscripts. When used in an array subscript; end returns
the highest value taken on by that subscript. For example. Suppose that array arr3 is defined
as follows:
Arr3=[ 1 2 3 4 5 6 7 8];
Then Arr1(5:end) would be the array [5 6 7 8] and array(end) would generate 8.
The value returned by end is always the highest value of a given subscript. Is end appears in
different subscripts, it can return different values within the same expression. For example,
suppose that the 3X4 array Arr4 is defined as follows:
Arr4=[1 2 3 4; 5 6 7 8; 9 10 11 12];
Then the expression
Arr4(2:end, 2:end)
would return the array.(Try this one).
Question # 01:
This M-file calculates and plot the function sin(x) for 0<=x<=6
Question # 02:
The following Matlab statement plot the function y(x)=2e-0.2x for the range 0<=x<=10

Question # 03:
suppose that u=1 and v=3. Evaluate the following expressions using Matlab.
(a) 4u/3v
(b) 2v-2/(u+v)2
(c) v3/v3-u3
(d) 4/3 pi.v2
Question # 04:
(a) Generate a 6X6 Matrix.
(b) Generate a 6X1 Matrix
(c) Generate a 3X4 Matrix

Question # 05
Answer the following questions for the array shown below.
1.1 0.0 2.1 -3.5 6.0
0.0 1.1 -6.6 2.8 3.4
1.1 0.1 0.3 -0.4 1.3
-1.4 5.1 0.0 1.1 0.0
(a) what is the size of the array?
(b) What is the value of the array(4,1)?
(c) What is the size and value of array( : , 1 : 2 )?
(d) What is the size and Value of the (array[1 3],end)?

Question # 06
Are the following Matlab variable names legal or illegal? Why?
(a) dog1
(b) 1dog
(c) Do_you_know_the_way__to_kuala lampur
(d) _help

Signal Representation and Operations

Example : Here are three ways to create an array xp which has 11 equally spaced samples from 0
to : (a) use xp=[ 0: pi/l0: pi ] directly; (b) first create an array equally spaced between 0 and 1
then multiply it by ; (c) use linspace function:

xp = linspace(0,,11)

If not stated otherwise, all vectors in MATLAB are indexed starting from 1, e.g., x( 1) is the
first element of vector x. Thus, in order to represent a signal with negative or zero time index,
an additional index vector should be defined. The following code will define a discrete-time
signal x[n]=2n, for n = 3,..,3:

plot(n,x)<ret> % This command is to show the vector x[n] graphically
In MATLAB both stem and plot can be used to plot a signal. The difference between
these two functions can be seen by typing the following example:

n1= [5:5], n2=[-5:0. 1:5];

x1 =n1>=-2&n1 <2;
subplot(2, 1,1), stem (n1,x1)
subplot(2,1,2), plot (n2,x2)

Question # 07
Evaluate the following expressions (remember to include numerical answers in the result section
and executable MATLAB code as attachment of your report):

(a) y =sin(x), where x = (2 0.5)/2

(b) convert y in part (a) from radians to degrees
(c) c4 = 6+ j sin(0.5) + c (using c =1 )
(d) c5 = real part of c4

Question # 08
Exchange the stem and plot functions in the example, and watch the results.
Explain the advantages and disadvantages of each function.

The following example describes the process of creating a sampled sinusoidal signal:

Example 5: Evaluate sin(pi*x) from 0 to 1 using a sampling period of 0.1: create an array for the
argument of sine,

x = (0:0.1:1); % build an array starting at 0, ending at 1, and incremented by .1

length(x) % returns the length of the array x
xp = x*pi,. % multiply each element of the array bypi
xp(3) % refers to the third element in the xp array only
xp(2:6) % references the second through sixth element of the xp array

evaluate the function at the sample points,

y= sin(xp);<ret>
length(y)< ret>

plot the function,

plot(x,y) % creates a simple 2-dimensional plot of y

xlabel(' theta' );
ylabel(function value);
title(Sampled Sine Function for Example VIII);

and use close command to close current graph window,


Question # 09
Create and plot the following signals using sampling rate l0 Hz (i.e. 10 samples per second).
Include the plots in your report:
a. y1 = cos(5t);
b. y2 = 2exp(2t)*cos(5t);
c. add a noise scaled by 0.2 on y1.
(use function randn, and type help randn if you dont know how to use it).

Note: attach all the matlab code in your report