clc; clear all;
warning off

load Dati_Matrice_id.mat

tic

%intervallo dati di validazione
val_start=1100;
val_stop=1750;

%% sseh
fid = fopen('Validazione/sseh_yy.txt', 'w+');

str_spar = 'Sparsità:\t';
str_num_np = '';
str_nome_dati = '';
str_dati = '';

for num_sens=1:length(Dati) % scelta sensore
    
    yf = Dati(1,num_sens).t.y(val_start:val_stop,:);
    Uf = Dati(1,num_sens).t.Udati(val_start:val_stop,:);
    dat_val = iddata(yf,Uf);
    
    [len_p len_n] = size(Dati(1,num_sens).t.sseh_yy);
    
    str_nome_id = ['\tDati(1,' num2str(num_sens) ').t.sseh_yy(p,n).Mnpf'];
    fprintf(fid, [str_nome_id, '\n']);
    
    
    disp(['Sensore: Dati(1,' num2str(num_sens) ').t.sseh_yy(p,n).Mnpf']);

    for k=1:30 % passo di predizione
        
        str_dati = [str_dati, num2str(k) '\t'];
        
        for num_n=1:len_n
            for num_p=1:len_p
                               

                if k==1
                    spars = sparsi(Dati(1,num_sens).t.sseh_yy(num_p,num_n).Mnpf);
                    str_spar = [str_spar num2str(spars) '%%\t\t\t'];
                    str_num_np = [str_num_np 'n=' num2str(Dati(1,num_sens).t.sseh_yy(num_p,num_n).n_) ' p=' num2str(Dati(1,num_sens).t.sseh_yy(num_p,num_n).p_) '\t\t\t'];
                    str_nome_dati = [str_nome_dati 'fit\trms\tcod\t'];
                end
                
                compare(dat_val,Dati(1,num_sens).t.sseh_yy(num_p,num_n).Mnpf,k)
                y_pred = predict(Dati(1,num_sens).t.sseh_yy(num_p,num_n).Mnpf,dat_val,k);
                y_pred = iddata(y_pred);
                y_pred = y_pred.y;
                
                %Calcolo indici
                fit = (1 - norm(yf-y_pred)/norm(yf-mean(yf)));
                rms = sqrt(mean((yf-y_pred).^2));
                cod = 1-(rms^2/(mean((yf-mean(yf)).^2)));
                
                str_dati = [str_dati, num2str(fit) '\t', num2str(rms) '\t', num2str(cod) '\t'];
                
            end
        end
        str_dati = [str_dati, '\n'];
    end
    
    fprintf(fid, ['\t' str_num_np, '\n']);
    fprintf(fid, [str_spar '\n']);
    fprintf(fid, ['\t' str_nome_dati, '\n']);
    fprintf(fid, [str_dati, '\n \n \n' ]);
    
    str_spar = 'Sparsità:\t';
    str_num_np = '';
    str_nome_dati = '';
    str_dati = '';
end

fclose(fid);


fid = fopen('Validazione/sseh_ny.txt', 'w+');

str_spar = 'Sparsità:\t';
str_num_np = '';
str_nome_dati = '';
str_dati = '';

for num_sens=1:length(Dati) % scelta sensore
    
    yf = Dati(1,num_sens).t.y(val_start:val_stop,:);
    Uf = Dati(1,num_sens).t.Udati(val_start:val_stop,:);
    dat_val = iddata(yf,Uf);
    
    [len_p len_n] = size(Dati(1,num_sens).t.sseh_ny);
    
    str_nome_id = ['\tDati(1,' num2str(num_sens) ').t.sseh_ny(p,n).Mnpf'];
    fprintf(fid, [str_nome_id, '\n']);

    disp(['Sensore: Dati(1,' num2str(num_sens) ').t.sseh_ny(p,n).Mnpf']);

    for k=1:30 % passo di predizione
        
        str_dati = [str_dati, num2str(k)];
        
        for num_n=1:len_n
            for num_p=1:len_p
                
                if k==1
                    spars = sparsi(Dati(1,num_sens).t.sseh_yy(num_p,num_n).Mnpf);
                    str_spar = [str_spar num2str(spars) '%%\t\t\t'];
                    str_num_np = [str_num_np 'n=' num2str(Dati(1,num_sens).t.sseh_ny(num_p,num_n).n_) ' p=' num2str(Dati(1,num_sens).t.sseh_ny(num_p,num_n).p_) '\t\t\t'];
                    str_nome_dati = [str_nome_dati 'fit\trms\tcod\t'];
                end
                
                y_pred = predict(Dati(1,num_sens).t.sseh_ny(num_p,num_n).Mnpf,dat_val,k);
                y_pred = iddata(y_pred);
                y_pred = y_pred.y;
                
                %Calcolo indici
                fit = 100*(1 - norm(yf-y_pred)/norm(yf-mean(yf)));
                rms = sqrt(mean((yf-y_pred).^2));
                cod = 1-(rms^2/(mean((yf-mean(yf)).^2)));
                
                str_dati = [str_dati, num2str(fit) '\t', num2str(rms) '\t', num2str(cod) '\t'];
                
            end
        end
        str_dati = [str_dati, '\n'];
    end
    
    fprintf(fid, ['\t' str_num_np, '\n']);    
    fprintf(fid, [str_spar '\n']);
    fprintf(fid, ['\t' str_nome_dati, '\n']);
    fprintf(fid, [str_dati, '\n \n \n' ]);
    
    str_spar = 'Sparsità:\t';
    str_num_np = '';
    str_nome_dati = '';
    str_dati = '';
end

fclose(fid);

%% sseh_norm

fid = fopen('Validazione/sseh_yy_norm_Mnpf.txt', 'w+');

str_spar = 'Sparsità:\t';
str_num_np = '';
str_nome_dati = '';
str_dati = '';

