Code covered by the BSD License  

Highlights from
acfplot.m

from acfplot.m by Phillip M. Feldman
compute and plot estimate of the autocorrelation of an input sequence

acfplot(Y, Fs, maxlag, titlestr)
function acf= acfplot(Y, Fs, maxlag, titlestr)
%
% acfplot calls autocov() to compute an extimate of the autocorrelation
% function (acf) of the input vector Y, and then plots the acf with the
% time axis labeled in units of either seconds or ms.  Note: This function
% does not use the Statistics Toolbox.
%
% Syntax:
%
% acf= acfplot(Y, Fs, maxlag, titlestr)
%
% Y is a column vector containing a sequence of samples.  The remaining
% input arguments are optional.  Fs specifies the sampling rate (samples
% per second); the default is 1.  maxlag is the maximum lag for which the
% autocorrelation is to be computed; if no value is specified, a default
% value is generated by the function autocov.  titlestr specifies a title
% to be printed at the top of the plot; the default is a null string.
%
% Dr. Phillip M. Feldman   14 Jan, 2007


% Section 1: Check input arguments and assign default values if necessary.

if nargin < 1 | nargin > 4
   error('The number of calling arguments must be between 1 and 4.');
end

% Supply default values for optional input arguments:

if nargin < 4, titlestr= ''; end
if nargin < 3, maxlag= 0; end
if nargin < 2, Fs= 1; end


% Section 2: Call autocov to compute autocovariance (acv).  We then
% convert the autocovariance to autocorrelation (acf) and generate a
% vector T containing time values corresponding to the lags in acf.

% Compute autocovariance function:

if maxlag <= 0 % Let autocov supply a value for maxlag.
   acv= autocov(Y);
else
   acv= autocov(Y, maxlag);
end

% Normalize autocovariance to obtain autocorrelation:

acf= acv/acv(1);

% Create a column vector T that counts starting at 0, with same number
% of elements as the acf vector:

T= [0 : size(acf,1)-1]';

% Fix time vector so that steps correspond to 1/Fs, with units of either
% seconds or ms, depending on the last value of T:

T= T / Fs;

if T(size(T,1),1) < 3
   T= 1000.0 * T;
   xtext= 'Time (ms)';
else
   xtext= 'Time (s)';
end

% Section 3: Generate plot.

fig(0.8, 0.6);

h1= plot(T, acf);
set(h1, 'LineWidth', 2, 'Color', 'Blue');
set(gca, 'FontSize',12);

% Add axis labels and title:
h1= xlabel(xtext);
set(h1, 'FontSize', 14);
h1= ylabel('Autocorrelation');
set(h1, 'FontSize', 14);
h1= title(titlestr);
set(h1, 'FontSize', 14);

% Force lower y-axis limit to be highest multiple of 0.1 that is less
% than or equal to all of the acf values.  Force upper y-axis limit to
% unity:

ylim([0.1*floor(10.0*min(acf)) 1]);

grid on;

Contact us at files@mathworks.com