【python】Reed-solomon codes的二维码应用(六)

之前已经了解了BCH码检查错误,今天说一下BCH纠错,只要了解过汉明距离就非常好理解了。(4.29-5.1三天由于不计入诚信状时间内,可能不会更了,不过这个系列还是想快快快快点更完。。奈何有太多不会的地方,速度太慢了。。。)

对于二进制串a和b来说,汉明距离等于aXORb中1的数目,我们又称其为汉明权重。长度为n的二进制字符串通过汉明距离构成了一个度量空间,我们称其为汉明立方。

  • 下图表示在立方中 0100→1001 (红色)的汉明距离是 3; 0110→1110 (蓝色)的汉明距离是 1 
    在hypercube中 0100→1001 has distance 3; 0110→1110 has distance 1
如果检查错误的函数qr_check_format(fmt)得到的余数不是0,那么这个码被损坏或者是读取错误了(译注:即使是0也不能100%保证就对了)。下一步是要找出哪一个格式码最可能是原数据。因为总共只有32个格式串,因此遍历找出所有码字中与fmt不同位数最小的那个会更简单,如下:

def hamming_weight(x): #不同bit的数量
    weight = 0
    while x > 0:
        weight += x & 1
        x >>= 1
    return weight
def qr_decode_format(fmt):
    best_fmt = -1
    best_dist = 15
    for test_fmt in range(0,32):
        test_code = (test_fmt<<10) ^ qr_check_format(test_fmt<<10)
        test_dist = hamming_weight(fmt ^ test_code)
        if test_dist < best_dist:
            best_dist = test_dist
            best_fmt = test_fmt
        elif test_dist == best_dist: #如多个码字与fmt距离相同,则都不选
            best_fmt = -1
    return best_fmt



猜你喜欢

转载自blog.csdn.net/weixin_39878297/article/details/80140177