You are on page 1of 7

% Aim: Implementation Of linear Block Code

clc;
clear all;

% Input Generator Matrix


g=input('Enter The Generator Matrix: ')
disp ('G = ')
disp ('The Order of Linear block Code for given Generator Matrix is:')
[n,k] = size(transpose(g))
for i = 1:2^k
for j = k:-1:1
if rem(i-1,2^(-j+k+1))>=2^(-j+k)
u(i,j)=1;
else
u(i,j)=0;
end
end
end
u;
disp('The Possible Codewords are :')
c = rem(u*g,2)
disp('The Minimum Hamming Distance dmin for given Block Code is= ')
d_min = min(sum((c(2:2^k,:))'))

% Code Word
r = input('Enter the Received Code Word:')
p = [g(:,n-k+2:n)];
h = [transpose(p),eye(n-k)];
disp('Hammimg Code')
ht = transpose(h)
disp('Syndrome of a Given Codeword is :')
s = rem(r*ht,2)
for i = 1:1:size(ht)
if(ht(i,1:3)==s)
r(i) = 1-r(i);
break;
end
end
disp('The Error is in bit:')
i
disp('The Corrected Codeword is :')
r

%******************** OUTPUT *******************


Enter The Generator Matrix: [1 0 0 0 1 0 1;0 1 0 0 1 1 1;0 0 1 0 1 1 0;0 0 0 1 0 1 1]
g=
1000101
0100111
0010110
0001011

G=
The Order of Linear block Code for given Generator Matrix is:
n=
7
k=
4

The Possible Codewords are :

c=
0000000
0001011
0010110
0011101
0100111
0101100
0110001
0111010
1000101
1001110
1010011
1011000
1100010
1101001
1110100
1111111

The Minimum Hamming Distance dmin for given Block Code is=

d_min =

Enter the Received Code Word:[1 0 0 0 1 0 0]

r=
1000100

Hammimg Code
ht =
101
111
110
011
100
010
001

Syndrome of a Given Codeword is :


s=
001

The Error is in bit:


i=
7

The Corrected Codeword is :


r=
1000101

%This function generates error by desired pattern%


%Input arguments:
%n=number of blocks
%pc=probability of bad state
function [errr]= noiseh(n,pc);
errr=zeros(n,40);
for i=1:n
p=random('bino',1,pc);
if p~=0
e=zeros(1,40);
er=zeros(1,40);
d=random('poiss',8);
a=unidrnd(40-1)+1;
if d==0
er=zeros(1,40);
end
if d>0
e(a)=1;e(a+d-1)=1;
end
if d>2
for j=a+1:a+d-2
e(j)=randint(1);
end
end
[mm,nn]=size(e);
for j=40+1:nn
er(j-40)=e(j);
end
for j=(nn-40)+1:40
er(j)=e(j);
end
errr(i,:)=er;
end
end

clear all
clc
%---------------------------------------------------------------------%
%Encoding%
K=30; %C(40,K)%
cycl=cyclpoly(40,K); %generating polynomial for the code%
[H,G,k]=cyclgen(40,cycl); %G & H matrices for system%
p=1*1e5; %p=number of blockes to tranmit over the channel%
u=randint(p,K); %u is a random message for determining the
performance%
aa=20;
for jj=1:aa %this loop sweeps p to find BER as a function of
p=0.01-0.2%
pc=jj/100;
v=zeros(p,40); %v is the encoded matrice of u%
v=mod(u*G,2); %v is encoded from u%
%---------------------------------------------------------------------%
%Transmitting over the channel%
r=zeros(p,40); %r is the output of the channel%
r=xor(v,noiseh(p,pc)); %r is made by adding the channel
noise to matrice v%
rnon=r; %rnon will remain uncoded%
c1=0; %c1 counts number of first retransmitions%
c2=0; %c2 counts number of second transmittions%
%---------------------------------------------------------------------%
%Making syndrom and retransmitting%
s=sum((mod(r*H',2))'); %s is syndrom based on erceived
matrice r%
for i=1:p
if s(i)~=0 %if syndrom does not equal zero(if there is
error in block)%
r(i,:)=xor(v(i,:),noiseh(1,pc)); %retransmit the
defected block for first time%
c1=c1+1;
s(i)=sum((mod(r(i,:)*H',2))'); %again make the
syndrom%
if s(i)~=0 %if syndrom does not equal zero(if there
is error in block after first retransmition)%
c2=c2+1;
r(i,:)=xor(v(i,:),noiseh(1,pc)); %retransmit the defected
block for second time%
end
end
end
%---------------------------------------------------------------------%
%Decoding%
for i=1:K
uu(:,i)=r(:,i+40-K); %uu is the corrected message%
end
%---------------------------------------------------------------------%
%Calculating BER for coded system%
clc
error=xor(u,uu); %
q=sum(error); %
BER(jj)=sum(q(1,:))/(p*K) %original message u is compared with
corrected message uu%
analyticalBER(jj)=((pc)^3)/8; %analyticalBER is the BER which
is ecpected%
eff(jj)=(p*K)/(40*(p+c1+c2)); %eff is the efficiency of
system%
analyticaleff(jj)=K/(40*(1+pc)); %analyticaleff is ffficiency
which is expected%
Perfcoded(jj)=abs(log(BER(jj)));
%---------------------------------------------------------------------%
%Calculating BER for uncoded system%
errornon=xor(v,rnon); %
qnon=sum(errornon); %
BERnon(jj)=sum(qnon(1,:))/(p*40) %sent message v is compared with
received message rnon%
end
tt=1-t;
%---------------------------------------------------------------------%
%Showing results%
%---------------------------------------------------------------------%
figure;semilogy(tt,analyticalBER,'s',tt,BER,'r',tt,BERnon,'linewidth',3);
grid on
legend('Analytical','Cyclic Coded C(40,30)','Un-coded',3,'location','best');
xlabel('Probability of Bad State')
ylabel('Bit Error Rate')
title('Comparing BER for a C(40,30) Cyclic Code and Un-coded System as a
Function of Channel Error')
%---------------------------------------------------------------------%
figure;semilogy(tt,analyticalBER,'--',tt,BER,'r','linewidth',3);
grid on
legend('Analytical','Cyclic Coded C(40,30)',2,'location','best');
xlabel('Probability of Bad State')
ylabel('Bit Error Rate')
title('Comparing BER for a C(40,30) Cyclic Code and expected analytical BER')
%---------------------------------------------------------------------%
figure;plot(tt,eff,'r',tt,analyticaleff,'--','linewidth',3);
grid on
legend('Cyclic Coded C(40,30)','Analytical',2,'location','best');
xlabel('Probability of Bad State')
ylabel('Efficiency')
title('Comparing Efiiciency for a C(40,30) Cyclic Code and expected
analytical Efficiency')

save('CA2.mat')
clear all
clc
%---------------------------------------------------------------------%
%Encoding%
K=30; %C(40,K)%
cycl=cyclpoly(40,K); %generating polynomial for the code%
[H,G,k]=cyclgen(40,cycl); %G & H matrices for system%
p=1*1e5; %p=number of blockes to tranmit over the channel%
u=randint(p,K); %u is a random message for determining the
performance%
aa=20;
for jj=1:aa %this loop sweeps p to find BER as a function of
p=0.01-0.2%
pc=jj/100;
v=zeros(p,40); %v is the encoded matrice of u%
v=mod(u*G,2); %v is encoded from u%
%---------------------------------------------------------------------%
%Transmitting over the channel%
r=zeros(p,40); %r is the output of the channel%
r=xor(v,noiseh(p,pc)); %r is made by adding the channel
noise to matrice v%
rnon=r; %rnon will remain uncoded%
c1=0; %c1 counts number of first retransmitions%
c2=0; %c2 counts number of second transmittions%
%---------------------------------------------------------------------%
%Making syndrom and retransmitting%
s=sum((mod(r*H',2))'); %s is syndrom based on erceived
matrice r%
for i=1:p
if s(i)~=0 %if syndrom does not equal zero(if there is
error in block)%
r(i,:)=xor(v(i,:),noiseh(1,pc)); %retransmit the
defected block for first time%
c1=c1+1;
s(i)=sum((mod(r(i,:)*H',2))'); %again make the
syndrom%
if s(i)~=0 %if syndrom does not equal zero(if there
is error in block after first retransmition)%
c2=c2+1;
r(i,:)=xor(v(i,:),noiseh(1,pc)); %retransmit the defected
block for second time%
end
end
end
%---------------------------------------------------------------------%
%Decoding%
for i=1:K
uu(:,i)=r(:,i+40-K); %uu is the corrected message%
end
%---------------------------------------------------------------------%
%Calculating BER for coded system%
clc
error=xor(u,uu); %
q=sum(error); %
BER(jj)=sum(q(1,:))/(p*K) %original message u is compared with
corrected message uu%
analyticalBER(jj)=((pc)^3)/8; %analyticalBER is the BER which
is ecpected%
eff(jj)=(p*K)/(40*(p+c1+c2)); %eff is the efficiency of
system%
analyticaleff(jj)=K/(40*(1+pc)); %analyticaleff is ffficiency
which is expected%
Perfcoded(jj)=abs(log(BER(jj)));
%---------------------------------------------------------------------%
%Calculating BER for uncoded system%
errornon=xor(v,rnon); %
qnon=sum(errornon); %
BERnon(jj)=sum(qnon(1,:))/(p*40) %sent message v is compared with
received message rnon%
end
tt=1-t;
%---------------------------------------------------------------------%
%Showing results%
%---------------------------------------------------------------------%
figure;loglog(tt,analyticalBER,'s',tt,BER,'r',tt,BERnon,'linewidth',3);
grid on
legend('Analytical','Cyclic Coded C(40,30)','Un-coded',3,'location','best');
xlabel('Probability of Bad State')
ylabel('Bit Error Rate')
title('Comparing BER for a C(40,30) Cyclic Code and Un-coded System as a
Function of Channel Error')
%---------------------------------------------------------------------%
figure;loglog(tt,analyticalBER,'--',tt,BER,'r','linewidth',3);
grid on
legend('Analytical','Cyclic Coded C(40,30)',2,'location','best');
xlabel('Probability of Bad State')
ylabel('Bit Error Rate')
title('Comparing BER for a C(40,30) Cyclic Code and expected analytical BER')
%---------------------------------------------------------------------%
figure;plot(tt,eff,'r',tt,analyticaleff,'--','linewidth',3);
grid on
legend('Cyclic Coded C(40,30)','Analytical',2,'location','best');
xlabel('Probability of Bad State')
ylabel('Efficiency')
title('Comparing Efiiciency for a C(40,30) Cyclic Code and expected
analytical Efficiency')

You might also like