# how does the xcorr fun works and what is the difference between corr and xcorr?

108 views (last 30 days)

Show older comments

##### 0 Comments

### Accepted Answer

William Rose
on 24 May 2022

Edited: William Rose
on 24 May 2022

[edited to crrect typos]

@Matt J's references are exactly right. Here is an alternative, less precise, description of the difference between corr() and xcorr().

corr() computes the correlation, "r" or ρ, that you learnd about in a basic statistics class. It tells you the extent to which two variables vary together. The corr() of two column vectors (which must have equal length) is a single number between -1 and +1. If you have a buch of x-y data points and they all lie exactly on a straight line (with non-zero slope), then corr(x,y) will be 1 or -1, depending on whether the slope is + or -. If you add "noise" to the points, the correlation decreases toward zero, the more noise you add.

xcorr() computes the cross correlation of two signals, which is quite a bit more complicated. For one thing, xcorr(x,y) returns a vector, not a scalar. The vector tells you how correlated x and y are, when there are varying amounts of "horizontal shifting" of the vectors x and y relative to each other. The elements of the vector returned by xcorr(x,y) correspond to diferent amounts of shifting. x and y don't have to have the same length. There are different options for how to normalize the values returned by xcorr(). The corss correlation is very useful for different kinds of signal processing. For example it plays a key role in radar detection and ultrasound imaging. In both cases, you are trying to determine distance to objects by detecting the echos of a signal you send out. Cross correlation of the outgoing and returned signals is a good ways to detect echos.

##### 0 Comments

### More Answers (2)

William Rose
on 24 May 2022

@tony jubran, by default, xcorr() computes the cross correlation at all possible overlap values for two signals. If x and y are vectors of length M and N respectively, then xcorr() returns a vector of length N+M-1. Special case: If x and y have equal length, N, then the vector returned by xcorr() has length 2N-1, and element N (the middle element) of the vector corresponds to full overlap, i.e. zero lag.

x=ones(1,4);

y=xcorr(x,x); disp(y)

1 2 3 4 3 2 1

This is with the default normalization. Hope that helps.

##### 0 Comments

### See Also

### Categories

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!