function [th,it_inf]=oe(z,nn,maxiter,tol,lim,maxsize,Tsamp,trc)
%OE	Computes the prediction error estimate of an output-error model.
%	TH = OE(Z,NN)  or TH = OE(Z,NN,'trace')
%
%	TH: Returned as the estimated parameters of the output-error model
%	y(t) = [B(q)/F(q)] u(t-nk) + e(t)
%	along with estimated covariances and structure information.
%	For the exact format of TH, see also THETA.
%
%	Z : The output-input data Z=[y u], with y and u being column vectors.
%	This routine only works for single-output systems. Use PEM otherwise.
%
%	NN: Initial value and structure information, given either as
%	NN=[nb nf nk], the orders and delay of the above model, or as
%	NN=THI, with THI being a theta matrix of the standard format. In
%	the latter case the criterion minimization is initialized at THI.
%       For multi-input systems, nb, nf and nk are row vectors, so that nb(k)
%	nf(k) and nk(k) is the order and delay associated with input # k.
%
%       If a *last* argument to OE is entered as 'trace', information about
%	the criterion minimization is given in the MATLAB Command Window.
%
%	Some parameters associated with the algorithm are accessed by
%	[TH,IT_INF] = OE(Z,NN,maxiter,tol,lim,maxsize,T,'trace')
%	See also AUXVAR for an explanation of these, and their default values.
%	See also ARX, ARMAX, BJ, IV4, N4SID, PEM.

%	L. Ljung 10-1-86,7-24-94
%	Copyright (c) 1986-95 by the MathWorks, Inc.
%	$Revision: 1.5 $  $Date: 1995/05/16 09:01:05 $

if nargin<2
  disp('Usage: TH = OE(Z,NN)')
  disp('       TH = OE(Z,NN,''trace'')')
  disp('       TH = OE(Z,NN,maxiter,tol,lim,maxsize,T,''trace'')')
  return
end

% *** Set up default values ***
global XIDiter
gui=0;display=0;stopp=0;flag=[];
eval('flag=findobj(get(0,''children''),''flat'',''tag'',''sitb22'');','');
if ~isempty(flag),if strcmp(get(flag,'vis'),'on')
   gui=1;set(XIDiter(7),'userdata',0);
end,end 

[nr,cc]=size(nn); [Ncap,nz]=size(z); nu=nz-1;
maxsdef=idmsize(Ncap);

if nargin==8,if strcmp(lower(trc),'trace'),display=1;end,end
if nargin==7,if strcmp(lower(Tsamp),'trace'),display=1;Tsamp=[];end,end
if nargin==6,if strcmp(lower(maxsize),'trace'),display=1;maxsize=[];end,end
if nargin==5,if strcmp(lower(lim),'trace'),display=1;lim=[];end,end
if nargin==4,if strcmp(lower(tol),'trace'),display=1;tol=[];end,end
if nargin==3,if strcmp(lower(maxiter),'trace'),display=1;maxiter=[];end,end

if nargin<7, Tsamp=[];end
if nargin<6, maxsize=[];end
if nargin<5, lim=[];end
if nargin<4, tol=[];, end
if nargin<3, maxiter=[]; end
if isempty(Tsamp),if nr>1,Tsamp=gett(nn);else Tsamp=1;end,end
if isempty(maxsize),maxsize=maxsdef;end
if isempty(lim),lim=1.6;end,if isempty(tol),tol=0.01;end
if isempty(maxiter),maxiter=10;end
if Tsamp<0,Tsamp=1;end, if maxsize<0,maxsize=maxsdef;end, if lim<0,lim=1.6;end
if tol<0,tol=0.01;end,if maxiter<0, maxiter=10;end

if display==1,arg='trace';else arg='notrace';end

%  *** Do some consistency tests ***

if nz>Ncap, error('The data should be organized in column vectors')
return,end

if nu==0, error('OE makes sense only if an input is present!')
return,end
if nr==1 
  if cc~=3*nu,
         disp('Incorrect number of orders specified:')
         disp('Should be  nn=[nb nf nk]')
         disp('where nb nf and nk are row vectors, each of length = # of inputs')
         error
  end
  if any(nn<0)
     error('All orders must be non-negative.')
  end
  kz=find(nn(1:nu)==0);
  if ~isempty(kz),nn(nu+kz)=zeros(1,length(kz));end
end

if nu>1&nr==1
   th=pem(z,[0 nn(1:nu) 0 0  nn(nu+1:3*nu)] ,...
            [],maxiter,tol,lim,maxsize,Tsamp,arg);
   return
end

