Cross-correlation of two inputs

The `dsp.Crosscorrelator`

System
object™ computes the cross-correlation of two *N*-D input arrays along
the first dimension. The computation can be done in the time domain or frequency domain. You
can specify the domain through the Method property. In the time domain, the object convolves
the first input signal, *u*, with the time-reversed complex conjugate of the
second input signal, *v*. To compute the cross-correlation in the frequency
domain, the object:

Takes the Fourier transform of both input signals, resulting in

*U*and*V*.Multiplies

*U*and*V*, where * denotes the complex conjugate.^{*}Computes the inverse Fourier transform of the product.

If you set `Method`

to `'Fastest'`

, the
object chooses the domain that minimizes the number of computations. For information on these
computation methods, see Algorithms.

To obtain the cross-correlation for two discrete-time deterministic inputs:

Create the

`dsp.Crosscorrelator`

object and set its properties.Call the object with arguments, as if it were a function.

To learn more about how System objects work, see What Are System Objects? (MATLAB).

returns a
cross-correlator object, `xcorr`

= dsp.Crosscorrelator`xcorr`

, that computes the cross-correlation of
two inputs in the time domain or frequency domain.

returns a cross-correlator object with each specified property set to the specified value.
Enclose each property name in single quotes.`xcorr`

= dsp.Crosscorrelator(`Name,Value`

)

Unless otherwise indicated, properties are *nontunable*, which means you cannot change their
values after calling the object. Objects lock when you call them, and the
`release`

function unlocks them.

If a property is *tunable*, you can change its value at
any time.

For more information on changing property values, see System Design in MATLAB Using System Objects (MATLAB).

`Method`

— Domain for computing correlations`'Time Domain'`

(default) | `'Frequency Domain'`

| `'Fastest'`

Domain in which the System object computes the correlation, specified as one of the following:

`'Time Domain'`

–– Computes the cross-correlation in the time domain, which minimizes the memory usage.`'Frequency Domain'`

–– Computes the cross-correlation in the frequency domain. For more information, see Algorithms.`'Fastest'`

–– Computes the cross-correlation in the domain that minimizes the number of computations.

To cross-correlate fixed-point signals, set this property to ```
'Time
Domain'
```

.

Fixed-point signals are supported for the time domain only. To use these properties,
set `Method`

to `'Time Domain'`

.

`FullPrecisionOverride`

— Full-precision override for fixed-point arithmetic`true`

(default) | `false`

Flag to use full-precision rules for fixed-point arithmetic, specified as one of the following:

`true`

–– The object computes all internal arithmetic and output data types using the full-precision rules. These rules provide the most accurate fixed-point numerics. In this mode, other fixed-point properties do not apply. No quantization occurs within the object. Bits are added, as needed, to ensure that no roundoff or overflow occurs.`false`

–– Fixed-point data types are controlled through individual fixed-point property settings.

For more information, see Full Precision for Fixed-Point System Objects and Set System Object Fixed-Point Properties.

`RoundingMethod`

— Rounding method for fixed-point operations`'Floor'`

(default) | `'Ceiling'`

| `'Convergent'`

| `'Nearest'`

| `'Round'`

| `'Simplest'`

| `'Zero'`

Rounding method for fixed-point operations. For more details, see rounding mode.

This property is not visible and has no effect on the numerical results when the following conditions are met:

`FullPrecisionOverride`

set to`true`

.`FullPrecisionOverride`

set to`false`

,`OutputDataType`

set to`'Same as accumulator'`

,`ProductDataType`

set to`'Full precision'`

, and`AccumulatorDataType`

set to`'Full precision'`

Under these conditions, the object operates in full precision mode.

In addition, if `Method`

is set to either ```
'Frequency
Domain'
```

or `'Fastest'`

, the
`RoundingMethod`

property does not apply.

`OverflowAction`

— Overflow action for fixed-point operations`'Wrap'`

(default) | `'Saturate'`

Overflow action for fixed-point operations, specified as one of the following:

`'Wrap'`

–– The object wraps the result of its fixed-point operations.`'Saturate'`

–– The object saturates the result of its fixed-point operations.

For more details on overflow actions, see overflow mode for fixed-point operations.

This property is not visible and has no effect on the numerical results when the following conditions are met:

`FullPrecisionOverride`

set to`true`

.`FullPrecisionOverride`

set to`false`

,`OutputDataType`

