Random Signals for Engineers using MATLAB and Mathcad Copyright 1999 Springer Verlag NY Example 4.9 Gaussian Random Sequences In this example we will generate a jointly gaussian correlated random variable. The procedure follows the outlinedescribed in section 4.8. In this development we will use tranformation formulas that have been modified to allow the randomo varibles that have nonunity variances and sample means. These formulas are simple extensions to the relations derived in section 4.8. We first generate two uniform 0 -1 random sequences. We then transform the results to two uncorrelated gaussian sequences. We generate a zero mean and unit variance gaussian sequences. We then scale the resultant random variables to the desired mean and variance as we apply the transfoation equation. N=400; az=0;aw=0;sz=1;sw=1; rv1=rand(N,1); rv2=rand(N,1); z=az+sqrt(-2*sz^2*log(rv1)).*cos(2*pi*rv2); w=aw+sqrt(-2*sw^2*log(rv1)).*sin(2*pi*rv2); The sequences just generated are gaussian. We can verify that the sequences have been generated with the desired mean and variance by calculation and by plotting the cumulative distribution curves we may verify that the distribution is gaussian. mean(z) mean(w) corrcoef(z,w) ans = 0.0238 ans = -0.0382 ans = 1.0000 0.0257 0.0257 1.0000 The latter calculation is the correlation coefficient, which is given, in the off diagonal term of the matrix. This should be near zero because the sequences are generated to be uncorrelated. A convenient technique for determining the cumulative distribution function is to sort the random sequences and plot the index vs. the sorted sequence. This is in effect a cumulative histogram with the bins set to one index value. This histogram can be compared to a gaussian cnorm or the cumulative distribution function. zs=sort(z); ws=sort(w); i=1:length(zs); xp=-3:.05:3; gcdf=0.5 * (1 + erf((xp - az) ./ (sz * sqrt(2)))); plot(xp,gcdf,zs,i/N) 1 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0 -4 -3 -2 -1 0 1 2 3 A similar set can be plotted for the w variable plot(xp,gcdf,ws,i/N) 1 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0 -3 -2 -1 0 1 2 3 In order to show the correlation the 2 ellipse can be plotted as an overlay to the z,w point plot. The technique for constructing the ellipse follows example 4.6. A mean value has been added. szr=1; swr=1; az=0; aw=0; s=2; zmax=sqrt(2*s)*szr; zp=-zmax:.02:zmax; wzp=(-(zp-az).^2+2*s*szr^2).^(1/2)*swr/szr+aw; plot(z,w,'.',zp,wzp,-zp,-wzp) axis([-4 4 -4 4]) 4 3 2 1 0 -1 -2 -3 -4 -4 -3 -2 -1 0 1 2 3 4 From the plot it can be seen that the random points tend to follow the orientation of the ellipse with most of the points distributed in the same orientation as the ellipse. We now complete the transformation to generate correlated variables. transformation equations for non-zero means and variances. ax=4;ay=2;sx=3;sy=1;rho=0.7; x=sx*z+ax; y=rho*sy*z+sqrt(1-rho^2)*sy*w+ay; The means and variances are calculated and compared to the desired ones. [mean(x) std(x)] [mean(y) std(y) ] corrcoef(x,y) ans = 4.0714 ans = 1.9894 ans = 1.0000 0.7000 2.8818 0.9865 0.7000 1.0000 We have corrected the The correlated ellipse is now drawn. This requires the use of the transformation that was explained in Example 4.6. zmax=sqrt(2*s)*sqrt(1-rho^2)*sx; zp=-zmax:.02:zmax; wzp=(-(zp).^2+2*s*sx^2-2*s*sx^2*rho^2).^(1/2)*sy/sx; The angular rotation is now computed th0 =1/2*atan(2*rho*sx*sy/(sx^2-sy^2)); th0*180/pi ans = 13.8497 xp=cos(th0)*zp-wzp*sin(th0)+ax; yp=sin(th0)*zp+wzp*cos(th0)+ay; plot(x,y,'.',ax,ay,'square',xp,yp,-xp+2*ax,-yp+2*ay) axis([-2 12 -2 6]) 6 5 4 3 2 1 0 -1 -2 -2 0 2 4 The generated points follow the ellipse as expected. 6 8 10 12