100%(1)1 out of 1 people found this document helpful
This preview shows page 730 - 733 out of 780 pages.
In all cases, the cross-correlation or autocorrelation computed by xcorrhas the 0-th lag in the middle of the sequence, at element or row maxlags+1(element or row Nif maxlagsis not specified).ExamplesThe second output, lags, is useful for plotting the cross-correlation or autocorrelation. For example, the estimated autocorrelation of zero-mean Gaussian white noise cww(m) can be displayed for -10 ≤m≤10 usingww = randn(1000,1);[c_ww,lags] = xcorr(ww,10,’coeff’);stem(lags,c_ww)Swapping the xand yinput arguments reverses (and conjugates) the output correlation sequence. For row vectors, the resulting sequences are reversed left to right; for column vectors, up and down. The following example illustrates this property (mat2stris used for a compact display of complex numbers).x=[1,2i,3]; y= [4,5,6];[c1,lags] = xcorr(x,y);c1=mat2str(c1,2), lagsc1 =[12-i*8.9e-016 15-i*8 22-i*10 5-i*12 6+i*8.9e-016]lags =-2-1 0 1 2c2 = conj(fliplr(xcorr(y,x)));c2 = mat2str(c2,2)c2 =[12-i*8.9e-016 15-i*8 22-i*10 5-i*12 6+i*8.9e-016]For the case where input argument xis a matrix, the output columns are arranged so that extracting a row and rearranging it into a square array
xcorr0-419produces the cross-correlation matrix corresponding to the lag of the chosen row. For example, the cross-correlation at zero lag can be retrieved byrandn(’seed’,0)X = randn(2,2);[M,P] = size(X);c = xcorr(X);c0=zeros(P); c0(:)= c(M,:) % Extract zero-lag rowc0 =1.7500 0.30790.3079 0.1293You can calculate the matrix of correlation coefficients that the MATLAB function corrcoefgenerates by substituting c = xcov(X,’coef’)in the last example. The function xcovsubtracts the mean and then calls xcorr.Use fftshiftto move the second half of the sequence starting at the zeroth lag to the front of the sequence. fftshiftswaps the first and second halves of a sequence.AlgorithmFor more information on estimating covariance and correlation functions, see  and .DiagnosticsThere must be at least one vector input argument; otherwise, xcorrgives the following error message.1st arg must be a vector or matrix.The string ’option’must be the last argument; otherwise, xcorrgives the following error message.Argument list not in correct order.If the second argument was entered as a scalar, it is taken to be maxlagsand no succeeding input can be a scalar. When the second argument is a vector, the first must also be a signal vector. The third argument, when present, must be
xcorr0-420a scalar or a string. If they are not, xcorrgives the appropriate error message(s).3rd arg is maxlag, 2nd arg cannot be scalar.When b is a vector, a must be a vector.Maxlag must be a scalar.Normally the lengths of the vector inputs should be the same; if they are not, then the only allowable scaling option is ’none’. If it is not, xcorrgives the following error message.OPTION must be ’none’ for different length vectors A and B.See AlsoReferences Bendat, J.S., and A.G. Piersol, Random Data: Analysis and Measurement Procedures, John Wiley & Sons, New York, 1971, p. 332.