set to`'Same as accumulator'`

,`ProductDataType`

set to`'Full precision'`

, and`AccumulatorDataType`

set to`'Full precision'`

Under these conditions, the object operates in full precision mode.

In addition, if `Method`

is set to either ```
'Frequency
Domain'
```

or `'Fastest'`

, the
`OverflowAction`

property does not apply.

`ProductDataType`

— Data type of product output`'Full precision'`

(default) | `'Custom'`

| `'Same as first input'`

Data type of the product output in this object, specified as one of the following:

`'Full precision'`

–– The product output data type has full precision.`'Same as first input'`

–– The object specifies the product output data type to be the same as that of the first input data type.`'Custom'`

–– The product output data type is specified as a custom numeric type through the CustomProductDataType property.

For more information on the product output data type, see Multiplication Data Types and the Fixed Point section.

This property applies when you set `FullPrecisionOverride`

to
`false`

.

`CustomProductDataType`

— Word and fraction lengths of product data type`numerictype([],32,30)`

(default)Word and fraction lengths of the product data type, specified as an autosigned numeric type with a word length of 32 and a fraction length of 30.

This property applies only when you set
`FullPrecisionOverride`

to `false`

and ProductDataType to
`'Custom'`

.

`AccumulatorDataType`

— Data type of accumulation operation`'Full precision'`

(default) | `'Same as first input'`

| `'Same as product'`

| `'Custom'`

Data type of an accumulation operation in this object, specified as one of the following:

`'Full precision'`

–– The accumulation operation has full precision.`'Same as product'`

–– The object specifies the accumulator data type to be the same as that of the product output data type.`'Same as first input'`

–– The object specifies the accumulator data type to be the same as that of the first input data type.`'Custom'`

–– The accumulator data type is specified as a custom numeric type through the CustomAccumulatorDataType property.

For more information on the accumulator data type this object uses, see the Fixed Point section.

This property applies when you set `FullPrecisionOverride`

to
`false`

.

`CustomAccumulatorDataType`

— Word and fraction lengths of accumulator data type`numerictype([],32,30)`

(default)Word and fraction lengths of the accumulator data type, specified as an autosigned numeric type with a word length of 32 and a fraction length of 30.

This property applies only when you set
`FullPrecisionOverride`

to `false`

and AccumulatorDataType to
`'Custom'`

.

`OutputDataType`

— Data type of object output`'Same as accumulator'`

(default) | `'Same as first input'`

| `'Same as product'`

| `'Custom'`

Data type of the object output, specified as one of the following:

`'Same as accumulator'`

–– The output data type is the same as that of the accumulator output data type.`'Same as first input'`

–– The output data type is the same as that of the first input data type.`'Same as product'`

–– The output data type is the same as that of the product output data type.`'Custom'`

–– The output data type is specified as a custom numeric type through the CustomOutputDataType property.

For more information on the output data type this object uses, see the Fixed Point section.

This property applies when you set `FullPrecisionOverride`

to
`false`

.

`CustomOutputDataType`

— Word and fraction lengths of output data type`numerictype([],16,15)`

(default)Word and fraction lengths of the output data type, specified as an autosigned numeric type with a word length of 16 and a fraction length of 15.

This property applies only when you set
`FullPrecisionOverride`

to `false`

and OutputDataType to
`'Custom'`

.

`u`

— First data input signalvector | matrix |

First data input signal, specified as a vector, matrix, or an
*N*-D array. The object accepts real-valued or complex-valued
multichannel and multidimensional inputs. The input can be a fixed-point signal when
you set the `Method`

property to `'Time Domain'`

.
When one or both of the input signals are complex, the output signal is also complex.
Both data inputs must have the same data type.

**Data Types: **`single`

| `double`

| `int8`

| `int16`

| `int32`

| `int64`

| `uint8`

| `uint16`

| `uint32`

| `uint64`

| `fi`

**Complex Number Support: **Yes

`v`

— Second data input signalscalar | column vector | matrix

Second data input signal, specified as a vector, matrix, or an
*N*-D array. The object accepts real-valued or complex-valued
multichannel and multidimensional inputs. The input can be a fixed-point signal when
you set the `Method`

property to `'Time Domain'`

.
When one or both of the input signals are complex, the output signal is also complex.
Both data inputs must have the same data type.

**Data Types: **`single`

| `double`

| `int8`

| `int16`

| `int32`

