clc;clear all;close all;
% nacteni InputVector
load InputVector;
InputVector1 = InputVector(:,1:2740);    %pro aritmii ost
InputVectorR = InputVector(:,2741:3030);    %pro ostatni aritmie R
InputVector2 = InputVector(:,3031:4168);    %pro aritmii ost
InputVectorpom = [InputVector1 InputVector2];
c = randperm(3878);
InputVectorost = InputVectorpom(:,c);
InputVectorost = InputVectorost(:,1:290);
InputVector = [InputVectorR InputVectorost];

% deklarace Targetvektoru
o = ones(1,290); z = zeros(1,290); 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_simulaceR =chyby_simulace(1:290);
chyby_simulaceost =chyby_simulace(291:580);
% ......................................................
for i=1:1:length(chyby_simulaceR)
    if abs(chyby_simulaceR(i)) < 0.4
         VN(i)=0;
    else VN(i)=1;
    end
end
VN'; T = zeros(290,1);
chyba_natrenovanych_sigR = 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(290,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,580,580); y = 0.4*ones(580);
plot(x,y, 'r');
% ......................................................
procentni_chybaR = (chyba_natrenovanych_sigR/290)*100
% ......................................................
procentni_chybaost = (chyba_natrenovanych_sigost/290)*100
% ......................................................
