
function [err0,err1,Err,RM,R2M,M]=CompErrold(eta,gtrue,htrue,Ind,err0,err1,nv,ytest_tot,ytest,Utest,p2,nt,VarTest,Multi);%ff è per l'u

nM=size(Multi,2);

if ~isempty(nv);%per il nonparametrico

    if size(gtrue,1)>size(gtrue,2);
        gtrue=gtrue';
    end
    if size(htrue,1)>size(htrue,2);
        htrue=htrue';
    end

    g=eta(:,:,1)';
    g=[0 g zeros(1,200)];
    g=g(1:200);
    n=size(g,2);
    e=[1 zeros(1,n-1)];
    g=-g+e;
    gg=sqrt(nv)*filter(1,g,e);
    n=300-max(size(gg));
    gg=[gg zeros(1,n)];

    m=size(eta,3)-1;
    for i=1:m;
        ff=eta(:,:,i+1)';
        n=300-max(size(ff))-1;
        ff=[0 ff zeros(1,200+n)];
        ff=ff(1:200);
        F(i,:)=filter(ff,1,gg);
        Ft(i,:)=gtrue(i,1:300);
    end

    htrue=htrue(1:300);
    gtrue=gtrue(:,1:300);


    off0=size(err0,2);
    off1=size(err1,2);
    err1(1+off1)=norm(htrue-gg)/norm(htrue);%l'htrue potrebbe essere un impulso ma non è mai nulla
    t1=1;
    t0=0;
    for i=1:m;
        if Ind(i+1)==1;
            t1=t1+1;
            err1(t1+off1)=norm(gtrue(i,:)-F(i,:))/norm(gtrue(i,:));
        else
            t0=t0+1;
            err0(t0+off0)=norm(gtrue(i,:)-F(i,:));
        end
    end
    
    err(1)=norm(htrue-gg)/norm(htrue);
    Err1=norm(htrue-gg);
    Err2=norm(htrue);
    for i=1:m;
        err(i+1)=norm(gtrue(i,:)-F(i,:))/norm(gtrue(i,:));
        Err1=Err1+norm(gtrue(i,:)-F(i,:));
        Err2=Err2+norm(gtrue(i,:));
    end
    Err=Err1/Err2;
    
    lf=size(F(1,:),2); 
    M = idpoly(1,F,gg(1:230)/gg(1),1,ones(m,1),nv,1);
    for gi=1:nM;
        pred=predict(M,[ytest_tot Utest],Multi(gi));pred=iddata(pred);pred=pred.y;
        RM(gi)=norm(pred((p2+1):(p2+nt))-ytest)/sqrt(nt);
        R2M(gi)=1-sum([pred((p2+1):(p2+nt))-ytest].^2)/VarTest;
    end

else%PEM
    
    %[err,Err,gh]=subsp2(stima,gtrue,htrue)
    stima=eta;
    
    m=min(size(gtrue));
    T = 300; %length of impulse response to compute L2 norm

    if size(gtrue,1)>size(gtrue,2);
        gtrue=gtrue';
    end
    if size(htrue,1)>size(htrue,2);
        htrue=htrue';
    end
    gtrue=gtrue(:,1:(T+1));
    htrue=htrue(:,1:(T+1));

    C=stima.c;
    BB=stima.b;
    A=stima.a;ll1=size(A,2)-size(BB,2);ll2=size(A,2)-size(C,2);
    for i=1:m
        B(i,:)=BB(i,:);
    end
    B=[B zeros(m,ll1)];
    C=stima.c;C=[C zeros(1,ll2)];
    Lambda=stima.noisevariance;

    for i=1:m;
        gh(i,:)=impulse(tf(B(i,:),A,1),T);
    end
    hh=impulse(tf(C*sqrt(Lambda),A,1),T)';

    off0=size(err0,2);
    off1=size(err1,2);
    err1(1+off1)=norm(htrue-hh)/norm(htrue);%l'htrue potrebbe essere un impulso ma non è mai nulla
    t1=1;
    t0=0;
    for i=1:m;
        if Ind(i+1)==1;
            t1=t1+1;
            err1(t1+off1)=norm(gtrue(i,:)-gh(i,:))/norm(gtrue(i,:));
        else
            t0=t0+1;
            err0(t0+off0)=norm(gtrue(i,:)-gh(i,:));
        end
    end
   
    err(1)=norm(htrue-hh)/norm(htrue);
    Err1=norm(htrue-hh);
    Err2=norm(htrue);
    for i=1:m;
        err(i+1)=norm(gtrue(i,:)-gh(i,:))/norm(gtrue(i,:));
        Err1=Err1+norm(gtrue(i,:)-gh(i,:));
        Err2=Err2+norm(gtrue(i,:));
    end
    Err=Err1/Err2;
    
    M=stima;
    for gi=1:nM;
        pred=predict(M,[ytest_tot Utest],Multi(gi));pred=iddata(pred);pred=pred.y;
        RM(gi)=norm(pred((p2+1):(p2+nt))-ytest)/sqrt(nt);
        R2M(gi)=1-sum([pred((p2+1):(p2+nt))-ytest].^2)/VarTest;%norm([pred((p2+1):(p2+nt))-ytest]./ytest,1)/nt;
    end
    
    F=[];
    gg=[];

end

% l1=m+1-size(err0,2);
% err0=[err0 inf*ones(1,l1)];
% l2=m+1-size(err1,2);
% err1=[err1 inf*ones(1,l2)];
