<< moc_unwrap Matlab/Octave Compatibility toolbox moc_xcorr2 >>

Matlab/Octave Compatibility toolbox >> Matlab/Octave Compatibility toolbox > moc_xcorr

moc_xcorr

Compute correlation R_xy of X and Y for various lags k:

Calling Sequence

[R, lag] = moc_xcorr (X [, Y] [, maxlag] [, scale])

Description

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) == moc_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.

unbiased: ( hankel(x(1:k),[x(k:N); zeros(k-1,1)]) * y ) ./ [N:-1:N-k+1](:) biased: ( hankel(x(1:k),[x(k:N); zeros(k-1,1)]) * y ) ./ N

If length(x) == length(y) + k, then you can use the simpler ( hankel(x(1:k),x(k:N-k)) * y ) ./ N

Authors


Report an issue
<< moc_unwrap Matlab/Octave Compatibility toolbox moc_xcorr2 >>