%
%*** if nn has more than 1 row, then it is a theta matrix
%     and we jump directly to the minimization ***
%
if nr>1 nu=nn(1,3);
   if nu>1, 
      [th,it_inf]=pem(z,nn,[],maxiter,tol,lim,maxsize,Tsamp,arg);
      return
   end
   nf=nn(1,8); nb=nn(1,5); nk=nn(1,9); n=nf+nb;ni=max(nf,nb+nk-1);
   t=nn(3,1:n).';
   if nf>0; f=[1 t(nb+1:n).'];else f=1;end
end
%
%
if nr==1
   nb=nn(1); nf=nn(2); nk=nn(3); n=nb+nf;ni=max(nf,nb+nk-1);
%
%
% *** compute initial estimate via LS and IV ***
%
  t=arx(z,[nf nb nk],maxsize,Tsamp,0);
  f=fstab([1 t(1:nf).']);
        
  t=iv(z,[nf nb nk],f,[zeros(1,nk) t(nf+1:n).'],maxsize,Tsamp,0);
  f=fstab([1 t(1:nf).']);
        
%
  if nf>0, t=[t(nf+1:n); t(1:nf)];end
end
% *** Display initial estimate ***
%
w=pefilt([zeros(1,nk) t(1:nb).'],f,z(:,2),z(1:ni,1));
v=z(:,1)-w; Vcap=v'*v/(Ncap-ni);
if isnan(Vcap)|~finite(Vcap)
   disp('WARNING: Your input signal is apparently not persistently exciting.')
   disp('         Use other input or lower model orders.')
   disp('         No model returned.')
   th=[];return
end
if gui,drawnow,display=get(XIDiter(2),'value');end
if display
%clc
disp(['  INITIAL ESTIMATE'])
disp(['Current loss:' num2str(Vcap)])
disp(['th-vector'])
disp(t)
end
%
% *** start minimizing ***%
% ** determine limit for robustification **
if lim~=0, lim=median(abs(v-median(v)))*lim/0.7;end
if lim==0
    vl=v;
else
    [ne,me]=size(v);
    ll=ones(ne,me)*lim;la=abs(v)+eps*ll;vl=v.*(min(la,ll)./la);
    clear ll,clear la
end

 testnorm=tol+1;l=0; st=0; nmax=max(nf,nb+nk-1);
%
% ** the minimization loop **
%
while [testnorm>tol l<maxiter st~=1]
      l=l+1;
%     * compute gradient *
      wf=filter(-1,f,w); uf=filter(1,f,z(:,2));
   M=floor(maxsize/n);
   R=zeros(n);Fcap=zeros(n,1);
   for k=nmax:M:Ncap-1
      jj=(k+1:min(Ncap,k+M));
      psi=zeros(length(jj),n);
      for k=1:nb, psi(:,k)=uf(jj-k-nk+1);end
      for k=1:nf, psi(:,k+nb)=wf(jj-k);end
      R=R+psi'*psi; Fcap=Fcap+psi'*vl(jj);
   end
   if Ncap>M, g=R\Fcap; else g=psi\vl(jj);end,grad=Fcap;
   testnorm=abs(grad'*g/Vcap/Ncap*100);

%
%     * search along the g-direction *
%
      [t1,w,vl,v,V1,f,st]=searchoe(z,t,g,lim,Vcap,nb,nf,nk,ni,display);
if st==1,
      [t1,w,vl,v,V1,f,st]=searchoe(z,t,grad/trace(R)*length(R),...
                      lim,Vcap,nb,nf,nk,ni,display);
end
if gui,drawnow,display=get(XIDiter(2),'value');end
if display
      %home
      disp(['      ITERATION # ' int2str(l)])
      disp(['Current loss: ' num2str(V1) '  Previous loss: ' num2str(Vcap)])
      disp(['Current th  prev. th   gn-dir'])
      disp([t1 t g])
      disp(['Norm of gn-vector: ' num2str(norm(g))])
      disp(['Expected improvement: ',num2str(testnorm),' %'])
      disp(['Achieved improvement: ',num2str((Vcap-V1)*100/Vcap),' %'])

      if st==1
	 disp('No improvement of the criterion possible along the search direction')
	 disp('Iterations therefore terminated'),end
end %if display
if gui
  set(XIDiter(4),'string',int2str(l));
  set(XIDiter(5),'string',num2str(V1));
  set(XIDiter(9),'string',['(',num2str(Vcap),')']);
  set(XIDiter(6),'string',num2str(testnorm));
  drawnow
  stopp=get(XIDiter(7),'userdata');
end

      t=t1; impr=V1-Vcap;Vcap=V1;
    if stopp,break,end
end
it_inf=[l,impr,testnorm];
Vcap=v'*v/(length(v)-ni);
if isempty(Vcap)|isempty(t)
   disp('WARNING: Your input signal is apparently not persistently exciting.')
   disp('         Use other input or lower model orders.')
   disp('         No model returned.')
   th=[];return
end

th(1,:)=[Vcap Tsamp 1 0 nb 0 0 nf nk];
th(2,1)=Vcap*(1+n/Ncap)/(1-n/Ncap);
ti=fix(clock); ti(1)=ti(1)/100;
th(2,2:6)=ti(1:5);
th(2,7)=8;
th(3,1:n)=t.';
if maxiter==0, return,end
if Ncap>M, PP=inv(R); else PP=inv(psi'*psi);end
th(4:3+n,1:n)=Vcap*PP;

