%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                        %
%                               LTE_Koder                                %
%                                                                        %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%KODOVANIE DAT JEDNOTLIVE KODERY
%Vstupne premenne:
%data - vstupne generovane data [bin]
%c - poradie kodera [int]
%Vystupna premenna:
%out - kodovane data [bin]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function out = LTE_Koder(data,c)

g = [1 1 0 1;1 0 1 1];                                                      %polynomy LTE kodera (15,13)(13SV)
[n,K] = size(g);                                                         
m = K - 1;                                                           
dlz = length(data);                                                     
clkdlz = dlz + m;                                                           %zistenie celkovej dlzky (dlzka + ukn bity)
stav = zeros(1,m);                                                 
y = [];

%%KODER1
if(c == 1)
    for i = 1:clkdlz
        if(i<=dlz)
            sys_b = data(1,i);                                              %sys. bit
            s_v = mod(data(1,i)+sum(and(g(2,2:end),stav(1:end))),2);        %vypocet SV
            vystup(1,2) = mod(s_v+sum(and(g(1,2:end),stav(1:end))),2);      %par. bit  
        else
            %%URCENIE UKONCOVACICH BITOV
            for j = 1:m
                x = mod(sum(and(g(2,2:end),stav)),2);                       %ukoncovaci bit 
                s_v=0;                                                      %SV
                vystup(1,2) = mod(s_v+sum(and(g(1,2:end),stav)),2);         %par. bit           
                sys_b = x;                                   
            end
        end
        stav = [s_v, stav(1:m-1)];                                          %nastavenie stavu pam. buniek
        vystup(1,1) = sys_b;                                                %sys. bit
        y = [y vystup];                                                   
    end
%%KODER2
elseif(c == 2)
    for k = 1:dlz
        sys_b = data(1,k);                                                  %sys. bit
        s_v = mod(data(1,k)+sum(and(g(2,2:end),stav(1:end))),2);            %vypocet SV
        vystup(1,2) = mod(s_v+sum(and(g(1,2:end),stav(1:end))),2);          %par. bit

        stav = [s_v, stav(1:m-1)];                                          %nastavenie stavu pam. buniek
        vystup(1,1) = sys_b;                                                %sys. bit
        y = [y vystup];                                              
    end
end
out = y;
end