%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                        %
%                                   LTE_Dem                              %
%                                                                        %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%DEMULTIPLEXOVANIE DAT Z PRENOSOVEHO KANALA PRE PRVY A DRUHY DEKODER
%vstupnne premenne:
%data - bloky dat z prenosoveho kanala [double]
%rate - rate kodera [double]
%Vystupna premenna:
%out - rozdelenie dat pre prvy (1.riadok) a druhy (2.riadok) dekoder [double] 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function out = LTE_Dem(data,punc)

g = [1 1 0 1;1 0 1 1];                                                      %vytvaracie polynomy LTE (15,13) 15S 13P
[n,K] = size(g);                                                    
m = K - 1;                                                         
sys_b = [];
vystup = [];

%%RATE 1/3
if(punc == 1/3)
    dlz = length(data)/(n+1);                                               %S P1 P2
    for i = 1:dlz  
        sys_b(i) = data(3*(i-1)+1);                                      
        for j = 1:n
            vystup(j,2*i) = data(3*(i-1)+1+j);                          
        end
    end
    sys_b_int = LTE_Int(sys_b,1);                                      
    for j = 1:dlz
        vystup(1,2*(j-1)+1) = sys_b(j);                          
        vystup(2,2*(j-1)+1) = sys_b_int(j);                            
    end
    out = vystup;
        
%RATE 1/2
elseif(punc == 1/2)
    dlz = length(data);                                                     %S P1(1/2) P2(1/2)                                             
    s = 1;                                                          
    for i = 1:2:dlz
        sys_b(1,s) = data(i);
        if(mod(i+1,4)==0)
            vystup(2,i+1)=data(i+1);                                    
        else
            vystup(1,i+1)=data(i+1);                                 
        end
    s = s + 1;
    end
    sys_b_int = LTE_Int(sys_b,1);
    for i = 1:dlz/2
        vystup(1,2*i-1)=sys_b(i);
        vystup(2,2*i-1)=sys_b_int(i);
    end
    out = vystup;
end
end