


% error: [R, lag] = xcorr (X [, Y] [, maxlag] [, scale])
%
% Compute correlation R_xy of X and Y for various lags k:
%
% R_xy(k) = sum_{i=1}^{N-k}{x_i y_{i-k}}/(N-k), for k >= 0
% R_xy(k) = R_yx(-k), for k <= 0
%
% Returns R(k+maxlag+1)=Rxy(k) for lag k=[-maxlag:maxlag].
% Scale is one of:
% 'biased' for correlation=raw/N,
% 'unbiased' for correlation=raw/(N-|lag|),
% 'coeff' for correlation=raw/(rms(x).rms(y)),
% 'none' for correlation=raw
%
% If Y is omitted, compute autocorrelation.
% If maxlag is omitted, use N-1 where N=max(length(X),length(Y)).
% If scale is omitted, use 'none'.
%
% If X is a matrix, computes the cross correlation of each column
% against every other column for every lag. The resulting matrix has
% 2*maxlag+1 rows and P^2 columns where P is columns(X). That is,
% R(k+maxlag+1,P*(i-1)+j) == Rij(k) for lag k=[-maxlag:maxlag],
% so
% R(:,P*(i-1)+j) == xcorr(X(:,i),X(:,j))
% and
% reshape(R(k,:),P,P) is the cross-correlation matrix for X(k,:).
%
% xcorr computes the cross correlation using an FFT, so the cost is
% dependent on the length N of the vectors and independent of the
% number of lags k that you need. If you only need lags 0:k-1 for
% vectors x and y, then the direct sum may be faster:
%
% Ref: Stearns, SD and David, RA (1988). Signal Processing Algorithms.
% New Jersey: Prentice-Hall.