Mình mới mò Matlab nên vẫn còn lờ mờ, mình có 2 hàm như thế này về hamming code mong các bạn có thể giải thích dùm các lệnh bên dưới.
=========encodeing========
p=2;
n=2^(m)-1;
k=n-m;
if ( (p == 2) & (m <= 24) )
switch m
case 1
pol = [1 1];
case 2
pol = [1 1 1];
case 3
pol = [1 1 0 1];
case 4
pol = [1 1 0 0 1];
case 5
pol = [1 0 1 0 0 1];
case 6
pol = [1 1 0 0 0 0 1];
case 7
pol = [1 0 0 1 0 0 0 1];
case 8
pol = [1 0 1 1 1 0 0 0 1];
case 9
pol = [1 0 0 0 1 0 0 0 0 1];
case 10
pol = [1 0 0 1 0 0 0 0 0 0 1];
case 11
pol = [1 0 1 0 0 0 0 0 0 0 0 1];
case 12
pol = [1 1 0 0 1 0 1 0 0 0 0 0 1];
case 13
pol = [1 1 0 1 1 0 0 0 0 0 0 0 0 1];
case 14
pol = [1 1 0 0 0 0 1 0 0 0 1 0 0 0 1];
case 15
pol = [1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1];
case 16
pol = [1 1 0 1 0 0 0 0 0 0 0 0 1 0 0 0 1];
end
end
% encoder circuit implementation;
b=zeros(1,m); % tao mang 0 co 1 hang va m cot
g=pol;
c=b;
for i=length(msg):-1:1
u1=xor(msg(i),c(m-1));
c(1)=xor(b(1),g(2)*u1);
b(1)=u1;
for j=2:m-1
c(j)=xor(b(j),g(j+1)*u1);
b(j)=c(j-1);
end
c(m)=b(m);
b(m)=c(m-1);
end
encodedmsg=[b msg];
=========deencodeing========
function [out,decoded]=hamd(m,encoded)
p=2;
n=2^(m)-1;
k=n-m;
% find prim poly
if ( (p == 2) & (m <= 24) )
switch m
case 1
pol = [1 1];
case 2
pol = [1 1 1];
case 3
pol = [1 1 0 1];
case 4
pol = [1 1 0 0 1];
case 5
pol = [1 0 1 0 0 1];
case 6
pol = [1 1 0 0 0 0 1];
case 7
pol = [1 0 0 1 0 0 0 1];
case 8
pol = [1 0 1 1 1 0 0 0 1];
case 9
pol = [1 0 0 0 1 0 0 0 0 1];
case 10
pol = [1 0 0 1 0 0 0 0 0 0 1];
case 11
pol = [1 0 1 0 0 0 0 0 0 0 0 1];
case 12
pol = [1 1 0 0 1 0 1 0 0 0 0 0 1];
case 13
pol = [1 1 0 1 1 0 0 0 0 0 0 0 0 1];
case 14
pol = [1 1 0 0 0 0 1 0 0 0 1 0 0 0 1];
case 15
pol = [1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1];
case 16
pol = [1 1 0 1 0 0 0 0 0 0 0 0 1 0 0 0 1];
end
end
present ,
s=zeros(1,m);
g=pol;
t=s;
bu=encoded;
kl=0;
for i=length(encoded):-1:1
u1=xor(encoded(i),xor(t(m-1),kl));
t(1)=xor(s(1),g(2)*u1);
s(1)=u1;
for j=2:m-1
t(j)=xor(s(j),g(j+1)*u1);
s(j)=t(j-1);
end
t(m)=s(m);
s(m)=t(m-1);
end
s1=s;
h=1;
for l=1:length(s)-1
h=and(~s1(l),h);
end
s1(end)=and(s1(end),h);
kl=s1(end);
for i=length(encoded):-1:1
out(i)=xor(bu(i),kl);
u1=xor(0,xor(t(m-1),kl));
t(1)=xor(s(1),g(2)*u1);
s(1)=u1;
for j=2:m-1
t(j)=xor(s(j),g(j+1)*u1);
s(j)=t(j-1);
end
t(m)=s(m);
s(m)=t(m-1);
s1=s;
h=1;
for l=1:length(s)-1
h=and(~s1(l),h);
end
s1(end)=and(s1(end),h);
kl=s1(end);
end
decoded=out(m+1:n);
=========encodeing========
p=2;
n=2^(m)-1;
k=n-m;
if ( (p == 2) & (m <= 24) )
switch m
case 1
pol = [1 1];
case 2
pol = [1 1 1];
case 3
pol = [1 1 0 1];
case 4
pol = [1 1 0 0 1];
case 5
pol = [1 0 1 0 0 1];
case 6
pol = [1 1 0 0 0 0 1];
case 7
pol = [1 0 0 1 0 0 0 1];
case 8
pol = [1 0 1 1 1 0 0 0 1];
case 9
pol = [1 0 0 0 1 0 0 0 0 1];
case 10
pol = [1 0 0 1 0 0 0 0 0 0 1];
case 11
pol = [1 0 1 0 0 0 0 0 0 0 0 1];
case 12
pol = [1 1 0 0 1 0 1 0 0 0 0 0 1];
case 13
pol = [1 1 0 1 1 0 0 0 0 0 0 0 0 1];
case 14
pol = [1 1 0 0 0 0 1 0 0 0 1 0 0 0 1];
case 15
pol = [1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1];
case 16
pol = [1 1 0 1 0 0 0 0 0 0 0 0 1 0 0 0 1];
end
end
% encoder circuit implementation;
b=zeros(1,m); % tao mang 0 co 1 hang va m cot
g=pol;
c=b;
for i=length(msg):-1:1
u1=xor(msg(i),c(m-1));
c(1)=xor(b(1),g(2)*u1);
b(1)=u1;
for j=2:m-1
c(j)=xor(b(j),g(j+1)*u1);
b(j)=c(j-1);
end
c(m)=b(m);
b(m)=c(m-1);
end
encodedmsg=[b msg];
=========deencodeing========
function [out,decoded]=hamd(m,encoded)
p=2;
n=2^(m)-1;
k=n-m;
% find prim poly
if ( (p == 2) & (m <= 24) )
switch m
case 1
pol = [1 1];
case 2
pol = [1 1 1];
case 3
pol = [1 1 0 1];
case 4
pol = [1 1 0 0 1];
case 5
pol = [1 0 1 0 0 1];
case 6
pol = [1 1 0 0 0 0 1];
case 7
pol = [1 0 0 1 0 0 0 1];
case 8
pol = [1 0 1 1 1 0 0 0 1];
case 9
pol = [1 0 0 0 1 0 0 0 0 1];
case 10
pol = [1 0 0 1 0 0 0 0 0 0 1];
case 11
pol = [1 0 1 0 0 0 0 0 0 0 0 1];
case 12
pol = [1 1 0 0 1 0 1 0 0 0 0 0 1];
case 13
pol = [1 1 0 1 1 0 0 0 0 0 0 0 0 1];
case 14
pol = [1 1 0 0 0 0 1 0 0 0 1 0 0 0 1];
case 15
pol = [1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1];
case 16
pol = [1 1 0 1 0 0 0 0 0 0 0 0 1 0 0 0 1];
end
end
present ,
s=zeros(1,m);
g=pol;
t=s;
bu=encoded;
kl=0;
for i=length(encoded):-1:1
u1=xor(encoded(i),xor(t(m-1),kl));
t(1)=xor(s(1),g(2)*u1);
s(1)=u1;
for j=2:m-1
t(j)=xor(s(j),g(j+1)*u1);
s(j)=t(j-1);
end
t(m)=s(m);
s(m)=t(m-1);
end
s1=s;
h=1;
for l=1:length(s)-1
h=and(~s1(l),h);
end
s1(end)=and(s1(end),h);
kl=s1(end);
for i=length(encoded):-1:1
out(i)=xor(bu(i),kl);
u1=xor(0,xor(t(m-1),kl));
t(1)=xor(s(1),g(2)*u1);
s(1)=u1;
for j=2:m-1
t(j)=xor(s(j),g(j+1)*u1);
s(j)=t(j-1);
end
t(m)=s(m);
s(m)=t(m-1);
s1=s;
h=1;
for l=1:length(s)-1
h=and(~s1(l),h);
end
s1(end)=and(s1(end),h);
kl=s1(end);
end
decoded=out(m+1:n);
