www.pudn.com > yuyinxinhaochulisuanfa.rar > acorrl.m
function r=acorrl(X,maxlag,varargin)
% returns the biased autocorrelation of X in r. r is a row vector.
%
% r=acorrl(X,maxlag,'half')
% X--> input data
% maxlag --> maximum lag,which should be less than N-1.N is the length of X
% 'half'--> returns the r(m) ,whose m is from 0 to maxlag
%
% r=acorrl(X,maxlag,'whole'), returns the r(m) ,whose m is from -maxlag
% to maxlag. If not determinate the third argument, namely r=acorrl(X,maxlag),
% the function returns the same result.
%
% The algorithm is follow as:
% 1 N-m
% r(m)= --- sum conj(x(n))*x(n+m) 0<=m<=N-1
% N n=1
% Copyright by Zhilin Zhang, Hanlin Laboratory of UESTC
% $ Revision: 1.1 $ $ Date: 2003/04/02 01:29 $
%
% Reference:
% [1] 胡广书,数字信号处理——理论、算法与实现,清华大学出版社,1997
LenX=length(X);
% make sure the maxlag is correct
if maxlag<0 | maxlag>=LenX
error('The max lag should be nonnegative and less than the length of input data\n');
end
% computer the r(m),which is from 0 lag to maxlag
rm=zeros(1,maxlag+1);
for m=0:maxlag
rsum=0;
for n=1:LenX-m
rsum=rsum+conj(X(n))*X(n+m);
end
rm(m+1)=rsum/LenX;
end
% If the third argument is 'whole' or default ,returns the r(m) from
% -maxlag to maxlag. If 'half',returns only the r(m) from 0 lag to
% maxlag.
if nargin==2 | strcmp(varargin{1},'whole')
r=[conj(rm(maxlag+1:-1:2)),rm];
elseif strcmp(varargin{1},'half')
r=rm;
else
error('The third argument is wrong\n');
end