# Cross-correlation

Visual comparison of convolution, cross-correlation and autocorrelation. For the operations involving function f, and assuming the height of f is 1.0, the value of the result at 5 different points is indicated by the shaded area below each point. Also, the vertical symmetry of f is the reason ${\displaystyle f*g}$ and ${\displaystyle f\star g}$ are identical in this example.

In signal processing, cross-correlation is a measure of similarity of two series as a function of the displacement of one relative to the other. This is also known as a sliding dot product or sliding inner-product. It is commonly used for searching a long signal for a shorter, known feature. It has applications in pattern recognition, single particle analysis, electron tomography, averaging, cryptanalysis, and neurophysiology.

The cross-correlation is similar in nature to the convolution of two functions. In an autocorrelation, which is the cross-correlation of a signal with itself, there will always be a peak at a lag of zero, and its size will be the signal energy.

In probability and statistics, the term cross-correlations is used for referring to the correlations between the entries of two random vectors ${\displaystyle \mathbf {X} }$ and ${\displaystyle \mathbf {Y} }$, while the correlations of a random vector ${\displaystyle \mathbf {X} }$ are considered to be the correlations between the entries of ${\displaystyle \mathbf {X} }$ itself, those forming the correlation matrix (matrix of correlations) of ${\displaystyle \mathbf {X} }$. If each of ${\displaystyle \mathbf {X} }$ and ${\displaystyle \mathbf {Y} }$ is a scalar random variable which is realized repeatedly in temporal sequence (a time series), then the correlations of the various temporal instances of ${\displaystyle \mathbf {X} }$ are known as autocorrelations of ${\displaystyle \mathbf {X} }$, and the cross-correlations of ${\displaystyle \mathbf {X} }$ with ${\displaystyle \mathbf {Y} }$ across time are temporal cross-correlations.

Furthermore, in probability and statistics the definition of correlation always includes a standardising factor in such a way that correlations have values between −1 and +1.

If ${\displaystyle X}$ and ${\displaystyle Y}$ are two independent random variables with probability density functions ${\displaystyle f}$ and ${\displaystyle g}$, respectively, then the probability density of the difference ${\displaystyle Y-X}$ is formally given by the cross-correlation (in the signal-processing sense) ${\displaystyle f\star g}$; however this terminology is not used in probability and statistics. In contrast, the convolution ${\displaystyle f*g}$ (equivalent to the cross-correlation of ${\displaystyle f(t)}$ and ${\displaystyle g(-t)}$) gives the probability density function of the sum ${\displaystyle X+Y}$.

## Cross-correlation of deterministic signals

For continuous functions ${\displaystyle f}$  and ${\displaystyle g}$ , the cross-correlation is defined as:[1][2][3]

${\displaystyle (f\star g)(\tau )\ \triangleq \int _{-\infty }^{\infty }{\overline {f(t)}}g(t+\tau )\,dt}$

(Eq.1)

which is equivalent to

${\displaystyle (f\star g)(\tau )\ \triangleq \int _{-\infty }^{\infty }{\overline {f(t-\tau )}}g(t)\,dt}$

where ${\displaystyle {\overline {f(t)}}}$  denotes the complex conjugate of ${\displaystyle f(t)}$ , and ${\displaystyle \tau }$  is the displacement, also known as lag (a feature in ${\displaystyle f}$  at ${\displaystyle t}$  occurs in ${\displaystyle g}$  at ${\displaystyle t+\tau }$ ).

Similarly, for discrete functions, the cross-correlation is defined as:[4][5]

${\displaystyle (f\star g)[n]\ \triangleq \sum _{m=-\infty }^{\infty }{\overline {f[m]}}g[m+n]}$

(Eq.2)

which is equivalent to

${\displaystyle (f\star g)[n]\ \triangleq \sum _{m=-\infty }^{\infty }{\overline {f[m-n]}}g[m]}$ .

### Explanation

As an example, consider two real valued functions ${\displaystyle f}$  and ${\displaystyle g}$  differing only by an unknown shift along the x-axis. One can use the cross-correlation to find how much ${\displaystyle g}$  must be shifted along the x-axis to make it identical to ${\displaystyle f}$ . The formula essentially slides the ${\displaystyle g}$  function along the x-axis, calculating the integral of their product at each position. When the functions match, the value of ${\displaystyle (f\star g)}$  is maximized. This is because when peaks (positive areas) are aligned, they make a large contribution to the integral. Similarly, when troughs (negative areas) align, they also make a positive contribution to the integral because the product of two negative numbers is positive.

Animation displaying visually how cross correlation is calculated

