## How to compute sliding or running window correlation coefficient?

### Kathleen (view profile)

on 3 Apr 2015
Latest activity Answered by David J. Mack

### David J. Mack (view profile)

on 21 Dec 2017
Dear colleagues,
I want to compute the sliding or running window correlation coefficient. I have read related papers, the formula is as following:  t=n,n+1,n+2,n+3，......。 n means the length of silding or running window.
Could you translate this formula into Matlad codes? Any help is very much appreciated! Many many thanks!

Thomas Koelen

on 3 Apr 2015
Roger Stafford

on 3 Apr 2015

on 6 Sep 2016
Edited by Victor

### Victor (view profile)

on 6 Sep 2016

For a fast computation you can implement moving sums of X and X^2 for both signals, then obtain moving averages and variances as
M = sum(X)/windowLen;
V = ( sum(X^2) - sum(X)^2 )/windowLen;
Then find sum
V12 = sum( (X1-M1)*(X2-M2) );
And then sliding correlation itself:
C = V12 / sqrt(V1*V2);
It can be done efficiently within one for loop by adding one new value and substacting the old one.
*The same way we can find statistical moments, by adding moving sums of higher powers - X^3, X^4 etc.