资源描述
MATLAB实现汉明码编码译码
精品文档
MATLAB实现汉明码编码译码
汉明码的编码就是如何根据信息位数k,求出纠正一个错误的监督矩阵H,然后根据H求出信息位所对应的码字。
1、根据已知的信息位数k,从汉明不等式中求出校验位数m=n-k;
2、在每个码字C:(C1,C2,⋯ ,C2m -1)中,用c02 ,c12 ,cn-12作为监督位,剩下的位作为信息位;
3)用二进制数字表示2m-1 列,得到2m-1列和m行监督矩阵H; 4)用3步的H形成HCT =0,从而得出m个监督方程;
5)将已知的信息代入方程组,然后求出满足上述方程组的监督位c (i=0,1,⋯ ,m一1)。
例如,用以上方法,很容易求出[7,4,3]汉明码的监督矩阵:
及编码所对应的码字为C=011001。
clear
m=3; %给定m=3的汉明码
[h,g,n,k]=hammgen(m);
msg=[0 0 0 1;0 0 0 1;0 0 0 1;0 0 1 1;0 0 1 1;0 1 0 1;0 1 1 0;0 1 1 1;1 0 0 0;1 0 0 1;1 0 1 0;1 0 1 1;1 1 0 0;1 1 0 1;1 1 1 0;1 1 1 1];
code=encode(msg,n,k,'hamming/binary') %编码
C=mod(code*h',2) %对伴随式除2取余数
newmsg=decode(code,n,k,'hamming/binary') %解码
d_min=min(sum((code(2:2^k,:))')) %最小码距
运行结果:
>> hangming
code =
1 0 1 0 0 0 1
1 0 1 0 0 0 1
1 0 1 0 0 0 1
0 1 0 0 0 1 1
0 1 0 0 0 1 1
1 1 0 0 1 0 1
1 0 0 0 1 1 0
0 0 1 0 1 1 1
1 1 0 1 0 0 0
0 1 1 1 0 0 1
0 0 1 1 0 1 0
1 0 0 1 0 1 1
1 0 1 1 1 0 0
0 0 0 1 1 0 1
0 1 0 1 1 1 0
1 1 1 1 1 1 1
C =
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
newmsg =
0 0 0 1
0 0 0 1
0 0 0 1
0 0 1 1
0 0 1 1
0 1 0 1
0 1 1 0
0 1 1 1
1 0 0 0
1 0 0 1
1 0 1 0
1 0 1 1
1 1 0 0
1 1 0 1
1 1 1 0
1 1 1 1
d_min =
3
clear
m=3; %给定m=3的汉明码
[h,g,n,k]=hammgen(m);
msg=[0 0 0 1;0 0 0 1;0 0 0 1;0 0 1 1;0 0 1 1;0 1 0 1;0 1 1 0;0 1 1 1;1 0 0 0;1 0 0 1;1 0 1 0;1 0 1 1;1 1 0 0;1 1 0 1;1 1 1 0;1 1 1 1];
code=encode(msg,n,k,'hamming/binary') %编码
subplot(121)
plot(code)
C=mod(code*h',2) %对伴随式除2取余数
newmsg=decode(code,n,k,'hamming/binary') %解码
subplot(122)
plot(newmsg)
d_min=min(sum((code(2:2^k,:))')) %最小码距
运行结果:
收集于网络,如有侵权请联系管理员删除
展开阅读全文