clc;clear all;close all;
% nacteni InputVector
load InputVector;
InputVectorN = InputVector(:,1:2540);    %pro aritmii N
InputVectorost = InputVector(:,2541:4168);    %pro ostatni aritmie ost
InputVector = [InputVectorN InputVectorost];

% deklarace Targetvektoru
o = ones(1,2540); z = zeros(1,1628); 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 = 2000;
net.trainParam.goal = 0.0001; 
net.trainParam
% ..................................................
net = 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_simulaceN =chyby_simulace(1:2540);
chyby_simulaceost =chyby_simulace(2541:4168);
% ......................................................
for i=1:1:length(chyby_simulaceN)
    if abs(chyby_simulaceN(i)) < 0.4
         VN(i)=0;
    else VN(i)=1;
    end
end
VN'; T = zeros(2540,1);
chyba_natrenovanych_sigN = sum(or(VN',T));
% ......................................................
for i=1:1:length(chyby_simulaceost)
    if abs(chyby_simulaceV(i)) < 0.4
         VV(i)=0;
    else VV(i)=1;
    end
end
VV'; T = zeros(1628,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(abs(chyby_simulace')); title('Chyby simulace natrenovanych vzorku');
xlabel('pocet vzorku'); ylabel('chyba');
hold on
x = linspace(1,4168,4168); y = 0.4*ones(1,4168);
plot(x,y, 'r');
% ......................................................
procentni_chybaN = (chyba_natrenovanych_sigN/2540)*100
% ......................................................
procentni_chybaost = (chyba_natrenovanych_sigost/1628)*100
% ......................................................
