function []=acf(x,k,caption)
%ACF	Plot of sample autocorrelation function.
%
%	ACF(x) plots the sample autocorrelation function up to
%	lag 25 for the univariate time series represented by
% 	the vector x. ACF(x,k) plots the sample autocorrelation 
%	function up to lag k, and ACF(x,k,name) also sets the plot's 
%	title to name. 
%
%	The 95% confidence intervals for the autocorrelation 
%	function of an IID process of the same length are also 
%	displayed. Estimates lying outside these bounds are 
%	marked by an asterisk.

%	Modified 2-Sep-97
%	Author: Tapio Schneider
%		AOS Program, Princeton University
%		Princeton, NJ 08544-0710
%		tapio@splash.Princeton.EDU

if ~exist('xcorr')
   error('ACF requires XCORR from the Signal Processing Toolbox.')
end   

[m,n] = size(x);
if (min(m,n) > 1) error('Time series must be univariate.'); end
n = max(m,n);

if (nargin < 3) caption='ACF'; end
if (nargin < 2) k=25; end
if (nargin < 1) error('Usage: ACF(vector).'); end

% Compute autocorrelation sequence using XCORR from the Signal 
% Processing Toolbox
cor = xcorr(x,'coeff');
rho = cor(n:n+k);             % autocorrelation function up to lag k
abscis = [0:1:k];             % abscissa for plot

% 95% confidence intervals for IID process of the same length
bound = zeros(size(rho));
bound = bound+(1.96/sqrt(n));

% Do not mark lag 0 autocorrelation by an asterisk
inl(1) = rho(1);              % stores ACF at lags within 95% bounds
inabsc(1) = 0;                % corresponding lags
% What if there are no points outside the bounds?
outl(1) = rho(1);
outabsc(1)=0;
% My modification-Calvin L. Williams
nin = 1;                      % number of points within 95% confidence bounds 
nout = 0;                     % number of points outside 95% confidence bounds 
for i=2:k 
    if abs(rho(i)) > bound(1) % point outside bounds
       nout = nout+1;
       outl(nout) = rho(i);
       outabsc(nout) = i-1;
    else                      % point within bounds
       nin = nin+1;
       inl(nin) = rho(i);
       inabsc(nin) = i-1;
    end;
end;

% Plot it
plot(abscis,rho, abscis,bound,'-.', abscis,-bound,'-.', ...
     outabsc,outl,'*', inabsc,inl,'o');
axis([0 k -1 1])
title(caption)
xlabel('Lag')
ylabel('Autocorrelation Function')









