Signal Processing - Cross-Correlation to obtain Lag times.

11 views (last 30 days)
I have written code that produces a graph giving the xcorr coeff of two stochastic electromyogram signals: sEMGL and sEMGbR. % x=(sEMGL); y=(sEMGbR); [c,lags]=xcorr(x,y,'coeff'); figure (8); plot (c,lags) % My question is how to write code in Editor so that in the command window I can type 'time lag =' to obtain this scalar value in milliseconds. A secondary question is to find the derivative of each signal in order to calculate the rate of change in each signal: to evaluate whether the time lag between the signals varies in relation to the rate of change in each signal.
Please help. Kind regards, Trudy

Accepted Answer

Wayne King
Wayne King on 3 Nov 2011
Hi, you need to know the sampling interval of course. Here I assume the sampling interval is 1 msec and delay one signal ten samples, 0.01 seconds.
dt = 1e-3;
x = randn(100,1);
y =[zeros(10,1); x(1:end-10)];
[c,lags] = xcorr(y,x,40,'coeff');
[~,index] = max(abs(c));
fprintf('Time lag = %2.3f seconds\n',lags(index)*dt)

More Answers (1)

Trudy
Trudy on 3 Nov 2011
Hi,thank you for your answer, the sampling frequency was as follows: fs=1000; dt=1/fs; Tg=(length(RGMax)-1)*dt; tm=(0:dt:Tg);
The idea is for the xcorr function to give the time lag that exists when the two signals are maximally correlated. The aim of the analysis is to investigate whether the time lag decreases following a period of motor learning.
How does this relate to the sampling interval?
  1 Comment
Wayne King
Wayne King on 3 Nov 2011
The sampling interval is the inverse of the sampling frequency. So in the example I gave, the sampling frequency is 1000 Hz. Each lag in the cross correlation sequence then corresponds to a time of 1/1000 seconds.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!