function [ro,acf] = facf(x)
%FFT-BASED COMPUTATION OF THE AUTOCORRELATION (AUTOCOVARIANCE) FUNCTION ESTIMATE
%        (suitable for long sequences of well-factorizable length)
% function [ro,acf] = facf(x)
% x ... time series data vector of length n
% ro... estimate of the autocorrelation function of length n/4 (column vector)
%       (leading significant part only)
% acf ... full estimate of the autocovariance 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(x,2)>1 , error('x is not a vector'); end
n  = length(x);
realdata = isreal(x);
if n<50
   disp('WARNING: few samples (less than 50) to get a reliable estimate');
end
x0 = [x-mean(x); zeros(n-1,1)];
x0 = fft(x0);
z  = abs(x0).^2;
z  = ifft(z);
if realdata, z = real(z); end
acf = z(1:n)./n;
n4 = ceil(n/4);
ro = acf(1:n4)./acf(1);
