function [ PQ, PS, QRSb, QRSe ] = Q( ECGDER, PR )
% Funkce Q detekuje kmity Q a S, dále pak začátek a konec komplexu QRS. 
% Vstupem je derivovaný signál (ECGDER) a detekované (už zpřesněné) pozice
% kmitů R.

%% Inicializace proměnných
[Nsvod, N] = size(ECGDER);
okno = 65;  
pom = [];
QRSb = [];
QRSe = [];

%% Detekce Q a S
for i = 1:Nsvod
    x = ECGDER(i,:);
    R = PR(i,:);
    for j = 1:length(PR(i,:));
            k = 2;
            Q = 0;
            S = 0;
            Sstr = 0;
            strmold = 0;
            while k<okno-1
                if R(j)<okno
                        if j>length(R)-1
                            k = okno+1;
                        elseif R(j) == 0
                            k = okno+1;
                        else
                        R(1:end-1) = R(2:end);
                        end
                else
                        if (x(R(j)-k)<0 & x(R(j)-k+1)>0 |  x(R(j)-k)>0 & x(R(j)-k+1)<0) & Q==0 & k>9;
                                PQ(i,j) = R(j)-k+1;
                                Q = 1;
                        end
                        if x(R(j)+k+1)>0 & x(R(j)+k)<0 | x(R(j)+k+1)<0 & x(R(j)+k)>0 & S==0;
                                PS(i,j) = R(j)+k;
                                S = 1;
                        end
                        if Q==1 & S==1
                                k = okno+1;
                        end

                        k = k+1;
                        
                        strm = x(R(j)+k)-x(R(j)+k+1);
                        if strm >= strmold & S==0
                            strmpom = k;
                            strmold = strm;
                            Sstr = 1;
                        end
                        
                        if k >45 & S == 0 & Sstr == 1
                            PS(i,j) = R(j)+strmpom+10;
                            S = 1;
                        end
                end
            end
            if k>okno-2 & Q==0
                switch i
                    case i==1
                        PQ(i,1:10) = zeros;
                    case i>1
                PQ(i,1:length(PQ(i-1))) = zeros(length(PQ(i-1)));
                end
            end
    end

    %% Detekce začátku a konce QRS
    window = 15;        
    const = 1.8;
   
    for j = 1:length(PQ(i,:))
            if PQ(i,j) > 2*window & PQ(i,j) ~= 0
                    oknoQRSb = x(PQ(i,j)-window:PQ(i,j)-1);
                    [val, loc] = max(abs(oknoQRSb));
                    locM = PQ(i,j)-window+loc-1;
                    oknoQRSb = x(PQ(i,j)-window+loc-15:locM);       
                    oknoQRSb = fliplr(oknoQRSb);
                    pom = find(abs(oknoQRSb)<(abs(x(locM))/const),1);
                    if isempty(pom) == 1
                            [val, loc] = min(abs(oknoQRSb));
                            QRSb(i,j) = locM-loc;
                    else
                            QRSb(i,j) = PQ(i,j)-window+loc-1-pom;
                    end
            end
    end
    
    window = 25;
    const = 1;
    for j = 1:length(PS(i,:))
            if PS(i,j) < 5000-36
                    oknoQRSe = x(PS(i,j)+1:PS(i,j)+window);
                    [val, loc] = max(abs(oknoQRSe));
                    locM = PS(i,j)+loc;
                    oknoQRSe = x(locM:locM+15);             
                    pom = find(abs(oknoQRSe)<(abs(x(locM))/const),1);
                    if isempty(pom) == 1
                            [val, loc] = min(abs(oknoQRSe));
                            QRSe(i,j) = locM+round(loc/2);
                    else
                            QRSe(i,j) = locM+pom;
                    end
            end                
end
end