for num_sens=1:length(Dati) % scelta sensore 
    
    yf = Dati(1,num_sens).t.y_norm(val_start:val_stop,:);
    Uf = Dati(1,num_sens).t.Udati_norm(val_start:val_stop,:);
    dat_val = iddata(yf,Uf);
    
    [len_p len_n] = size(Dati(1,num_sens).t.sseh_ny_norm);
    
    str_nome_id = ['\tDati(1,' num2str(num_sens) ').t.sseh_yy_norm(p,n).Mnpf'];
    fprintf(fid, [str_nome_id, '\n']);
    
    disp(['Sensore: Dati(1,' num2str(num_sens) ').t.sseh_yy_norm(p,n).Mnpf']);
    
    for k=1:30 % passo di predizione
        

        str_dati = [str_dati, num2str(k), '\t'];
        
        for num_n=1:len_n
            for num_p=1:len_p
                
                if k==1
                    spars = sparsi(Dati(1,num_sens).t.sseh_yy_norm(num_p,num_n).Mnpf);
                    str_spar = [str_spar num2str(spars) '%%\t\t\t'];
                    str_num_np = [str_num_np 'n=' num2str(Dati(1,num_sens).t.sseh_yy_norm(num_p,num_n).n_) ' p=' num2str(Dati(1,num_sens).t.sseh_yy_norm(num_p,num_n).p_) '\t\t\t'];
                    str_nome_dati = [str_nome_dati 'fit\trms\tcod\t'];
                end
                
                y_pred = predict(Dati(1,num_sens).t.sseh_yy_norm(num_p,num_n).Mnpf,dat_val,k);
                y_pred = iddata(y_pred);
                y_pred = y_pred.y;
                
                %Calcolo indici
                fit = (1 - norm(yf-y_pred)/norm(yf-mean(yf)));
                rms = sqrt(mean((yf-y_pred).^2));
                cod = 1-(rms^2/(mean((yf-mean(yf)).^2)));
                
                
                str_dati = [str_dati, num2str(fit) '\t', num2str(rms) '\t', num2str(cod) '\t'];
                
            end
        end
        str_dati = [str_dati, '\n'];
    end
    
    fprintf(fid, ['\t' str_num_np, '\n']);    
    fprintf(fid, [str_spar '\n']);
    fprintf(fid, ['\t' str_nome_dati, '\n']);
    fprintf(fid, [str_dati, '\n \n \n' ]);
    
    str_spar = 'Sparsità:\t';
    str_num_np = '';
    str_nome_dati = '';
    str_dati = '';
    
end

fclose(fid);

fid = fopen('Validazione/sseh_ny_norm_Mnpf.txt', 'w+');

str_spar = 'Sparsità:\t';
str_num_np = '';
str_nome_dati = '';
str_dati = '';

for num_sens=1:length(Dati) % scelta sensore 
    
    yf = Dati(1,num_sens).t.y_norm(val_start:val_stop,:);
    Uf = Dati(1,num_sens).t.Udati_norm(val_start:val_stop,:);
    dat_val = iddata(yf,Uf);
    
    [len_p len_n] = size(Dati(1,num_sens).t.sseh_ny_norm);
    
    str_nome_id = ['\tDati(1,' num2str(num_sens) ').t.sseh_ny_norm(p,n).Mnpf'];
    fprintf(fid, [str_nome_id, '\n']);
    
    disp(['Sensore: Dati(1,' num2str(num_sens) ').t.sseh_ny_norm(p,n).Mnpf']);
    
    for k=1:30 % passo di predizione
        

        str_dati = [str_dati, num2str(k), '\t'];
        
        for num_n=1:len_n
            for num_p=1:len_p
                
                if k==1
                    spars = sparsi(Dati(1,num_sens).t.sseh_ny_norm(num_p,num_n).Mnpf);
                    str_spar = [str_spar num2str(spars) '%%\t\t\t'];
                    str_num_np = [str_num_np 'n=' num2str(Dati(1,num_sens).t.sseh_ny_norm(num_p,num_n).n_) ' p=' num2str(Dati(1,num_sens).t.sseh_ny_norm(num_p,num_n).p_) '\t\t\t'];
                    str_nome_dati = [str_nome_dati 'fit\trms\tcod\t'];
                end
                
                y_pred = predict(Dati(1,num_sens).t.sseh_ny_norm(num_p,num_n).Mnpf,dat_val,k);
                y_pred = iddata(y_pred);
                y_pred = y_pred.y;
                
                %Calcolo indici
                fit = (1 - norm(yf-y_pred)/norm(yf-mean(yf)));
                rms = sqrt(mean((yf-y_pred).^2));
                cod = 1-(rms^2/(mean((yf-mean(yf)).^2)));
                
                
                str_dati = [str_dati, num2str(fit) '\t', num2str(rms) '\t', num2str(cod) '\t'];
                
            end
        end
        str_dati = [str_dati, '\n'];
    end
    
    fprintf(fid, ['\t' str_num_np, '\n']);    
    fprintf(fid, [str_spar '\n']);
    fprintf(fid, ['\t' str_nome_dati, '\n']);
    fprintf(fid, [str_dati, '\n \n \n' ]);
    
    str_spar = 'Sparsità:\t';
    str_num_np = '';
    str_nome_dati = '';
    str_dati = '';
    
end

fclose(fid);

%% ssglar

fid = fopen('Validazione/ssglar_Mnpg.txt', 'w+');

str_spar = 'Sparsità:\t';
str_num_np = '';
str_nome_dati = '';
str_dati = '';

