3

【纠错码】BCH

 9 months ago
source link: https://www.guofei.site/2023/10/15/bch.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

【纠错码】BCH

2023年10月15日    Author:Guofei

文章归类: 0xb0_Python语法    文章编号: 1269

版权声明:本文作者是郭飞。转载随意,但需要标明原文链接,并通知本人
原文链接:https://www.guofei.site/2023/10/15/bch.html

BCH

https://github.com/jkent/python-bchlib

import random
import bchlib

ECC_POLY = 8219
ECC_BITS = 16  # 16-bit ECC encoder
bch = bchlib.BCH(ECC_BITS, prim_poly=ECC_POLY)

# 待编码数据
text = 'hello world ' * 30

# 转 bytearray
data = bytearray('hello world ' * 30, 'utf-8')

# 创建一个包含数据和错误校正码的数据包
ecc = bch.encode(data)
packet = data + ecc

# 展示原始数据包
print('original packet: %s' % packet)


# 模拟在数据包上引入错误
def bit_flip(packet):
    byte_num = random.randint(0, len(packet) - 1)
    bit_num = random.randint(0, 7)
    packet[byte_num] ^= (1 << bit_num)


# 引入5个错误
for _ in range(5):
    bit_flip(packet)

# 展示引入错误后的数据包
print('corrupt packet: %s' % packet)

# 解码并纠正数据包
data, ecc = packet[:-bch.ecc_bytes], packet[-bch.ecc_bytes:]
bitflips = bch.decode(data, ecc)

if bitflips >= 0:
    print('bitflips: %d' % bitflips)
else:
    print('unrecoverable error')

bch.correct(data, ecc)
print('corrected packet: %s' % (data + ecc))

您的支持将鼓励我继续创作!

qr.jpeg

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK