% experiment 2 
% Martin Radvansky 
% Diplomova prace 2022
% skript, ktery poskytne veskere obrazky k experimentu 2, lze poustet po
% sekcich, kde je uvedeno, co skript v dane casti ulozi do prislusne slozky
% ploty_fuzzy_porovnani. 

% TUTO CAST PUSTIT VZDY
clc
clear
close all
clear global;

slozka_fuzzy_modely = '.\fuzzy_modely';  % slozka s fuzzy modely
slozka_data = '.\documents';   % slozka, se vstupnimi daty
slozka_ulozeni_plotu = '.\porovnani'; % slozka, kam se maji ukladat vysledky

if ~isfolder(slozka_fuzzy_modely)
  errorMessage = sprintf('Error: The following folder does not exist:\n%s', slozka_fuzzy_modely);
  uiwait(warndlg(errorMessage));
  return;
elseif ~isfolder(slozka_data)
  errorMessage = sprintf('Error: The following folder does not exist:\n%s', slozka_data);
  uiwait(warndlg(errorMessage));
  return;
elseif ~isfolder(slozka_ulozeni_plotu)
  errorMessage = sprintf('Error: The following folder does not exist:\n%s', slozka_ulozeni_plotu);
  uiwait(warndlg(errorMessage));
  return;
end

all_fuzzy = dir(fullfile(slozka_fuzzy_modely,'*.fis'));
all_data = dir(fullfile(slozka_data,'*.csv'));

% zjisteni delky dat 
prvni_data_name = all_data(1).name;
prvni_data_path = fullfile(slozka_data, prvni_data_name);   
delka_dat = size(load(prvni_data_path),2);
        
