%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                        %
%                                   CCSDS                                %
%                                                                        %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%SIMULACIE VYUZITIE TURBO CODOV V SYSTEME CCSDS
%Vstupne premenne:
%data - velkost vstupnych dat [int]
%blok - velkost jendotlivych blokov dat [int]
%it - pocet dekodovacich iteracii [int]
%it_bool - urcuje zobrazenie iteracii v grafe [bool]
%EbN0db - rozsah odtupu signal/sum [double]
%rate - urcuje rate kodera [string]
%alg - vyber dekodovacieho algoritmu [int]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [out1,out2] = CCSDS (data,blok,it,it_bool,EbN0db,rate,alg)

%%INICIALIZACIA DAT
blok_it = ceil(data/(blok-4));                                            
Data = randi([0 1],1,data);                                               
punc = str2num(rate);

%%KODOVANIE DAT
h = waitbar(0,'Completed 0%','Name','Please wait...','CreateCancelBtn',...
    'setappdata(gcbf,''canceling'',1)');
setappdata(h,'Canceling',0);
KodData0 = CCSDS_Kod(Data,blok,punc);                                      

chyb0 = zeros(it,length(EbN0db));

steps = length(EbN0db)*blok_it;
waitbar(1/steps);
set(get(findobj(h,'type','axes'),'title'),'string',...
    sprintf('Completed %0.2f%%',1/(steps/100)));
if getappdata(h,'canceling')                                                %cancel tlacitko waitbar
    out1 = 0;
    out2 = 0;
    delete(h);
    return;
end
step = 2;

%%SIMULACIE PRENOSU DAT A DEKODOVANIE PRE JEDNOTLIVE EbN0db
for i = 1:length(EbN0db)
    [PreDataU,chU,berU(i),L_c1] = PreKanAWGN(Data,EbN0db(i),1);             %simulacia prenosoveho kanala nekodovane data
    [PreData0,ch0,ber0,L_c2] = PreKanAWGN(KodData0,EbN0db(i),punc);         %simulacia prenosoveho kanala kodovane data
    
    yyy = CCSDS_Blok(PreData0,blok,punc);                             
    
    %%DEKODOVANIE DAT
    for j = 1:blok_it
    DeData0 = CCSDS_Dem(yyy(j,:),punc);                                    
    Data0 = CCSDS_Dek(DeData0,L_c2,it,alg);                             
        for k = 1:it
            a = j*(blok-4)-(blok-4)+1;
            b = j*(blok-4);
            c = data - a + 1;
            if(b<=data)                                                   
                chyb0(k,i) = chyb0(k,i) + biterr(Data(a:b),Data0(k,:));    
            else
                chyb0(k,i) = chyb0(k,i) + biterr(Data(a:a+c-1),...        
                    Data0(k,1:c));
            end
        end
        if getappdata(h,'canceling')                                        %cancel tlacitko waitbar
            out1 = 0;
            out2 = 0;
            delete(h);
            return;
        end
        waitbar(step/steps);
        set(get(findobj(h,'type','axes'),'title'),'string',...
        sprintf('Completed %0.2f%%',step/(steps/100)));
        step = step + 1;
    end
    
    %%VYPOCET BER
    for l = 1:it
        Deber0(l,i) = chyb0(l,i)/length(Data);                         
    end
end

for j = 1:it
    for i = 2:length(EbN0db)
        if(Deber0(j,i)>=Deber0(j,i-1))
            Deber0(j,i:end) = 0;
        end
    end
end
if isequal(rate,'1/2')
    for j = 2:it
        for i = 1:length(EbN0db)
            if (EbN0db(i)>=2&&Deber0(j,i)>Deber0(j-1,i))
                Deber0(j:end,i) = 0;
            end
        end
    end
else
    for j = 2:it
        for i = 1:length(EbN0db)
            if (EbN0db(i)>=1&&Deber0(j,i)>Deber0(j-1,i))
                Deber0(j:end,i) = 0;
            end
        end
    end
end

delete(h);

[data_name,alg_name] = CCSDS_convert(data,alg);

%%VYKRESLENIE VYSLEDKOV DO GRAFOV
if(it_bool == 1)                                                        
    iteracie = [1,2,4,6,8,10];
    line = '-';
    marker = ['o','+',' ','*',' ','x',' ','s',' ','d'];
    color = [1,0,0;0,1,0;0,0,0;0,0,1;0,0,0;1,0,1;0,0,0;1,1,0;0,0,0;0,1,1];
    a = figure;
    set(a,'position',[10,10,800,600]);
    plot = semilogy(EbN0db,berU);
    set(plot,'LineStyle',line,'Color',[0,0,0],'DisplayName','nekodovane');
    hold on;
    for i=1:it
        if (any(i==iteracie))
            plot = semilogy(EbN0db,Deber0(i,:));
            set(plot,'LineStyle',line,'Marker',marker(i),'Color',color(i,:)...
                ,'DisplayName',sprintf('%d.teracia',i));
            hold on;
        end
    end
    grid on;
    xlabel('E_b/N_0 (dB)');
    ylabel('BER');
    title(sprintf('BER v závislosti na E_b/N_0 pre CCSDS [alg=%s, data=%s, blok=%d, rate=%s]',...
        alg_name,data_name,blok,rate));
    legend('show','Location','southwest');
else                                                                        
    a = figure;
    set(a,'position',[10,10,800,600]);
    plot = semilogy(EbN0db,berU);
    set(plot,'LineStyle','-','Color',[0,0,0],'DisplayName','nekodovane');
    hold on;
    plot = semilogy(EbN0db,Deber0(it,:));
    set(plot,'LineStyle','-','Marker','o','Color',[0,0,0],'DisplayName',...
        sprintf('%d.iteracia',it));
    grid on;
    xlabel('E_b/N_0 (dB)');
    ylabel('BER');
    title(sprintf('BER v závislosti na E_b/N_0 pre CCSDS [alg=%s, data=%s, blok=%d, rate=%s]',...
        alg_name,data_name,blok,rate));
    legend('show','Location','southwest');
end
out1 = Deber0;
out2 = berU;
end

