-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path产生(7,4)汉明码.txt
29 lines (27 loc) · 2.9 KB
/
产生(7,4)汉明码.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/local/easyops/python/bin/python
# -*- coding: utf-8 -*-
import math
import random
import numpy as np
# 进行编码,使用异或规则生成有校验位的(7,4)汉明码字
def hanming(code_0):
code1 = bin(int(code_0))
code = str(code1)[2:]
# # 判断待验证位数是否达到4位,不足位数前面补0
while len(code) < 4:
code = '0' + code
# 将码字转变成列表格式,方便后面进行操作
code_list = list(code)
# 编码结构即码字,对于(7,4)线性分组码汉明码而言
code_1 = int(code_list[0]) ^ int(code_list[2]) ^ int(code_list[3])
code_2 = int(code_list[0]) ^ int(code_list[1]) ^ int(code_list[2])
code_4 = int(code_list[1]) ^ int(code_list[2]) ^ int(code_list[3])
code_list.insert(0, str(code_1))
code_list.insert(1, str(code_2))
code_list.insert(2, str(code_4))
hanming_code = ''.join(code_list)
print (hanming_code)
return code_list
if __name__ == '__main__':
for i in range(16):
hanming(i)