You are here : matlab → Signal Processing → xcorr2
# xcorr2() - Signal Processing

### Syntax

### Example

### Output / Return Value

### Limitations

### Alternatives / See Also

### Reference

C = xcorr2(A,B) returns the cross-correlation of matrices A and B with no scaling. xcorr2 is the two-dimensional version of xcorr.C = xcorr2(A) is the autocorrelation matrix of input matrix A. It is identical to xcorr2(A,A).C = xcorr2(gpuArrayA,gpuArrayB) returns the cross-correlation of two matrices of class gpuArray. The output cross-correlation matrix, C, is also a gpuArray object. See Establish Arrays on a GPU for details on gpuArray objects.Note: Using xcorr2 with gpuArray objects requires Parallel Computing Toolbox™ software and a CUDA-enabled NVIDIA GPU with compute capability 1.3 or above. See System Requirements for details.

C = xcorr2(A,B)C = xcorr2(A)C = xcorr2(gpuArrayA,gpuArrayB)

Output Matrix Size and Element ComputationOpen This Example Create two matrices, M1 and M2.M1 = [17 24 1 8 15; 23 5 7 14 16; 4 6 13 20 22; 10 12 19 21 3; 11 18 25 2 9]; M2 = [8 1 6; 3 5 7; 4 9 2]; M1 is 5-by-5 and M2 is 3-by-3, so their cross-correlation has size (5+3-1)-by-(5+3-1) or 7-by-7. In terms of lags, the resulting matrix is As an example, compute the element (or C(3,5) in MATLAB®). Line up the two matrices so their (1,1) elements coincide. Because M2 is 3-by-3, the sum of the element-by-element products corresponds to . To find , slide M2 two rows to the right. Now M2 is on top of the matrix M1(1:3,3:5). Compute the element-by-element products and sum them. The answer should be [r2,c2] = size(M2); CC = sum(sum(M1(-2+r2+(0:r2-1),0+c2+(0:c2-1)).*M2)) CC = 585 Verify the result using xcorr2.D = xcorr2(M1,M2); [r1,c1] = size(M1); DD = D(-2+r1,0+c1) DD = 585 Two-Dimensional Cross-Correlation of Arbitrary Complex MatricesOpen This Example Given a matrix of size and a matrix of size , their two-dimensional cross-correlation, , is a matrix of size with elements is the trace and the dagger denotes Hermitian conjugation. The matrices and have size and nonzero elements given by and Calling xcorr2 is equivalent to this procedure for general complex matrices of arbitrary size. Create two complex matrices, of size and of size .X = randn([7 22])+1j*randn([7 22]); H = randn([6 17])+1j*randn([6 17]); [M,N] = size(X); m = 1:M; n = 1:N; [P,Q] = size(H); p = 1:P; q = 1:Q; Initialize and .Xt = zeros([M+2*(P-1) N+2*(Q-1)]); Xt(m+P-1,n+Q-1) = X; C = zeros([M+P-1 N+Q-1]); Compute the elements of by looping over and . Reset to zero at each step. Save time and memory by summing element products instead of multiplying and taking the trace.for k = 1:M+P-1 for l = 1:N+Q-1 Hkl = zeros([M+2*(P-1) N+2*(Q-1)]); Hkl(p+k-1,q+l-1) = H; C(k,l) = sum(sum(Xt.*conj(Hkl))); end end max(max(abs(C-xcorr2(X,H)))) ans = 2.9192e-14 The answer coincides to machine precision with the output of xcorr2.Align Two Images Using Cross-CorrelationOpen This Example Use cross-correlation to find where a section of an image fits in the whole. Cross-correlation enables you to find the regions in which two signals most resemble each other. For two-dimensional signals, like images, use xcorr2. Load a black-and-white test image into the workspace. Display it with imagesc.load durer img = X; White = max(max(img)); imagesc(img) axis image off colormap gray title('Original') Select a rectangular section of the image. Display the larger image with the section missing.x = 435; X = 535; szx = x:X; y = 62; Y = 182; szy = y:Y; Sect = img(szx,szy); kimg = img; kimg(szx,szy) = White; kumg = White*ones(size(img)); kumg(szx,szy) = Sect; subplot(1,2,1) imagesc(kimg) axis image off colormap gray title('Image') subplot(1,2,2) imagesc(kumg) axis image off colormap gray title('Section') Use xcorr2 to find where the small image fits in the larger image. Subtract the mean value so that there are roughly equal numbers of negative and positive values.nimg = img-mean(mean(img)); nSec = nimg(szx,szy); crr = xcorr2(nimg,nSec); The maximum of the cross-correlation corresponds to the estimated location of the lower-right corner of the section. Use ind2sub to convert the one-dimensional location of the maximum to two-dimensional coordinates.[ssr,snd] = max(crr(:)); [ij,ji] = ind2sub(size(crr),snd); figure plot(crr(:)) title('Cross-Correlation') hold on plot(snd,ssr,'or') hold off text(snd*1.05,ssr,'Maximum') Place the smaller image inside the larger image. Rotate the smaller image to comply with the convention that MATLAB® uses to display images. Draw a rectangle around it.img(ij:-1:ij-size(Sect,1)+1,ji:-1:ji-size(Sect,2)+1) = rot90(Sect,2); imagesc(img) axis image off colormap gray title('Reconstructed') hold on plot([y y Y Y y],[x X X x x],'r') hold off Recovery of Template Shift with Cross-CorrelationOpen This Example Shift a template by a known amount and recover the shift using cross-correlation. Create a template in an 11-by-11 matrix. Create a 22-by-22 matrix and shift the original template by 8 along the row dimension and 6 along the column dimension.template = 0.2*ones(11); template(6,3:9) = 0.6; template(3:9,6) = 0.6; offsetTemplate = 0.2*ones(22); offset = [8 6]; offsetTemplate((1:size(template,1))+offset(1), ... (1:size(template,2))+offset(2)) = template; Plot the original and shifted templates.imagesc(offsetTemplate) colormap gray hold on imagesc(template) axis equal Cross-correlate the two matrices and find the maximum absolute value of the cross-correlation. Use the position of the maximum absolute value to determine the shift in the template. Check the result against the known shift.cc = xcorr2(offsetTemplate,template); [max_cc, imax] = max(abs(cc(:))); [ypeak, xpeak] = ind2sub(size(cc),imax(1)); corr_offset = [(ypeak-size(template,1)) (xpeak-size(template,2))]; isequal(corr_offset,offset) ans = 1 The shift obtained from the cross-correlation equals the known template shift in the row and column dimensions.GPU Acceleration for Cross-Correlation Matrix Computation This example requires Parallel Computing Toolbox software and a CUDA-enabled NVIDIA GPU with compute capability 1.3 or above. Shift a template by a known amount and recover the shift using cross-correlation. Create a template in an 11-by-11 matrix. Create a 22-by-22 matrix and shift the original template by 8 along the row dimension and 6 along the column dimension.template = 0.2*ones(11); template(6,3:9) = 0.6; template(3:9,6) = 0.6; offsetTemplate = 0.2*ones(22); offset = [8 6]; offsetTemplate((1:size(template,1))+offset(1),... (1:size(template,2))+offset(2)) = template;Put the original and shifted template matrices on your GPU using gpuArray objects. template = gpuArray(template); offsetTemplate = gpuArray(offsetTemplate);Compute the cross-correlation on the GPU.cc = xcorr2(offsetTemplate,template);Return the result to the MATLAB® workspace using gather. Use the maximum absolute value of the cross-correlation to determine the shift, and compare the result with the known shift.cc = gather(cc); [max_cc, imax] = max(abs(cc(:))); [ypeak, xpeak] = ind2sub(size(cc),imax(1)); corr_offset = [ (ypeak-size(template,1)) (xpeak-size(template,2)) ]; isequal(corr_offset,offset)