Random Signals for Engineers using MATLAB and Mathcad Copyright 1999 Springer Verlag NY Example 3.1 Histogram and Averages In this example we illustrate the different way averages may be computed for sequence of random variables. We must first generate a sequence of random variables and set the values to five discrete values of xi. We use the Matlab built in random number generator rand(1,N). Multiplying by 5.0 we get a 0 - 5 uniform number sequence. We discretize the result by the use of the integer (floor) and obtain a sequence that has values from a set of possible outcomes, { 0 1 2 3 4}. We will work with a 15 number sequence to illustrate the process. y=floor(5*rand(1,15)); A companion array cj is generated to indicate the index on yi. In this example we used the Matlab built in random number generator since it behaves in a manner similar to the generator of Example 2.9. c=1:15 c = Columns 1 through 12 1 2 3 4 12 Columns 13 through 15 13 14 15 5 6 7 8 9 10 11 4 3 2 0 4 2 3 y y = Columns 1 through 12 4 1 3 2 3 Columns 13 through 15 4 3 0 A histogram function can be computed for the sequence yi and tabulated to indicate the number of terms in the sequence that correspond to each item, xi , of the set. n=hist(y,5); Comparing the items in the sequence, y, we find that there are n(1) ans = 2 items with value 0 in the yj sequence. The next quantity we would like to compute is the index of the items in the sequence that are valued 0, 1 etc. i=find(y==0) y(i) i = 8 15 0 0 ans = The indices that correspond to x1 = 0 are recovered. The entire set of operation can be repeated for the other values of u, such as u = 1 i=find(y==1) y(i) i = 2 ans = 1 and for other values of u, such as u =2 i=find(y==2) y(i) i = 4 7 10 2 2 2 ans = and for other values of u, such as u =3 i=find(y==3) y(i) i = 3 6 11 12 14 3 3 3 3 3 ans = Since the histogram counts the number of terms in the sequence that corresponds to each item of the set x i, we may sum the elements in the histogram to recover N as sum(n)/15 ans = 1 Averaging can also be performed, we may verify Equation 3.1-9 by first summing the relative frequency or probabilities in the limit. x=0:4 sum(x.*n)/15 x = 0 ans = 1 2 3 4 2.5333 The alternate way of computing the averages is by the sequence directly or sum(y)/15 ans = 2.5333 The same averaging process can be used for sum(y.^2)/15 ans = 8.1333 By the use of histograms sum(x.^2.*n)/15 ans = 8.1333 For this sequence we can plot the histogram approximation to the probability density function. Remember that in this example we have used a small number for N and we do not expect a good probability density representation. bar(n/15,.2) 0.35 0.3 0.25 0.2 0.15 0.1 0.05 0 1 2 3 4 5