for num_sens=1:length(Dati) % scelta sensore 
    
    yf = Dati(1,num_sens).t.y_norm(val_start:val_stop,:);
    Uf = Dati(1,num_sens).t.Udati_norm(val_start:val_stop,:);
    dat_val = iddata(yf,Uf);
    
    [len_p len_n] = size(Dati(1,num_sens).t.ssglar.mod);
    
    str_nome_id = ['\tDati(1,' num2str(num_sens) ').t.ssglar.mod(p,n).Mnpg'];
    fprintf(fid, [str_nome_id, '\n']);
    
    disp(['Sensore: Dati(1,' num2str(num_sens) ').t.ssglar.mod(p,n).Mnpg']);
    
    for k=1:30 % passo di predizione
        

        str_dati = [str_dati, num2str(k), '\t'];
        
        for num_n=1:len_n
            for num_p=1:len_p
                
                if k==1
                    spars = sparsi(Dati(1,num_sens).t.ssglar.mod(num_p,num_n).Mnpg);
                    str_spar = [str_spar num2str(spars) '%%\t\t\t'];
                    str_num_np = [str_num_np 'n=' num2str(Dati(1,num_sens).t.ssglar.mod(num_p,num_n).n) ' p=' num2str(Dati(1,num_sens).t.ssglar.mod(num_p,num_n).pRKHS) '\t\t\t'];
                    str_nome_dati = [str_nome_dati 'fit\trms\tcod\t'];
                end
                
                y_pred = predict(Dati(1,num_sens).t.ssglar.mod(num_p,num_n).Mnpg,dat_val,k);
                y_pred = iddata(y_pred);
                y_pred = y_pred.y;
                
                %Calcolo indici
                fit = (1 - norm(yf-y_pred)/norm(yf-mean(yf)));
                rms = sqrt(mean((yf-y_pred).^2));
                cod = 1-(rms^2/(mean((yf-mean(yf)).^2)));
                
                
                str_dati = [str_dati, num2str(fit) '\t', num2str(rms) '\t', num2str(cod) '\t'];
                
            end
        end
        str_dati = [str_dati, '\n'];
    end
    
    fprintf(fid, ['\t' str_num_np, '\n']);    
    fprintf(fid, [str_spar '\n']);
    fprintf(fid, ['\t' str_nome_dati, '\n']);
    fprintf(fid, [str_dati, '\n \n \n' ]);
    
    str_spar = 'Sparsità:\t';
    str_num_np = '';
    str_nome_dati = '';
    str_dati = '';
    
end

fclose(fid);

fid = fopen('Validazione/ssglar_Mnpc.txt', 'w+');

str_spar = 'Sparsità:\t';
str_num_np = '';
str_nome_dati = '';
str_dati = '';

for num_sens=1:length(Dati) % scelta sensore   
    
    yf = Dati(1,num_sens).t.y(val_start:val_stop,:);
    Uf = Dati(1,num_sens).t.Udati(val_start:val_stop,:);
    dat_val = iddata(yf,Uf);
    
    [len_p len_n] = size(Dati(1,num_sens).t.ssglar.mod);
    
    str_nome_id = ['\tDati(1,' num2str(num_sens) ').t.ssglar.mod(p,n).Mnpc'];
    fprintf(fid, [str_nome_id, '\n']);
    
    disp(['Sensore: Dati(1,' num2str(num_sens) ').t.ssglar.mod(p,n).Mnpc']);

    for k=1:30 % passo di predizione
        

        str_dati = [str_dati, num2str(k), '\t'];
        
        for num_n=1:len_n
            for num_p=1:len_p
                
                if k==1
                    spars = sparsi(Dati(1,num_sens).t.ssglar.mod(num_p,num_n).Mnpc);
                    str_spar = [str_spar num2str(spars) '%%\t\t\t'];
                    str_num_np = [str_num_np 'n=' num2str(Dati(1,num_sens).t.ssglar.mod(num_p,num_n).n) ' p=' num2str(Dati(1,num_sens).t.ssglar.mod(num_p,num_n).pRKHS) '\t\t\t'];
                    str_nome_dati = [str_nome_dati 'fit\trms\tcod\t'];
                end
                
                y_pred = predict(Dati(1,num_sens).t.ssglar.mod(num_p,num_n).Mnpc,dat_val,k);
                y_pred = iddata(y_pred);
                y_pred = y_pred.y;
                
                %Calcolo indici
                fit = (1 - norm(yf-y_pred)/norm(yf-mean(yf)));
                rms = sqrt(mean((yf-y_pred).^2));
                cod = 1-(rms^2/(mean((yf-mean(yf)).^2)));
                
                
                str_dati = [str_dati, num2str(fit) '\t', num2str(rms) '\t', num2str(cod) '\t'];
                
            end
        end
        str_dati = [str_dati, '\n'];
    end
    
    fprintf(fid, ['\t' str_num_np, '\n']);    
    fprintf(fid, [str_spar '\n']);
    fprintf(fid, ['\t' str_nome_dati, '\n']);
    fprintf(fid, [str_dati, '\n \n \n' ]);
    
    str_spar = 'Sparsità:\t';
    str_num_np = '';
    str_nome_dati = '';
    str_dati = '';
    
end

fclose(fid);


%% ssglar_norm

fid = fopen('Validazione/ssglar_norm_Mnpg.txt', 'w+');

str_spar = 'Sparsità:\t';
str_num_np = '';
str_nome_dati = '';
str_dati = '';

