zgebac26
zgebac26

Reputation: 109

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. 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.

# 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 . Is there a way to implement a better Error Correction than Hamming Codes?

Thanks!

Upvotes: 1

Views: 1124

Answers (1)

Lodinn
Lodinn

Reputation: 502

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. 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. A more modern solution is LDPC.

An example of less efficient, but more easily understandable codes would be Reed-Muller. It is available as a package (reedmuller), and you can check out the code here.

Since you have requested the code example, here is one using the reedmuller library:

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).

Upvotes: 5

Related Questions