Obsidian | Level 7

## Using Cross Correlation to determine time delay between two signals

What I have is two signals in time (x=time in seconds, y=Force) and they are lagged (see graph bellow). The lag is unknown to me and I have more than 5000 of this pair of curves. I need to align all this pairs or curves to enable other posterior analysis.

To do that, I would normally use the cross correlation method to find the the point in time where the two signals are best aligned in seconds . After I have the this optimal set point, I can use this number to correct one of the time series and make them match.

The problem is that I never did this in sas and I was wondering if there is a procedure that I can use or I need to code this in sas base from scratch myself. I google quite a bit and I'm not sure if what I call a cross correlation could be done with proc timeseries, or proc ARIMA in EG or even with some TS node in EM.

I would take any recommendation regardless of it is the name of a procedure, a sas base program, or an em node.

Thanks!!

1 ACCEPTED SOLUTION

Accepted Solutions
SAS Employee

## Re: Using Cross Correlation to determine time delay between two signals

You can use PROC TIMESERIES to compute crosscorrelations. You need to use the CROSSCORR and CROSSVAR statements. Here is simple example.

``````data temp;
call streaminit(123);
do i=1 to 103;
x = rand("Uniform");
y = lag3(x);
if i >  3 then output;
end;
drop i;
run;

proc timeseries data = temp crossplot=all outcrosscorr=outcrosscorr;
crosscorr lag n ccov ccf ccfstd;
var x;
crossvar y;
run;``````

I am not sure exactly what the purpose of your analysis is, but you may want to look also at the SIMILARITY procedure. It allows you to compute a distance matrix between series using time warping rather than cross-correlation.

6 REPLIES 6
Obsidian | Level 7

## Re: Using Cross Correlation to determine time delay between two signals

You can call PROC TIMESERIES to compute cross correlation of two time series. See the following for a simple code example:

`proc timeseries data=sashelp.pricedata out=_NULL_ outcrosscorr=crosscorr; crosscorr _ALL_ / nlags=12; id date interval=month; var sale price; by region line product;run;`

You can also check http://support.sas.com/documentation/cdl/en/etsug/66840/HTML/default/viewer.htm#etsug_timeseries_syn... for more details on the PROC and related functionalities.

SAS Employee

## Re: Using Cross Correlation to determine time delay between two signals

You can use PROC TIMESERIES to compute crosscorrelations. You need to use the CROSSCORR and CROSSVAR statements. Here is simple example.

``````data temp;
call streaminit(123);
do i=1 to 103;
x = rand("Uniform");
y = lag3(x);
if i >  3 then output;
end;
drop i;
run;

proc timeseries data = temp crossplot=all outcrosscorr=outcrosscorr;
crosscorr lag n ccov ccf ccfstd;
var x;
crossvar y;
run;``````

I am not sure exactly what the purpose of your analysis is, but you may want to look also at the SIMILARITY procedure. It allows you to compute a distance matrix between series using time warping rather than cross-correlation.

Obsidian | Level 7

## Re: Using Cross Correlation to determine time delay between two signals

Thanks for that! I also thought about DTW but it does not fit to what I need to do afterwards with the data.

SAS Employee

## Re: Using Cross Correlation to determine time delay between two signals

BTW, I forget to mention that if you use SAS Studio, the forecasting tasks provide an easy point-and-click interface that generates code for this type of analysis and much more.

I should put this sentence as my signature

SAS Employee

SAS Employee

## Re: Using Cross Correlation to determine time delay between two signals

Let's see how my signature looks now.

Discussion stats
• 6 replies
• 3164 views
• 1 like
• 3 in conversation