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<x<=1)$

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)$

title('Autocorrelation function of (X)');


title('Autocorrelation function of (Y)=(X-0.5) ');


autocorrelation of two processes

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)

  • $\begingroup$ You should consider to use the autocorr function which demeans the input and normalizes the output $\endgroup$ Commented Sep 19, 2019 at 13:18
  • 1
    $\begingroup$ 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$ $\endgroup$ Commented Sep 19, 2019 at 13:55
  • 1
    $\begingroup$ 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$. $\endgroup$ Commented Sep 19, 2019 at 14:22
  • $\begingroup$ @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$) $\endgroup$
    – 1lc
    Commented Sep 19, 2019 at 14:38
  • 1
    $\begingroup$ 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. $\endgroup$ Commented Sep 19, 2019 at 14:54

1 Answer 1


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.

  • $\begingroup$ 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! $\endgroup$ Commented Sep 20, 2019 at 17:09
  • $\begingroup$ 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. $\endgroup$
    – mark leeds
    Commented Sep 20, 2019 at 21:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.