You are on page 1of 6

BIRLA INSTITUTE OF TECHNOLOGY & SCIENCE, PILANI

EEE G613: Advanced Digital Signal Processing


Semester I: 2021-2022

Lab 1: Introduction to MATLAB

Note: Write your answers in this .doc file only and save it. Capture and paste
snapshots of graphs, wherever required.

1) Generating Array elements


a) Create an array of 3 elements having values 100 200 and 300. Name the array x.
x=[100 200 300]
b) Append the same array with 3 other elements all having values 100.
x(4:6)=100
c) Pad the array with 4 zeros.
x=padarray(x,1,0,'post')
d) Limit the number of elements in the array to 5 elements.
e) Display the 2nd and the 3rd element of the array at the command prompt.
disp(x(2:3))
f) Make a two dimensional array of size 3*3. Call it x3d.
x3d = [1 2 3; 4 5 6; 7 8 9]
g) Display the 2nd row 3rd column element of the array at the command prompt.
disp(x3d(2,3))
h) Find the transpose of this matrix and display both the 3*3 matrices. Call it x3dc.
x3dc = transpose(x3d)
i) Find the sum of the array elements in x (the one -dimensional array created in c).
S= sum(x)
j) What happens when I use the same command to the 3d array x3d.
it will compute the sum of the elements in each column.
k) Make two arrays containing 100 elements from 1 to 100 in steps of 1 and 5,
respectively. Find the lengths of both the recently created arrays.

1
2) Getting used to functions to abs, sqrt and exp
a) Find the square root of a positive number and store it in a variable called xsqrt. Obtain
the positive number from the user using the input command.
disp('Enter the value');
x=input('x: ');
xsqrt = realsqrt(x);
disp(xsqrt);

b) Find the square root of a negative number and store it in a variable called xsqrt1.
Obtain the number again from the user.
con=1;
while con
disp('Enter the value');
x=input('x: ');
if x < 0
xsqrt1 = sqrt(x);
disp(xsqrt1);
else
disp('enter the negative value');
end
end

2
c) Note the difference in the values stored in xsqrt and xsqrt1.
For negative value of x, sqrt(X) produces complex results.
d) Find the abs value of xsqr1 and store that in the same variable.
xsqrt1= abs(xsqrt1)
e) Obtain 10 random elements using rand and randn functions. Observe the difference.
y= rand(1,10)
y1= randn(1,10)
result- rand() function generates random value in the interval (0,1) whereas randn()
generates random value in the interval (-1,1)

f) Generate 10 values from the uniform distribution between -2 and 3. Save the variable
and called it as y.
y = randi([-2,3],10,1)

g) Generate values from the standard distribution with mean = 2 and standard deviation
3. Save the variable and called it as z.
mean = 2;
std = 3;
z = std.*randn(1000,1) + mean
plot(z)

3
3) To get used to control functions
a) Generate a random array of 100 elements between 0 and 10. Call it x. Also generate
another 100-element array containing values from 1 to 100 in steps of 1 and call it x1.
x=randi([0 10],1,100)
a = 1;
r = 1;
x1(1)=1;
for n=1:99
x1(n+1)=x1(n)+(a*(r^n))
end

b) Set a counter that counts the number of odd elements in x and x1.
x=randi([0 10],1,100)
a = 1;
r = 1;
x1(1)=1;
for n=1:99
x1(n+1)=x1(n)+(a*(r^n));
end
evenx = sum(~rem(x,2));
oddx = 100 - evenx;
evenx1 = sum(~rem(x1,2));

4
oddx1 = 100 - evenx1;
X = sprintf('number of odd values in x is %d and x1 is
%d',oddx,oddx1);
disp(X)

c) Use rand function to generate a random sequence and then apply 0.5 as a threshold to
obtain the binary digits. Display the binary digits.
x=rand(1,10)
for n=1:10
if x(n)<0.5
x(n)=0;
else
x(n)=1;
end
end
disp(x)

4) To Create functions and call and execute them


a) Create a function that counts the number of multiples of 3 in an array. Call it mult3.
x=randi([1 100],1,50)
mult3(x)
function mult3(x)
mult=0;
for i= 1:50

5
if rem(x(i),3)==0
mult=mult+1;
else
mult=mult;
end
end
y = sprintf('number of multiples is %d',mult);
disp(y);
end

b) Run this function from a main command using some of the test examples.

You might also like