function acv = acov(ts, maxlag)
%acov.m: computes the sample autocovariance function
%            ts     = 1-D time series
%            maxlag = maximum lag ( < length(ts))
%            usage: z = autocov (a,maxlag);
n = length(ts);
ts = ts(:) - mean(ts); %note overall mean
suma = zeros(n,maxlag+1);
suma(:,1) = ts.^2;
for h = 2:maxlag+1
   suma(1:(n-h+1), h) = ts(h:n);
   suma(:,h) = suma(:,h) .* ts;
end
acv = sum(suma)/n; %note the division by n
                   % not by (n-h)


function [acrr] = acorr(ts , maxlag)
  acr =  acov(ts, maxlag);
  acrr = acr ./ acr(1);
