【问题标题】:CRC Polynomial DivisionCRC多项式除法
【发布时间】:2016-07-25 08:34:01
【问题描述】:

我正在尝试使用多项式除法来查找 CRC 校验位,但我在计算的最后阶段遇到了困难。

我相信以下转换是正确的:

Pattern = 1010 
        = x^3 + x

Dataword = 9 8 7 
         = 1001 1000 0111
         = x^11 + x^8 + x^7 + x^2 + x + 1

最后我尝试的多项式长除法是:

              x^8 + x^6 + x^5 + x^3 + x
        _______________________________________
x^3 + x | x^11 + x^8 + x^7 + x^2 + x + 1
          x^11 + x^9

        ....
              x^4 + x^2 + x + 1
              x^4 + x^2

                  = x + 1

我的问题是,余数/答案是 x + 1 还是我更进一步并删除 x 将余数保留为 1

感谢您的帮助!

【问题讨论】:

  • 它是x + 1,即CRC校验和是0011。也就是说,如果你的计算是正确的
  • @NiklasB。谢谢你的回复小伙伴!也许我的计算有错误,因为当我将它添加到数据的末尾并再次进行除法时,我的答案不为零......
  • 对不起我错了,校验和是3位,如果我没记错应该是110。你的计算可能有错误
  • @NiklasB。谢谢!我使用的是“011”,这就是它不起作用的原因。

标签: algorithm division checksum crc polynomials


【解决方案1】:

我们也可以通过mod 2除法(XOR)进行校验,下面的代码展示了一个python实现的CRC校验,我们需要按照下面列出的步骤进行:

  • 将 CRC / 数据多项式转换为相应的二进制等效项。

  • 如果 CRC 密钥(从多项式获得的二进制表示)有 k 位,我们需要用数据填充额外的 k-1 位以检查错误.在给出的示例中,应将位 011 附加到数据中,而不是 0011,因为 k=4

在发送端,

  • 首先通过在数据末尾添加 k-1 个零来扩充二进制数据。

  • 使用模2二进制除法将二进制数据除以CRC密钥并存储除法的余数。

  • 在数据末尾追加余数,形成编码后的数据并发送

在接收端(检查传输中是否引入错误)

  • 用 CRC 密钥对发送的数据再次进行模 2 除法,如果余数为 0,则没有错误。

现在让我们实现上面的:

def CRC_polynomial_to_bin_code(pol):
    return bin(eval(pol.replace('^', '**').replace('x','2')))[2:]

def get_remainder(data_bin, gen_bin):
    ng = len(gen_bin)
    data_bin += '0'*(ng-1)
    nd = len(data_bin)
    divisor = gen_bin
    i = 0
    remainder = ''
    print('\nmod 2 division steps:')
    print('divisor dividend remainder')
    while i < nd:
        j = i + ng - len(remainder)
        if j > nd: 
            remainder += data_bin[i:]
            break
        dividend = remainder + data_bin[i:j]
        remainder = ''.join(['1' if dividend[k] != gen_bin[k] else '0' for k in range(ng)])
        print('{:8s} {:8s} {:8s}'.format(divisor, dividend, remainder[1:]))
        remainder = remainder.lstrip('0')
        i = j
    return remainder.zfill(ng-1)

gen_bin = CRC_polynomial_to_bin_code('x^3+x')
data_bin = CRC_polynomial_to_bin_code('x^11 + x^8 + x^7 + x^2 + x + 1') 
print('transmitter end:\n\nCRC key: {}, data: {}'.format(gen_bin, data_bin))
r = get_remainder(data_bin, gen_bin)
data_crc = data_bin + r
print('\nencoded data: {}'.format(data_crc))
print('\nreceiver end:')
r = get_remainder(data_crc, gen_bin)
print('\nremainder {}'.format(r))

if eval(r) == 0:
    print('data received at the receiver end has no errors')

# ---------------------------------
# transmitter end:
# 
# CRC key: 1010, data: 100110000111
# 
# mod 2 division steps:
# divisor dividend remainder
# 1010     1001     011     
# 1010     1110     100     
# 1010     1000     010     
# 1010     1000     010     
# 1010     1011     001     
# 1010     1100     110     
# 1010     1100     110     
# 
# encoded data: 100110000111110
# ---------------------------------
# receiver end:
# 
# mod 2 division steps:
# divisor dividend remainder
# 1010     1001     011     
# 1010     1110     100     
# 1010     1000     010     
# 1010     1000     010     
# 1010     1011     001     
# 1010     1111     101     
# 1010     1010     000     
# 
# remainder 000
# data received at the receiver end has no errors
# ---------------------------------

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-17
    • 2016-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多