
    
function  [gtrue,htrue,pred1,pred2,y,U,Pr,ytest_tot,ytest,Utest,Ttest,yval,Uval]=genri(den,numG,numH,inp,ou,oe,p,p2,nn,al,nt,lowpar,ntval);

warning off

m=size(numG,1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%ARMAX%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
np=max(size(numH))-1;
for kk=1:m;
    Gkk=dimpulse(numG(kk,:),den,nn+al);
    G(:,:,kk)=toeplitz(Gkk,zeros(1,nn+al));
    gtrue(:,kk)=G(:,1,kk);
end
H=dimpulse(numH,den,nn+al);
H=toeplitz(H,zeros(1,nn+al));
htrue=H(:,1);
ou=1;
oe=1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%PREDITTORE OTTIMO%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
pred1=dimpulse(numH-den,numH,p+1+al)';pred1(1)=[];
Pr=pred1';
for kk=1:m
    pred2(kk,:)=dimpulse(numG(kk,:),numH,p+1+al)';
end
pred2(:,1)=[];
for kk=1:m;
    Pr=[Pr' pred2(kk,:)]';
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%DATA%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if inp==1;
    for kk=1:m;
        U(:,kk)=ou*randn(nn+al,1);
    end
else
    for kk=1:m;
        U(:,kk)=idinput(nn+al,'rgs',[0 lowpar]);
    end
%     for kk=1:m;
%         UU=zeros(nn+al,1);for i=1:floor((nn+al)/80)+1;sel=(1+40*(2*(i-1)));UU(sel:(sel+40))=1;end;
%         for i=1:floor((nn+al)/80)+1;sel=(1+40*(2*(i-1)));UU((sel+40):(sel+80))=0;end;UU=UU(1:(nn+al));
%         U(:,kk)=UU;
%     end
end
E=oe*randn(nn+al,1);
y=H*E;
for kk=1:m;
    y=y+G(:,:,kk)*U(:,kk);
end
y=y((p2+1):nn);
U=U((p2+1):nn,:);
%figure(1);plot(y,'ko--');pause(2);%close all
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%TEST SET%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ntest=nt+p2;
for kk=1:m;
    Gkk=dimpulse(numG(kk,:),den,ntest);
    Gkk=toeplitz(Gkk,zeros(1,ntest));
    Gtest(:,:,kk)=toeplitz(Gkk,zeros(1,ntest));
    if inp==1;
        Utest(:,kk)=ou*randn(ntest,1);
    elseif inp==2;
        Utest(:,kk)=idinput(ntest,'rgs',[0 lowpar]);%ou*randn(ntest,1);%%%%%%%%%%%%%%%%%%%%%%%Utest WN per ora%%%%%%%%%%%%%%%%
    end
end
Htest=dimpulse(numH,den,ntest);
Htest=toeplitz(Htest,zeros(1,ntest));
Etest=oe*randn(ntest,1);
ytest=Htest*Etest;
for kk=1:m;
    ytest=ytest+Gtest(:,:,kk)*Utest(:,kk);
    w(:,:,kk)=toeplitz([0 Utest(1:ntest-1,kk)'],zeros(1,ntest));
    Atest(:,:,kk+1)=w(:,1:max(size(pred2(kk,:))),kk);
end
q=toeplitz([0 ytest(1:ntest-1)'],zeros(1,ntest));
Atest(:,:,1)=q(:,1:max(size(pred1)));
Ttest=[Atest(:,:,1)];
for kk=1:m;
    Ttest=[Ttest Atest(:,:,kk+1)];
end
Ttest=Ttest((p2+1):(p2+nt),:);%dimensione nt x (p+al)
ytest_tot=ytest;
ytest=ytest((p2+1):(p2+nt));%dimensione nt

% RMSEbest=norm(Ttest*Pr-ytest)/sqrt(nt)
% VarTest=sum((ytest-mean(ytest)).^2);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%VALIDATION SET%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ntest=ntval+p2;
for kk=1:m;
    Gkk=dimpulse(numG(kk,:),den,ntest);
    Gkk=toeplitz(Gkk,zeros(1,ntest));
    Gval(:,:,kk)=toeplitz(Gkk,zeros(1,ntest));
    if inp==1;
        Uval(:,kk)=ou*randn(ntest,1);
    elseif inp==2;
        Uval(:,kk)=idinput(ntest,'rgs',[0 lowpar]);%%%%%%%%%%%%%%%%%%%%%%%%Utest non WN per ora%%%%%%%%%%%%%%%%
    end
end
Hval=dimpulse(numH,den,ntest);
Hval=toeplitz(Hval,zeros(1,ntest));
Etest=oe*randn(ntest,1);
yval=Hval*Etest;
for kk=1:m;
    yval=yval+Gval(:,:,kk)*Uval(:,kk);
end
Uval=Uval((p2+1):(p2+ntval),:);
yval=yval((p2+1):(p2+ntval));%dimensione nt

% RMSEbest=norm(Ttest*Pr-ytest)/sqrt(nt)
% VarTest=sum((ytest-mean(ytest)).^2);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    