for num_sens=1:length(Dati) % scelta sensore 
    
    yf = Dati(1,num_sens).t.y_norm(val_start:val_stop,:);
    Uf = Dati(1,num_sens).t.Udati_norm(val_start:val_stop,:);
    dat_val = iddata(yf,Uf);
    
    [len_p len_n] = size(Dati(1,num_sens).t.ssglar_norm.mod);
    
    str_nome_id = ['\tDati(1,' num2str(num_sens) ').t.ssglar_norm.mod(p,n).Mnpg'];
    fprintf(fid, [str_nome_id, '\n']);
    
    disp(['Sensore: Dati(1,' num2str(num_sens) ').t.ssglar_norm.mod(p,n).Mnpg']);
    
    for k=1:30 % passo di predizione
        

        str_dati = [str_dati, num2str(k), '\t'];
        
        for num_n=1:len_n
            for num_p=1:len_p
                
                if k==1 
                    spars = sparsi(Dati(1,num_sens).t.ssglar_norm.mod(num_p,num_n).Mnpg);
                    str_spar = [str_spar num2str(spars) '%%\t\t\t'];
                    str_num_np = [str_num_np 'n=' num2str(Dati(1,num_sens).t.ssglar_norm.mod(num_p,num_n).n) ' p=' num2str(Dati(1,num_sens).t.ssglar_norm.mod(num_p,num_n).pRKHS) '\t\t\t'];
                    str_nome_dati = [str_nome_dati 'fit\trms\tcod\t'];
                end
                
                y_pred = predict(Dati(1,num_sens).t.ssglar_norm.mod(num_p,num_n).Mnpg,dat_val,k);
                y_pred = iddata(y_pred);
                y_pred = y_pred.y;
                
                %Calcolo indici
                fit = (1 - norm(yf-y_pred)/norm(yf-mean(yf)));
                rms = sqrt(mean((yf-y_pred).^2));
                cod = 1-(rms^2/(mean((yf-mean(yf)).^2)));
                
                
                str_dati = [str_dati, num2str(fit) '\t', num2str(rms) '\t', num2str(cod) '\t'];
                
            end
        end
        str_dati = [str_dati, '\n'];
    end
    
    fprintf(fid, ['\t' str_num_np, '\n']);    
    fprintf(fid, [str_spar '\n']);
    fprintf(fid, ['\t' str_nome_dati, '\n']);
    fprintf(fid, [str_dati, '\n \n \n' ]);
    
    str_spar = 'Sparsità:\t';
    str_num_np = '';
    str_nome_dati = '';
    str_dati = '';
    
end

fclose(fid);

fid = fopen('Validazione/ssglar_norm_Mnpc.txt', 'w+');

str_spar = 'Sparsità:\t';
str_num_np = '';
str_nome_dati = '';
str_dati = '';

for num_sens=1:length(Dati) % scelta sensore   
    
    yf = Dati(1,num_sens).t.y_norm(val_start:val_stop,:);
    Uf = Dati(1,num_sens).t.Udati_norm(val_start:val_stop,:);
    dat_val = iddata(yf,Uf);
    
    [len_p len_n] = size(Dati(1,num_sens).t.ssglar_norm.mod);
    
    str_nome_id = ['\tDati(1,' num2str(num_sens) ').t.ssglar_norm.mod(p,n).Mnpc'];
    fprintf(fid, [str_nome_id, '\n']);
    
    disp(['Sensore: Dati(1,' num2str(num_sens) ').t.ssglar_norm.mod(p,n).Mnpc']);

    for k=1:30 % passo di predizione
        

        str_dati = [str_dati, num2str(k), '\t'];
        
        for num_n=1:len_n
            for num_p=1:len_p
                
                if k==1
                    spars = sparsi(Dati(1,num_sens).t.ssglar_norm.mod(num_p,num_n).Mnpc);
                    str_spar = [str_spar num2str(spars) '%%\t\t\t'];
                    str_num_np = [str_num_np 'n=' num2str(Dati(1,num_sens).t.ssglar_norm.mod(num_p,num_n).n) ' p=' num2str(Dati(1,num_sens).t.ssglar_norm.mod(num_p,num_n).pRKHS) '\t\t\t'];
                    str_nome_dati = [str_nome_dati 'fit\trms\tcod\t'];
                end
                
                y_pred = predict(Dati(1,num_sens).t.ssglar_norm.mod(num_p,num_n).Mnpc,dat_val,k);
                y_pred = iddata(y_pred);
                y_pred = y_pred.y;
                
                %Calcolo indici
                fit = (1 - norm(yf-y_pred)/norm(yf-mean(yf)));
                rms = sqrt(mean((yf-y_pred).^2));
                cod = 1-(rms^2/(mean((yf-mean(yf)).^2)));
                
                
                str_dati = [str_dati, num2str(fit) '\t', num2str(rms) '\t', num2str(cod) '\t'];
                
            end
        end
        str_dati = [str_dati, '\n'];
    end
    
    fprintf(fid, ['\t' str_num_np, '\n']);    
    fprintf(fid, [str_spar '\n']);
    fprintf(fid, ['\t' str_nome_dati, '\n']);
    fprintf(fid, [str_dati, '\n \n \n' ]);
    
    str_spar = 'Sparsità:\t';
    str_num_np = '';
    str_nome_dati = '';
    str_dati = '';
    
end

fclose(fid);


%% glar

fid = fopen('Validazione/glar_Mgl.txt', 'w+');

str_spar = 'Sparsità:\t';
str_num_np = '';
str_nome_dati = '';
str_dati = '';

