clc; clear all;
warning off

load Dati_Matrice_id_chiuso_prova_glar.mat

tic

%intervallo dati di validazione
val_start=1600;
val_stop=2400;


%% glar_norm

fid = fopen('ValidazioneChiuso\glar_norm_Mgl_prova_con_newfile.txt', 'w+');

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

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.glar_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
                
                try
                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'];
                    
                    y_sim = sim(Dati(1,num_sens).t.glar_norm.mod(num_p,num_n).Mgl,dat_val.u);
                    y_sim = iddata(y_sim);
                    y_sim = y_sim.y;

                    %Calcolo indici
                    fit = (1 - norm(yf-y_sim)/norm(yf-mean(yf)));
                    rms = sqrt(mean((yf-y_sim).^2));
                    cod = 1-(rms^2/(mean((yf-mean(yf)).^2)));

                    str_dati_sim = [str_dati_sim, num2str(fit) '\t', num2str(rms) '\t', num2str(cod) '\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)));
                
                catch exception
                    
                fit = NaN;
                rms = NaN;
                cod = NaN; 
                    
                if k==1
                    str_spar = [str_spar num2str(NaN) '%%\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'];
                    str_dati_sim = [str_dati_sim, num2str(fit) '\t', num2str(rms) '\t', num2str(cod) '\t'];
                end
                                   
                end
                
                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]);
    fprintf(fid, [str_dati_sim, '\n \n \n' ]);
    
    str_spar = 'Sparsità:\t';
    str_num_np = '';
    str_nome_dati = '';
    str_dati = '';
    str_dati_sim = 'Spar_sim\t';
    
end

fclose(fid);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fid = fopen('ValidazioneChiuso\glar_norm_Mgls_prova_con_newfile.txt', 'w+');

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

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
                
                try
                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'];
                    
                    y_sim = sim(Dati(1,num_sens).t.glar_norm.mod(num_p,num_n).Mgls,dat_val.u);
                    y_sim = iddata(y_sim);
                    y_sim = y_sim.y;

                    %Calcolo indici
                    fit = (1 - norm(yf-y_sim)/norm(yf-mean(yf)));
                    rms = sqrt(mean((yf-y_sim).^2));
                    cod = 1-(rms^2/(mean((yf-mean(yf)).^2)));

                    str_dati_sim = [str_dati_sim, num2str(fit) '\t', num2str(rms) '\t', num2str(cod) '\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)));
                
                catch exception
                    
                fit = NaN;
                rms = NaN;
                cod = NaN;     
                    
                if k==1
                    spars = NaN;
                    str_spar = [str_spar num2str(NaN) '%%\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'];
                    str_dati_sim = [str_dati_sim, num2str(fit) '\t', num2str(rms) '\t', num2str(cod) '\t'];
                end
                                  
                end
                
                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]);
    fprintf(fid, [str_dati_sim, '\n \n \n' ]);
    
    str_spar = 'Sparsità:\t';
    str_num_np = '';
    str_nome_dati = '';
    str_dati = '';
    str_dati_sim = 'Spar_sim\t';
    
end

fclose(fid);





