%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                        %
%                                   CCSDS_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 = CCSDS_Dem(data,rate)

g = [1 0 0 1 1;1 1 0 1 1;1 0 1 0 1;1 1 1 1 1];                              %vytvaracie polynomy CCSDS kodera (23,33,25,37) 23SV 33(1) 25(2) 37(3)
[n,K] = size(g);                                                      
m = K - 1;                                                            
sys_b = [];
vystup = [];

%%RATE 1/6
if(rate == 1/6)
    dlz = length(data)*rate;                                                %0a, 1a, 2a, 3a, 1b, 3b
    
    sys_b = data(1:6:end); 
    sys_b_int = CCSDS_Int(sys_b,1);                                    
    
    vystup(1,2:4:dlz*4) = data(2:6:end);                                    %1a
    vystup(1,3:4:dlz*4) = data(3:6:end);                                    %2a
    vystup(1,4:4:dlz*4) = data(4:6:end);                                    %3a
    vystup(2,2:4:dlz*4) = data(5:6:end);                                    %1b 
    vystup(2,4:4:dlz*4) = data(6:6:end);                                    %3b
    
    vystup(1,1:4:dlz*4) = sys_b(1:end);                                     %0a
    vystup(2,1:4:dlz*4) = sys_b_int(1:end);                                 %0b

    out = vystup;
%%RATE 1/4        
elseif(rate == 1/4)
    dlz = length(data)*rate;                                                %0a, 2a, 3a, 1b
    
    sys_b = data(1:4:end);
    sys_b_int = CCSDS_Int(sys_b,1);
    
    vystup(1,3:4:dlz*4) = data(2:4:end);                                    %2a
    vystup(1,4:4:dlz*4) = data(3:4:end);                                    %3a
    vystup(2,2:4:dlz*4) = data(4:4:end);                                    %1b
    
    vystup(1,1:4:dlz*4) = sys_b(1:end);                                     %0a
    vystup(2,1:4:dlz*4) = sys_b_int(1:end);                                 %0b
    
    out = vystup;
%%RATE 1/3
elseif(rate == 1/3)
    dlz = length(data)*rate;                                                %0a, 1a, 1b
    
    sys_b = data(1:3:end);
    sys_b_int = CCSDS_Int(sys_b,1);
    vystup = zeros(2,4*dlz);
    
    vystup(1,2:4:dlz*4) = data(2:3:end);                                    %1a
    vystup(2,2:4:dlz*4) = data(3:3:end);                                    %1b
    
    vystup(1,1:4:dlz*4) = sys_b(1:end);                                     %0a
    vystup(2,1:4:dlz*4) = sys_b_int(1:end);                                 %0b
    
    out = vystup;
%%RATE 1/2
elseif(rate == 1/2)
    dlz = length(data)*rate;                                                %0a, 1a, 1b
    
    sys_b = data(1:2:end);
    sys_b_int = CCSDS_Int(sys_b,1);
    vystup = zeros(2,4*dlz);
    
    vystup(1,2:8:dlz*4) = data(2:4:end);
    vystup(2,6:8:dlz*4) = data(4:4:end);
    
    vystup(1,1:4:dlz*4) = sys_b(1:end);
    vystup(2,1:4:dlz*4) = sys_b_int(1:end);
    
    out = vystup;
end
end