for num_sens=1:length(Dati) % scelta sensore 
    
    yf = Dati(1,num_sens).t.y(val_start:val_stop,:);
    Uf = Dati(1,num_sens).t.Udati(val_start:val_stop,:);
    dat_val = iddata(yf,Uf);
    
    [len_p len_n] = size(Dati(1,num_sens).t.glar.mod);
    
    str_nome_id = ['\tDati(1,' num2str(num_sens) ').t.glar.mod(p,n).Mgl'];
    fprintf(fid, [str_nome_id, '\n']);
    
    disp(['Sensore: Dati(1,' num2str(num_sens) ').t.glar.mod(p,n).Mgl']);
    
    for k=1:30 % passo di predizione
        

        str_dati = [str_dati, num2str(k), '\t'];
        
        for num_n=1:len_n
            for num_p=1:len_p
                
                if k==1
                    spars = sparsi(Dati(1,num_sens).t.glar.mod(num_p,num_n).Mgl);
                    str_spar = [str_spar num2str(spars) '%%\t\t\t'];
                    str_num_np = [str_num_np 'n=' num2str(Dati(1,num_sens).t.glar.mod(num_p,num_n).n) '\t\t\t'];
                    str_nome_dati = [str_nome_dati 'fit\trms\tcod\t'];
                end
                
                y_pred = predict(Dati(1,num_sens).t.glar.mod(num_p,num_n).Mgl,dat_val,k);
                y_pred = iddata(y_pred);
                y_pred = y_pred.y;
                
                %Calcolo indici
                fit = (1 - norm(yf-y_pred)/norm(yf-mean(yf)));
                rms = sqrt(mean((yf-y_pred).^2));
                cod = 1-(rms^2/(mean((yf-mean(yf)).^2)));
                
                
                str_dati = [str_dati, num2str(fit) '\t', num2str(rms) '\t', num2str(cod) '\t'];
                
            end
        end
        str_dati = [str_dati, '\n'];
    end
    
    fprintf(fid, ['\t' str_num_np, '\n']);    
    fprintf(fid, [str_spar '\n']);
    fprintf(fid, ['\t' str_nome_dati, '\n']);
    fprintf(fid, [str_dati, '\n \n \n' ]);
    
    str_spar = 'Sparsità:\t';
    str_num_np = '';
    str_nome_dati = '';
    str_dati = '';
    
end

fclose(fid);

fid = fopen('Validazione/glar_Mgls.txt', 'w+');

str_spar = 'Sparsità:\t';
str_num_np = '';
str_nome_dati = '';
str_dati = '';

for num_sens=1:length(Dati) % scelta sensore   
    
    yf = Dati(1,num_sens).t.y(val_start:val_stop,:);
    Uf = Dati(1,num_sens).t.Udati(val_start:val_stop,:);
    dat_val = iddata(yf,Uf);
    
    [len_p len_n] = size(Dati(1,num_sens).t.glar.mod);
    
    str_nome_id = ['\tDati(1,' num2str(num_sens) ').t.glar.mod(p,n).Mgls'];
    fprintf(fid, [str_nome_id, '\n']);
    
    disp(['Sensore: Dati(1,' num2str(num_sens) ').t.glar.mod(p,n).Mgls']);

    for k=1:30 % passo di predizione
        

        str_dati = [str_dati, num2str(k), '\t'];
        
        for num_n=1:len_n
            for num_p=1:len_p
                
                if k==1
                    spars = sparsi(Dati(1,num_sens).t.glar.mod(num_p,num_n).Mgls);
                    str_spar = [str_spar num2str(spars) '%%\t\t\t'];
                    str_num_np = [str_num_np 'n=' num2str(Dati(1,num_sens).t.glar.mod(num_p,num_n).n) '\t\t\t'];
                    str_nome_dati = [str_nome_dati 'fit\trms\tcod\t'];
                end
                
                y_pred = predict(Dati(1,num_sens).t.glar.mod(num_p,num_n).Mgls,dat_val,k);
                y_pred = iddata(y_pred);
                y_pred = y_pred.y;
                
                %Calcolo indici
                fit = (1 - norm(yf-y_pred)/norm(yf-mean(yf)));
                rms = sqrt(mean((yf-y_pred).^2));
                cod = 1-(rms^2/(mean((yf-mean(yf)).^2)));
                
                
                str_dati = [str_dati, num2str(fit) '\t', num2str(rms) '\t', num2str(cod) '\t'];
                
            end
        end
        str_dati = [str_dati, '\n'];
    end
    
    fprintf(fid, ['\t' str_num_np, '\n']);    
    fprintf(fid, [str_spar '\n']);
    fprintf(fid, ['\t' str_nome_dati, '\n']);
    fprintf(fid, [str_dati, '\n \n \n' ]);
    
    str_spar = 'Sparsità:\t';
    str_num_np = '';
    str_nome_dati = '';
    str_dati = '';
    
end

fclose(fid);

%% glar_norm

fid = fopen('Validazione/glar_norm_Mgl.txt', 'w+');

str_spar = 'Sparsità:\t';
str_num_np = '';
str_nome_dati = '';
str_dati = '';

for num_sens=1:length(Dati) % scelta sensore 
    
    yf = Dati(1,num_sens).t.y_norm(val_start:val_stop,:);
    Uf = Dati(1,num_sens).t.Udati_norm(val_start:val_stop,:);
    dat_val = iddata(yf,Uf);
    
    [len_p len_n] = size(Dati(1,num_sens).t.glar_norm.mod);
    
    str_nome_id = ['\tDati(1,' num2str(num_sens) ').t.glar_norm.mod(p,n).Mgl'];
    fprintf(fid, [str_nome_id, '\n']);
    
    disp(['Sensore: Dati(1,' num2str(num_sens) ').t.ssglar_norm.mod(p,n).Mgl']);
    
    for k=1:30 % passo di predizione
        

        str_dati = [str_dati, num2str(k), '\t'];
        
        for num_n=1:len_n
            for num_p=1:len_p
                
                if k==1
                    spars = sparsi(Dati(1,num_sens).t.glar_norm.mod(num_p,num_n).Mgl);
                    str_spar = [str_spar num2str(spars) '%%\t\t\t'];
                    str_num_np = [str_num_np 'n=' num2str(Dati(1,num_sens).t.glar_norm.mod(num_p,num_n).n)  '\t\t\t'];
                    str_nome_dati = [str_nome_dati 'fit\trms\tcod\t'];
                end
                
                y_pred = predict(Dati(1,num_sens).t.glar_norm.mod(num_p,num_n).Mgl,dat_val,k);
                y_pred = iddata(y_pred);
                y_pred = y_pred.y;
                
                %Calcolo indici
                fit = (1 - norm(yf-y_pred)/norm(yf-mean(yf)));
                rms = sqrt(mean((yf-y_pred).^2));
                cod = 1-(rms^2/(mean((yf-mean(yf)).^2)));
                
                
                str_dati = [str_dati, num2str(fit) '\t', num2str(rms) '\t', num2str(cod) '\t'];
                
            end
        end
        str_dati = [str_dati, '\n'];
    end
    
    fprintf(fid, ['\t' str_num_np, '\n']);    
    fprintf(fid, [str_spar '\n']);
    fprintf(fid, ['\t' str_nome_dati, '\n']);
    fprintf(fid, [str_dati, '\n \n \n' ]);
    
    str_spar = 'Sparsità:\t';
    str_num_np = '';
    str_nome_dati = '';
    str_dati = '';
    
