lag time in matlab

9 views (last 30 days)
Susan
Susan on 2 Aug 2011
Answered: nadom mutlib on 16 Mar 2014
hey, I researched how to find the lag time in Matlab and I found a code that worked for the online example, but did not calculate the correct lag in mine.. I have two signal that are 2x100 size both and one is delayed version of other. The code though its complicated a bit, Is there a quicker way in Matlab to calculate the lag time, any in-built function?
mxlag=100; % Make a guess
R=zeros(2*mxlag+1,1);
for ilag=1:mxlag
t1=detrend(X(1:end-ilag),0); % Remove mean
t2=detrend(X1(ilag+1:end),0);
R(ilag+mxlag+1)=mean(t1.*t2)/(std(t1)*std(t2));
t1=detrend(X1(1:end-ilag),0);
t2=detrend(X(ilag+1:end),0);
R(mxlag+1-ilag)=mean(t1.*t2)/(std(t1)*std(t2));
end
% Zero lag
t1=detrend(X,0);
t2=detrend(X1,0);
R(mxlag+1)=mean(t1.*t2)/(std(t1)*std(t2));
lag=[-mxlag:mxlag]';
[Rmx,imx]=max(R);
num2str(lag(imx)*pi);
disp(['The calculated lag is: ' num2str(lag(imx)*pi)]);

Answers (1)

nadom mutlib
nadom mutlib on 16 Mar 2014
use cross correlation function it is easier. xcorr(x,y).

Categories

Find more on Language Fundamentals in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!