With complex-valued functions ${\displaystyle f}$  and ${\displaystyle g}$ , taking the conjugate of ${\displaystyle f}$  ensures that aligned peaks (or aligned troughs) with imaginary components will contribute positively to the integral.

In econometrics, lagged cross-correlation is sometimes referred to as cross-autocorrelation.[6]:p. 74

### Properties

• The cross-correlation of functions ${\displaystyle f(t)}$  and ${\displaystyle g(t)}$  is equivalent to the convolution (denoted by ${\displaystyle *}$ ) of ${\displaystyle {\overline {f(-t)}}}$  and ${\displaystyle g(t)}$ . That is:
${\displaystyle [f(t)\star g(t)](t)=[{\overline {f(-t)}}*g(t)](t).}$
• ${\displaystyle [f(t)\star g(t)](t)=[{\overline {g(t)}}\star {\overline {f(t)}}](-t).}$
• If ${\displaystyle f}$  is a Hermitian function, then ${\displaystyle f\star g=f*g.}$
• If both ${\displaystyle f}$  and ${\displaystyle g}$  are Hermitian, then ${\displaystyle f\star g=g\star f}$ .
• ${\displaystyle \left(f\star g\right)\star \left(f\star g\right)=\left(f\star f\right)\star \left(g\star g\right)}$ .
• Analogous to the convolution theorem, the cross-correlation satisfies
${\displaystyle {\mathcal {F}}\left\{f\star g\right\}={\overline {{\mathcal {F}}\left\{f\right\}}}\cdot {\mathcal {F}}\left\{g\right\},}$
where ${\displaystyle {\mathcal {F}}}$  denotes the Fourier transform, and an ${\displaystyle {\overline {f}}}$  again indicates the complex conjugate of ${\displaystyle f}$ , since ${\displaystyle {\mathcal {F}}\left\{{\overline {f(-t)}}\right\}={\overline {{\mathcal {F}}\left\{f(t)\right\}}}}$ . Coupled with fast Fourier transform algorithms, this property is often exploited for the efficient numerical computation of cross-correlations [7] (see circular cross-correlation).
• The cross-correlation is related to the spectral density (see Wiener–Khinchin theorem).
• The cross-correlation of a convolution of ${\displaystyle f}$  and ${\displaystyle h}$  with a function ${\displaystyle g}$  is the convolution of the cross-correlation of ${\displaystyle g}$  and ${\displaystyle f}$  with the kernel ${\displaystyle h}$ :
${\displaystyle g\star \left(f*h\right)=\left(g\star f\right)*h}$ .

### Definition for periodic signals

If ${\displaystyle f}$  and ${\displaystyle g}$  are both continuous periodic functions of period ${\displaystyle T}$ , the integration from ${\displaystyle -\infty }$  to ${\displaystyle \infty }$  is replaced by integration over any interval ${\displaystyle [t_{0},t_{0}+T]}$  of length ${\displaystyle T}$ :

${\displaystyle (f\star g)(\tau )\ \triangleq \int _{t_{0}}^{t_{0}+T}{\overline {f(t)}}g(t+\tau )\,dt}$

which is equivalent to

${\displaystyle (f\star g)(\tau )\ \triangleq \int _{t_{0}}^{t_{0}+T}{\overline {f(t-\tau )}}g(t)\,dt}$

## Cross-correlation of random vectors

### Definition

For random vectors ${\displaystyle \mathbf {X} =(X_{1},\ldots ,X_{m})^{\rm {T}}}$  and ${\displaystyle \mathbf {Y} =(Y_{1},\ldots ,Y_{n})^{\rm {T}}}$ , each containing random elements whose expected value and variance exist, the cross-correlation matrix of ${\displaystyle \mathbf {X} }$  and ${\displaystyle \mathbf {Y} }$  is defined by[8]:p.337

${\displaystyle \operatorname {R} _{\mathbf {X} \mathbf {Y} }\triangleq \ \operatorname {E} [\mathbf {X} \mathbf {Y} ^{\rm {T}}]}$

(Eq.3)

and has dimensions ${\displaystyle m\times n}$ . Written component-wise:

${\displaystyle \operatorname {R} _{\mathbf {X} \mathbf {Y} }={\begin{bmatrix}\operatorname {E} [X_{1}Y_{1}]&\operatorname {E} [X_{1}Y_{2}]&\cdots &\operatorname {E} [X_{1}Y_{n}]\\\\\operatorname {E} [X_{2}Y_{1}]&\operatorname {E} [X_{2}Y_{2}]&\cdots &\operatorname {E} [X_{2}Y_{n}]\\\\\vdots &\vdots &\ddots &\vdots \\\\\operatorname {E} [X_{m}Y_{1}]&\operatorname {E} [X_{m}Y_{2}]&\cdots &\operatorname {E} [X_{m}Y_{n}]\\\\\end{bmatrix}}}$

