clc;clear all;close all;
% nacteni InputVector
load InputVector;
InputVector1 = InputVector(:,1:4068);    %pro aritmii ost
InputVectorP = InputVector(:,4069:4168);    %pro ostatni aritmie P

% deklarace Targetvektoru
c = randperm(4068);
InputVectorost = InputVector1(:,c);
InputVectorost = InputVectorost(:,1:100);
InputVector = [InputVectorP InputVectorost];
o = ones(1,100); z = zeros(1,100); 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.0001; 
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_simulaceP =chyby_simulace(1:100);
chyby_simulaceost =chyby_simulace(101:200);
% ......................................................
for i=1:1:length(chyby_simulaceP)
    if abs(chyby_simulaceP(i)) < 0.4
         VN(i)=0;
    else VN(i)=1;
    end
end
VN'; T = zeros(100,1);
chyba_natrenovanych_sigP = 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(100,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,200,200); y = 0.4*ones(1,200);
plot(x,y, 'r');
% ......................................................
procentni_chybaP = (chyba_natrenovanych_sigP/100)*100
% ......................................................
procentni_chybaost = (chyba_natrenovanych_sigost/100)*100
% ......................................................