end

fclose(fid);

fid = fopen('Validazione/glar_norm_Mgls.txt', 'w+');

str_spar = 'Sparsità:\t';
str_num_np = '';
str_nome_dati = '';
str_dati = '';

for num_sens=1:length(Dati) % scelta sensore   
    
    yf = Dati(1,num_sens).t.y_norm(val_start:val_stop,:);
    Uf = Dati(1,num_sens).t.Udati_norm(val_start:val_stop,:);
    dat_val = iddata(yf,Uf);
    
    [len_p len_n] = size(Dati(1,num_sens).t.glar_norm.mod);
    
    str_nome_id = ['\tDati(1,' num2str(num_sens) ').t.glar_norm.mod(p,n).Mgls'];
    fprintf(fid, [str_nome_id, '\n']);
    
    disp(['Sensore: Dati(1,' num2str(num_sens) ').t.glar_norm.mod(p,n).Mgls']);

    for k=1:30 % passo di predizione
        

        str_dati = [str_dati, num2str(k), '\t'];
        
        for num_n=1:len_n
            for num_p=1:len_p
                
                if k==1
                    spars = sparsi(Dati(1,num_sens).t.glar_norm.mod(num_p,num_n).Mgls);
                    str_spar = [str_spar num2str(spars) '%%\t\t\t'];
                    str_num_np = [str_num_np 'n=' num2str(Dati(1,num_sens).t.glar_norm.mod(num_p,num_n).n) '\t\t\t'];
                    str_nome_dati = [str_nome_dati 'fit\trms\tcod\t'];
                end
                
                y_pred = predict(Dati(1,num_sens).t.glar_norm.mod(num_p,num_n).Mgls,dat_val,k);
                y_pred = iddata(y_pred);
                y_pred = y_pred.y;
                
                %Calcolo indici
                fit = (1 - norm(yf-y_pred)/norm(yf-mean(yf)));
                rms = sqrt(mean((yf-y_pred).^2));
                cod = 1-(rms^2/(mean((yf-mean(yf)).^2)));
                
                
                str_dati = [str_dati, num2str(fit) '\t', num2str(rms) '\t', num2str(cod) '\t'];
                
            end
        end
        str_dati = [str_dati, '\n'];
    end
    
    fprintf(fid, ['\t' str_num_np, '\n']);    
    fprintf(fid, [str_spar '\n']);
    fprintf(fid, ['\t' str_nome_dati, '\n']);
    fprintf(fid, [str_dati, '\n \n \n' ]);
    
    str_spar = 'Sparsità:\t';
    str_num_np = '';
    str_nome_dati = '';
    str_dati = '';
    
end

fclose(fid);


%% pem

fid = fopen('Validazione/pem_Mpem_BIC.txt', 'w+');

str_num_np = '';
str_nome_dati = '';
str_dati = '';

for num_sens=1:length(Dati) % scelta sensore 
    
    yf = Dati(1,num_sens).t.y(val_start:val_stop,:);
    Uf = Dati(1,num_sens).t.Udati(val_start:val_stop,:);
    dat_val = iddata(yf,Uf);
    
    [len_p len_n] = size(Dati(1,num_sens).t.pem.mod);
    
    str_nome_id = ['\tDati(1,' num2str(num_sens) ').t.pem.mod(p,n).Mpem_BIC'];
    fprintf(fid, [str_nome_id, '\n']);
    
    disp(['Sensore: Dati(1,' num2str(num_sens) ').t.pem.mod(p,n).Mpem_BIC']);
    
    for k=1:30 % passo di predizione
        

        str_dati = [str_dati, num2str(k), '\t'];
        
        for num_n=1:len_n
            for num_p=1:len_p
                
                if k==1
                    str_num_np = [str_num_np 'n=' num2str(Dati(1,num_sens).t.pem.mod(num_p,num_n).n) '\t\t\t'];
                    str_nome_dati = [str_nome_dati 'fit\trms\tcod\t'];
                end
                
                y_pred = predict(Dati(1,num_sens).t.pem.mod(num_p,num_n).Mpem_BIC,dat_val,k);
                y_pred = iddata(y_pred);
                y_pred = y_pred.y;
                
                %Calcolo indici
                fit = (1 - norm(yf-y_pred)/norm(yf-mean(yf)));
                rms = sqrt(mean((yf-y_pred).^2));
                cod = 1-(rms^2/(mean((yf-mean(yf)).^2)));
                
                
                str_dati = [str_dati, num2str(fit) '\t', num2str(rms) '\t', num2str(cod) '\t'];
                
            end
        end
        str_dati = [str_dati, '\n'];
    end
    
    fprintf(fid, ['\t' str_num_np, '\n\n']);    
    fprintf(fid, ['\t' str_nome_dati, '\n']);
    fprintf(fid, [str_dati, '\n \n \n' ]);
    
    str_num_np = '';
    str_nome_dati = '';
    str_dati = '';
    