The random vectors ${\displaystyle \mathbf {X} }$  and ${\displaystyle \mathbf {Y} }$  need not have the same dimension, and either might be a scalar value.

### Example

For example, if ${\displaystyle \mathbf {X} =\left(X_{1},X_{2},X_{3}\right)^{\rm {T}}}$  and ${\displaystyle \mathbf {Y} =\left(Y_{1},Y_{2}\right)^{\rm {T}}}$  are random vectors, then ${\displaystyle \operatorname {R} _{\mathbf {X} \mathbf {Y} }}$  is a ${\displaystyle 3\times 2}$  matrix whose ${\displaystyle (i,j)}$ -th entry is ${\displaystyle \operatorname {E} [X_{i}Y_{j}]}$ .

### Definition for complex random vectors

If ${\displaystyle \mathbf {Z} =(Z_{1},\ldots ,Z_{m})^{\rm {T}}}$  and ${\displaystyle \mathbf {W} =(W_{1},\ldots ,W_{n})^{\rm {T}}}$  are complex random vectors, each containing random variables whose expected value and variance exist, the cross-correlation matrix of ${\displaystyle \mathbf {Z} }$  and ${\displaystyle \mathbf {W} }$  is defined by

${\displaystyle \operatorname {R} _{\mathbf {Z} \mathbf {W} }\triangleq \ \operatorname {E} [\mathbf {Z} \mathbf {W} ^{\rm {H}}]}$

where ${\displaystyle {}^{\rm {H}}}$  denotes Hermitian transposition.

## Cross-correlation of stochastic processes

In time series analysis and statistics, the cross-correlation of a pair of random process is the correlation between values of the processes at different times, as a function of the two times. Let ${\displaystyle (X_{t},Y_{t})}$  be a pair of random processes, and ${\displaystyle t}$  be any point in time (${\displaystyle t}$  may be an integer for a discrete-time process or a real number for a continuous-time process). Then ${\displaystyle X_{t}}$  is the value (or realization) produced by a given run of the process at time ${\displaystyle t}$ .

### Cross-correlation function

Suppose that the process has means ${\displaystyle \mu _{X}(t)}$  and ${\displaystyle \mu _{Y}(t)}$  and variances ${\displaystyle \sigma _{X}^{2}(t)}$  and ${\displaystyle \sigma _{Y}^{2}(t)}$  at time ${\displaystyle t}$ , for each ${\displaystyle t}$ . Then the definition of the cross-correlation between times ${\displaystyle t_{1}}$  and ${\displaystyle t_{2}}$  is[8]:p.392

${\displaystyle \operatorname {R} _{XY}(t_{1},t_{2})=\operatorname {E} [X_{t_{1}}{\overline {Y_{t_{2}}}}]}$

(Eq.4)

where ${\displaystyle \operatorname {E} }$  is the expected value operator. Note that this expression may be not defined.

### Cross-covariance function

Subtracting the mean before multiplication yields the cross-covariance between times ${\displaystyle t_{1}}$  and ${\displaystyle t_{2}}$ :[8]:p.392

${\displaystyle \operatorname {K} _{XY}(t_{1},t_{2})=\operatorname {E} [(X_{t_{1}}-\mu _{X}(t_{1})){\overline {(Y_{t_{2}}-\mu _{Y}(t_{2}))}}]}$

(Eq.5)

Note that this expression is not well-defined for all-time series or processes, because the mean may not exist, or the variance may not exist.

### Definition for wide-sense stationary stochastic process

Let ${\displaystyle (X_{t},Y_{t})}$  represent a pair of stochastic processes that are jointly wide-sense stationary. Then the Cross-covariance function and the cross-correlation function are given as follows.

#### Cross-correlation function

${\displaystyle \operatorname {R} _{XY}(\tau )=\operatorname {E} \left[X_{t}{\overline {Y_{t+\tau }}}\right]}$

(Eq.6)

or equivalently

${\displaystyle \operatorname {R} _{XY}(\tau )=\operatorname {E} \left[X_{t-\tau }{\overline {Y_{t}}}\right]}$

#### Cross-covariance function

${\displaystyle \operatorname {K} _{XY}(\tau )=\operatorname {E} \left[\left(X_{t}-\mu _{X}\right){\overline {\left(Y_{t+\tau }-\mu _{Y}\right)}}\right]}$

(Eq.7)

or equivalently

