Error Correcting Codes and Reed-Solomon
Tags: cs, data
Published: 2017-10-25 16:55:23

---
title: "Error Correcting Codes and Reed-Solomon"
published: true
tags: cs, data
canonical_url: https://monades.roperzh.com/error-correction-reed-solomon/
cover_image: https://user-images.githubusercontent.com/4419992/31799037-1933a3c2-b50e-11e7-8046-4981b527747b.jpg
---

_This post was originally published on [monades.roperzh.com](https://monades.roperzh.com/error-correction-reed-solomon/)_

Transmitting digital data is tied to the risk of losing parts of it, just as a picture is exposed to scratches or sound is exposed to noise, digital information is susceptible to error, and while this risk can be arbitrarily decreased it always exists.

This article explores Error Correcting Codes, one of the methods used to mitigate data loss, along with a specific algorithm used for error correction called Reed-Solomon.

The explanations are focused only on the high-level concepts, leaving most of the mathematics aside.

## Error Correcting Codes

Error Correcting Codes help receivers of a message to detect and correct damaged data. But how?

The fundamental idea is to add extra bits (called redundancy or parity) to the original data, that helps the receiver to detect and recover from errors. Encoding data with error correction doesn't mean that it can _always_ be recovered, but adds an extra layer of safety.

![error-codes](https://user-images.githubusercontent.com/4419992/31954501-274e22aa-b8bc-11e7-8e7d-e6c89fb18c72.png)

One of the simpler ways to go about it's by repetition: append the original data three or more times. The receiver can compare this pieces of data and correct errors. In this specific case, the more you repeat the data, more chances you have to detect and correct an error.

While simple, the method of repeating data is not efficient. Something to remember when dealing with this kind of problems is the fact that in computing (like in life) it's all about compromises, you need to find a code that strikes the balance between the amount of data you send, the number of errors you can detect/correct, and the computational power needed to generate the parity bits. As you see, while the base idea is not complex, the problem gets tricky when defining the extra bits of data to add.


## Enter Reed-Solomon

This idea of adding redundancy to the data opened the door to a world of different approaches to calculate it. Reed-Solomon (RS for short) is one particularly popular error correcting code: CDs, DVDs, Blu-ray Discs, QR Codes, and even satellites uses it.

RS codes became popular because they are relatively easy to encode/decode, and powerful. To give you a rough idea, typical RS configurations provide superior data durability and superior storage efficiency compared to the three-way replication described above with _much less_ overhead.

### The idea behind Reed-Solomon

_Note: There's a considerable amount of mathematics involved in the process to generate RS codes, but this article focuses only on the concepts, letting to the reader the possibility to expand on the implementation details from any of the [suggested resources](#suggested-resources)._

RS bases it's underlying machinery in the concept that digital data is represented as _binary numbers_, therefore you can manipulate it arithmetically.

Think of the data to be transmitted as a long binary number, and imagine splitting this number into many numbers of arbitrary size. For example, you can split the number `01001001`, into four chunks: `01`, `00`, `10`, `01`.

Now, if you assign to every chunk a number representing the order in which they appear in the original number, you end up with pairs:

![reed-solomon-chunks](https://user-images.githubusercontent.com/4419992/31843564-2471342c-b5ca-11e7-91a2-8091209a31a8.png)

At this point, it's important for you to fully understand what has just happened. The data has been split into pieces, and every piece got assigned an index.

And this is where the magic happens: think of this group of pairs as points of a polynomial, and indeed, you can mathematically find a polynomial that contains those points!

![reed-solomon-graph](https://user-images.githubusercontent.com/4419992/31843565-25a3f1e0-b5ca-11e7-967a-eed6075a2149.png)

Since you have a polynomial, you are able to calculate an arbitrary number of points of it and store these points as the redundancy data. Then, if the original data points get lost/corrupted, the client can use this extra data points to reconstruct the polynomial and get the original data back!

Things get a lot more complicated when implementing them in actual code, but that's the base concept.

Based on that, you can play with two variables that affect the capacity to recover/detect errors.

**The number of chunks in which you split the original data**

As you increase the number of chunks, you get a polynomial that better represents your data, this comes with the cost of more computing to encode/decode the data.

**The number of extra points you store**

The more points you store, the more errors you can correct, but this comes with the cost of transmitting a bigger amount of data.

## Suggested resources

There's a lot more about error codes and Reed-Solomon to be learned if you're interested here are some resources that I've found helpful:

- [Codes: How to Protect Your Data](http://www.eecs.harvard.edu/~michaelm/FUTUREOFCS/codes-mitzenmacher.pdf) - Michael Mitzenmacher
- [Introduction to Reed-Solomon](https://innovation.vivint.com/introduction-to-reed-solomon-bc264d0794f8) - Jeff Wendling
- [Reed-Solomon Codes](https://www.cs.cmu.edu/~guyb/realworld/reedsolomon/reed_solomon_codes.html) - Martyn Riley and Iain Richardson
- [Engineering CS144 unit 7-4](https://suclass.stanford.edu/courses/course-v1:Engineering+CS144+Fall2016/courseware/8470e3e5702245579e28e508065118ee/ccf91e95fa4b48e58365cb55c2a08dab/) - Stanford University
- [An Introduction to Reed-Solomon Codes](http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.93.8022&rep=rep1&type=pdf) - Wicker and Bhargava