| `int64`

| `uint8`

| `uint16`

| `uint32`

| `uint64`

| `fi`

**Complex Number Support: **Yes

`y`

— Cross-correlated outputvector | matrix |

Cross-correlated output of the two input signals.

When the inputs are *N*-D arrays, the object outputs an
*N*-D array. All the dimensions of the output array, except for the
first dimension, match the input array. For example:

When the inputs

*u*and*v*have dimensions*M*-by-_{u}*N*-by-*P*and*M*-by-_{v}*N*-by-*P*, respectively, the object outputs an (*M*+_{u}*M*– 1)-by-_{v}*N*-by-*P*array.When the inputs

*u*and*v*have the dimensions*M*-by-_{u}*N*and*M*-by-_{v}*N*, the object outputs an (*M*+_{u}*M*– 1)-by-_{v}*N*matrix.

If one input is a column vector and the other input is an *N*-D
array, the object computes the cross-correlation of the vector with each column in the
*N*-D array. For example:

When the input

*u*is an*M*-by-1 column vector and_{u}*v*is an*M*-by-_{v}*N*matrix, the object outputs an (*M*+_{u}*M*– 1)-by-_{v}*N*matrix.Similarly, when

*u*and*v*are column vectors with lengths*M*and_{u}*M*, respectively, the object performs the vector cross-correlation._{v}

**Data Types: **`single`

| `double`

| `int8`

| `int16`

| `int32`

| `int64`

| `uint8`

| `uint16`

| `uint32`

| `uint64`

| `fi`

**Complex Number Support: **Yes

To use an object function, specify the
System
object as the first input argument. For
example, to release system resources of a System
object named `obj`

, use
this syntax:

release(obj)

**Note**: If you are using R2016a or an earlier release, replace each call to the object with the equivalent step syntax. For example, `obj(x)`

becomes `step(obj,x)`

.

Compute the cross-correlation between two sinusoidal signals using the `dsp.Crosscorrelator`

object. Perform the computation in the time domain, which is the object's default setting.

xcorr = dsp.Crosscorrelator; t = 0:0.001:1; x1 = sin(2*pi*2*t)+0.05*sin(2*pi*50*t); x2 = sin(2*pi*2*t); y = xcorr(x1,x2); figure plot(t,x1,'b',t,x2,'g') xlabel('Time') ylabel('Signal Amplitude') legend('Input signal 1','Input signal 2')

Plot the correlated output.

```
figure,
plot(y)
title('Correlated output')
```

**Note**: If you are using R2016a or an earlier release, replace each call to the object with the equivalent step syntax. For example, `obj(x)`

becomes `step(obj,x)`

.

Compute the cross-correlation of a noisy input signal with its delayed version. The peak of the correlation output occurs at the lag, which corresponds to the delay between the signals.

Use `randn`

to create the white Gaussian noisy input, `x`

. Create a delayed version of this input, `x1`

, using the `dsp.Delay`

object.

```
S = rng('default');
x = randn(100,1);
delay = dsp.Delay(10);
x1 = delay(x);
```

Compute the cross-correlation between the two inputs. Plot the correlation output with respect to the lag between the inputs.

xcorr = dsp.Crosscorrelator; y = xcorr(x1,x); lags = 0:99; stem(lags,y(100:end),'markerfacecolor',[0 0 1]) axis([0 99 -125 125]) xlabel('Lags') title('Cross-Correlation of Input Noise and Delayed Version')

The correlation sequence peaks when the lag is 10, indicating that the correct delay between the two signals is 10 samples.

Cross-correlation is the measure of similarity of two discrete-time sequences as a function of the lag of one relative to the other.

For two length-*N* deterministic inputs or realizations of jointly wide-sense
stationary (WSS) random processes, *x* and *y*, the
cross-correlation is computed using the following relationship:

