%% Cross-Correlation of Input Noise and Delayed Version
% 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.
% Copyright 2012 The MathWorks, Inc.