home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l455 / 9.ddi / IDENT.DI$ / AR.M next >
Encoding:
Text File  |  1993-03-11  |  4.3 KB  |  117 lines

  1. function [th,ref]=ar(y,n,approach,win,maxsize,T)
  2. %AR    Computes AR-models of signals using various approaches
  3. %
  4. %    TH = ar(y,n)  or  TH = ar(y,n,approach)  or TH = ar(y,n,approach,win)
  5. %    
  6. %    TH: returned as the estimated parameters of the AR-model,see HELP THETA
  7. %    y: The time series to be modelled, a column vector
  8. %    n: The order of the AR-model
  9. %    approach: The method used, one of the following ones:
  10. %       'fb' : The forward-backward approach (default)
  11. %       'ls' : The Least Squares method
  12. %       'yw' : The Yule-Walker method
  13. %       'burg': Burg's method
  14. %       'gl' : A geometric lattice method
  15. %       For the two latter ones, reflection coefficients and loss functions
  16. %       are returned in REFL by [TH,REFL]=ar(y,n,approach)
  17. %       If any of these arguments end with a zero (like 'burg0'), the 
  18. %       computation of the covariance is suppressed.
  19. %    win : Windows employed, one of the following ones:
  20. %       'now' : No windowing (default, except when approach='yw')
  21. %       'prw' : Prewindowing
  22. %       'pow' : Postwindowing
  23. %       'ppw' : pre- and post-windowing
  24. %    for TH = ar(y,n,approach,win,maxsize,T) see HELP AUXVAR
  25.  
  26. %    L. Ljung 10-7-87
  27. %    Copyright (c) 1987-90 by the MathWorks, Inc.
  28. %    All Rights Reserved.
  29.  
  30. % Some initial tests on the input arguments
  31. [Ncap,ny]=size(y);Ncap0=Ncap;
  32. maxsdef=idmsize(Ncap,n);
  33. if nargin<6,T=[];end
  34. if nargin<5,maxsize=[];end
  35. if nargin<4,win=[];end
  36. if nargin<3,approach=[];end
  37. if isempty(T),T=1;end,if T<0,T=1;end
  38. if isempty(maxsize), maxsize=maxsdef;end,if maxsize<0,maxsize=maxsdef;end
  39. if isempty(win);win='now';end ,if win<0,win='now';end
  40. if isempty(approach),approach='fb';end,if approach<0,approach='fb';end
  41. pt=1; %pt=1 means that covariances should be computed
  42. if approach(length(approach))=='0',pt=0;end
  43. if length(approach)>1,appr=approach(1:2);else appr=[approach ' '];end
  44. ap=[appr;appr;appr;appr;appr;appr;appr;appr;appr;appr];
  45. ss=(ap==['fb';'FB';'yw';'YW';'ls';'LS';'bu';'BU';'gl';'GL']);
  46. if sum(ss(:,1).*ss(:,2))==0,error('The input argument APPROACH must be one of the following (within quotes): ''fb'', ''ls'',''yw'', ''burg'', ''gl'''),end
  47. ap=[win;win;win;win]; ss=(ap==['prw';'pow';'ppw';'now']);
  48. if sum((ss(:,1).*ss(:,2)).*ss(:,3))==0,error('The input argument WIN must be one of the following (within quotes): ''now'',     ''prw'', ''pow'', ''ppw'''),end
  49. [Ncap,ny]=size(y); if ny>Ncap, y=y';end
  50. if min(Ncap,ny)>1, error('Only single time series can be handled'),end
  51. nstart=1;nend=Ncap;
  52. if appr=='yw' | appr=='YW',win='ppw';end
  53. if win=='prw' | win=='ppw',y=[zeros(n,1);y];nstart=n+1;nend=length(y);end
  54. if win=='pow' | win=='ppw',y=[y;zeros(n,1)];nend=length(y)-n;end
  55.  
  56. [Ncap,ny]=size(y);
  57.  
  58. %build up the theta-matrix
  59. I=[0 T 0 n 0 0];
  60. if pt~=0,th=zeros(n+3,max(7,n));else th=zeros(3,max(7,n));end
  61. th(1,1:6)=I;
  62. ti=fix(clock);ti(1)=ti(1)/100;
  63. th(2,2:6)=ti(1:5);th(2,7)=12;
  64.  
  65. % First the lattice based algorithms
  66.  
  67. if appr=='bu' | appr=='BU' | appr=='gl' | appr=='GL'
  68.    ef=y;eb=y;
  69.    rho(1)=y'*y/Ncap;
  70.    for p=1:n
  71.    nef=ef(p+1:Ncap)'*ef(p+1:Ncap); neb=eb(p:Ncap-1)'*eb(p:Ncap-1);
  72.    if appr=='gl' | appr=='GL',den=sqrt(nef*neb);end
  73.    if appr=='bu' | appr=='BU',den=(nef+neb)/2;end
  74.    r(p)=(-eb(p:Ncap-1)'*ef(p+1:Ncap))/den;
  75.    efold=ef;
  76.    ef(2:Ncap)=ef(2:Ncap)+r(p)*eb(1:Ncap-1);
  77.    A(p)=r(p);
  78.    eb(2:Ncap)=eb(1:Ncap-1)+conj(r(p))*efold(2:Ncap);
  79.    A(1:p-1)=A(1:p-1)+r(p)*conj(A(p-1:-1:1));
  80.    rho(p+1)=rho(p)*(1-r(p)*r(p));
  81.    end
  82. th(3,1:n)=A; 
  83. e=pe(y(nstart:nend),th);lam=e'*e/(nend-nstart+1-n);
  84. th(1,1)=lam; th(2,1)=lam*(1+n/Ncap0)/(1-n/Ncap0);
  85. ref=[0 r;rho];
  86. if pt==0, return,end
  87. end
  88. % Now compute the regression matrix
  89.  
  90. nmax=n;
  91. M=floor(maxsize/n);
  92. R=zeros(n);F=zeros(n,1);
  93. fb=((appr=='fb') | (appr=='FB'));
  94. if fb,RB=zeros(n);FBc=zeros(n,1);yb=conj(y(Ncap:-1:1));end
  95. for k=nmax:M:Ncap-1
  96.     jj=(k+1:min(Ncap,k+M));
  97.     phi=zeros(length(jj),n);if fb,phib=zeros(length(jj),n);end
  98.     for k=1:n,phi(:,k)=-y(jj-k);end
  99.     if fb,for k=1:n,phib(:,k)=-yb(jj-k);end,end
  100.     R=R+phi'*phi;F=F+phi'*y(jj);
  101.     if fb,RB=RB+phib'*phib;FBc=FBc+phib'*yb(jj);end
  102. end
  103. P=inv(R);
  104. if appr~='bu' & appr~='gl'
  105. if ~fb
  106.     if Ncap>M , t=R\F;else t=phi\y(jj);end
  107. end
  108. if fb
  109.     if Ncap>M/2 , t=(R+RB)\(F+FBc);else t=[phi;phib]\[y(jj);yb(jj)];end
  110. end
  111. th(3,1:n)=t.';
  112. e=pe(y(nstart:nend),th);lam=e'*e/(nend-nstart+1-n);
  113. th(1,1)=lam; th(2,1)=lam*(1+n/Ncap0)/(1-n/Ncap0);
  114. end
  115. if pt~=0,P=lam*P;th(4:n+3,1:n)=P;end
  116.