%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                        %
%                                 LTE_Kod                                %
%                                                                        %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%KODOVANIE DAT PRE PRVY A DRUHY KODER
%Vstupne premenne:
%data - data vstupujuce do kodera [bin]
%b - celkova dlzka bloku
%rate - rate kodera
%Vystupna premenna:
%out - kodovane data
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function out = LTE_Kod(data,b,punc)

dlz = b;                                                                  
blok = b - 3;                                                           
it1 = ceil(length(data)/blok);                                     
it2 = floor(length(data)/blok);                                            
out = [];

for i = 1:it1
    %%ROZDELOVANIE DAT DO BLOKOV
    if(i<=it2)
       z = data(1,(i*blok-blok)+1:i*blok); 
    else
       sss = blok - length(data(1,(i*blok-blok)+1:end));
       z = [data(1,(i*blok-blok)+1:end) zeros(1,sss)];                  
    end
    
    %%KODER1
    k_d1 = LTE_Koder(z,1);
    x(1,:) = k_d1(1:2:2*dlz);                                               %sys. bity S
    x(2,:) = k_d1(2:2:2*dlz);                                               %par. bity P1

    int_data = LTE_Int(x(1,:),1);                                        

    %%KODER2
    k_d2 = LTE_Koder(int_data,2);                                           %kodovanie dat v druhom koderi
    x(3,:) = k_d2(2:2:2*dlz);                                               %par. bity P2
    
    %%RATE 1/3
    if(punc == 1/3)
        for j = 1:dlz
            for k = 1:3
                y(1,(3*(j-1)+k)) = x(k,j);
            end
        end
        out = [out y];

    %%RATE 1/2     
    elseif(punc == 1/2)
        for j = 1:dlz
            if(mod(j,2)==0)                                              
                y(1,2*j-1) = x(1,j);                                 
                y(1,2*j) = x(3,j);                                          %paritne druhy koder 0,1,0,1,0,1...
            else  
                y(1,2*j-1) = x(1,j);                                        
                y(1,2*j) = x(2,j);                                          %paritne prvy koder 1,0,1,0,1,0...
            end
        end
        out = [out y];
    else
        error('CHYBA!!!');
    end
end
end