You are here : matlabSignal Processingxcov

xcov() - Signal Processing

```c = xcov(x,y) returns
the cross-covariance of two discrete-time sequences, x and y.
Cross-covariance measures the similarity between x and
shifted (lagged) copies of y as a function of
the lag. If x and y have
different lengths, the function appends zeros at the end of the shorter
vector so it has the same length as the other.
examplec = xcov(x) returns
the autocovariance sequence of x. If x is
a matrix, then c is a matrix whose columns contain
the autocovariance and cross-covariance sequences for all combinations
of the columns of x.

examplec = xcov(___,maxlag) limits
the lag range from –maxlag to maxlag.
This syntax accepts one or two input sequences. maxlag defaults
to N – 1.
specifies a normalization option for the cross-covariance or autocovariance.
Any option other than 'none' (the default) requires x and y to
have the same length.

[c,lags]
= xcov(___) also outputs a vector with the
lags at which the covariances are computed.```

Syntax

```c = xcov(x,y)c = xcov(x) examplec = xcov(___,maxlag) examplec = xcov(___,scaleopt) example[c,lags]
= xcov(___)```

Example

```Cross-Covariance of Two Shifted SignalsOpen This Example
Create
, a two-channel 150-sample signal consisting of
, a uniform random sequence, and
, a copy of
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
. Plot the result. The maxima at
and
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

Autocovariance of White Gaussian NoiseOpen This Example
Display the estimated autocovariance of white Gaussian noise,
, for
. Reset the random number generator for reproducible results. Normalize the sequence so that it is unity at zero lag.
rng default

ww = randn(1000,1);
[cov_ww,lags] = xcov(ww,10,'coeff');

stem(lags,cov_ww)

GPU Acceleration for Autocovariance Sequence Estimation
This example requires Parallel Computing Toolbox™ software
and a CUDA-enabled NVIDIA GPU with compute capability 1.3 or above.
See GPU
System Requirements for details.
Create a signal consisting of a 10 Hz sine wave in additive
noise, sampled at 1 kHz. Use gpuArray to create
a gpuArray object stored on your computer's
GPU.t = 0:0.001:10-0.001;
x = cos(2*pi*10*t) + randn(size(t));
X = gpuArray(x);
Compute the autocovariance sequence to lag 200.[xc,lags] = xcov(X,200);The output, xc, is a gpuArray object.Use gather to transfer the data from the
GPU to the MATLAB® workspace as a double-precision vector.xc = gather(xc);Related ExamplesMeasuring Signal SimilaritiesAccelerating Correlation with GPUs```