# Autocorrelation of a uniform random process

i am currently learning the basics of signal processing.

As you may know the definition of the autocorrelation is different if you look at a random process or for example a deterministic signal

My question is about the autocorrelation of random processes:

Suppose $$X$$ is a random variable with uniform distribution over $$[0,1]$$

so: $$f_X(x)=1$$ for $$(0

The autocorrelation $$r_{XX}(n_1,n_2)$$ is defined as: $$r_{XX}(n_1,n_2)=E[X(n_1)X(n_2)]$$.

if $$X$$ is stationary up to the second order the autocorrelation is only a function of $$\tau$$: $$r_{XX}(\tau)=E[X(n+\tau)X(n)]$$.

If i generate such a random variable in matlab with the "rand" command and compute the autocorrelation (which should be possible because the random process is ergodic [ time and ensemble averages are equal]) i get a strange result which looks more like the convolution of the propability density functions. If i subtract the mean i get the result i would expect, because i assume that X is uncorrelated white noise, so $$r_{XX}(\tau)=\sigma_x^2\delta(\tau)$$

x=rand(1,100)
Rxx=xcorr(x,x);
subplot(2,1,1)
plot(Rxx);
grid;
title('Autocorrelation function of (X)');

ylabel('Autocorrelation');

y=rand(1,100)-0.5
Ryy=xcorr(y,y);
subplot(2,1,2)
plot(Ryy);
grid;
title('Autocorrelation function of (Y)=(X-0.5) ');

ylabel('Autocorrelation');


So my questions are:

$$(1)$$ Am i wrong with the assumption that $$r_{XX}(\tau)=\sigma_x^2\delta(\tau)$$? And if yes, how can we compute $$r_{XX}(\tau)$$ because we don't know the join-propability densitiy function $$f_{X_1X_2}(x_1,x_2)$$

$$(2)$$ Why does the result of the first computed autocorrelation look so strange? (first plot)

• You should consider to use the autocorr function which demeans the input and normalizes the output Sep 19, 2019 at 13:18
• Consider $x$ being zero mean then adding a constant $c$, $\sum_{n=0}^{N-m-1} (x(n)+c)(x(n+m)+c)$ results in approximately in $\sum_{n=0}^{N-m-1} (x(n)x(n+m)) + (N-m-1)c^2$. That is what you are seeing in plot1 compared to plot2. You are adding depending on the lag $m$ $(N-m-1)c^2$, which has a triangle shape over all $m=-100,...,100$. In your case $c$ is the mean with $c=0.5$ Sep 19, 2019 at 13:55
• For independent random variables (more generally for uncorrelated random variables), $E]XY] = E[X]E[Y]$ and so it should not be surprising to you that $E[X(n)X(n+k)] = E[X(n)]E[X(n+k)= \frac 14$ since both $X(n)$ and $X(n+k)$ have mean $\frac 12$. Sep 19, 2019 at 14:22
• @DilipSarwate really? because if X would be white noise (zero mean) then by your calculation $r_{xx}$ would be 0, but $r_{xx}$ of white noise is a dirac impulse (You didn't consider the case for $k=0$)
– 1lc
Sep 19, 2019 at 14:38
• When $k=0$, are $X(n)$ and $X(n+k) = X(n)$ independent? So what I was was writing doesn't apply to that case. Big deal. The comment by @Irreducible is spot on and should be changed to an answer. Sep 19, 2019 at 14:54

As suggested I am adding my comments as an answer. The sample autocorrelation function (ACF) for $$n$$ observations is given by

$$\hat{p}_x(m) = \frac{\sum_{n=0}^{N-m-1}(x_n - \bar{x})(x_{n+m} - \bar{x})}{\sum_{n=0}^{N-m-1}(x_n - \bar{x})^2}$$

To understand why you have a difference between figure 1 and figure 2, lets assume we are looking at observations $$x_n$$ being zero mean. Let be $$y_n = x_n +c$$ with c being a constant. Looking now at matlabs help for xcorr for the input $$y_n$$ we get

$$r(m)=\sum_{n=0}^{N-m-1}y_n y_{n+m}^* = \sum_{n=0}^{N-m-1}(x_n + c)(x_{n+m} + c)$$

which can be summarized to

$$\sum_{n=0}^{N-m-1}(x_nx_{n+m}) + (N-m)c^2$$

In the case of xcorr $$m$$ equals $$m=-N,...,N$$. Which means you are adding a triangle to your result.

To calculate the ACF matlab has the autocorr function, which takes care of normalization and demeaning of the input.

• I think a nonzero mean stationary process should have an offset in the autocorrelation as well, namely the squared mean of the process, which woulf also follow from the definition in the question. The triangle shape, im my opinion, is the result of of correlating finite sequences (due to the fact matlab cannot process infinite observation vectors). The zero mean auto-correlation function is what I would consider to be auto-covariance. Feel free to correct me if I'm wrong, though! Sep 20, 2019 at 17:09
• I can't correct you because DSP is not my field. All I can say is that, in statistics, auto-correlation is the scaled version of auto-covariance and correlation is the scaled version of covariance. Maybe it's different in DSP. I can say that the triangle shape is due to the non-zero mean of $X$. See the other link I pointed for an R example of the same phenomena. Sep 20, 2019 at 21:21