function [ro,ccf] = ccf(x,y)
%    DIRECT COMPUTATION OF THE CROSS-CORRELATION/COVARIANCE FUNCTION ESTIMATE
%                 (suitable for short sequences)
% function [ro,ccf] = ccf(x,y)
% x,y ... time series data vectors of length n
% ro  ... estimate of the cross-correlation function of length n/4 (column vec.)
%         (leading significant part only)
% ccf ... full estimate of the cross-covariance function of length n
%         (column vector)

% (C) 1996, V. Vesely, Masaryk University of Brno (Czech Republic)
%
if size(x,1)==1, x=x.'; end
if size(y,1)==1, y=y.'; end
if size(x,2)>1 , error('x is not a vector'); end
if size(y,2)>1 , error('y is not a vector'); end
n  = length(x);
if n~=length(y), error('the lengths of x and y differ'); end
if n<50
   disp('WARNING: few samples (less than 50) to get a reliable estimate');
end
x0 = x-mean(x);
y0 = y-mean(y);
ccf= zeros(n,1);
for h=1:n
    ccf(h) = dot(y0(1:n-h+1),x0(h:n));
end
n4 = ceil(n/4);
ro = ccf(1:n4) ./ (norm(x0)*norm(y0));
ccf = ccf./n;
