0% found this document useful (0 votes)
13 views

EEN330 Lab 1 and Lab 2

Signals and system practice questions

Uploaded by

anzeemai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

EEN330 Lab 1 and Lab 2

Signals and system practice questions

Uploaded by

anzeemai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

EEN330: Random Signals and Noise

Lab Assignment 1
Random Signals, Relative Frequencies and Probability
Densities
Prepared By:
Eng. Yasmina Al Khalil

Objectives
• Learn how to generate random signals and simulate random events in Matlab.
• Construct a histogram for random numbers according to uniform and Gaussian distributions in Matlab.
• Calculate the PDF for the Gaussian distribution in Matlab.
• Estimate the mean and variance of a random variable.

Required Equipment
1. PC/Laptop
2. Matlab

Instructions
• Follow the given instructions and hints while solving your experiment.
• Use the help xyz command to get more information on the command xyz().
• Answer all of the questions in the manual and provide the Matlab code for the output. Each group is required to
submit one copy of the filled manual sheet.
• Follow the manual for writing your lab report. Make sure you include all answers to the questions provided with
the analysis of the code and results.

Lab Experiment
(i) Simulating the Flipping of Coins
In the first part of our lab, we are going to use rand() command to simulate the flipping of coins. The command rand(m,
n) creates a matrix of m rows and n columns, where each element of the matrix is a randomly selected number equally
likely to fall anywhere in the interval (0,1). By rounding this number to the nearest integer, we can create a randomly
selected number equally likely to be 0 or 1. This can be used to simulate the flipping of a coin if we interpret 0 as tails
and 1 as heads or vice versa.
(a) If we flip an unbiased coin once, what is the sample space in such an event (all possible outcomes) ?

(b) What is the probability of getting any values from the sample space of this event?

1
(c) What kind of distribution does a random variable from the event of an unbiased coin toss belong to? Sketch and
describe the PDF.

(d) Open Matlab and create a new script.


(e) Create a 1x1 matrix of random numbers and store it in a variable named random number. The numbers fall in
the interval between 0 and 1. [Hint: use the command rand()].

(f) What do you notice? What values can random number take? Is it continuous or discrete?

(g) How can we have random number take only a value of 0 or 1 to resemble our event of tossing a coin?

(h) Round random number to the nearest integer and store the result in coin flip. [Hint: use the command round()].What
does round() do?

(i) Save the script and run it.


(j) Write down your result.

2
(k) Run the script a number of times. What do you observe?

(ii) Simulating The Rolling of Dice


In the second part of our lab, we are going to use rand() command to simulate the rolling of dice. If we multiply rand(1)
by 6 and round up to the nearest integer, we will get one of the numbers 1,2, ..., 6 with equal probability. This can be
used to simulate the rolling of a die.

(a) If we roll an unbiased die once, what is the sample space in such an event (all possible outcomes) ?

(b) What is the probability of each outcome in the above event? What kind of distribution is that?

(c) On the same script, create a 1x1 matrix of random numbers and store it in a variable named random number2.
The numbers fall in the interval between 0 and 6. [Hint: use the command rand()].

(d) What is the problem with the above? What do we need to do to fix the interval?

(e) Perform the fix suggested above and store the result in die toss.

(f) Save the script and run it.

(g) Write down your result.

3
(h) Run the script a number of times. What do you observe?

(i) Change the above interval to be from 7 to 12 and repeat the rest of the experiment. Write down your result.

(j) Update the script so you can simulate toss of two dice.

(iii) Constructing A Histogram For The Random Numbers Generated By rand()


The Matlab function rand() generates random numbers that are uniformly distributed in the interval (0, 1). To construct
a histogram for the random numbers generated by rand(), we write a script that calls rand() repeatedly. Since we can
do this only a finite number of times, we quantize the range of the random numbers into increaments of 0.1. We then
calculate the number of times a random number falls in each quantized interval and divide by the total number of
numbers generated for the example. If we plot this ratio of relative frequencies using a bar graph, the resulting plot is
called histogram.

(a) What is a histogram? What is it used for?

(b) How can we construct a histogram?

4
(c) Create a new script.
(d) Define the total number of random values generated to be 10000 and store that number in a variable called N.

(e) Use the number in N to generate N random numbers.

(f) Then quantize the range of the random numbers into increments of 0.1. This is done by creating 10 bins, with
centers at 0.05, 0.15, ...

