function [ACF,WACF] = acovf(Z,KMAX);
% Autocovariance function 
% [ACF,WACF] = acovf(Z,N);
%
% Input:	Z    Signal
%		N+1  # of coefficients
% Output:	ACF  autocovariance function
%		WACF weighted autocorrelation function

% 	Version 1.65
%	09.09.1996
%	Copyright (c) by Alois Schloegl
%	E-Mail: a.schloegl@ieee.org	

Z=Z(:)';
[K N] = size(Z);
if (nargin == 1) KMAX = N-1; 
elseif (KMAX >= N-1) KMAX = N-1;
end;

if N<=K fprintf(2,'Warning ACOVF: # of elements in Z is less than N\n');
end;

if 10*KMAX > N,      % use fast Built-in function
	ACF = filter(Z(N:-1:1),1,Z);
	ACF = ACF(N:-1:N-KMAX);
else
	for K = 0:KMAX,
		ACF(K+1) = Z(1:N-K) * Z(1+K:N)';
	end;
end;
ACF=ACF/N;
WACF=ACF*(N/ACF(1));
