Autocorrelation

# Overview
Autocorrelation is a mathematical tool used frequently in signal processing for analysing functions or series of values, such as time domain signals.  Informally, it is a measure of how well a signal matches a time-shifted version of itself, as a function of the amount of time shift. More precisely, it is the cross-correlation of a signal with itself.  Autocorrelation is useful for finding repeating patterns in a signal, such as determining the presence of a periodic signal which has been buried under noise, or identifying the missing fundamental frequency in a signal implied by its harmonic frequencies.

# Definitions
Different definitions of autocorrelation are in use depending on the field of study which is being considered and not all of them are equivalent.  In some fields, the term is used interchangeably with autocovariance.

## Statistics
In statistics, the autocorrelation function (ACF) of a random process describes the correlation between the process at different points in time.  Let Xt be the value of the process at time t (where t may be an integer for a
discrete-time process or a real number for a continuous-time process).
If Xt has mean  and variance 2 then the definition of the ACF is
R(t,s) = \frac{E}{\sigma^2}\, ,
where E is the expected value operator.  Note that this expression is not well-defined for all time series or processes, since the variance 2 may be zero (for a constant process) or infinite.  If the function R is well-defined its value must lie in the range , with 1 indicating perfect correlation and 1 indicating perfect anti-correlation.
If Xt is second-order stationary then the ACF depends only on the difference between t and s and can be expressed as a function of a single variable.  This gives the more familiar form
R(k) = \frac{E}{\sigma^2}\, ,
where k is the lag, | t  s |.  It is common practice in many disciplines to drop the normalization by 2 and use the term autocorrelation interchangeably with autocovariance.
For a discrete time series of length n {X1, X2, ... Xn} with known mean and variance, an estimate of the autocorrelation may be obtained as
\hat{R}(k)=\frac{1}{(n-k) \sigma^2} \sum_{t=1}^{n-k} 
for any positive integer k < n.  When the true mean  is known, this estimate is unbiased.  However, if the true mean and variance of the process are not known, and  and 2 are replaced by the standard formulae for sample mean and sample variance, then this is a biased estimate.  As an alternative, a periodogram based estimate replaces n-k in the above formula with n.  This estimate is always biased; however, it usually has a smaller mean square error.

## Signal processing
In signal processing, the above definition is often used without the normalization, that is, without subtracting the mean and dividing by the variance.  When the autocorrelation function is normalized by mean and variance, it is sometimes referred to as the autocorrelation coefficient.
Given a signal f(t), the continuous autocorrelation Rff() is most often defined as the continuous cross-correlation integral of f(t) with itself, at lag .
where \bar f represents the complex conjugate and - represents convolution.  For a real function, \bar f = f.
The discrete autocorrelation R at lag j for a discrete signal xn is
The above definitions work for signals that are square integrable, or square summable, that is, of finite energy.  Signals that "last forever" are treated instead as random processes, in which case different definitions are needed, based on expected values.  For wide-sense-stationary random processes, the autocorrelations are defined as
For processes that are not stationary, these will also be functions of t, or n.
For processes that are also ergodic, the expectation can be replaced by the limit of a time average.  The autocorrelation of an ergodic process is sometimes defined as or equated to
These definitions have the advantage that they give sensible well-defined single-parameter results for periodic functions, even when those functions are not the output of stationary ergodic processes.
Alternatively, signals that last forever can be treated by a short-time autocorrelation function analysis, using finite time integrals.  (See short-time Fourier transform for a related process.)
Multi-dimensional autocorrelation is defined similarly.  For example, in three dimensions the autocorrelation of a square-summable discrete signal would be
When mean values are subtracted from signals before computing an autocorrelation function, the resulting function is usually called an auto-covariance function.

# Properties
In the following, we will describe properties of one-dimensional autocorrelations only, since most properties are easily transferred from the one-dimensional case to the multi-dimensional cases.
- A fundamental property of the autocorrelation is symmetry, R(i) = R(i), which is easy to prove from the definition. In the continuous case, the autocorrelation is an even function
- The continuous autocorrelation function reaches its peak at the origin, where it takes a real value, i.e. for any delay , |R_f(\tau)| \leq R_f(0).  This is a consequence of the CauchySchwarz inequality.  The same result holds in the discrete case.
- The autocorrelation of a periodic function is, itself, periodic with the very same period.
- The autocorrelation of the sum of two completely uncorrelated functions (the cross-correlation is zero for all ) is the sum of the autocorrelations of each function separately.
- Since autocorrelation is a specific type of cross-correlation, it maintains all the properties of cross-correlation.
- The autocorrelation of a continuous-time white noise signal will have a strong peak (represented by a Dirac delta function) at  = 0 and will be absolutely 0 for all other .
- The WienerKhinchin theorem relates the autocorrelation function to the power spectral density  via the Fourier transform:
- For real-valued functions, the symmetric autocorrelation function has a real symmetric transform, so the WienerKhinchin theorem can be re-expressed in terms of real cosines only:

# Regression analysis
In regression analysis using time series data, autocorrelation of the residuals ("error terms", in econometrics) is a problem.
Autocorrelation violates the OLS assumption that the error terms are uncorrelated.  While it does not bias the OLS coefficient estimates, the standard errors tend to be underestimated (and the t-scores overestimated).
The traditional test for the presence of first-order autocorrelation is the DurbinWatson statistic or, if the explanatory variables include a lagged dependent variable, Durbin's h statistic.  A more flexible test, covering autocorrelation of higher orders and applicable whether or not the regressors include lags of the dependent variable, is the BreuschGodfrey test.  This involves an auxiliary regression, wherein the residuals obtained from estimating the model of interest are regressed on (a) the original regressors and (b) k lags of the residuals, where k is the order of the test.  The simplest version of the test statistic from this 
auxiliary regression is TR2, where T is the sample size and R2 is the coefficient of determination.  Under the null hypothesis of no autocorrelation, this statistic is
asymptotically distributed as 2 with k degrees of freedom.
Responses to nonzero autocorrelation include generalized least squares and NeweyWest standard errors.

# Applications
- One application of autocorrelation is the measurement of optical spectra and the measurement of very-short-duration light pulses produced by lasers, both using optical autocorrelators.
- In optics, normalized autocorrelations and cross-correlations give the degree of coherence of an electromagnetic field.
- In signal processing, autocorrelation can give information about repeating events like musical beats or pulsar frequencies, though it cannot tell the position in time of the beat. It can also be used to estimate the pitch of a musical tone.