Reading time: 7 min

29 /3/2019

Time Shift Cross Correlation of Two Time Series Matlab

One of the most basic techniques to quantify the changes between two time series is to determine the cross correlation index (CCI). In the context of damage detection, if you have a reference signal (baseline) and some time later you obtain a new signal from the same source/transmitter arrangement, you are able to quantify changes in the signal. Out of the box application for cross correlation takes two inputs and returns one output which is the index of similarity, namely (CCI).

If you’re doing research, it may be a good idea to stay away from basic techniques as they are not state of the art. However, for this post, I will demonstrate a slightly improved algorithm for cross correlation to uncover temporal variations in the time series data, called Time Shift Cross Correlation or Local Peak Coherence.

Instead of using the entire signal as an input for cross correlation, the signal is segmented into N windows with length T. Therefore, by calculating the CCI for each time window, you will obtain CCI with respect to time. It may be useful if you’re trying to detect irregularities in certain section of a signal.

The cross-correlation with window length T and at particular time is given by:

where omega(t) is a rectangular window, t is time, tau is delay.

The corresponding estimate of time windowed correlation coefficient function or normalised cross correlation is:

If the correlation index is 1, there is no change, corresponding to no damage has occured, and if it’s below 1, some changes in the medium has occured. For application in damage detection or structural health monitoring, even with no damage, it is unlikely you will ever get a CCI of 1 due to temperature, loading and moisture variations in the structure. You could either apply compensation algorithms or apply a threshold level.

Now on to writing the time shift cross correlation algorithm in Matlab.

function [corr, delay, t_interval] = localcoherence(s1,s2,t_win,dt)
%This function takes in 5 inputs: S1 = Signal 1, S2 = Signal 2,
%t_win = time window, dt = sampling time
if nargin~=4
error('4 inputs required');
s1_length = length(s1);
s2_length = length(s2);
if s1_length ~= s2_length
if s1_length > s2_length
s2 = [s2; zeros((s2_length-s1_length),1)];
s1= [s1; zeros((s1_length-s2_length),1)];
N = s1_length;
%initalise variables
k = 1;
R_vec = zeros(1,floor(N/t_win));
lag_vec = zeros(1,floor(N/t_win));
t_init = t_win/2:t_win:N;

for t=1:t_win:N
if t+t_win < N
s1_temp = s1(t:t+t_win-1);
s2_temp = s2(t:t+t_win-1);
[R, lags]=xcorr(s2_temp,s1_temp,'coeff'); %normalised cross correlation/ local temporal coherence
[vm, im]=max(R); %max. correlation and index
s1_lag=lags(im); %delay at max correlation
%store R and lags into vector
R_vec(k) = vm;
lag_vec(k) = s1_lag;
%increment store vector
k = k +1;

corr = R_vec;
delay = lag_vec*dt;
t_interval = t_init*dt;


Putting the Matlab function into action. Let’s create two sinusodial waves.

t = 0:1e-8:0.001; f1 = 5000; f2 = 10000;
s1 = sin(2*pi*f1*t);
s2 = sin(2*pi*f2*t);
t_win = 1000;
dt = 1e-8;
[corr, delay, t_interval] = localcoherence(s1,s2,t_win,dt);

Plotting time vs correlation index.

plot(t,s1,'LineWidth',1.5); hold on; plot(t,s2,'LineWidth',1.5);
xlabel('Time (s)');
plot(t_interval, corr,'LineWidth',2);
xlabel('Time (s)');
ylabel('CC Index');

There you have it, a Time Shift Cross Correlation/Normalised Cross Correlation/Peak Coherence algorithm. I also have a python version of this algorithm in which I’ll post if requested.

Feel free contact me, if you have found a bug in the code.


Submit a form to get to touch