# Autocorrelation of a signal comprised of only the same number

In MATLAB I generated a signal and its autocorrealtion function using:

signal = ones(1,500).*5; % a vector of the number 5

acf = autocorr(signal,numel(signal)-1));

To my suprise the resulting ACF vector is all NaN.

But If i generate the signal with numbers between zero and one (0.3 in this case),

signal = ones(1,500).*0.3; % a vector of the number 0.3

acf = autocorr(signal,numel(signal)-1));

I get the following plot,

I am just confused as to what is going on here. I would think in either case the autocorrelation of the same number will produce just all '1's indicating no decay what so ever? So why do I get NaN when the signal is above 1 but a linear decaying signal when its between 0 and 1.

Thanks

What you see are mainly numerical effects, even though the second result is actually correct, but not according to what Matlab intends to compute. But let's go slow ...

Matlab computes the autocorrelation function by first subtracting the mean of the signal and then correlating it with itself. Then the autocorrelation function is normalized such that its value for lag 0 equals 1 (this is where you get the NaNs from).

What happens in your case is that the signal equals its mean (because it is constant), so by subtracting the mean you generate a zero signal, the autocorrelation of which is zero. Then in the normalization step you divide the zero signal by its first value (which is zero), and you get NaNs. This shouldn't happen in a well written function, so you could complain with Mathworks about it.

What is even worse, is that this behavior is not consistent. You found some scaling values for which you actually get a reasonably looking autocorrelation function. This is really weird but I think I understand what's going on. Try the following. Look at the values of

signal - mean(signal)


This is what Matlab bases its computiation on. What you'll see is that for the scaling values which give you NaNs, this difference is exactly zero (which is the correct result). For the other scaling values (which give a well-behaved autocorrelation function), this difference will be some constant in the order of $10^{-17}$. So you're again left with a constant signal, albeit it much smaller than before. The autocorrelation function of such a constant signal (without subtracting the mean, because this has already been done) is triangular, simply because it is the convolution of two rectangular functions. The normalization gets rid of the $10^{-17}$ factor and you actually get a nice looking autocorrelation of your signal. This one is even correct if you were to compute the autocorrelation without subtracting the mean of the signal (but this is not what the Matlab function intends to do!).

Conclusion: the first result (NaNs) is useless but at least consistent with the definition of the autocorrelation function used by Matlab (even though a division by zero should be prevented in a good program). The second result is not consistent with Matlab's definition of the autocorrelation, but it is - due to numerical errors (!) - correct when the autocorrelation is defined without first subtracting the mean of the signal. Note that both definitions are used in practice so it's always important to check what is actually meant by 'autocorrelation'.

• Thanks for that very detailed explanation. It makes perfect sense. I see now when I do the mean of the 0.3 vector signal it becomes and array of 5.5511e-17... So for any signal which doesn't change, the triangular shape of the ACF is the expected behavior? Is there any way to get an ACF that never decays? Thanks – Steve Hatcher Aug 16 '14 at 10:10
• @SteveHatcher: The triangular shape of the ACF is a consequence of the fact that you're considering signals of finite length. If you had a constant signal with infinite extension (i.e. $x(n)=c$ for $-\infty<n<\infty$) then you'd have a constant ACF. – Matt L. Aug 16 '14 at 10:31