我正在尝试在 Python 中实现我自己的具有偶校验的汉明代码版本(如图所示,除了我开始从左到右而不是从右到左计算位)。
我的代码如下:
# Python program to demonstrate Hamming code
def calcCheckBits(m):
# Use the formula 2 ^ r >= m + r + 1 to calculate the number of redundant bits.
# Iterate over 0 ... m and return the value that satisfies the equation.
for i in range(m):
if(2**i >= m + i + 1):
print("Number of redundant bits: " + str(i))
return i
def posCheckBits(data, r):
# Check bits are placed at the positions that correspond to the power of 2.
j = 0
k = 1
m = len(data)
res = ''
# If position is power of 2, then insert '0'; else, append the data.
for i in range(1, m + r + 1):
if(i == 2**j):
print("If " + str(i) + " == " + str(2**j))
res = res + '0'
print("check bit placed at position " + str(i))
print("res = " + res)
j += 1
else:
res = res + data[k - 1]
print("appending data at position " + str(i))
print("res = " + res)
k += 1
print("res: " + res)
return res
def determineCheckBits(data, r):
# Check bits are placed at the positions that correspond to powers of 2.
j = 0
m = len(data)
res = data
activatedBitCounter = 0
# If position i is a power of 2, then find the value of the check bit; else, do nothing.
for i in range(1, m + r + 1):
if(i == 2**j):
contributedToBy = [] # the bits that have i in their sum of powers of 2
print("If " + str(i) + " == " + str(2**j))
# Find the values that i is contributed to by (that is, the values where i contributes to its decomposition into a sum of powers of 2)
print("Now finding the values where " + str(i) + " contributes to its decomposition into a sum of powers of 2.")
for y in range(3, m + r + 1):
powers = []
z = 1
while z <= y:
if z & y:
powers.append(z)
z <<= 1
print("The list of powers is " + str(powers))
print("The powers of 2 that sum to " + str(y) + " are ")
for l in range(len(powers)):
print(powers[l])
if(i in powers):
contributedToBy.append(y)
print("The bits that have " + str(i) + " in their sum of powers of 2 are ")
for l in range(len(contributedToBy)):
print(contributedToBy[l])
for l in contributedToBy:
if(int(res[l - 1]) == 1):
activatedBitCounter += 1
# Check if the number of activated bits is odd.
# If so, then, since we want even parity, set the checkbit to 1; else, set to 0.
if((activatedBitCounter % 2) != 0):
res[i] = 1
print("activatedBitCounter is " + str(activatedBitCounter) + " so set bit " + str(i) + " of res to 1")
print("res is now " + str(res))
else:
res[i] = 0
print("activatedBitCounter is " + str(activatedBitCounter) + " so set bit " + str(i) + " of res to 0")
print("res is now " + str(res))
activatedBitCounter = 0 # reset counter
j += 1 # Append j by 1 to move onto next power
else:
print("else, position " + str(i) + " is not a power of 2, so res = " + res)
print("Final res value from function determineCheckBits: " + res)
return res
# Enter the data to be transmitted
data = '1100011' # codeword
print("data is: " + data)
# Calculate the number of parity bits required
m = len(data)
r = calcCheckBits(m)
# Determine the positions of check bits
arr = posCheckBits(data, r)
# Determine the check bits
arr = determineCheckBits(arr, r)
在这种情况下传输的码字是 1100011。
序数位置为 2 的幂(1、2、4、8)的每个位都是一个校验位,并强制对包括其自身在内的某些“集合”位进行奇偶校验。奇偶校验可能被强制为偶数或奇数。
一个数据位有助于将其分解为 2 的幂和中的所有位的奇偶性。例如:
3 = 2 + 1
7 = 4 + 2 + 1
11 = 8 + 2 + 1
列出这些:
1 由 3、5、7、9、11
贡献 2 由 3、6 贡献, 7, 10, 11
4 由 5, 6, 7
贡献 8 由 9, 10, 11 贡献
然后我们将数据位放在它们的位置并计算每个校验位。
例如,对于 1100011,使用偶校验:
1 由 3、5、7、9、11 贡献,如果我们查看这些位进行检查 1,我们在位 3、5 和 11 处有一个 1,这是奇数 (1 + 1 + 1 = 3 ),所以因为我们想要偶校验,所以我们将位 1 设置为 1。跳到 8,我们看到 8 是由 9、10、11 贡献的,如果我们查看这些位以进行检查 4,我们有一个 1在第 10 位和第 11 位,这是偶数 (1 + 1 = 2),所以由于想要偶校验,我们将第 8 位设置为 0。这就是我们得到 11111000011 的方式。
上面的代码应该从输入 1100011 生成 11111000011。
正如您在代码中看到的,我添加了一堆用于调试的打印语句。问题是这个python代码只是随机停止输出任何东西
The powers of 2 that sum to 3 are
1
2
如此处所示:
它只是卡在那里。
这很令人困惑,因为如果我只执行包含此打印语句的部分代码,它似乎可以正常工作:
def testPowers(m, r):
# If position i is power of 2, then find the value of the parity bit; else, do nothing.
j = 0
for i in range(1, m + r + 1):
if(i == 2**j):
contributedToBy = [] # the bits that have i in their sum of powers of 2
print("If " + str(i) + " == " + str(2**j))
# Find the values that i is contributed to by (that is, the values where i contributes to its decomposition into a sum of powers of 2)
print("Now finding the values where " + str(i) + " contributes to its decomposition into a sum of powers of 2.")
for y in range(3, m + r + 1):
powers = []
z = 1
while z <= y:
if z & y:
powers.append(z)
z <<= 1
print("The powers of 2 that sum to " + str(y) + " are ")
for l in range(len(powers)):
print(powers[l])
if(i in powers):
contributedToBy.append(y)
print("The bits that have " + str(i) + " in their sum of powers of 2 are ")
for l in range(len(contributedToBy)):
print(contributedToBy[l])
m = 7
r = 4
testPowers(m, r)
我才刚刚开始学习 Python,所以我真的不知道出了什么问题。为什么我的代码不起作用,我该如何解决?
编辑1
正如蒂姆·罗伯茨(Tim Roberts)建议的那样,我想我用 修复了缩进错误z <<= 1
,但现在我又遇到了另一个错误。
# Python program to demonstrate Hamming code
def calcCheckBits(m):
# Use the formula 2 ^ r >= m + r + 1 to calculate the number of redundant bits.
# Iterate over 0 ... m and return the value that satisfies the equation.
for i in range(m):
if(2**i >= m + i + 1):
print("Number of redundant bits: " + str(i))
return i
def posCheckBits(data, r):
# Check bits are placed at the positions that correspond to the power of 2.
j = 0
k = 1
m = len(data)
res = ''
# If position is power of 2, then insert '0'; else, append the data.
for i in range(1, m + r + 1):
if(i == 2**j):
print("If " + str(i) + " == " + str(2**j))
res = res + '0'
print("check bit placed at position " + str(i))
print("res = " + res)
j += 1
else:
res = res + data[k - 1]
print("appending data at position " + str(i))
print("res = " + res)
k += 1
print("res: " + res)
return res
def determineCheckBits(data, r):
# Check bits are placed at the positions that correspond to powers of 2.
j = 0
m = len(data)
res = data
activatedBitCounter = 0
# If position i is a power of 2, then find the value of the check bit; else, do nothing.
for i in range(1, m + r + 1):
if(i == 2**j):
contributedToBy = [] # the bits that have i in their sum of powers of 2
print("If " + str(i) + " == " + str(2**j))
# Find the values that i is contributed to by (that is, the values where i contributes to its decomposition into a sum of powers of 2)
print("Now finding the values where " + str(i) + " contributes to its decomposition into a sum of powers of 2.")
for y in range(3, m + r + 1):
powers = []
z = 1
while z <= y:
if z & y:
powers.append(z)
z <<= 1
print("The list of powers is " + str(powers))
print("The powers of 2 that sum to " + str(y) + " are ")
for l in range(len(powers)):
print(powers[l])
if(i in powers):
contributedToBy.append(y)
print("The bits that have " + str(i) + " in their sum of powers of 2 are ")
for l in range(len(contributedToBy)):
print(contributedToBy[l])
for l in contributedToBy:
if(int(res[l - 1]) == 1):
activatedBitCounter += 1
# Check if the number of activated bits is odd.
# If so, then, since we want even parity, set the checkbit to 1; else, set to 0.
if((activatedBitCounter % 2) != 0):
res[i] = 1
print("activatedBitCounter is " + str(activatedBitCounter) + " so set bit " + str(i) + " of res to 1")
print("res is now " + str(res))
else:
res[i] = 0
print("activatedBitCounter is " + str(activatedBitCounter) + " so set bit " + str(i) + " of res to 0")
print("res is now " + str(res))
activatedBitCounter = 0 # reset counter
j += 1 # Append j by 1 to move onto next power
else:
print("else, position " + str(i) + " is not a power of 2, so res = " + res)
print("Final res value from function determineCheckBits: " + res)
return res
# Enter the data to be transmitted
data = '1100011' # codeword
print("data is: " + data)
# Calculate the number of parity bits required
m = len(data)
r = calcCheckBits(m)
# Determine the positions of check bits
arr = posCheckBits(data, r)
# Determine the check bits
arr = determineCheckBits(arr, r)
错误:
appending data at position 11
res = 00101000011
res: 00101000011
If 1 == 1
Now finding the values where 1 contributes to its decomposition into a sum of powers of 2.
The list of powers is [1, 2]
The powers of 2 that sum to 3 are
1
2
The list of powers is [4]
The powers of 2 that sum to 4 are
4
The list of powers is [1, 4]
The powers of 2 that sum to 5 are
1
4
The list of powers is [2, 4]
The powers of 2 that sum to 6 are
2
4
The list of powers is [1, 2, 4]
The powers of 2 that sum to 7 are
1
2
4
The list of powers is [8]
The powers of 2 that sum to 8 are
8
The list of powers is [1, 8]
The powers of 2 that sum to 9 are
1
8
The list of powers is [2, 8]
The powers of 2 that sum to 10 are
2
8
The list of powers is [1, 2, 8]
The powers of 2 that sum to 11 are
1
2
8
The list of powers is [4, 8]
The powers of 2 that sum to 12 are
4
8
The list of powers is [1, 4, 8]
The powers of 2 that sum to 13 are
1
4
8
The list of powers is [2, 4, 8]
The powers of 2 that sum to 14 are
2
4
8
The list of powers is [1, 2, 4, 8]
The powers of 2 that sum to 15 are
1
2
4
8
The bits that have 1 in their sum of powers of 2 are
3
5
7
9
11
13
15
Traceback (most recent call last):
File "/Users/x/testMultiParts.py", line 106, in <module>
arr = determineCheckBits(arr, r)
File "/Users/x/testMultiParts.py", line 73, in determineCheckBits
if(int(res[l - 1]) == 1):
IndexError: string index out of range
同样,另一个程序没有此错误:
If 1 == 1
Now finding the values where 1 contributes to its decomposition into a sum of powers of 2.
The powers of 2 that sum to 3 are
1
2
The powers of 2 that sum to 4 are
4
The powers of 2 that sum to 5 are
1
4
The powers of 2 that sum to 6 are
2
4
The powers of 2 that sum to 7 are
1
2
4
The powers of 2 that sum to 8 are
8
The powers of 2 that sum to 9 are
1
8
The powers of 2 that sum to 10 are
2
8
The powers of 2 that sum to 11 are
1
2
8
The bits that have 1 in their sum of powers of 2 are
3
5
7
9
11
编辑2
# Python program to demonstrate Hamming code
def calcCheckBits(m):
# Use the formula 2 ^ r >= m + r + 1 to calculate the number of redundant bits.
# Iterate over 0 ... m and return the value that satisfies the equation.
for i in range(m):
if(2**i >= m + i + 1):
print("Number of redundant bits: " + str(i))
return i
def posCheckBits(res, r):
# Check bits are placed at the positions that correspond to the power of 2.
j = 0
k = 1
m = len(data)
res = ''
# If position is power of 2, then insert '0'; else, append the data.
for i in range(1, m + r + 1):
if(i == 2**j):
print("If " + str(i) + " == " + str(2**j))
res = res + '0'
print("check bit placed at position " + str(i))
print("res = " + res)
j += 1
else:
res = res + data[k - 1]
print("appending data at position " + str(i))
print("res = " + res)
k += 1
print("res: " + res)
return res
def determineCheckBits(res, r):
# Check bits are placed at the positions that correspond to powers of 2.
j = 0
res = list(data)
m = len(res)
activatedBitCounter = 0
print("At the beginning, res is " + str(res))
print("At the beginning, m is " + str(m))
# If position i is a power of 2, then find the value of the check bit; else, do nothing.
for i in range(1, m + r + 1):
if(i == 2**j):
contributedToBy = [] # the bits that have i in their sum of powers of 2
print("If " + str(i) + " == " + str(2**j))
# Find the values that i is contributed to by (that is, the values where i contributes to its decomposition into a sum of powers of 2)
print("Now finding the values where " + str(i) + " contributes to its decomposition into a sum of powers of 2.")
for y in range(3, m + r + 1):
powers = []
z = 1
while z <= y:
if z & y:
powers.append(z)
z <<= 1
print("The list of powers is " + str(powers))
print("The powers of 2 that sum to " + str(y) + " are ")
for l in range(len(powers)):
print(powers[l])
if(i in powers):
contributedToBy.append(y)
print("The bits that have " + str(i) + " in their sum of powers of 2 are ")
for l in range(len(contributedToBy)):
print(contributedToBy[l])
for l in contributedToBy:
if(int(res[l - 1]) == 1):
activatedBitCounter += 1
# Check if the number of activated bits is odd.
# If so, then, since we want even parity, set the checkbit to 1; else, set to 0.
if((activatedBitCounter % 2) != 0):
res[i] = '1'
print("activatedBitCounter is " + str(activatedBitCounter) + " so set bit " + str(i) + " of res to 1")
print("res is now " + str(res))
else:
res[i] = '0'
print("activatedBitCounter is " + str(activatedBitCounter) + " so set bit " + str(i) + " of res to 0")
print("res is now " + str(res))
activatedBitCounter = 0 # reset counter
j += 1 # Append j by 1 to move onto next power
else:
print("else, position " + str(i) + " is not a power of 2, so res = " ''.join(res))
res = ''.join(res)
print("Final res value from function determineCheckBits: " + res)
return res
# Enter the data to be transmitted
data = '1100011' # codeword
print("data is: " + data)
# Calculate the number of parity bits required
m = len(data)
r = calcCheckBits(m)
# Determine the positions of check bits
arr = posCheckBits(data, r)
# Determine the check bits
arr = determineCheckBits(arr, r)
错误:
res = 00101000011
res: 00101000011
At the beginning, res is ['1', '1', '0', '0', '0', '1', '1']
At the beginning, m is 7
If 1 == 1
Now finding the values where 1 contributes to its decomposition into a sum of powers of 2.
The list of powers is [1, 2]
The powers of 2 that sum to 3 are
1
2
The list of powers is [4]
The powers of 2 that sum to 4 are
4
The list of powers is [1, 4]
The powers of 2 that sum to 5 are
1
4
The list of powers is [2, 4]
The powers of 2 that sum to 6 are
2
4
The list of powers is [1, 2, 4]
The powers of 2 that sum to 7 are
1
2
4
The list of powers is [8]
The powers of 2 that sum to 8 are
8
The list of powers is [1, 8]
The powers of 2 that sum to 9 are
1
8
The list of powers is [2, 8]
The powers of 2 that sum to 10 are
2
8
The list of powers is [1, 2, 8]
The powers of 2 that sum to 11 are
1
2
8
The bits that have 1 in their sum of powers of 2 are
3
5
7
9
11
Traceback (most recent call last):
File "/Users/x/testMultiParts.py", line 108, in <module>
arr = determineCheckBits(arr, r)
File "/Users/x/testMultiParts.py", line 75, in determineCheckBits
if(int(res[l - 1]) == 1):
IndexError: list index out of range
编辑3
好的,我想我明白了!
# Python program to demonstrate Hamming code
def calcCheckBits(m):
# Use the formula 2 ^ r >= m + r + 1 to calculate the number of redundant bits.
# Iterate over 0 ... m and return the value that satisfies the equation.
for i in range(m):
if(2**i >= m + i + 1):
print("Number of redundant bits: " + str(i))
return i
def posCheckBits(res, r):
# Check bits are placed at the positions that correspond to the power of 2.
j = 0
k = 1
m = len(data)
res = ''
# If position is power of 2, then insert '0'; else, append the data.
for i in range(1, m + r + 1):
if(i == 2**j):
print("If " + str(i) + " == " + str(2**j))
res = res + '0'
print("check bit placed at position " + str(i))
print("res = " + res)
j += 1
else:
res = res + data[k - 1]
print("appending data at position " + str(i))
print("res = " + res)
k += 1
print("res: " + res)
return res
def determineCheckBits(arr, r):
# Check bits are placed at the positions that correspond to powers of 2.
j = 0
res = list(arr)
m = len(data)
activatedBitCounter = 0
print("At the beginning, res is " + str(res))
print("At the beginning, m is " + str(m))
# If position i is a power of 2, then find the value of the check bit; else, do nothing.
for i in range(1, m + r + 1):
if(i == 2**j):
contributedToBy = [] # the bits that have i in their sum of powers of 2
print("If " + str(i) + " == " + str(2**j))
# Find the values that i is contributed to by (that is, the values where i contributes to its decomposition into a sum of powers of 2)
print("Now finding the values where " + str(i) + " contributes to its decomposition into a sum of powers of 2.")
for y in range(3, m + r + 1):
powers = []
z = 1
while z <= y:
if z & y:
powers.append(z)
z <<= 1
print("The list of powers is " + str(powers))
print("The powers of 2 that sum to " + str(y) + " are ")
for l in range(len(powers)):
print(powers[l])
if(i in powers):
contributedToBy.append(y)
print("The bits that have " + str(i) + " in their sum of powers of 2 are ")
for l in range(len(contributedToBy)):
print(contributedToBy[l])
for l in contributedToBy:
print("contributedToBy is " + str(contributedToBy))
print("res is " + str(res))
print("res[l - 1] is " + res[l - 1])
if(int(res[l - 1]) == 1):
activatedBitCounter += 1
# Check if the number of activated bits is odd.
# If so, then, since we want even parity, set the checkbit to 1; else, set to 0.
if((activatedBitCounter % 2) != 0):
res[i - 1] = '1'
print("activatedBitCounter is " + str(activatedBitCounter) + " so set bit " + str(i) + " of res to 1")
print("res is now " + str(res))
else:
res[i - 1] = '0'
print("activatedBitCounter is " + str(activatedBitCounter) + " so set bit " + str(i) + " of res to 0")
print("res is now " + str(res))
activatedBitCounter = 0 # reset counter
j += 1 # Append j by 1 to move onto next power
else:
print("else, position " + str(i) + " is not a power of 2, so res = " ''.join(res))
res = ''.join(res)
print("Final res value from function determineCheckBits: " + res)
return res
# Enter the data to be transmitted
data = '1100011' # codeword
print("data is: " + data)
# Calculate the number of parity bits required
m = len(data)
r = calcCheckBits(m)
# Determine the positions of check bits
arr = posCheckBits(data, r)
# Determine the check bits
arr = determineCheckBits(arr, r)
输出:
res = 00101000011
res: 00101000011
At the beginning, res is ['0', '0', '1', '0', '1', '0', '0', '0', '0', '1', '1']
At the beginning, m is 7
If 1 == 1
Now finding the values where 1 contributes to its decomposition into a sum of powers of 2.
The list of powers is [1, 2]
The powers of 2 that sum to 3 are
1
2
...跳到8 == 8
:
If 8 == 8
Now finding the values where 8 contributes to its decomposition into a sum of powers of 2.
The list of powers is [1, 2]
The powers of 2 that sum to 3 are
1
2
The list of powers is [4]
The powers of 2 that sum to 4 are
4
The list of powers is [1, 4]
The powers of 2 that sum to 5 are
1
4
The list of powers is [2, 4]
The powers of 2 that sum to 6 are
2
4
The list of powers is [1, 2, 4]
The powers of 2 that sum to 7 are
1
2
4
The list of powers is [8]
The powers of 2 that sum to 8 are
8
The list of powers is [1, 8]
The powers of 2 that sum to 9 are
1
8
The list of powers is [2, 8]
The powers of 2 that sum to 10 are
2
8
The list of powers is [1, 2, 8]
The powers of 2 that sum to 11 are
1
2
8
The bits that have 8 in their sum of powers of 2 are
8
9
10
11
contributedToBy is [8, 9, 10, 11]
res is ['1', '1', '1', '1', '1', '0', '0', '0', '0', '1', '1']
res[l - 1] is 0
contributedToBy is [8, 9, 10, 11]
res is ['1', '1', '1', '1', '1', '0', '0', '0', '0', '1', '1']
res[l - 1] is 0
contributedToBy is [8, 9, 10, 11]
res is ['1', '1', '1', '1', '1', '0', '0', '0', '0', '1', '1']
res[l - 1] is 1
contributedToBy is [8, 9, 10, 11]
res is ['1', '1', '1', '1', '1', '0', '0', '0', '0', '1', '1']
res[l - 1] is 1
activatedBitCounter is 2 so set bit 8 of res to 0
res is now ['1', '1', '1', '1', '1', '0', '0', '0', '0', '1', '1']
else, position 91 is not a power of 2, so res = 1 is not a power of 2, so res = 1 is not a power of 2, so res = 1 is not a power of 2, so res = 1 is not a power of 2, so res = 0 is not a power of 2, so res = 0 is not a power of 2, so res = 0 is not a power of 2, so res = 0 is not a power of 2, so res = 1 is not a power of 2, so res = 1
else, position 101 is not a power of 2, so res = 1 is not a power of 2, so res = 1 is not a power of 2, so res = 1 is not a power of 2, so res = 1 is not a power of 2, so res = 0 is not a power of 2, so res = 0 is not a power of 2, so res = 0 is not a power of 2, so res = 0 is not a power of 2, so res = 1 is not a power of 2, so res = 1
else, position 111 is not a power of 2, so res = 1 is not a power of 2, so res = 1 is not a power of 2, so res = 1 is not a power of 2, so res = 1 is not a power of 2, so res = 0 is not a power of 2, so res = 0 is not a power of 2, so res = 0 is not a power of 2, so res = 0 is not a power of 2, so res = 1 is not a power of 2, so res = 1
Final res value from function determineCheckBits: 11111000011
所以我们有 11111000011,根据需要!
编辑4
我清理了最后一部分,将其改回
else:
print("else, position " + str(i) + " is not a power of 2, so res = " + str(res))
这似乎清理了输出:
res is now ['1', '1', '1', '1', '1', '0', '0', '0', '0', '1', '1']
else, position 9 is not a power of 2, so res = ['1', '1', '1', '1', '1', '0', '0', '0', '0', '1', '1']
else, position 10 is not a power of 2, so res = ['1', '1', '1', '1', '1', '0', '0', '0', '0', '1', '1']
else, position 11 is not a power of 2, so res = ['1', '1', '1', '1', '1', '0', '0', '0', '0', '1', '1']
Final res value from function determineCheckBits: 11111000011