haming_encoding.m

来自「汉明码编码仿真程序」· M 代码 · 共 69 行

M
69
字号
function encodedmsg=hamc(m,msg)
% m is positive integer, msg= binary msg to be encoded whose length is(( 2^m-1)-m)
%encoded msg is given as out put
% function to hamming encoder circuit implementation 

% hamming code, encoding
p=2;
n=2^(m)-1;
k=n-m;

% primitive polynomial is same as generator 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

% encoder circuit implementation;
b=zeros(1,m);
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];
    

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?