(g) Calculate the number of times a random number falls in each quantized and display both the number of times (y
values) and their quantized (x values). [Hint: use the command hist()]. What does command hist() do?

(h) Normalize the y values that are generated in the previous part to produce relative frequencies by dividing it by the
total number of random numbers generated. Why is normalization important? Why do we normalize the y axis
only?

(i) Plot the bar graph of the x values and the y values. [Hint: use the command bar()].

5
(j) Name the x-axis (x) and the y-axis (Relative Frequencies).

(k) Save and run the script. What do you get?

(l) Run the script a number of times. What do you observe? What does a generated histogram look like (is the height
of each bar equal, was this expected and why)?

(iv) Constructing A Histogram For The Random Numbers Generated By randn()


In addition to rand() function, Matlab also has a built-in function, randn(), which generates random variables according
to a Gaussian or normal distribution. In particular, randn(k,n) creates a k x n matrix whose elements are randomly
chosen according to a standard normal distribution. In this section, we are going to construct a histogram of numbers
generated by the randn() function similar to what was done in the previous section using the rand() function. Note
that by multiplying the output of the randn() function by σ(standard deviation) and adding m (mean), the Gaussian
random variable produced by randn() now has mean m and variance σ 2 . Also, the Gaussian PDF has a domain that is
infinite and, thus, in principle we would need an infinite number of bins in our histogram. Since this is impractical, we
choose a sufficiently large number of bins such that those not included would represent relatively insignificant values.

(a) How can you describe a Gaussian random variable?

(b) Create a new script.


(c) Define the total number of random values generated to be 10000 and store that number in a variable called N .

(d) Initialize the mean (m) and σ (sigma) to be 5 and 2 respectively.

6
(e) Use the number in N to generate N random numbers using randn(), then multiply the output by σ and add it
with m. Store the final result in a variable called x.

(f) Create bins with centers at -4.5, -4.5 + 1, ... 14.5 (increment by 1) similar to question (d) in section (iii). Store
-4.5 in lef t variable, 1 in width variable, and 14.5 in right variable

(g) Calculate the number of times a random number falls in each bin and display both the number of times (yvalues)
and their bins (xvalues). [Hint: use the command hist()].

(h) Normalize the yvalues that are generated in the previous part to produce probability densities by dividing it by
the multiplication of the total number of random numbers generated and the width of the bin.

(i) Plot the bar graph of the xvalues and the yvalues. [Hint: use the command bar()].

(j) Save the script and run it. What do you observe?

(k) Change the values of the mean m and standard deviation σ. Write down your observations.

The second thing we need to do in this section is to compute true PDF and compare it with the histogram that we
generate from the previous steps. The normal distribution with location parameter µ (mean) and scale parameter σ has
(z−µ)2
a PDF given by: √1 e− 2σ 2 .
σ 2π

7
(a) In the same script, create a matrix z that starts from lef t − width/2 to right + width/2 with a step equal to
width/10.

(z−µ)2
(b) Compute the true PDF using the equation √1 e− 2σ 2 .
σ 2π

(c) In the same output figure, place the plot of true PDF on top of histogram.

(d) Name the x-axis (x) and the y-axis (f X(x)).

(e) Save the script and run it. What do you observe? What is the sum of values under the generated PDF function?

(v) Estimating The Mean and Variance of Z Using The Sample Mean and Sample Vari-
ance of a Large Number of Matlab-generated Realization of The Random Variable
Z

Suppose we form a random variable Z according to Z = X 2 + Y 2 where X and Y are independent Gaussian random
variables with means of µ and variance of σ 2 . In this section, we are going to estimate the mean and variance of Z using
the sample mean and sample variance of a large number of Matlab-generated realizations of the random variable Z.

(a) Create a new script.


(b) Define the total number of random values generated to be 10000 and store that number in a variable called N.

(c) Initialize the mean (m) and σ (sigma) to be 5 and 2 respectively.

8
(d) Use the number in N to generate N random numbers using randn(), then multiply the output by σ and add it
with m. Store the final result in a variable called X.

(e) Repeat the steps in (d) but use Y as a second variable.


(f) Calculate Z in the equation Z = X 2 + Y 2.

(g) Using Matlab generate the sample mean and the sample variance.

(h) Save and run the script. What are the values of the mean and the variance? What do you observe if you run the
script multiple times? Why do we get these values of the mean and variance?

You might also like