$$\begin{array}{l}{r}_{xy}(h)=\{\begin{array}{ll}{\displaystyle \sum _{n=0}^{N-h-1}x}(n+h){y}^{*}(n)\hfill & 0\le h\le N-1\hfill \\ {r}_{yx}^{*}(h)\hfill & -(N-1)\le h\le 0\hfill \end{array}\\ \end{array}$$

where *h* is the lag and ***
denotes the complex conjugate. If the inputs are realizations of jointly WSS stationary
random processes, r_{xy}(h) is an unnormalized estimate of the theoretical
cross-correlation:

$${\rho}_{xy}(h)=E\{x(n+h){y}^{*}(n)\}$$

where E{ } is the expectation operator.

When you set the computation domain to time, the algorithm computes the cross-correlation of two signals in the time domain. The input signals can be fixed-point signals in this domain.

**Correlate Two 2-D Arrays**

When the inputs are two 2-D arrays, the *j*th column of the output,
*y _{uv}*, has these elements:

$$\begin{array}{l}{y}_{uv(i,j)}={\displaystyle \sum _{k=0}^{\mathrm{max}({M}_{u},{M}_{v})-1}{u}_{k,j}^{*}{v}_{(k+i),j}^{}}\text{}0\le i{M}_{v}\\ \\ {y}_{uv(i,j)}={y}_{vu(-i,j)}^{*}\text{}-{M}_{u}i0\end{array}$$

where:

`*`

denotes the complex conjugate.*u*is an*M*-by-_{u}*N*input matrix.*v*is an*M*-by-_{v}*N*input matrix.*y*is an (_{u,v}*M*+_{u}*M*– 1)-by-_{v}*N*matrix.

Inputs *u* and *v* are zero
when indexed outside their valid ranges.

**Correlate a Column Vector with a 2-D Array**

When one input is a column vector and the other input is a 2-D
array, the algorithm independently cross-correlates the input vector
with each column of the 2-D array. The *j*th column
of the output, *y _{u,v}*, has
these elements:

$$\begin{array}{l}{y}_{uv(i,j)}={\displaystyle \sum _{k=0}^{\mathrm{max}({M}_{u},{M}_{v})-1}{u}_{k}^{*}{v}_{(k+i),j}^{}}\text{}0\le i{M}_{v}\\ \\ {y}_{uv(i,j)}={y}_{vu(-i,j)}^{*}\text{}-{M}_{u}i0\end{array}$$

where:

`*`

denotes the complex conjugate.*u*is an*M*-by-1 column vector._{u}*v*is an*M*-by-_{v}*N*matrix.*y*is an (_{uv}*M*+_{u}*M*– 1)-by-_{v}*N*matrix.

Inputs *u* and *v* are zero when
indexed outside their valid ranges.

**Correlate Two Column Vectors**

When the inputs are two column vectors, the *j*th column of the output,
*y _{uv}*, has these elements:

$$\begin{array}{l}{y}_{uv(i)}={\displaystyle \sum _{k=0}^{\mathrm{max}({M}_{u},{M}_{v})-1}{u}_{k}^{*}{v}_{(k+i)}^{}}\text{}0\le i{M}_{v}\\ \\ {y}_{uv(i)}={y}_{vu(-i)}^{*}\text{}-{M}_{u}i0\end{array}$$

where:

`*`

denotes the complex conjugate.*u*is an*M*-by-1 column vector._{u}*v*is an*M*-by-1 column vector._{v}*y*is an (_{uv}*M*+_{u}*M*– 1)-by-1 column vector._{v}

Inputs *u* and *v* are zero when
indexed outside their valid ranges.

When you set the computation domain to frequency, the algorithm computes the cross-correlation in the frequency domain.

To compute the cross-correlation, the algorithm:

Takes the Fourier transform of both input signals,

*U*and*V*.Multiplies

*U*and*V*, where * denotes the complex conjugate.^{*}Computes the inverse Fourier transform of the product.

In this domain, depending on the input length, the algorithm can require fewer computations.

Generate C and C++ code using MATLAB® Coder™.

Usage notes and limitations:

See System Objects in MATLAB Code Generation (MATLAB Coder).

Design and simulate fixed-point systems using Fixed-Point Designer™.

The diagram shows the data types the `dsp.Crosscorrelator`

object uses
for fixed-point signals (time domain only).

You can set the product output, accumulator, and output data types using the corresponding fixed-point properties of the object.

When the input is real, the output of the multiplier is in the product output data type. When the input is complex, the output of the multiplier is in the accumulator data type. For details on the complex multiplication performed, see Multiplication Data Types.

When one or both of the inputs are signed fixed-point signals, all internal object data types are signed fixed point. The internal object data types are unsigned fixed point only when both inputs are unsigned fixed-point signals.

A modified version of this example exists on your system. Do you want to open this version instead?

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

Select web siteYou can also select a web site from the following list:

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

- América Latina (Español)
- Canada (English)
- United States (English)

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)