%% Cross-Covariance of Two Shifted Signals
% Create s, a two-channel 150-sample signal consisting of s_1, a uniform random sequence, and s_2, a copy of s_1 shifted circularly by 50 samples. Reset the random number generator for reproducible results. Plot the sequences.

rng default
shft = 50;

s1 = rand(150,1);
s2 = circshift(s1,[shft 0]);
s = [s1 s2];

subplot(2,1,1)
plot(s1)
title('s_1')
subplot(2,1,2)
plot(s2)
title('s_2')
hold on
plot([shft shft],[0 1])

%
% Compute biased estimates of the autocovariance and mutual cross-covariance sequences. The output array is organized as {\tt c}=\pmatrix{c_{s_1s_1}&c_{s_1s_2}&c_{s_2s_1}&c_{s_2s_2}\cr}. Plot the result. The maxima at \mp50 and \pm100 are a result of the circular shift.

[c,lg] = xcov(s,'biased');

figure
plot(lg,c)
legend('c_{s_1s_1}','c_{s_1s_2}','c_{s_2s_1}','c_{s_2s_2}')

%
% Change the normalization so that the autocovariance sequences are unity at zero lag. Plot each sequence in its own subplot.

[c,lg] = xcov(s,'coeff');

for a = 1:2
    for b = 1:2
        nm = 2*(a-1)+b;
        subplot(2,2,nm)
        plot(lg,c(:,nm))
        title(sprintf('c_{s_%ds_%d}',a,b))
        axis([-150 150 -0.2 1])
    end
end

% Copyright 2015 The MathWorks, Inc.