function yhat = predicts(z,th,m)
%PREDICTS
%	yhat = predicts(Z,TH,M)
%
%	Auxiliary function to PREDICT

%	L. Ljung 10-2-90,11-2-92
%	Copyright (c) 1990 by the MathWorks, Inc.
%	All Rights Reserved.


[Ncap,nc]=size(z);
nu=th(1,3);ny=th(1,4);
if m==inf, yhat=idsimss(z(:,ny+1:ny+nu),th);return,end
[nr,nc]=size(th);
nd=th(1,5);
sspmod=getmfth(th);

[etapar,P,lambda]=th2par(th);
if length(etapar)==0,etapar=0;end
arg=getargth(th);
T=th(1,2);
if any(th(2,8)==[2 3]),Tmod=-1;else Tmod=abs(T);end
[A,B,C,D,K,X0]=feval(sspmod,etapar,Tmod,arg);
x=ltitr(A-K*C,[K B-K*D],z,X0);
if m==1, yhat=(C*x')'; if ~isempty(D),yhat=yhat + (D*z(:,ny+1:ny+nu)')';end
else

F=D;Mm=eye(length(A));
for km=1:m-1
F=[F C*Mm*B];
Mm=A*Mm;
end
yhat=zeros(Ncap,ny);%corr 911111
for ky=1:ny
for ku=1:nu
yhat(:,ky)=yhat(:,ky)+filter(F(ky,ku:nu:m*nu),1,z(:,ny+ku));
end
end
if isempty(yhat),yhat=zeros(Ncap,ny);end
yhat(m:Ncap,:)=yhat(m:Ncap,:)+(C*Mm*x(1:Ncap-m+1,:)')';
x=ltitr(A,B,z(1:m,ny+1:ny+nu),X0);
yhat(1:m,:)=(C*x')'; 
if ~isempty(D),yhat(1:m,:)=yhat(1:m,:) + (D*z(1:m,ny+1:ny+nu)')';end
end


