Bit-Flipping Decoding
The simplest way to decode an LDPC code uses only the hard received bits and the parity checks. Each check that fails is a vote that one of its bits is wrong. Bits that collect many such votes are flipped, the checks are recomputed, and the process repeats. Gallager proposed this bit-flipping algorithm in 1962; it is fast, easy to build in hardware, and already corrects many errors.
The algorithm
- Compute the syndrome . If it is zero, stop: is a codeword.
- For each bit, count the unsatisfied checks it belongs to.
- Flip the bits for which (almost) all their checks fail. If there are none, flip the bits with the highest count.
- Repeat until the syndrome is zero or an iteration limit is reached.
With column weight 3, "almost all" means all 3 checks; the code below uses that rule.
import numpy as np
def gallager_H(n, wc, wr, rng):
band = np.zeros((n // wr, n), dtype=np.int8)
for i in range(n // wr):
band[i, i * wr:(i + 1) * wr] = 1
return np.vstack([band] + [band[:, rng.permutation(n)] for _ in range(wc - 1)]).astype(int)
def bit_flip(H, r, max_iter=50):
r = r.copy()
for it in range(max_iter):
z = H @ r % 2
if not z.any():
return r, it, True
votes = z @ H # unsatisfied checks per bit
flip = votes >= 3
if not flip.any():
flip = votes == votes.max()
r[flip] ^= 1
return r, max_iter, False
rng = np.random.default_rng(0)
H = gallager_H(240, 3, 6, rng)
c = np.zeros(240, dtype=int) # the all-zero word is a codeword of every linear code
for e in [3, 5, 8, 12]:
good = 0
for _ in range(100):
r = c.copy(); r[rng.choice(240, e, replace=False)] = 1
good += int(np.array_equal(bit_flip(H, r)[0], c))
print(f"{e:2d} random errors: corrected in {good} of 100 trials") 3 random errors: corrected in 97 of 100 trials
5 random errors: corrected in 96 of 100 trials
8 random errors: corrected in 78 of 100 trials
12 random errors: corrected in 37 of 100 trials
A few patterns fail even with only 3 errors. They are trapping sets: small groups of bits connected by short cycles of the Tanner graph, where every wrong bit sees only one failing check. Avoiding short cycles in the construction makes them rarer.
Why sending zeros is enough
For a linear code on a symmetric channel, the decoder's success does not depend on which codeword was sent: flipping the sent codeword to zero just XORs a fixed codeword onto everything, and the syndrome (which drives the decoder) depends only on the noise (Parity-Check Matrices and Syndromes). Simulations therefore usually send the all-zero codeword, so no encoder is needed.
Performance on a BSC
import numpy as np
def gallager_H(n, wc, wr, rng):
band = np.zeros((n // wr, n), dtype=np.int8)
for i in range(n // wr):
band[i, i * wr:(i + 1) * wr] = 1
return np.vstack([band] + [band[:, rng.permutation(n)] for _ in range(wc - 1)]).astype(int)
def bit_flip(H, r, max_iter=50):
r = r.copy()
for it in range(max_iter):
z = H @ r % 2
if not z.any():
return r
votes = z @ H
flip = votes >= 3
if not flip.any():
flip = votes == votes.max()
r[flip] ^= 1
return r
rng = np.random.default_rng(1)
n = 480
H = gallager_H(n, 3, 6, rng)
for f in [0.01, 0.02, 0.03, 0.04, 0.05]:
blocks = 200
fails = bit_errs = 0
for _ in range(blocks):
r = (rng.random(n) < f).astype(int)
d = bit_flip(H, r)
fails += int(d.any()); bit_errs += int(d.sum())
print(f"f = {f:.2f}: block error rate {fails / blocks:.3f}, bit error rate {bit_errs / (blocks * n):.4f}")f = 0.01: block error rate 0.010, bit error rate 0.0002
f = 0.02: block error rate 0.020, bit error rate 0.0002
f = 0.03: block error rate 0.170, bit error rate 0.0077
f = 0.04: block error rate 0.370, bit error rate 0.0287
f = 0.05: block error rate 0.680, bit error rate 0.0594
The code has rate about , so its Shannon limit is . Bit flipping works well only up to a few percent: hard decisions and greedy flips waste information. The sum-product algorithm does much better (Sum-Product (Belief Propagation) Decoding).
Variants
- Parallel threshold flipping: flip every bit whose count exceeds a threshold, all at once. Fast in hardware, but can oscillate.
- Gallager's algorithm B: messages on the edges instead of whole-bit votes, which avoids reusing a check's own information.
- Weighted bit flipping: uses the reliability of each received value (its magnitude on a Gaussian channel) when counting votes.
import numpy as np
def gallager_H(n, wc, wr, rng):
band = np.zeros((n // wr, n), dtype=np.int8)
for i in range(n // wr):
band[i, i * wr:(i + 1) * wr] = 1
return np.vstack([band] + [band[:, rng.permutation(n)] for _ in range(wc - 1)]).astype(int)
def flip_threshold(H, r, thr, max_iter=50):
r = r.copy()
for _ in range(max_iter):
z = H @ r % 2
if not z.any():
return r
votes = z @ H
flip = votes >= thr
if not flip.any():
flip = votes == votes.max()
r[flip] ^= 1
return r
rng = np.random.default_rng(2)
n = 480
H = gallager_H(n, 3, 6, rng)
noise = [(rng.random(n) < 0.03).astype(int) for _ in range(200)]
for thr in [2, 3]:
fails = sum(int(flip_threshold(H, r, thr).any()) for r in noise)
print(f"flip all bits with >= {thr} unsatisfied checks: block error rate {fails / 200:.3f}")flip all bits with >= 2 unsatisfied checks: block error rate 0.965
flip all bits with >= 3 unsatisfied checks: block error rate 0.095
With column weight 3, flipping on 2 votes flips too many correct bits at once. Requiring all 3 checks to fail is much safer.
In software
- Bit flipping needs only XORs and counters, so it suits very high-speed links and low-power chips; flash memory controllers often try it first and fall back to soft decoding when it fails.
- Always stop as soon as the syndrome is zero; most blocks at low noise decode in a few iterations.
Exercises
Exercise 1: Votes
A bit is in 3 checks, 2 of which fail. Another bit is in 3 checks, 1 of which fails. Which does the max-vote rule flip first?
Answer
The first one.
Solution
It has 2 unsatisfied checks against 1. With a single error in a column-weight-3 code, the wrong bit sees 3 failing checks, more than any other bit.
Exercise 2: Detecting failure
How does the decoder know it has failed?
Answer
The syndrome is still non-zero after the iteration limit.
Solution
A non-zero syndrome means the output is not a codeword. (If it converges to the wrong codeword, it cannot tell; that is an undetected error, which is rare for LDPC codes.)
Exercise 3: Count unsatisfied checks (code)
For the Hamming code's and the received word 1000000, compute the number of unsatisfied checks for each bit.
Answer
[2, 1, 2, 1, 1, 0, 1]: bits 0 and 2 are both in the two failing checks.
Solution
import numpy as np
P = np.array([[1, 0, 1], [1, 1, 0], [1, 1, 1], [0, 1, 1]])
H = np.hstack([P.T, np.eye(3, dtype=int)])
r = np.array([1, 0, 0, 0, 0, 0, 0])
z = H @ r % 2
print("syndrome:", z, " votes:", (z @ H).tolist())syndrome: [1 0 1] votes: [2, 1, 2, 1, 1, 0, 1]
Bit 2 has as many votes as bit 0, so plain bit flipping cannot decide here: the dense Hamming code is a poor fit for this decoder, which is designed for sparse .
Test yourself
In bit-flipping decoding, a bit is flipped when…
- it belongs to many unsatisfied checks
- it is a 1
- its neighbours are 0
- it was flipped before
Why may LDPC simulations send only the all-zero codeword?
- For linear codes on symmetric channels the error probability does not depend on the codeword
- Zero is the most likely message
- The decoder only works on zeros
- To save memory only
Compared with sum-product decoding, bit flipping…
- is simpler but corrects fewer errors
- is always better
- uses soft information
- cannot correct any errors