function [G,PV] = trfss(th,nnu,nny,w)
%TRFSS	Auxiliary routine to TRF
%
%	[G,PV] = trfss(th,nnu,nny,w)
%

%	L. Ljung 10-2-90
%	Copyright (c) 1986-95 by the MathWorks, Inc.
%	$Revision: 1.2 $  $Date: 1995/01/24 00:54:03 $

[a,b,c,d,k]=eta2ss(th); 
[nx,dum]=size(a);
[dum,nu]=size(b);[ny,dum]=size(c);
if any(nnu>nu),error('There are not that many inputs in the model!'),end
if any(nny>ny),error('There are not that many outputs in the model!'),end
T=gett(th);
if isempty(nnu),nnu=1:nu;end
if isempty(nny),nny=1:ny;end
lny=length(nny);
if length(nnu)>0
G=[];G1=zeros(1+length(w),3*lny);
for ku=nnu
if ku>0, [g,p]=trfsaux(a,b,c(nny,:),d(nny,:),ku,w,T); 
G1(1,1:3:3*lny)=1000*(nny-1)+100+ku;
G1(1,2:3:3*lny)=1000*(nny-1)+ku;
G1(1,3:3:3*lny)=1000*(nny-1)+20+ku;
G1(2:length(w)+1,[1:3:3*lny 2:3:3*lny 3:3:3*lny])=[w'*ones(1,length(nny)) g p];
G=[G G1];
end ,end,end
if nargout>1 | nu==0
    [par,PM,lam]=th2par(th);lam=sqrtm(lam);g=[];
    for ke=1:ny
       g=[g,trfsaux(a,k*lam,c,lam,ke,w,T).^2];
    end
    PV=[];
    for ky=nny
       PV1(1,1:2)=[(ky-1)*1000+100,(ky-1)*1000];
       index=[ky:ny:ny*ny]; li=length(index);
       if li==1,col=g(:,index);else col=sum(g(:,index)')';end
       PV1(2:length(w)+1,[1:2])=...
          [w' abs(T)*col];
       PV=[PV PV1];
    end
    if nu==0 G=PV;end
end

