Package: dsp
Crosscorrelation of two inputs
The Crosscorrelator
returns the crosscorrelation
sequence for two discretetime deterministic inputs. This object can
also return the crosscorrelation sequence estimate for two discretetime,
jointly widesense stationary (WSS), random processes.
To obtain the crosscorrelation for two discretetime deterministic inputs:
Define and set up your crosscorrelator. See Construction.
Call step
to compute the crosscorrelation
according to the properties of dsp.Crosscorrelator
.
The behavior of step
is specific to each object in
the toolbox.
H = dsp.Crosscorrelator
returns
a crosscorrelator object, H
, that computes the crosscorrelation
of two inputs. For ND arrays, the crosscorrelator computes the correlation
columnwise. The inputs must have an equal number of columns. If one
input is a vector and the other is an ND array, the crosscorrelator
computes the crosscorrelation of the vector with each column of the
ND array. Cross correlating inputs of length N and M results
in a crosscorrelation sequence of length N+M–1.
Cross correlating matrices of size MbyN and PbyN results
in a matrix of crosscorrelation sequences of size M+P–1byN.
H = dsp.Crosscorrelator('
returns a crosscorrelator, PropertyName
',PropertyValue
,
...)H
, with
each property set to the specified value.

Domain for computing correlations Specify the domain for computing correlation as 
clone  Create crosscorrelator object with same property values 
getNumInputs  Number of expected inputs to step method 
getNumOutputs  Number of outputs of step method 
isLocked  Locked status for input attributes and nontunable properties 
release  Allow property value and input characteristics changes 
step  Crosscorrelation sequence 
The crosscorrelation of two length N deterministic inputs, or realizations of jointly WSS random processes, x and y, is:
$$\begin{array}{l}{r}_{xy}(h)=\{\begin{array}{ll}{\displaystyle \sum _{n=0}^{Nh1}x}(n+h){y}^{*}(n)\hfill & 0\le h\le N1\hfill \\ {r}_{yx}^{*}(h)\hfill & (N1)\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 crosscorrelation:
$${\rho}_{xy}(h)=E\{x(n+h){y}^{*}(n)\}$$
where E{ } is the expectation operator.
Compute correlation between two signals:
hcorr = 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=step(hcorr,x1,x2); %computes crosscorrelation of x1 and x2 figure,plot(t,x1,'b',t, x2, 'g'); legend('Input signal 1',' Input signal 2') figure, plot(y); title('Correlated output')
Use crosscorrelation to detect delay in jointly stationary white Gaussian noise inputs:
S = rng('default'); % white Gaussian noise input x = randn(100,1); % Create copy delayed by 10 samples % x1[n]=x[n10] Hdelay = dsp.Delay(10); x1= step(Hdelay,x); Hxcorr = dsp.Crosscorrelator; y = step(Hxcorr,x1,x); lags = 0:99; %Positive lags stem(lags,y(100:end),'markerfacecolor',[0 0 1]); axis([0 99 125 125]); xlabel('Lags'); title('CrossCorrelation of Input Noise and Delayed Version');
The theoretical crosscorrelation sequence is identically zero except at lag 10. Note this is not true in the sample crosscorrelation sequence, but the estimate demonstrates a peak at the correct lag.
This object implements the algorithm, inputs, and outputs described on the Correlation block reference page. The object properties correspond to the block parameters.