www.pudn.com > CCF.zip > CCF.m, change:2015-03-18,size:821b


%%%% 计算互相关系数子程序 %%%% 
function [FI_final,lamda]=CCF(x,y,T) 
 
n=length(x); 
% for i=1:n 
%     x(i)=(x(i)-min(x))/(max(x)-min(x)); 
%     y(i)=(y(i)-min(y))/(max(y)-min(y)); 
% end 
%求均值 
mux=mean(x); 
muy=mean(y); 
%求方差 
deltax=std(x); 
deltay=std(y); 
 
%计算互相关系数 
k=1; 
FI=zeros(1,22); 
for t=T(1):T(end) 
    sum=0; 
    for i=1:(n-abs(t)) 
        if t>=0 
            sum=sum+(x(i)-mux)*(y(i+t)-muy)/((deltax*deltay)+eps); 
        else 
            sum=sum+(x(i-t)-mux)*(y(i)-muy)/((deltax*deltay)+eps); 
        end 
    end 
    FI(k)=sum/(n-abs(t)); 
    k=k+1; 
end 
% t=T(1):T(end); 
% plot(t,FI)  
 
[FImax,kmax]=max(FI); 
[FImin,kmin]=min(FI); 
if abs(FImax)>=abs(FImin) 
    lamda=kmax-(abs(T(1))+1); 
    FI_final=FImax; 
else 
    lamda=kmin-(abs(T(1))+1); 
    FI_final=FImin; 
end