简体   繁体   English

使用 Python 中的纠错码更正消息

[英]Correcting a message using Error Correcting Codes in Python

I am developing a data-hiding method using Python. I am sending a maximum of 16 bits of information - zeros, and ones.我正在开发一种使用 Python 的数据隐藏方法。我最多发送 16 位信息——0 和 1。 Sometimes, the original message gets messed up on its way to the decoder, so I am trying to implement a kind of Error Correction.有时,原始消息在到达解码器的途中会被弄乱,所以我正在尝试实施一种纠错。

I successfully implemented Hamming Codes, but it only makes sense if there is exactly one error in the message.我成功地实现了汉明码,但只有在消息中恰好有一个错误时才有意义。 Below is an example of a typical 7-bit message in my case.下面是一个典型的 7 位消息示例。

# Original message
message_encoder = [1, 0, 0, 1, 1, 0, 1] 

### COMMUNICATION CHANNEL ###
### COMMUNICATION CHANNEL ###
### COMMUNICATION CHANNEL ###

# Message at the decoder
message_decoder = [1, 0, 0, 0, 1, 0, 0]

So, in this example, I have two errors, with a Bit Error Ratio = 2/7 .因此,在此示例中,我有两个错误, Bit Error Ratio = 2/7 Is there a way to implement a better Error Correction than Hamming Codes?有没有一种方法可以实现比汉明码更好的纠错?

Thanks!谢谢!

Well, yes, but this rabbit hole is rather deep.嗯,是的,但是这个兔子洞相当深。

Whatever the implementation is, the transfer rate will be subject to the Shannon's theorem .无论实施方式如何,传输速率都将服从香农定理 There is no telling which error correction code is "better" - the more bits you are able to correct, the more the overhead becomes.无法确定哪个纠错码“更好”——您能够纠正的位数越多,开销就越大。 There are so, so many design considerations involved...涉及到如此多的设计考虑因素......

For example, if you are dealing with a data stream, bit flips would not be your only problem.例如,如果您正在处理数据 stream,位翻转将不是您唯一的问题。 You get smart and introduce markers for the transition start and end?您变得聪明并为过渡开始和结束引入标记? How big and complex are they?它们有多大和复杂? What if you are dealing with adversarial attacks, and they can trick your system into starting the transcription from the wrong spot?如果您正在处理对抗性攻击,并且它们可以诱骗您的系统从错误的位置开始转录怎么办? (Cool fact of the day - this is what mother Nature uses extensively ). (今天很酷的事实——这是大自然母亲广泛使用的)。

Further, correcting multiple bit flips quickly becomes very complex.此外,快速纠正多个位翻转变得非常复杂。 Academics write (somewhat recent) papers on decoding (23,12,7) Golay , it is not something you would generally do out of boredom over a weekend.学者撰写(最近)关于解码(23,12,7) Golay的论文,这不是您通常会在周末无聊时做的事情。 A more modern solution is LDPC .一个更现代的解决方案是LDPC

An example of less efficient, but more easily understandable codes would be Reed-Muller. Reed-Muller 是效率较低但更易于理解的代码示例。 It is available as a package ( reedmuller ), and you can check out the code here .它以 package ( reedmuller ) 的形式提供,您可以在此处查看代码。

Since you have requested the code example, here is one using the reedmuller library:由于您请求了代码示例,这里是一个使用reedmuller库的代码示例:

from reedmuller import reedmuller

rm = reedmuller.ReedMuller(2, 5)
message = r'1100110101010101'
encoded = ''.join(map(str, rm.encode(list(map(int, message)))))
# encoded =           r'10111000011101000001110111010001'
encoded_with_errors = r'10111010011101000101010111010001'
decoded = ''.join(map(str, rm.decode(list(map(int, encoded_with_errors)))))
assert(decoded == message)

(Note: list(map(int(...))) is only needed because I treated messages as strings. In your representation, they are already lists of ints). (注意: list(map(int(...)))是必需的,因为我将消息视为字符串。在您的表示中,它们已经是整数列表)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM