function [periodicity] = NCC_Periodicita_lin(y, Fs, delka_okna)

    persistent current_state;

    current_state = 1;
    
            
    pocet_vzorku_okna = 0.001*Fs*delka_okna;
    pointer = 1;
    j = 1;    
        
    segment_ms = [];
    locs = [];
    delka_periody = [];
    delka_delsiho_segmentu = [];
    periodicita = []; 
    
    
    while true
        
        global periodicita;

    switch current_state       
    
                        
        case 1 %SEGMENT_ESTIMATION
            
            if numel(y) < pointer(j)-1+pocet_vzorku_okna
                warning('Konec signalu')
                return
            end
            
            segment_ms = [];
            
            segment_ms = y(pointer(j):pointer(j)-1+pocet_vzorku_okna);      %segment o n milisekundach
            
            if length(segment_ms) > (length(y)-pointer(j))                                                               
                periodicity = periodicita;
                error('Konec')                                                %konec, pokud segment presahuje y
            end
            
            if pointer == 1
                pointer = [];
                pointer(1) = 1;
            end
            
            current_state = 2; %LOCS_ESTIMATION;
            
                       
            
            
        case 2 %LOCS_ESTIMATION
            
            locs = [];
            
            pocatecni_amdf = AMDF(segment_ms,Fs,delka_okna);
            pocatecni_amdf = -pocatecni_amdf(2:end);
            
            vaha = 0.5;
            [prahovana_amdf] = prahuj_amdf(pocatecni_amdf,vaha);
            
            [~,locs] = findpeaks(prahovana_amdf);
            
            while length(locs) < 2                                          %%%% ZMENY, neosefovana moznost jedineho peaku v amdf
               periodicity = periodicita;                                  %%%% misto toho rovnou break
               warning('Nalezen jediny peak v AMDF')
               return
%                 vaha = vaha-vaha*0.1;
%                 [prahovana_amdf] = prahuj_amdf(pocatecni_amdf,vaha);          
%                 [~,locs] = findpeaks(prahovana_amdf);                     %%%%
            end
            
            if locs(2)-locs(1) < 200
                pointer(j) = locs(2)+length(segment_ms);
                warning('Neperiodicky usek')
                return
                %current_state = 1 %SEGMENT_ESTIMATION
            else
                current_state = 3;
                
            end
            
            if locs(2)-locs(1) > 1000
                pointer(j) = locs(2)+length(segment_ms);
                warning('Neperiodicky usek')
                return
                %current_state = 1 %SEGMENT_ESTIMATION
            else
                current_state = 3;
                
            end
            
            %current_state = 3
            
            
            
            
        case 3 %TWO_PERIODS_ESTIMATION
            
            delka_delsiho_segmentu = [];
            
            locs = locs(1:2);
            delka_a = locs(1);
            delka_b = locs(2)-delka_a;

            if min(delka_a, delka_b) == delka_a
                delka_periody(j) = delka_a;
                delka_delsiho_segmentu = delka_b;
    
                signal_a = segment_ms(1:delka_a);
                signal_b = segment_ms(delka_a+1:delka_a+delka_b);
            else
                delka_periody(j) = delka_b;
                delka_delsiho_segmentu = delka_a;
        
                signal_b = segment_ms(1:delka_a);
                signal_a = segment_ms(delka_a+1:delka_a+delka_b);
            end
    
            if j > 1
                if delka_periody(j) < 0.5*(delka_periody(j-1))
                    warning('delka segmentu přesahuje y')
                    return
                end
            end
            
            current_state = 4; %LINEAR_TIMELINE;        
            
            
        case 4 %LINEAR_TIMELINE
            
            prebytek_bodu = delka_delsiho_segmentu - delka_periody(j);
            rozestup = fix((delka_delsiho_segmentu)/prebytek_bodu)-1;
            druhy_pointer = 0;

            for i = 1:1:prebytek_bodu
                indexy(i) = rozestup+1+druhy_pointer;
                druhy_pointer = indexy(i);
            end

            druhy_pointer = 0;

            for i = 0:1:prebytek_bodu-1
                signal_b(indexy(prebytek_bodu-i)) = [];
            end
            
            
            current_state = 5; %PERIODICITY;
            
            
            
        case 5 %PERIODICITY
            
            podobnost = normxcorr2(signal_b,signal_a);
            podobnost = podobnost((length(podobnost)/2):end);
            podobnost = podobnost.';
            
            periodicita(j) = 100*podobnost(1);
    
            pointer(j+1) = pointer(j) + delka_periody(j);
            j = j + 1;
                      
            %error(konec)
            %break
            

            
            current_state = 1; %SEGMENT_ESTIMATION;          
           
            
    end
    
    periodicity = periodicita;
    
    end
    
end
            
            
        
            
           
            
            
            
                                                          
                                    
             
                
              
    