lag time in matlab

6 ビュー (過去 30 日間)
Susan
Susan 2011 年 8 月 2 日
回答済み: nadom mutlib 2014 年 3 月 16 日
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)]);

回答 (1 件)

nadom mutlib
nadom mutlib 2014 年 3 月 16 日
use cross correlation function it is easier. xcorr(x,y).

カテゴリ

Help Center および File ExchangeLanguage Fundamentals についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by