这里给出一个例子,是由2个正弦信号构成的信号,x2对x1超前了35个样点,用求相关函数的方法求2信号之间的延迟。程序为:
N=1024;
fs=1000; f1=5; f2=20;
n=1:N; t=(n-1)/fs; t1=(n+34)/fs;
x1=cos(2*pi*f1*t)+0.5*sin(2*pi*f2*t);
x2=cos(2*pi*f1*t1)+0.5*sin(2*pi*f2*t1);
subplot 211; plot(n,x1,'r',n,x2,'b'); grid;
legend('x1','x2'); title('Signals Waveform');
R=xcorr(x1,x2);
nn=-N+1:N-1;
subplot 212; plot(nn,R); axis tight
grid; title('Correlation Function');
[Rmax,Rloc]=max(R);
Rloc=Rloc-N
最后计算出Rloc=35,表示x2对x1超前了35个样点。