${\displaystyle \operatorname {K} _{XY}(\tau )=\operatorname {E} \left[\left(X_{t-\tau }-\mu _{X}\right){\overline {\left(Y_{t}-\mu _{Y}\right)}}\right]}$

where ${\displaystyle \mu _{X}}$  and ${\displaystyle \sigma _{X}}$  are the mean and standard deviation of the process ${\displaystyle (X_{t})}$ , which are constant over time due to stationarity; and similarly for ${\displaystyle (Y_{t})}$ , respectively. ${\displaystyle \operatorname {E} [\ ]}$  indicates the expected value. That the cross-covariance and cross-correlation are independent of ${\displaystyle t}$  is precisely the additional information (beyond being individually wide-sense stationary) conveyed by the requirement that ${\displaystyle (X_{t},Y_{t})}$  are jointly wide-sense stationary.

The cross-correlation of a pair of jointly wide sense stationary stochastic processes can be estimated by averaging the product of samples measured from one process and samples measured from the other (and its time shifts). The samples included in the average can be an arbitrary subset of all the samples in the signal (e.g., samples within a finite time window or a sub-sampling[which?] of one of the signals). For a large number of samples, the average converges to the true cross-correlation.

### Normalization

It is common practice in some disciplines (e.g. statistics and time series analysis) to normalize to cross-correlation function to get a time-dependent Pearson correlation coefficient. However in other disciplines (e.g. engineering) the normalization is usally dropped and the terms "cross-correlation" and "cross-covariance" are used interchangeably.

The definition of the normalized cross-correlation of a stochastic process is

${\displaystyle \rho _{XX}(t_{1},t_{2})={\frac {\operatorname {K} _{XX}(t_{1},t_{2})}{\sigma _{X}(t_{1})\sigma _{Y}(t_{2})}}={\frac {\operatorname {E} [(X_{t_{1}}-\mu _{t_{1}}){\overline {(X_{t_{2}}-\mu _{t_{2}})}}]}{\sigma _{X}(t_{1})\sigma _{Y}(t_{2})}}}$ .

If the function ${\displaystyle \rho _{XX}}$  is well-defined, its value must lie in the range ${\displaystyle [-1,1]}$ , with 1 indicating perfect correlation and −1 indicating perfect anti-correlation.

For jointly wide-sense stationary stochastic processes, the definition is

${\displaystyle \rho _{XY}(\tau )={\frac {\operatorname {K} _{XY}(\tau )}{\sigma _{X}\sigma _{Y}}}={\frac {\operatorname {E} [\left(X_{t}-\mu _{X}\right){\overline {\left(Y_{t+\tau }-\mu _{Y}\right)}}]}{\sigma _{X}\sigma _{Y}}}}$ .

The normalization is important both because the interpretation of the autocorrelation as a correlation provides a scale-free measure of the strength of statistical dependence, and because the normalization has an effect on the statistical properties of the estimated autocorrelations.

### Properties

#### Symmetry property

For jointly wide-sense stationary stochastic processes, the cross-correlation function has the following symmetry property:[9]:p.173

${\displaystyle \operatorname {R} _{XY}(t_{1},t_{2})={\overline {\operatorname {R} _{YX}(t_{2},t_{1})}}}$

Respectively for jointly WSS processes:

${\displaystyle \operatorname {R} _{XY}(\tau )={\overline {\operatorname {R} _{YX}(-\tau )}}}$

## Time delay analysis

Cross-correlations are useful for determining the time delay between two signals, e.g. for determining time delays for the propagation of acoustic signals across a microphone array.[10][11][clarification needed] After calculating the cross-correlation between the two signals, the maximum (or minimum if the signals are negatively correlated) of the cross-correlation function indicates the point in time where the signals are best aligned; i.e., the time delay between the two signals is determined by the argument of the maximum, or arg max of the cross-correlation, as in

${\displaystyle \tau _{\mathrm {delay} }={\underset {t\in \mathbb {R} }{\operatorname {arg\,max} }}((f\star g)(t))}$

## Terminology in image processing

### Zero-normalized cross-correlation (ZNCC)

For image-processing applications in which the brightness of the image and template can vary due to lighting and exposure conditions, the images can be first normalized. This is typically done at every step by subtracting the mean and dividing by the standard deviation. That is, the cross-correlation of a template, ${\displaystyle t(x,y)}$  with a subimage ${\displaystyle f(x,y)}$  is

${\displaystyle {\frac {1}{n}}\sum _{x,y}{\frac {1}{\sigma _{f}\sigma _{t}}}\left(f(x,y)-\mu _{f}\right)\left(t(x,y)-\mu _{t}\right)}$ .

