function [STRES_HR_BIN,STRES_HR_FUZZY,RELAX_HR_BIN,...
          RELAX_HR_FUZZY,STRES_HR_BIN_FYZ,...
          STRES_HR_FUZZY_FYZ,STRES_HR_BIN_PSY,STRES_HR_FUZZY_PSY]=...
          detekce_HR_svm (HR_C_means)
%% shluk V3 validne detekuje stres

STRES_HR_BIN=[];
STRES_HR_FUZZY=[];
RELAX_HR_BIN=[];
RELAX_HR_FUZZY=[];

for i=1:1:size(HR_C_means,2);
if HR_C_means(3,i)>=HR_C_means(1,i)  || HR_C_means(2,i)>=HR_C_means(1,i)
   STRES_HR_BIN(i)=1;
   STRES_HR_FUZZY(i)=HR_C_means(3,i)+HR_C_means(2,i);
   
else STRES_HR_BIN(i)=0;
    STRES_HR_FUZZY(i)=0;
end

if HR_C_means(3,i)>=HR_C_means(1,i)  && HR_C_means(3,i)>=HR_C_means(2,i)
   STRES_HR_BIN_FYZ(i)=1;
   STRES_HR_FUZZY_FYZ(i)=HR_C_means(3,i);
   
else STRES_HR_BIN_FYZ(i)=0;
    STRES_HR_FUZZY_FYZ(i)=0;
end

if HR_C_means(2,i)>=HR_C_means(1,i)  && HR_C_means(2,i)>=HR_C_means(3,i)
   STRES_HR_BIN_PSY(i)=1;
   STRES_HR_FUZZY_PSY(i)=HR_C_means(2,i);
   
else STRES_HR_BIN_PSY(i)=0;
    STRES_HR_FUZZY_PSY(i)=0;
end


if HR_C_means(1,i)>=HR_C_means(2,i) && HR_C_means(1,i)>=HR_C_means(3,i);
   RELAX_HR_BIN(i)=1;
   RELAX_HR_FUZZY(i)=HR_C_means(1,i);
   
else RELAX_HR_BIN(i)=0; 
    RELAX_HR_FUZZY(i)=0;
end

end

% figure ('NAME','-STRES_ACC_BIN')
% stem (tm_AccTempEDA,STRES_ACC_BIN)

%% rozhodovaci pravidlo fyzicky nebo psychycky stres
pom_stres=0;
pom_stres_pozice=[];
pom_stres_zac=[];
pom_stres_kon=[];
okno=[];
pocitadlo=0;

for i=1:1:(size (STRES_HR_BIN,2)-8);

    okno=STRES_HR_BIN(i:i+8);
    okno=sum(okno);
    
    if okno>=1 & pom_stres==0;
        pom_stres=1;  
        pocitadlo=pocitadlo+1;
        pom_stres_zac(pocitadlo)=i;
        pom_stres==1
    end
    
    if okno==0 & pom_stres==1;
        pom_stres_kon(pocitadlo)=i;
        pom_stres=0;  
    end
end

%uprava na stejnou delku matice

if length(pom_stres_kon)~= length(pom_stres_zac)
    konec=size(STRES_HR_BIN,2);
    pom_stres_kon=[pom_stres_kon,konec]
end

pom_stres_pozice=[pom_stres_zac;pom_stres_kon];
pomV2=[];
pomV3=[];
pom=[];
roz=[];
STRES_HR_FYZ=zeros(1,size (STRES_HR_BIN,2));
STRES_HR_PSY=zeros(1,size (STRES_HR_BIN,2));

for i=1:size (pom_stres_pozice,2);
    pom=pom_stres_pozice(:,i);
    pomV2=HR_C_means(2,pom(1):pom(2));
    pomV3=HR_C_means(3,pom(1):pom(2));
    roz=pomV3-pomV2;
    roz=mean (roz);
    
    if roz>=0.2;
    STRES_HR_FYZ(pom(1):pom(2))=1;
    end
     if roz<0.2;
    STRES_HR_PSY(pom(1):pom(2))=1;
    end
    

end

% figure ('NAME','-STRES_ACC_FYZ')
% stem (tm_AccTempEDA,STRES_ACC_FYZ)
% 
% figure ('NAME','-STRES_ACC_PSY_EM')
% stem (tm_AccTempEDA,STRES_ACC_PSY_EM)

end