close all
clear all
clc

%% Klasifikace obratlu cyklu pomoci dopredne BP neuronove site

%% Start

load data_obratle.mat

for i = 1:size(data_obratle,1)
   data_obratle{i} = logical(data_obratle{i});
end

for i = 1:size(data_obratle,1)
   x(i) = size(data_obratle{i},1);
   y(i) = size(data_obratle{i},2);
   z(i) = size(data_obratle{i},3);
end

x = max(x);
y = max(y);
z = max(z);


for i = 1:size(data_obratle,1)
    
    delka = size(data_obratle{i}, 2);
    vyska = size(data_obratle{i}, 1);
    hloubka = size(data_obratle{i}, 3);
    
    r_delka = y - delka;
    r_vyska = x - vyska;
    
    r_delka1 = floor(r_delka/2);
    r_delka2 = ceil(r_delka/2);
    
    r_vyska1 = floor(r_vyska/2);
    r_vyska2 = ceil(r_vyska/2);
   
    
    
    data_obratle{i} = [zeros(r_vyska1, delka, hloubka); data_obratle{i};
        zeros(r_vyska2, delka, hloubka)];
    
    vyska_new = size(data_obratle{1}, 1);
    data_obratle{i} = [zeros(vyska_new, r_delka1, hloubka) data_obratle{i} ...
        zeros(vyska_new, r_delka2, hloubka)];
    
end

z = x*y*z;
for i = 1:size(data_obratle,1)
    p = size(data_obratle{i},1) * size(data_obratle{i},2) * size(data_obratle{i},3);
    
    p1 = floor((z - p)/2);
    p2 = ceil((z - p)/2);
    
    data2{i} = [zeros(1,p1) data_obratle{i}(:)' zeros(1,p2)];
    
end


%% Tvorba ucebni mnoziny

pocet_c3 = 3;
pocet_l3 = 3;
pocet_l5 = 3;
pocet_th4 = 3;
pocet_th10 = 3;

x = data2;
L2 = size(x{1},2);

ind2 = randperm(size(x,2));

S2 = zeros(L2, size(x,2));

S2(:,ind2(1:3)) = [x{1}' x{2}' x{3}'];
S2(:,ind2(4:6)) = [x{4}' x{5}' x{6}'];
S2(:,ind2(7:9)) = [x{7}' x{8}' x{9}'];
S2(:,ind2(10:12)) = [x{12}' x{11}' x{10}'];
S2(:,ind2(13:15)) = [x{13}' x{14}' x{15}'];

% podvzorkovani vstupu s faktorem 15
P = zeros(size(S2,1)/13,15);
for i = 1:15
    P(:,i) = resample(S2(:,i),1,13); % ukladani podvzorkovanych vstupnich vektoru do matice P
end
delka_resamp = size(P,1);
           
D   = zeros(1,15);     
D(ind2(1:3))     = 1;        
D(ind2(4:6))     = 2;        
D(ind2(7:9))     = 3;        
D(ind2(10:12))     = 4;        
D(ind2(13:15))     = 5;        

%% VYTVORENI DOPREDNE BP SITE:

pocet_skryta1 = 100;                                
pocet_skryta2 = 10;                                 
skryte_vrstvy = [pocet_skryta1 pocet_skryta2];      
trenovaci_alg = 'traingd';                          
net = feedforwardnet(skryte_vrstvy, trenovaci_alg); 
net = configure(net, P, D);                         
net=init(net);                                      


%% Parametry


net.performFcn        = 'mse';                    
net.trainParam.epochs = 500;                      
net.trainParam.lr     = 0.1;                      
net.trainParam.goal   = 0.01;                     

net.plotFcns =  {'plotperform', 'plotregression', 'plotconfusion'};

%% VYBAVOVANI NENAUCENE SITE:

Y_pred = net(P);
Y_pred = round(Y_pred);
chyby_neuc = sum(D ~= Y_pred)      
MSE_pred  = perform(net,D,Y_pred)  

%% UCENI SITE

[net, tr] = train(net,P,D); 
%% Vybaveni

Y_po = net(P); 
Y_po = round(Y_po);
chyby = sum(D ~= Y_po); 
disp(['Po trenovani spatne rozpoznano ' num2str(chyby) ' z ' num2str(15) ' signalu.'])
MSE_po=perform(net,Y_po,D)
%% Vysledky
figure
plotperform(tr)         % vykresleni prubehu chybove funkce
figure
plotconfusion(D,Y_po)   % zobrazeni matice zamen
figure
plotroc(D,Y_po)         % vykresleni ROC krivek pro ucebni, validacni a testovaci mnozinu dat
