function [Pb_u,Pb_c]=hamm_ing(n,k,N,epsElon)
%%%Find Eb_N0 required for BPSK system to acheive BER of epsElon
%Note: BER=0.5*erfc(sqrt(Eb_N0))
Eb_N0=(erfcinv(2*epsElon))^2;
%Assuming Eb=1, calculate N0
Eb=1;N0=Eb/Eb_N0;
%Generate the message b to be transmitted(0s and 1s)
b=round(rand(N,1));
%Initialize the received message
bb=b;
%Generate the gaussian noise with variance N0/2
noise=sqrt(N0/2)*randn(size(b));
%Find the error events
index_error_1=find((sqrt(Eb)+noise)<0 & b==1); %Error event when 1 is transmitted
bb(index_error_1)=0; %flip the bit in error from 1 to 0
index_error_0=find((-sqrt(Eb)+noise)>0 & b==0); %Error event when 0 is transmitted
bb(index_error_0)=1; %flip the bit in error from 0 to 1
%Calculate the uncoded BER
Pb_u=length(find(bb~=b))/N;
%Now apply channel coding
c=encode(b,n,k,'hamming');
%Initialize the received message before decoding
d=c;
%Generate the gaussian noise with variance N0/2
noise=sqrt(N0/2)*randn(size(c));
%Find the error events
index_error_1=find((sqrt(Eb)+noise)<0 & c==1); %Error event when 1 is transmitted
d(index_error_1)=0; %flip the bit in error from 1 to 0
index_error_0=find((-sqrt(Eb)+noise)>0 & c==0); %Error event when 0 is transmitted
d(index_error_0)=1; %flip the bit in error from 0 to 1
%Do the decoding
e=decode(d,n,k,'hamming');
%Calculate the BER after decoding
Pb_c=length(find(e~=b))/N;