% write a matlab program for hamming code
clc;
clear all;
close all;
m=input('Enter the value of m:');
%n=input('Enter the value of n:');
n=2^m-1
% k=input('Enter the value of k:');
k=2^m-m-1
m=n-k;
g(m,n)=0;
for row=1:m
a=row-1;
b=0;
d=0;
while(1)
if(b<n)
for no_1s=0:2^a-1
col=2^a+no_1s+b;
if(col<=n)
g(row,col)=1;
d=d+1;
else
break;
end
end
b=d+2^a;
d=b;
else
break;
end
end
end
disp('genretor materix:');
g
x(2^k,n)=0;
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
for x1=1:2^k
a=0;
i=1;
for j=1:n
if(j~=2^a)
x(x1,j)=u(x1,i);
i=i+1;
else
a=a+1;
end
end
for i=1:m
a=0;
w=1;
for j=1:n
if(j~=2^a)
if g(i,j)==1
b(i,w)=x(x1,j);
w=w+1;
end
else
a=a+1;
end
end
end
[row,col]=size(b);
for i=1:row
c(i)=0;
for j=1:col
c(i)=xor(c(i),b(i,j));
end
end
for i=0:m-1
for j=1:n
if(j==2^i)
x(x1,j)=c(i+1);
end
end
end
end
disp('The generated Code Word:')
x
r = input('Enter the Received Code Word:')
% r=[1 0 1 1 0 0 1]
for i=0:m-1
for j=1:n
if(j==2^i)
c(i+1)=r(1,j);
end
end
end
for i=1:m
a=0;
w=1;
for j=1:n
if(j~=2^a)
if g(i,j)==1
b(i,w)=r(1,j);
w=w+1;
end
else
a=a+1;
end
end
end
[row,col]=size(b);
for i=1:row
c1(i)=0;
for j=1:col
c1(i)=xor(c1(i),b(i,j));
end
end
s=xor(c,c1);
error=0;
for i=0:m-1
error=error+s(i+1)*2^i;
end
disp('The Error is in bit:')
error
e(1,n)=0;
for i=1:n
if i==error
e(1,i)=1;
end
end
correct_word=xor(r,e);
disp('The generated Code Word:')
correct_word
评论0