To demonstrate the mechanics of correlation, we are going to calculate the convolution of two small signals by hand
first signal 1 2 3 second signal 2 1 3First of all we subtract the mean of both signals.
first signal (minus mean) -1 0 1 second signal (minus mean) 0 -1 1Then we have to displace progressively the second signal and to add the product of both signals
first signal (minus mean) -1 0 1 second signal (minus mean) 0 -1 1 product 0 0 1 sum 1 <--------------- first signal (minus mean) -1 0 1 second signal (minus mean) 0 -1 1 product 1 0 sum 1 <--------------- first signal (minus mean) -1 0 1 second signal (minus mean) 0 -1 1 product -1 sum -1 <---------------And finally we divide the result by the length of the array
previous result 1 1 -1 divided by 3 (end result) 1/3 1/3 -1/3We can check the previous result with the next code
-->a = [1 2 3] a = ! 1. 2. 3. ! -->b = [2 1 3 ] b = ! 2. 1. 3. ! -->corr(a,b,3) ans = ! 0.3333333 0.3333333 - 0.3333333 !We have to notice that correlation is not commutative
-->corr(a,b,3) ans = ! 0.3333333 0.3333333 - 0.3333333 ! -->corr(b,a,3) ans = ! 0.3333333 - 0.3333333 0. !