where ${\displaystyle n}$  is the number of pixels in ${\displaystyle t(x,y)}$  and ${\displaystyle f(x,y)}$ , ${\displaystyle \mu _{f}}$  is the average of ${\displaystyle f}$  and ${\displaystyle \sigma _{f}}$  is standard deviation of ${\displaystyle f}$ .

In functional analysis terms, this can be thought of as the dot product of two normalized vectors. That is, if

${\displaystyle F(x,y)=f(x,y)-\mu _{f}}$

and

${\displaystyle T(x,y)=t(x,y)-\mu _{t}}$

then the above sum is equal to

${\displaystyle \left\langle {\frac {F}{\|F\|}},{\frac {T}{\|T\|}}\right\rangle }$

where ${\displaystyle \langle \cdot ,\cdot \rangle }$  is the inner product and ${\displaystyle \|\cdot \|}$  is the L² norm.

Thus, if ${\displaystyle f}$  and ${\displaystyle t}$  are real matrices, their normalized cross-correlation equals the cosine of the angle between the unit vectors ${\displaystyle F}$  and ${\displaystyle T}$ , being thus ${\displaystyle 1}$  if and only if ${\displaystyle F}$  equals ${\displaystyle T}$  multiplied by a positive scalar.

Normalized correlation is one of the methods used for template matching, a process used for finding incidences of a pattern or object within an image. It is also the 2-dimensional version of Pearson product-moment correlation coefficient.

### Normalized cross-correlation (NCC)

NCC is similar to ZNCC with the only difference of not subtracting the local mean value of intensities:

${\displaystyle {\frac {1}{n}}\sum _{x,y}{\frac {1}{\sigma _{f}\sigma _{t}}}f(x,y)t(x,y)}$

## Nonlinear systems

Caution must be applied when using cross correlation for nonlinear systems. In certain circumstances, which depend on the properties of the input, cross correlation between the input and output of a system with nonlinear dynamics can be completely blind to certain nonlinear effects.[12] This problem arises because some quadratic moments can equal zero and this can incorrectly suggest that there is little "correlation" (in the sense of statistical dependence) between two signals, when in fact the two signals are strongly related by nonlinear dynamics.

## References

1. ^ Bracewell, R. "Pentagram Notation for Cross Correlation." The Fourier Transform and Its Applications. New York: McGraw-Hill, pp. 46 and 243, 1965.
2. ^ Papoulis, A. The Fourier Integral and Its Applications. New York: McGraw-Hill, pp. 244–245 and 252-253, 1962.
3. ^ Weisstein, Eric W. "Cross-Correlation." From MathWorld--A Wolfram Web Resource. http://mathworld.wolfram.com/Cross-Correlation.html
4. ^ Rabiner, L.R.; Schafer, R.W. (1978). Digital Processing of Speech Signals. Signal Processing Series. Upper Saddle River, NJ: Prentice Hall. pp. 147–148. ISBN 0132136031.
5. ^ Rabiner, Lawrence R.; Gold, Bernard (1975). Theory and Application of Digital Signal Processing. Englewood Cliffs, NJ: Prentice-Hall. p. 401. ISBN 0139141014.
6. ^ Campbell; Lo; MacKinlay (1996). The Econometrics of Financial Markets. NJ: Princeton University Press. ISBN 0691043019.
7. ^ Kapinchev; Bradu; Barnes; Podoleanu (2015). "GPU Implementation of Cross-Correlation for Image Generation in Real Time". ICSPCS 2015. doi:10.1109/ICSPCS.2015.7391783.
8. ^ a b c Gubner, John A. (2006). Probability and Random Processes for Electrical and Computer Engineers. Cambridge University Press. ISBN 978-0-521-86470-1.
9. ^ Kun Il Park, Fundamentals of Probability and Stochastic Processes with Applications to Communications, Springer, 2018, 978-3-319-68074-3
10. ^ Rhudy, Matthew; Brian Bucci; Jeffrey Vipperman; Jeffrey Allanach; Bruce Abraham (November 2009). "Microphone Array Analysis Methods Using Cross-Correlations". Proceedings of 2009 ASME International Mechanical Engineering Congress, Lake Buena Vista, FL. doi:10.1115/IMECE2009-10798.
11. ^ Rhudy, Matthew (November 2009). "Real Time Implementation of a Military Impulse Classifier". University of Pittsburgh, Master's Thesis.
12. ^ Billings, S. A. (2013). Nonlinear System Identification: NARMAX Methods in the Time, Frequency, and Spatio-Temporal Domains. Wiley. ISBN 978-1-118-53556-1.