end

fclose(fid);

fid = fopen('Validazione/pem_Mpem_AICC.txt', 'w+');

str_num_np = '';
str_nome_dati = '';
str_dati = '';

for num_sens=1:length(Dati) % scelta sensore   
    
    yf = Dati(1,num_sens).t.y(val_start:val_stop,:);
    Uf = Dati(1,num_sens).t.Udati(val_start:val_stop,:);
    dat_val = iddata(yf,Uf);
    
    [len_p len_n] = size(Dati(1,num_sens).t.pem.mod);
    
    str_nome_id = ['\tDati(1,' num2str(num_sens) ').t.pem.mod(p,n).Mpem_AICC'];
    fprintf(fid, [str_nome_id, '\n']);
    
    disp(['Sensore: Dati(1,' num2str(num_sens) ').t.pem.mod(p,n).Mpem_AICC']);

    for k=1:30 % passo di predizione
        

        str_dati = [str_dati, num2str(k), '\t'];
        
        for num_n=1:len_n
            for num_p=1:len_p
                
                if k==1
                    str_num_np = [str_num_np 'n=' num2str(Dati(1,num_sens).t.pem.mod(num_p,num_n).n) '\t\t\t'];
                    str_nome_dati = [str_nome_dati 'fit\trms\tcod\t'];
                end
                
                y_pred = predict(Dati(1,num_sens).t.pem.mod(num_p,num_n).Mpem_AICC,dat_val,k);
                y_pred = iddata(y_pred);
                y_pred = y_pred.y;
                
                %Calcolo indici
                fit = (1 - norm(yf-y_pred)/norm(yf-mean(yf)));
                rms = sqrt(mean((yf-y_pred).^2));
                cod = 1-(rms^2/(mean((yf-mean(yf)).^2)));
                
                
                str_dati = [str_dati, num2str(fit) '\t', num2str(rms) '\t', num2str(cod) '\t'];
                
            end
        end
        str_dati = [str_dati, '\n'];
    end
    
    fprintf(fid, ['\t' str_num_np, '\n\n']);    
    fprintf(fid, ['\t' str_nome_dati, '\n']);
    fprintf(fid, [str_dati, '\n \n \n' ]);
    
    str_num_np = '';
    str_nome_dati = '';
    str_dati = '';
    
end

fclose(fid);

%% pem_norm

fid = fopen('Validazione/pem_norm_Mpem_BIC.txt', 'w+');

str_num_np = '';
str_nome_dati = '';
str_dati = '';

for num_sens=1:length(Dati) % scelta sensore 
    
    yf = Dati(1,num_sens).t.y_norm(val_start:val_stop,:);
    Uf = Dati(1,num_sens).t.Udati_norm(val_start:val_stop,:);
    dat_val = iddata(yf,Uf);
    
    [len_p len_n] = size(Dati(1,num_sens).t.pem_norm);
    
    str_nome_id = ['\tDati(1,' num2str(num_sens) ').t.pem_norm(p,n).Mpem_BIC'];
    fprintf(fid, [str_nome_id, '\n']);
    
    disp(['Sensore: Dati(1,' num2str(num_sens) ').t.pem_norm(p,n).Mpem_BIC']);
    
    for k=1:30 % passo di predizione
        

        str_dati = [str_dati, num2str(k), '\t'];
        
        for num_n=1:len_n
            for num_p=1:len_p
                
                if k==1
                    str_num_np = [str_num_np 'n=' num2str(Dati(1,num_sens).t.pem_norm(num_p,num_n).n)  '\t\t\t'];
                    str_nome_dati = [str_nome_dati 'fit\trms\tcod\t'];
                end
                
                y_pred = predict(Dati(1,num_sens).t.pem_norm(num_p,num_n).Mpem_BIC,dat_val,k);
                y_pred = iddata(y_pred);
                y_pred = y_pred.y;
                
                %Calcolo indici
                fit = (1 - norm(yf-y_pred)/norm(yf-mean(yf)));
                rms = sqrt(mean((yf-y_pred).^2));
                cod = 1-(rms^2/(mean((yf-mean(yf)).^2)));
                
                
                str_dati = [str_dati, num2str(fit) '\t', num2str(rms) '\t', num2str(cod) '\t'];
                
            end
        end
        str_dati = [str_dati, '\n'];
    end
    
    fprintf(fid, ['\t' str_num_np, '\n\n']);    
    fprintf(fid, ['\t' str_nome_dati, '\n']);
    fprintf(fid, [str_dati, '\n \n \n' ]);
    
    str_num_np = '';
    str_nome_dati = '';
    str_dati = '';
    
end

fclose(fid);

fid = fopen('Validazione/pem_norm_Mpem_AICC.txt', 'w+');

str_num_np = '';
str_nome_dati = '';
str_dati = '';

