%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                        %
%                         Prenosovy kanal AWGN                           %
%                                                                        %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%SIMULovanie PRENOSOveho KANALA AWGN S BPSK MODULACIOU
%Vstupne premenne:
%data - vstupne data z koderu [bin]
%EbN0db - hodota odstupu signal sum v dB [double]
%rate - kodovy pomer koderov [double]
%vystupne premenne:
%out - vystup data [double]
%chyby - pocet chyb [int]
%ber - chybovot kanala [double]
%L_c - spolahlivost kanala [double]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [out,chyby,ber,L_c] = PreKanAWGN(data,EbN0db,rate)

%%INICIALIZACIA HODNOT
dlz = length(data);                                                       
data2 = 2*data-1;                                                          

%%SIMULACIA PRENOSU
rand = randperm(dlz);                                                       
en = 10^(EbN0db(length(EbN0db))/10);  
L_c = 4*en*rate;                                                        
sigma = (1/sqrt(2*(rate)*en));                                         
nah_sig(rand)=(randn(1,(dlz)));                                         
out = data2+sigma*nah_sig;                                                  

%%VYPOCET CHYBOVOSTI
vystup10 = (sign(out)+1)/2;                                              
chyby = sum (xor(data,vystup10));                                         
ber = chyby/length(data);                                                  
end