CS Lab
CS Lab
9 . Cyclic Codes
clc;
clear all;
close all;
x=input('Enter the data : ');
x1=length(x);
if x1==4
y=[1 0 1 1];
else if x1==8
y=[1 0 0 0 0 0 1 1 1];
else if x1==10
y=[1 1 0 0 0 1 1 0 1 0 1];
else if x1==16
y=[1 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 1];
else if x1==32
y=[1 0 0 0 0 0 1 0 0 1 1 0 0 0 0 0 1 0 0 0 1 1 1 0 1 1 0
1 1 0 1 1 1];
end
end
end
end
end
x2=length(y);
disp('The Generator is : ');
disp(y);
z=[x zeros(1,x2-1)];
z1=length(z);
y1=x2;
i=1;
rem=z;
for y2=y1:1:z1
k=1;
for j=i:1:y1+i-1;
if rem(i)==1
crc(j)=xor(rem(j),y(k));
k=k+1;
else
crc(j)=xor(rem(j),0);
end
end
for j=i:1:y1+i-1;
rem(j)=crc(j);
end
i=i+1;
end
j=1;
for i=x1+1:1:z1;
r(j)=crc(i);
j=j+1;
end
disp('The CRC is : ');
disp(r);
a=[x r];
disp('The Code Word is : ');
disp(a);
%crc decoder
p=input('Enter the Received code word : ');
p2=length(p);
p1=p2/2;
if p1==3
y=[1 0 1 1];
else if p1==8
y=[1 0 0 0 0 0 1 1 1];
else if p1==10
y=[1 1 0 0 0 1 1 0 1 0 1];
else if p1==16
y=[1 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 1];
else if p1==32
y=[1 0 0 0 0 0 1 0 0 1 1 0 0 0 0 0 1 0 0 0 1 1 1 0 1 1 0
1 1 0 1 1 1];
end
end
end
end
end
x2=length(y);
y1=x2;
i=1;
rem=p;
for y2=y1:1:p2
k=1;
for j=i:1:y1+i-1;
if rem(i)==1
crc(j)=xor(rem(j),y(k));
k=k+1;
else
crc(j)=xor(rem(j),0);
end
end
for j=i:1:y1+i-1;
rem(j)=crc(j);
end
i=i+1;
end
j=1;
x1=p2-x2+1;
for i=x1+1:1:p2;
r(j)=crc(i);
j=j+1;
end
disp('The Syndrome is :');
disp(r);
if r==0
disp('code has received successfully');
break;
else
disp('Error has been occured');
end
i1=1;
for ia=1:1:x1;
g1=[];
if ia==1
g1=[ones(1,1) zeros(1,p2-ia)];
else
g1=[zeros(1,ia-1) ones(1,1) zeros(1,p2-ia)];
end
rem=[];
rem=g1;
i=1;
for y21=x1:1:p2;
k1=1;
for j=i:1:y1+i-1;
if rem(i)==1
crc1(i,j)=xor(rem(j),y(k1));
k1=k1+1;
else
crc1(i,j)=xor(rem(j),0);
end
end
for j=i:1:y1+i-1;
rem(j)=crc1(i,j);
end
i=i+1;
end
j=1;
for i=x1+1:1:p2;
ps(i1,j)=rem(i);
j=j+1;
end
i1=i1+1;
end
disp('The parity bits are :');
disp(ps);
h=[transpose(ps),eye(p2-x1)];
disp('The H matrix is');
disp(h);
ht=transpose(h);
disp('The H Transpose matrix is :');
disp(ht);
for i=1:1:size(ht)
if (eq(ht(i,1:p2-x1),r))
p(i)=1-p(i);
break;
end
end
disp('The error is in bit :')
disp(i);
disp('The corrected code word is :')
disp(p);
10 . Convolutional Codes
clc;
clear all;
close all;
x=input('Enter the Message : ');
g1=[1 1 1];
g2=[1 0 1];
a=conv(g1,x);
a1=length(a);
for i=1:1:a1;
if rem(a(i),2)==0
a(i)=0;
else
a(i)=1;
end
end
b=conv(g2,x);
b1=length(b);
for i=1:1:b1;
if rem(b(i),2)==0
b(i)=0;
else
b(i)=1;
end
end
j=1;
for i=1:2:a1+b1-1;
y(i:i+1)=[a(j),b(j)];
j=j+1;
end
disp('Encoded Data : ');
disp(y);
%Decode
a=[0 0];
b=[0 1];
c=[1 0];
d=[1 1];
x1=a;
l=1;m=1;n=1;hamm=0;
y=input('Enter the received Data : ');
for i=1:2:length(y)-1;
if x1==a;
for j=1:1:2;
g(j)=xor(a(j),y(l));
l=l+1;
h(j)=xor(d(j),y(m));
m=m+1;
end
e=sum(g(1:2));
e1=e+hamm;
f=sum(h(1:2));
f1=f+hamm;
if e1<f1
hamm=e1;
z(n)=0;
n=n+1;
x1=a;
else
hamm=f1;
z(n)=1;
n=n+1;
x1=b;
end
else if x1==b;
for j=1:1:2;
g(j)=xor(c(j),y(l));
l=l+1;
h(j)=xor(b(j),y(m));
m=m+1;
end
e=sum(g(1:2));
e1=e+hamm;
f=sum(h(1:2));
f1=f+hamm;
if e1<f1
hamm=e1;
z(n)=0;
n=n+1;
x1=c;
else
hamm=f1;
z(n)=1;
n=n+1;
x1=d;
end
else if x1==c;
for j=1:1:2;
g(j)=xor(d(j),y(l));
l=l+1;
h(j)=xor(a(j),y(m));
m=m+1;
end
e=sum(g(1:2));
e1=e+hamm;
f=sum(h(1:2));
f1=f+hamm;
if e1<f1
hamm=e1;
z(n)=0;
n=n+1;
x1=a;
else
hamm=f1;
z(n)=1;
n=n+1;
x1=b;
end
else if x1==d;
for j=1:1:2;
g(j)=xor(b(j),y(l));
l=l+1;
h(j)=xor(d(j),y(m));
m=m+1;
end
e=sum(g(1:2));
e1=e+hamm;
f=sum(h(1:2));
f1=f+hamm;
if e1<f1
hamm=e1;
z(n)=0;
n=n+1;
x1=c;
else
hamm=f1;
z(n)=1;
n=n+1;
x1=d;
end
end
end
end
end
end
s=length(z)-length(g1)+1;
for i=1:1:s;
z1(i)=z(i);
end
disp('Decoded Data :');
disp(z1);