clc;clear all;close all;
% nacteni InputVector
load InputVector;
InputVector1 = InputVector(:,1:2540);    %pro aritmii N
InputVectorL = InputVector(:,2541:2740);    %pro aritmii L
InputVector2 = InputVector(:,2741:4168);    %pro ost
InputVector = [InputVectorL InputVectorL InputVectorL InputVectorL InputVectorL InputVectorL InputVector1 InputVector2];

% deklarace Targetvektoru
o = ones(1,1200); z = zeros(1,3968); TargetVector = [o z];

% deklarace site
PR = minmax(InputVector);
Layers = [50 50 1];
TransFcns = {'logsig' 'logsig' 'logsig'};
BTF = 'trainscg';
BLF =  'learngd';
PF = 'mse';
net = newff(PR,Layers,TransFcns,BTF,BLF,PF);
% ......................................................

%Inicializace
net = init(net);

%Trenovani site
net.trainParam.epochs = 1000;
net.trainParam.goal = 0.00001; 
net.trainParam
% ......................................................
[net,tr] = train(net,InputVector,TargetVector);
% ......................................................
spravna_data = TargetVector;

%Simulace natrenovanych signalu
OutputVector = sim(net,InputVector);
vysledek_simulace = OutputVector;

% vyhodnoceni chyb
for i=1:length(OutputVector)
    R(i)= OutputVector(i) - TargetVector(i);
end
chyby_simulace = R;
chyby_simulaceL =chyby_simulace(1:1200);
chyby_simulaceost =chyby_simulace(1201:5168);
% ......................................................
for i=1:1:length(chyby_simulaceL)
    if abs(chyby_simulaceL(i)) < 0.4
         VN(i)=0;
    else VN(i)=1;
    end
end
VN'; T = zeros(1200,1);
chyba_natrenovanych_sigL = sum(or(VN',T));
% ......................................................
for i=1:1:length(chyby_simulaceost)
    if abs(chyby_simulaceost(i)) < 0.4
         VV(i)=0;
    else VV(i)=1;
    end
end
VV'; T = zeros(3968,1);
chyba_natrenovanych_sigost = sum(or(VV',T));
% ......................................................

% graficke zobrazeni
figure()
subplot(3,1,1)
plot(spravna_data'); title('Spravna data');
xlabel('pocet vzorku'); ylabel('Target');
subplot(3,1,2)
plot(vysledek_simulace'); title('Vysledek simulace natrenovanych vzorku');
xlabel('pocet vzorku'); ylabel('Target');
subplot(3,1,3)
plot(chyby_simulace'); title('Chyby simulace natrenovanych vzorku');
xlabel('pocet vzorku'); ylabel('chyba');
hold on
x = linspace(1,5168,5168); y = 0.4*ones(1,5168);
plot(x,y, 'r');
% ......................................................
procentni_chybaL = (chyba_natrenovanych_sigL/1200)*100
% ......................................................
procentni_chybaost = (chyba_natrenovanych_sigost/3968)*100
% ......................................................
