% corrx_example4.m: 
% Example of autocorrelation estimation.
% Still use time averaging, but do not use corb( ) this time.
% Get rid of the finite-duration effect of convolution.
% x(t) = cos(t + theta), where theta is uniformly distributed.
% David Pan for EE 385

sample_period = 0.001;
tmin = -10;         % Time shift range
tmax = 10;
theta = random('Uniform', 0, 2*pi);

tau = -tmax: sample_period: tmax;
M = length(tau); 

for i = 1: M
    if tau(i) >= 0
        t = tmin: sample_period: (tmax + tau(i));
        xt = cos(t + theta);
        xtau = cos(t + tau(i) + theta);
        R(i) = mean(xt .* xtau);
    else 
        t = (tmin +tau(i)): sample_period: tmax;
        xt = cos(t + theta);
        xtau = cos(t + tau(i) + theta);
        R(i) = mean(xt .* xtau);
    end      
end 
plot(tau,R);
xlabel('LAG'); ylabel('RX'); grid;