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?.
returns a
cross-correlator object, xcorr
= dsp.Crosscorrelatorxcorr
, 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.
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'
.
Note
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 arithmetictrue
(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 typenumerictype([],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 typenumerictype([],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 typenumerictype([],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 signalFirst 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 signalSecond 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 outputCross-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 Mu-by-N-by-P and Mv-by-N-by-P, respectively, the object outputs an (Mu + Mv – 1)-by-N-by-P array.
When the inputs u and v have the dimensions Mu-by-N and Mv-by-N, the object outputs an (Mu + Mv – 1)-by-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 Mu-by-1 column vector and v is an Mv-by-N matrix, the object outputs an (Mu + Mv – 1)-by-N matrix.
Similarly, when u and v are column vectors with lengths Mu and Mv, respectively, the object performs the vector cross-correlation.
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:
where h is the lag and * denotes the complex conjugate. If the inputs are realizations of jointly WSS stationary random processes, rxy(h) is an unnormalized estimate of the theoretical cross-correlation:
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 jth column of the output, yuv, has these elements:
where:
*
denotes the complex conjugate.
u is an Mu-by-N input matrix.
v is an Mv-by-N input matrix.
yu,v is an (Mu + Mv – 1)-by-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 jth column of the output, yu,v, has these elements:
where:
*
denotes the complex conjugate.
u is an Mu-by-1 column vector.
v is an Mv-by-N matrix.
yuv is an (Mu + Mv – 1)-by-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 jth column of the output, yuv, has these elements:
where:
*
denotes the complex conjugate.
u is an Mu-by-1 column vector.
v is an Mv-by-1 column vector.
yuv is an (Mu + Mv – 1)-by-1 column vector.
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.
Usage notes and limitations:
See System Objects in MATLAB Code Generation (MATLAB Coder).
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.
Note
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.
You have a modified version of this example. Do you want to open this example with your edits?