%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                        %
%                               LTE_Dek                                  %
%                                                                        %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%DEKODOVANIE DAT PRIJATYCH Z PRENOSOVEHO KANALA 
%Vstupne premenne: 
%L_uk - prenesene data [double]
%L_uky - a-priori hodnota z dekodera [double]
%L_c - spolahlivost prenosoveho kanala [double]
%it - pocet dekodovacich iteracii [int]
%alg - dekodovaci algoritmus [int]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function out = LTE_Dek(data,L_c,it,alg)

g = [1 1 0 1;1 0 1 1];                                                      %vytvaracie polynomy LTE kodera (15,13)
[n,K] = size(g);                                                         
m = K - 1;                                                                 
dlz = length(data(1,:))/2;                                             

Lc_yt = 0.5*L_c*data;                                            
L_e = zeros(1,dlz);                                                         %a-priori hodnoty pre prvy dekoder
[naOut, naStav, preOut, preStav] = LTE_Trellis(g);  

%%DEKODOVANIE PRE VSETKY ALGORITMY
if alg == 1
    %%SOVA
    for i = 1:it
        %%DEKODOVANIE PRVY DEKODER
        L_uk = LTE_Int(L_e,0);                                        
        L_uky = LTE_SOVA(Lc_yt(1,:), g, L_uk, 1, naOut, naStav,...
            preOut,preStav);
        L_e = L_uky - 2*Lc_yt(1,1:2:2*dlz) - L_uk;                      

        %%DEKODOVANIE DRUHY DEKODER
        L_uk = LTE_Int(L_e,1);                                          
        L_uky = LTE_SOVA(Lc_yt(2,:), g, L_uk, 2, naOut, naStav,...
            preOut,preStav);  
        L_e = L_uky - 2*Lc_yt(2,1:2:2*dlz) - L_uk;                       
        out_10 = LTE_Int(floor((sign(L_uky)+1)/2),0);                    
        out(i,1:(dlz-m)) = out_10(1:(dlz-m));                       
    end
elseif alg == 2
    %%logMAP
    for i = 1:it
        %%DEKODOVANIE PRVY DEKODER
        L_uk = LTE_Int(L_e,0);                                          
        L_uky = LTE_logMAP(Lc_yt(1,:), g, L_uk, 1, naOut, naStav,...
            preOut,preStav);
        L_e = L_uky - 2*Lc_yt(1,1:2:2*dlz) - L_uk;                         

        %%DEKODOVANIE DRUHY DEKODER
        L_uk = LTE_Int(L_e,1);                                       
        L_uky = LTE_logMAP(Lc_yt(2,:), g, L_uk, 2, naOut, naStav,...
            preOut,preStav);  
        L_e = L_uky - 2*Lc_yt(2,1:2:2*dlz) - L_uk;                       
        out_10 = LTE_Int(floor((sign(L_uky)+1)/2),0);                 
        out(i,1:(dlz-m)) = out_10(1:(dlz-m));                          
    end
elseif alg == 3
    %%MAXlogMAP
    for i = 1:it
        %%DEKODOVANIE PRVY DEKODER
        L_uk = LTE_Int(L_e,0);                                          
        L_uky = LTE_MAXlogMAP(Lc_yt(1,:), g, L_uk, 1, naOut, naStav,...
            preOut,preStav);
        L_e = L_uky - 2*Lc_yt(1,1:2:2*dlz) - L_uk;                    

        %%DEKODOVANIE DRUHY DEKODER
        L_uk = LTE_Int(L_e,1);                                             
        L_uky = LTE_MAXlogMAP(Lc_yt(2,:), g, L_uk, 2, naOut, naStav,...
            preOut,preStav);  
        L_e = L_uky - 2*Lc_yt(2,1:2:2*dlz) - L_uk;                       
        out_10 = LTE_Int(floor((sign(L_uky)+1)/2),0);                 
        out(i,1:(dlz-m)) = out_10(1:(dlz-m));                          
    end
else
    error('CHYBA!!!');
end
end