编码
function C=HM_code(M)
G=[1 1 1
1 1 0
1 0 1
0 1 1];
G=[eye(4) G];
C=M*G;
C=mod(C,2);
译码
function D=HM_Dcode(A)
H=[1 1 1 0 1 0 0
1 1 0 1 0 1 0
1 0 1 1 0 0 1];
S=H*A.';
S=mod(S,2);
s=[0 0 0 0 1 1 1 1
0 0 1 1 0 0 1 1
0 1 0 1 0 1 0 1];
index=[4 2 1]*S;
e=[7 6 4 5 3 2 1];
%if (index~=0)
% A(e(index))=mod(A(e(index))+1,2);
%end
for k=1:length(index)
if (index(k)~=0)
A(k,e(index(k)))=mod(A(k,e(index(k)))+1,2);
end
end
D=A;
测试
clear all;clc;
m=[1 1 1 0];
hm=HM_code(m)
hm2=[0 0 0 1 1 1 1];
hm_d=HM_Dcode(hm2)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/7b19df88342ad9f51b5207bd7ef03179.png)