function [hodnot] = Kvalita(SNR, Fvz, zasumenec)
%funkcia Kvalita prebieha v reálnom čase a zaraďuje úseky EKG do 3 oblastí
%kvality Q1, Q2 a Q3

%vytvorenie prázdneho obrázka, aby sme segmentáciu mohli sledova v reálnom
%čase
  FigHandle = figure;
  set(FigHandle, 'Position', [0, 400, 1950, 600]);
dlzka=length(SNR);
 axes('xlim',[1,dlzka],'ylim',[-2,5], 'FontSize', 20);
 title('Ukáka real-time segmentovania', 'FontSize', 20)
 xlabel('vzorky [-]')
 ylabel('U [mV]', 'FontSize', 20)
 x=(1:dlzka);
 y=zasumenec;
 lh=line(x,y, 'Color', 'k');
 lb=line([inf,inf],[-2,5], 'LineWidth', 3);
 shg;

hodnot = [];
ciara = [];


if SNR(1) < 0
    predosl = 3;
    zmena=[predosl; predosl; 1];
elseif SNR(1) > 15
    predosl = 1;
    zmena=[2; predosl; 1];
else
    predosl = 2;
    zmena=[2; predosl; 1];
end

j=2;
interval = Fvz*3;

txt = text(inf, 4, 'Q', 'FontSize', 15, 'FontWeight', 'bold');

%%
%podmienky segmentácie
for i = 1:dlzka
     y= zasumenec(1:i);
     set(lh,'ydata',y,'xdata', x(1:i), 'Color', 'k');
     set(lb,'xdata',[i,i]);
     set(txt,'Position',[i 4])
     
     SNR(SNR>=-2 & SNR<0)=0;
     
    if SNR(i) < 0
        set(lb,'Color','r');
        set(txt, 'Color', 'r');
        set(txt, 'String', 'Q3');
        hodnot(i) = 3;
        if predosl ~= hodnot(i)
            zmena(1,j)=predosl;
            zmena(2,j)=hodnot(i);
            zmena(3,j)=i;
            [ciara(1,j), ciara(2,j)] = CiaraKvality(i-1, predosl);
            
            if j>1
                if zmena(3,j)-zmena(3,j-1) < interval && zmena(1,j-1)==3 && zmena(2,j)==3 && predosl == 2
                    hodnot(zmena(3,j-1):(i-1))=3;
                    delete(ciara(1,j));
                    delete(ciara(2,j));
                    if j>2
                        delete(ciara(1,j-1));
                        delete(ciara(2,j-1));
                    end
                elseif zmena(3,j)-zmena(3,j-1) < interval && (zmena(1,j-1)== 1 && zmena(2,j)== 3) && predosl == 2
                    hodnot(zmena(3,j-1):(i-1))=3;
                    if hodnot(zmena(3,j-1)-interval-1:zmena(3,j-1)-1) == 2
                        [ciara(1,j), ciara(2,j)] = CiaraKvality(zmena(3,j-1)-1, 2);
                    end
                    delete(ciara(1,j));
                    delete(ciara(2,j));
                    zmena(1,j)=3;
                elseif zmena(3,j)-zmena(3,j-1) < interval && zmena(1,j-1)==3 && zmena(2,j)==3 && predosl == 1
                    hodnot(zmena(3,j-1):(i-1))=3;
                    delete(ciara(1,j));
                    delete(ciara(2,j));
                    if j>2
                        delete(ciara(1,j-1));
                        delete(ciara(2,j-1));
                    end
                end
            end
            
            j=j+1;
            
        end
        predosl = hodnot(i);
        
    elseif SNR(i) > 15
        set(lb,'Color','g');
        set(txt, 'Color', 'g');
        set(txt, 'String', 'Q1');
        hodnot(i)= 1;
        if predosl ~= hodnot(i)
            zmena(1,j)=predosl;
            zmena(2,j)=hodnot(i);
            zmena(3,j)=i;
            [ciara(1,j), ciara(2,j)] = CiaraKvality(i-1, predosl);
            
            if j>1
                if zmena(3,j)-zmena(3,j-1) < interval && (zmena(1,j-1)== 3 && zmena(2,j)==1) && predosl ==2
                    hodnot(zmena(3,j-1):(i-1))=3;
                    zmena(1,j)=3;
                    delete(ciara(1,j));
                    delete(ciara(2,j));
                    if j>2
                        delete(ciara(1,j-1));
                        delete(ciara(2,j-1));
                    end
                    [ciara(1,j), ciara(2,j)] = CiaraKvality(i-1, 3);
                end
            elseif zmena(3,j)-zmena(3,j-1) < interval && zmena(1,j-1)==1 && zmena(2,j)==1 && predosl == 2
                    hodnot(zmena(3,j-1):(i-1))=1;
                    delete(ciara(1,j));
                    delete(ciara(2,j));
                    if j>2
                        delete(ciara(1,j-1));
                        delete(ciara(2,j-1));
                    end
            end           
            j=j+1;
        end
        predosl = hodnot(i);
        
    else
        set(lb,'Color','b');
        set(txt, 'Color', 'b');
        set(txt, 'String', 'Q2');
        hodnot(i) = 2;
        if predosl ~= hodnot(i)
            zmena(1,j)=predosl;
            zmena(2,j)=hodnot(i);
            zmena(3,j)=i;
            [ciara(1,j), ciara(2,j)] = CiaraKvality(i-1, predosl);
            
            if j>1
                if zmena(3,j)-zmena(3,j-1) < interval && zmena(1,j-1)==2 && zmena(2,j)==2 && predosl == 1
                    hodnot(zmena(3,j-1):(i-1))=2;
                    delete(ciara(1,j));
                    delete(ciara(2,j));
                    if j>2
                        delete(ciara(1,j-1));
                        delete(ciara(2,j-1));
                    end
                elseif zmena(3,j)-zmena(3,j-1) < interval && zmena(1,j-1)==3 && zmena(2,j)==2 && predosl == 1
                    hodnot(zmena(3,j-1):(i-1))=3;
                    delete(ciara(1,j));
                    delete(ciara(2,j));
                    zmena(1,j)=3;
                    if j>2
                        delete(ciara(1,j-1));
                        delete(ciara(2,j-1));
                    end
                    [ciara(1,j), ciara(2,j)] = CiaraKvality(i-1, 3);
                end
            end

                    
            j=j+1;
        end
        predosl = hodnot(i);
        
    end
    pause(0.001);
end

