EEN330 Lab 1 and Lab 2
EEN330 Lab 1 and Lab 2
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.
(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?
2
(k) Run the script a number of times. What do you observe?
(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.
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.
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.
(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).
(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)?
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.
(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.
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.
√
(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?