%  Example 3.3.  Generates a sample function, plots the analytic correlation 
%  function, and plots the analytic spectral density.
clear
%  Input the time sample vector.
t=0:.1:50;
%  Generate the random phase.
theta=2*pi*rand;
%  A Rayleigh random variable is the square root of the sum of two 
%  Gaussian random variables squared.
%  Input the variance of the Gaussian random variables.
snormal=0.5;
%  Compute the meand and variance (using analytic formula) of the 
%  resulting Rayleigh random variable.
mean=snormal*sqrt(pi/2)
variance=(2-pi/2)*snormal^2
%  Generate the two Gaussian random variable.
a=snormal*randn;
b=snormal*randn;
%  Generate the Rayleigh random amplitude.
alpha=sqrt(a*a+b*b);
%  Generate the sample function.
w=0.5
x=alpha*cos(w*t+theta);
%  Plot the sample function.
set(0,'DefaultAxesFontName','times')
set(0,'DefaultAxesFontSize',16)
set(0,'DefaultTextFontName','times')
figure(1)
clf
plot(t,x,'r-')
xlabel('Time (sec)')
ylabel('Amplitude')
grid
%  Generate the correlation function from the analytic expression.
Amp=(mean^2+variance^2)/2
tau=-25:.1:25;
R=Amp*cos(w*tau);
%  Plot the correlation function.
figure(2)
clf
plot(tau,R,'r-')
axis([-25 25 -.3 .3])
xlabel('Time difference (sec)')
ylabel('Correlation')
grid
%  Generate the spectral density from the analytic expression.
w=[-1 -.5000001 -.5 -.4999999 .4999999 .5 .5000001 1];
Sw=[0 0 .2*pi 0 0 .2*pi 0 0];
%  Plot the spectral density.
figure(3)
clf
plot(w,Sw,'r-',w(3),Sw(3),'r^',w(6),Sw(6),'r^')
xlabel('Frequency (rad/sec)')
y=ylabel('Spectral density');
set(y,'Units','Normalized')
xpos=-0.1;
set(y,'Position',[xpos 0.4861 0])
grid
axis([-1 1 0 1])
