Birla Institute of Technology & Science, Pilani EEE G613: Advanced Digital Signal Processing Semester I: 2021-2022
Birla Institute of Technology & Science, Pilani EEE G613: Advanced Digital Signal Processing Semester I: 2021-2022
Note: Write your answers in this .doc file only and save it. Capture and paste
snapshots of graphs, wherever required.
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)
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.