lag time in matlab
9 views (last 30 days)
Show older comments
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)]);
0 Comments
Answers (1)
See Also
Categories
Find more on Language Fundamentals in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!