for num_sens=1:length(Dati) % scelta sensore   
    
    yf = Dati(1,num_sens).t.y_norm(val_start:val_stop,:);
    Uf = Dati(1,num_sens).t.Udati_norm(val_start:val_stop,:);
    dat_val = iddata(yf,Uf);
    
    [len_p len_n] = size(Dati(1,num_sens).t.pem_norm);
    
    str_nome_id = ['\tDati(1,' num2str(num_sens) ').t.pem_norm(p,n).Mpem_AICC'];
    fprintf(fid, [str_nome_id, '\n']);
    
    disp(['Sensore: Dati(1,' num2str(num_sens) ').t.pem_norm(p,n).Mpem_AICC']);

    for k=1:30 % passo di predizione
        

        str_dati = [str_dati, num2str(k), '\t'];
        
        for num_n=1:len_n
            for num_p=1:len_p
                
                if k==1
                    str_num_np = [str_num_np 'n=' num2str(Dati(1,num_sens).t.pem_norm(num_p,num_n).n) '\t\t\t'];
                    str_nome_dati = [str_nome_dati 'fit\trms\tcod\t'];
                end
                
                y_pred = predict(Dati(1,num_sens).t.pem_norm(num_p,num_n).Mpem_AICC,dat_val,k);
                y_pred = iddata(y_pred);
                y_pred = y_pred.y;
                
                %Calcolo indici
                fit = (1 - norm(yf-y_pred)/norm(yf-mean(yf)));
                rms = sqrt(mean((yf-y_pred).^2));
                cod = 1-(rms^2/(mean((yf-mean(yf)).^2)));
                
                
                str_dati = [str_dati, num2str(fit) '\t', num2str(rms) '\t', num2str(cod) '\t'];
                
            end
        end
        str_dati = [str_dati, '\n'];
    end
    
    fprintf(fid, ['\t' str_num_np, '\n\n']);    
    fprintf(fid, ['\t' str_nome_dati, '\n']);
    fprintf(fid, [str_dati, '\n \n \n' ]);
    
    str_num_np = '';
    str_nome_dati = '';
    str_dati = '';
    
end

fclose(fid);

%% n4sid

fid = fopen('Validazione/n4sid.txt', 'w+');

str_num_np = '';
str_nome_dati = '';
str_dati = '';

for num_sens=1:length(Dati) % scelta sensore 
    
    yf = Dati(1,num_sens).t.y(val_start:val_stop,:);
    Uf = Dati(1,num_sens).t.Udati(val_start:val_stop,:);
    dat_val = iddata(yf,Uf);
    
    [len_p len_n] = size(Dati(1,num_sens).t.n4sid.best_mod);
    
    str_nome_id = ['\tDati(1,' num2str(num_sens) ').t.n4sid.best_mod'];
    fprintf(fid, [str_nome_id, '\n']);
    
    disp(['Sensore: Dati(1,' num2str(num_sens) ').t.n4sid.best_mod']);
    
    for k=1:30 % passo di predizione
        

        str_dati = [str_dati, num2str(k), '\t'];
        
        for num_n=1:len_n
            for num_p=1:len_p
                
                if k==1
                    str_num_np = [str_num_np 'n=' num2str(Dati(1,num_sens).t.n4sid.best_mod.n) '\t\t\t'];
                    str_nome_dati = [str_nome_dati 'fit\trms\tcod\t'];
                end
                
                y_pred = predict(Dati(1,num_sens).t.n4sid.best_mod.iden_n4sid,dat_val,k);
                y_pred = iddata(y_pred);
                y_pred = y_pred.y;
                
                %Calcolo indici
                fit = (1 - norm(yf-y_pred)/norm(yf-mean(yf)));
                rms = sqrt(mean((yf-y_pred).^2));
                cod = 1-(rms^2/(mean((yf-mean(yf)).^2)));
                
                
                str_dati = [str_dati, num2str(fit) '\t', num2str(rms) '\t', num2str(cod) '\t'];
                
            end
        end
        str_dati = [str_dati, '\n'];
    end
    
    fprintf(fid, ['\t' str_num_np, '\n\n']);    
    fprintf(fid, ['\t' str_nome_dati, '\n']);
    fprintf(fid, [str_dati, '\n \n \n' ]);
    
    str_num_np = '';
    str_nome_dati = '';
    str_dati = '';
    
end

fclose(fid);



%% n4sid_norm

fid = fopen('Validazione/n4sid_norm.txt', 'w+');

str_num_np = '';
str_nome_dati = '';
str_dati = '';

for num_sens=1:length(Dati) % scelta sensore 
    
    yf = Dati(1,num_sens).t.y_norm(val_start:val_stop,:);
    Uf = Dati(1,num_sens).t.Udati_norm(val_start:val_stop,:);
    dat_val = iddata(yf,Uf);
    
    [len_p len_n] = size(Dati(1,num_sens).t.n4sid_norm.best_mod);
    
    str_nome_id = ['\tDati(1,' num2str(num_sens) ').t.n4sid_norm.best_mod'];
    fprintf(fid, [str_nome_id, '\n']);
    
    disp(['Sensore: Dati(1,' num2str(num_sens) ').t.n4sid_norm.best_mod']);
    
    for k=1:30 % passo di predizione
        

        str_dati = [str_dati, num2str(k), '\t'];
        
        for num_n=1:len_n
            for num_p=1:len_p
                
                if k==1
                    str_num_np = [str_num_np 'n=' num2str(Dati(1,num_sens).t.n4sid_norm.best_mod.n)  '\t\t\t'];
                    str_nome_dati = [str_nome_dati 'fit\trms\tcod\t'];
                end
                
                y_pred = predict(Dati(1,num_sens).t.n4sid_norm.best_mod.iden_n4sid,dat_val,k);
                y_pred = iddata(y_pred);
                y_pred = y_pred.y;
                
                %Calcolo indici
                fit = (1 - norm(yf-y_pred)/norm(yf-mean(yf)));
                rms = sqrt(mean((yf-y_pred).^2));
                cod = 1-(rms^2/(mean((yf-mean(yf)).^2)));
                
                
                str_dati = [str_dati, num2str(fit) '\t', num2str(rms) '\t', num2str(cod) '\t'];
                
            end
        end
        str_dati = [str_dati, '\n'];
    end
    
    fprintf(fid, ['\t' str_num_np, '\n\n']);    
    fprintf(fid, ['\t' str_nome_dati, '\n']);
    fprintf(fid, [str_dati, '\n \n \n' ]);
    
    str_num_np = '';
    str_nome_dati = '';
    str_dati = '';
    
end

fclose(fid);



toc






