function [kon_QRS]=konecQRS(SWC,x,org,poloha_S,tep,fvz,prah)

 
kon_QRS=[];
okno=floor((tep/fvz)*23);
    
for i=1 : length(poloha_S)  
        
    if (poloha_S(i)+okno)<length(org)
         [hodnota,poloha]=min(SWC(2,poloha_S(i):poloha_S(i)+okno));
         k=0;
         if abs(hodnota)>(prah/3)
            y=SWC(2,poloha_S(i)+poloha);
         
            while y < -12 && (poloha_S(i)+poloha+k)<length(org);  
                k=k+1;
                y=SWC(2,poloha_S(i)+poloha+k);
            end
            
            if abs(SWC(2,poloha_S(i)+poloha+k))< abs(SWC(2,poloha_S(i)+poloha+k-1))
                kon_QRS = [kon_QRS (poloha_S(i)+poloha+k)];   
            else
                kon_QRS = [kon_QRS (poloha_S(i)+poloha+k-1)]; 
            end
         else
             kon_QRS = [kon_QRS poloha_S(i)];
         end
        
    end
end

 for i=1:3
    for j=1:length (kon_QRS)
        k=kon_QRS(j);
        while k+2<length(org)&& abs(org(i,k)-org(i,k+2))>14
              k=k+1;
        end
        QRS_p(i,j)=k;
    end
 end
 k_kon_QRS=[];
 for i=1:length(QRS_p)
    
     if QRS_p(1,i)==QRS_p(2,i) && QRS_p(1,i)==QRS_p(3,i)
        k_kon_QRS(i)= QRS_p(1,i);
     else 
         m = max([QRS_p(1,i), QRS_p(2,i), QRS_p(3,i)]);
        if m==QRS_p(1,i)
          k_kon_QRS(i)= QRS_p(1,i);
         elseif m==QRS_p(2,i)
            k_kon_QRS(i)= QRS_p(2,i);
         elseif m==QRS_p(3,i)
            k_kon_QRS(i)= QRS_p(3,i);
        end
     end
 end
 kon_QRS=k_kon_QRS;
 

 for i=1:length(kon_QRS)
     k=0;
     while (kon_QRS(i)+k+1)<length(org) && (abs((SWC(2,kon_QRS(i)+k))- (SWC(2,kon_QRS(i)+k+1)))>15)
         k=k+1;
     end
     kon_QRS(i)=kon_QRS(i)+k;
 end
 