chyba = 0;
counter_plot = 1;
zapis_soubor = {'Pouzity fuzzy model','Pouzita original data','RMSE'};
%% 64 obrazku, kazdy fuzzy s kazdym original prubehem + RMSE + ulozeni do csv souboru
for k = 1:numel(all_fuzzy)
    curr_fuzzy_name = all_fuzzy(k).name;
    curr_fuzzy_path = fullfile(slozka_fuzzy_modely, curr_fuzzy_name);   
    fuzzy_model = readfis(curr_fuzzy_path);
    y_aprox = evalfis(fuzzy_model,1:delka_dat);
    for i=1:numel(all_data)
        curr_data_name = all_data(i).name;
        curr_data_path = fullfile(slozka_data, curr_data_name);   
        data = load(curr_data_path);
        chyba = sqrt(sum((data-y_aprox').^2));    % RMSE
        f = figure('visible','off');
        plot(1:size(data,2),data(:),'LineWidth',1.5);
        hold on;
        plot(1:size(data,2),y_aprox,'LineWidth',1.5);
        title(sprintf('%s *** %s',curr_data_name,curr_fuzzy_name),'Interpreter','none');
        grid on;
        legend('Original','Aproximace');
        xlabel('Čas');
        ylabel('Vzdálenost k splinu [m]');
        figure_name = sprintf('plot_%d.jpg',counter_plot);
        curr_data_path = fullfile(slozka_ulozeni_plotu, figure_name);
        txt = ['RMSE: ' num2str(chyba)];
        text(580,3.25,txt);
        saveas(f,curr_data_path);
        hold off;
        close(f);
        zapis_soubor(counter_plot+1,1:3)={curr_data_name,curr_fuzzy_name,chyba};
        counter_plot = counter_plot+1;
        chyba = 0;
    end 
end
writecell(zapis_soubor,'.\porovnani\chyby_porovnani.csv');
%% 1 obrazek --- Ulozeni prubehu vsech fuzzy modelu
f = figure('visible','off');
barvicka = ["red","red","green","green","blue","blue","magenta","magenta"];
for k = 1:numel(all_fuzzy)
    curr_fuzzy_name = all_fuzzy(k).name;
    curr_fuzzy_path = fullfile(slozka_fuzzy_modely, curr_fuzzy_name);   
    fuzzy_model = readfis(curr_fuzzy_path);
    y_aprox = evalfis(fuzzy_model,1:delka_dat);
    plot(1:delka_dat,y_aprox,'LineWidth',1.5,'color',barvicka(k));
    hold on;
    grid on;
    xlabel('Čas [Vzorek x 20 ms]');
    ylabel('Vzdálenost k splinu [m]');
    title('Průběh všech fuzzy modelů');
end
legend('Řidič200','','Řidič201','','Řidič202','','Řidič203','');
hold off;
figure_name = sprintf('all_fuzzy.jpg');
curr_data_path = fullfile(slozka_ulozeni_plotu, figure_name);
saveas(f,curr_data_path);
close(f);
%% 1 obrazek --- Ulozeni prubehu original data - 8 krivek
f = figure('visible','off');
barvicka = ["red","red","green","green","blue","blue","magenta","magenta"];
for k = 1:numel(all_data)
    curr_data_name = all_data(k).name;
    curr_data_path = fullfile(slozka_data, curr_data_name);   
    data = load(curr_data_path);
    plot(1:delka_dat,data(:),'LineWidth',1.5,'color',barvicka(k));
    hold on;
    grid on;
    xlabel('Čas [Vzorek x 20 ms]');
    ylabel('Vzdálenost k splinu [m]');
    title('Průběh všech měření');
end
legend('Řidič200','','Řidič201','','Řidič202','','Řidič203','');
hold off;
figure_name = sprintf('all_measuremet_data.jpg');
curr_data_path = fullfile(slozka_ulozeni_plotu, figure_name);
saveas(f,curr_data_path);
close(f);
%% 4 obrazky --- 1 clovek dva fuzzy modely
f = figure('visible','off');
barvicka = ["red","red","green","green","blue","blue","magenta","magenta"];
ridic = ["Řidič200","Řidič201","Řidič202","Řidič203"];
pocitadlo_fuzzy = 1;
for j = 1:(numel(all_fuzzy)/2)
    f = figure('visible','off');
    for k = 1:2
        curr_fuzzy_name = all_fuzzy(pocitadlo_fuzzy).name;
        curr_fuzzy_path = fullfile(slozka_fuzzy_modely, curr_fuzzy_name);   
        fuzzy_model = readfis(curr_fuzzy_path);
        y_aprox = evalfis(fuzzy_model,1:delka_dat);
        plot(1:delka_dat,y_aprox,'LineWidth',1.5,'color',barvicka(pocitadlo_fuzzy));
        hold on;
        grid on;
        xlabel('Čas [Vzorek x 20 ms]');
        ylabel('Vzdálenost k splinu [m]');
        title('Fuzzy modely od jednoho řidiče po týdnu');
        pocitadlo_fuzzy = pocitadlo_fuzzy + 1; 
    end
    hold off;
    legend(ridic(1,j));
    figure_name = sprintf('fuzzy_modely_ridic_%d.jpg',j);
    curr_data_path = fullfile(slozka_ulozeni_plotu, figure_name);
    saveas(f,curr_data_path);
    close(f);
end
%% jeden týden - 2 ploty, original data + fuzzy, druhej plot rozlozeni mfs
vyber_ridice = 1; % ktery ridic a jaky tyden % 1,2 -- ridic200 // 3,4 -- ridic 201 // 5,6 ridic202 // 7,8 ridic203
ridic = [200 200 201 201 202 202 203 203];
curr_fuzzy_name = all_fuzzy(vyber_ridice).name;
curr_fuzzy_path = fullfile(slozka_fuzzy_modely, curr_fuzzy_name);   
fuzzy_model = readfis(curr_fuzzy_path);
y_aprox = evalfis(fuzzy_model,1:delka_dat);
f = figure('visible','off');
curr_data_name = all_data(vyber_ridice).name;
curr_data_path = fullfile(slozka_data, curr_data_name);   
data = load(curr_data_path);  
plot(1:delka_dat,data(:),'LineWidth',1.5);
hold on;
plot(1:delka_dat,y_aprox,'LineWidth',1.5);
title(sprintf('Original průběh X fuzzy model, řidič: %d ',ridic(vyber_ridice)),'Interpreter','none');
grid on;
xlabel('Čas [Vzorek x 20 ms]');
ylabel('Vzdálenost k splinu [m]');
legend('Original data','Fuzzy model');
figure_name = sprintf('ridic_%d_orig_fuzzy_tyden.jpg',ridic(vyber_ridice));
curr_data_path = fullfile(slozka_ulozeni_plotu, figure_name);
saveas(f,curr_data_path);
hold off;
close(f);

f = figure('visible','off');
x = [fuzzy_model.Inputs.MembershipFunctions(1, 1).Parameters(1),fuzzy_model.Inputs.MembershipFunctions(1, 1).Parameters(2),fuzzy_model.Inputs.MembershipFunctions(1, 1).Parameters(3)];
plot(1:delka_dat+1,0);
hold on;
title(sprintf('Nastavené funkce příslušnosti - řidič: %d ',ridic(vyber_ridice)),'Interpreter','none');
ylim([0,1]);
xlim([0,delka_dat+140]);
grid on;
plot(x,[0,1,0],'b','LineWidth',1.4);
x = [fuzzy_model.Inputs.MembershipFunctions(1, 2).Parameters(1),fuzzy_model.Inputs.MembershipFunctions(1, 2).Parameters(2),fuzzy_model.Inputs.MembershipFunctions(1, 2).Parameters(3)];
plot(x,[0,1,0],'b','LineWidth',1.4);
x = [fuzzy_model.Inputs.MembershipFunctions(1, 3).Parameters(1),fuzzy_model.Inputs.MembershipFunctions(1, 3).Parameters(2),fuzzy_model.Inputs.MembershipFunctions(1, 3).Parameters(3)];
plot(x,[0,1,0],'b','LineWidth',1.4);
x = [fuzzy_model.Inputs.MembershipFunctions(1, 4).Parameters(1),fuzzy_model.Inputs.MembershipFunctions(1, 4).Parameters(2),fuzzy_model.Inputs.MembershipFunctions(1, 4).Parameters(3)];
plot(x,[0,1,0],'b','LineWidth',1.4);
xlabel('Čas [Vzorek x 20 ms]');
ylabel('Hodnota příslušnosti [-]');
figure_name = sprintf('ridic_%d_orig_fuzzy_tyden_mfs.jpg',ridic(vyber_ridice));
curr_data_path = fullfile(slozka_ulozeni_plotu, figure_name);
saveas(f,curr_data_path);
hold off;
close(f);

%% jeden týden --- SUBPLOT, original data + fuzzy, druhy plot rozlozeni mfs
vyber_ridice = 7; % ktery ridic a jaky tyden % 1,2 -- ridic200 // 3,4 -- ridic 201 // 5,6 ridic202 // 7,8 ridic203
ridic = [200 200 201 201 202 202 203 203];
curr_fuzzy_name = all_fuzzy(vyber_ridice).name;
curr_fuzzy_path = fullfile(slozka_fuzzy_modely, curr_fuzzy_name);   
fuzzy_model = readfis(curr_fuzzy_path);
y_aprox = evalfis(fuzzy_model,1:delka_dat);
f = figure('visible','off');
subplot(2,1,1);
curr_data_name = all_data(vyber_ridice).name;
curr_data_path = fullfile(slozka_data, curr_data_name);   
data = load(curr_data_path);  
plot(1:delka_dat,data(:),'LineWidth',1.5);
hold on;
plot(1:delka_dat,y_aprox,'LineWidth',1.5);
title(sprintf('Original průběh X fuzzy model, řidič: %d ',ridic(vyber_ridice)),'Interpreter','none');
grid on;
xlabel('Čas [Vzorek x 20 ms]');
ylabel('Vzdálenost k splinu [m]');
legend('Original data','Fuzzy model');
figure_name = sprintf('ridic_%d_orig_fuzzy_tyden_subplot.jpg',ridic(vyber_ridice));
curr_data_path = fullfile(slozka_ulozeni_plotu, figure_name);
hold off;

subplot(2,1,2);
x = [fuzzy_model.Inputs.MembershipFunctions(1, 1).Parameters(1),fuzzy_model.Inputs.MembershipFunctions(1, 1).Parameters(2),fuzzy_model.Inputs.MembershipFunctions(1, 1).Parameters(3)];
plot(1:delka_dat+1,0);
hold on;
title(sprintf('Nastavené funkce příslušnosti - řidič: %d ',ridic(vyber_ridice)),'Interpreter','none');
ylim([0,1]);
xlim([0,delka_dat+140]);
grid on;
plot(x,[0,1,0],'b','LineWidth',1.4);
x = [fuzzy_model.Inputs.MembershipFunctions(1, 2).Parameters(1),fuzzy_model.Inputs.MembershipFunctions(1, 2).Parameters(2),fuzzy_model.Inputs.MembershipFunctions(1, 2).Parameters(3)];
plot(x,[0,1,0],'b','LineWidth',1.4);
x = [fuzzy_model.Inputs.MembershipFunctions(1, 3).Parameters(1),fuzzy_model.Inputs.MembershipFunctions(1, 3).Parameters(2),fuzzy_model.Inputs.MembershipFunctions(1, 3).Parameters(3)];
plot(x,[0,1,0],'b','LineWidth',1.4);
x = [fuzzy_model.Inputs.MembershipFunctions(1, 4).Parameters(1),fuzzy_model.Inputs.MembershipFunctions(1, 4).Parameters(2),fuzzy_model.Inputs.MembershipFunctions(1, 4).Parameters(3)];
plot(x,[0,1,0],'b','LineWidth',1.4);
xlabel('Čas [Vzorek x 20 ms]');
ylabel('Hodnota příslušnosti [-]');
saveas(f,curr_data_path);
hold off;
close(f);