ENع
→Parity and Hamming codes
الدرس 68 · Noise, errors and OFDM

Parity and Hamming codes

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

16 دقيقة11 خطوة5 أسئلةAdvanced
عن هذا الدرس

Noise flips bits. A single parity bit can tell you that a block went wrong, but not where. Richard Hamming's 1950 code adds three parity bits over overlapping groups, so the checks that fail spell out the position of the bad bit. Follow one worked example: the data 1011 becomes the codeword 0110011, slot 5 flips on the way, the receiver's checks give the syndrome 101 = 5, and flipping that bit back recovers 1011. You will also see what the code buys, where it breaks, and how numpy does the checks as one matrix product. It follows the Shannon lesson on channel capacity.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 68 · تفاعلي
01 / 11

Noise flips a bit

Send four data bits, 1011, over a noisy link. Noise (random voltage added on the way) can push a bit past the receiver's cut-off between 0 and 1, so a 0 arrives as a 1. The receiver gets 1111 and cannot tell.

sent      1011
noise     flips one bit (slot 5 here)
received  1111

1111 is also a valid message,
so nothing looks wrong.

why bits flip: the AWGN lesson
(noise + picking the nearest level)
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. Noise flips a bit

    Send four data bits, 1011, over a noisy link. Noise (random voltage added on the way) can push a bit past the receiver's cut-off between 0 and 1, so a 0 arrives as a 1. The receiver gets 1111 and cannot tell.

    sent      1011
    noise     flips one bit (slot 5 here)
    received  1111
    
    1111 is also a valid message,
    so nothing looks wrong.
    
    why bits flip: the AWGN lesson
    (noise + picking the nearest level)
  2. One parity bit catches it

    Add one parity bit P so the count of 1s is even: 1011 has 3 ones, so P = 1. One flip makes the count odd, and the receiver knows something broke. It cannot tell which bit, and two flips cancel out.

    even parity: P = d1 ⊕ d2 ⊕ d3 ⊕ d4
      ⊕ = XOR: 1 if an odd number of 1s
      1 ⊕ 0 ⊕ 1 ⊕ 1 = 1
    
    sent      1011 1  (4 ones: even)
    received  1111 1  (5 ones: odd)
    
    detects 1, 3, 5 ... flips
    misses  2, 4 ... flips
    never says WHICH bit
  3. Hamming's layout: seven slots

    Hamming's idea (1950): use three parity bits, each over a different, overlapping group. Number seven slots 1 to 7. The parity bits go in slots 1, 2 and 4 (powers of two); the data 1011 fills slots 3, 5, 6 and 7.

    slot   1  2  3  4  5  6  7
    role   p1 p2 d1 p4 d2 d3 d4
    bit    ?  ?  1  ?  0  1  1
    
    slot in binary (4 2 1):
     1 = 001   3 = 011   5 = 101   7 = 111
     2 = 010   6 = 110
     4 = 100
    
    group k = the slots whose number has bit k set
  4. p1 checks slots 1, 3, 5, 7

    Parity bit p1 makes its group even. Its group is every odd slot: 1, 3, 5 and 7. The data there is 1, 0 and 1: two ones, already even, so p1 = 0.

    group 1 = slots 1 3 5 7
      (slot number ends in binary 1)
    
    p1 = slot3 ⊕ slot5 ⊕ slot7
       = 1 ⊕ 0 ⊕ 1
       = 0
    
    word so far:  0 ? 1 ? 0 1 1
  5. p2 checks slots 2, 3, 6, 7

    p2 covers slots 2, 3, 6 and 7 (their numbers have the 2s bit set). The data there is 1, 1 and 1: three ones, an odd count, so p2 = 1 to make it even.

    group 2 = slots 2 3 6 7
      (binary 010 011 110 111)
    
    p2 = slot3 ⊕ slot6 ⊕ slot7
       = 1 ⊕ 1 ⊕ 1
       = 1
    
    word so far:  0 1 1 ? 0 1 1
  6. p4 checks slots 4, 5, 6, 7

    p4 covers slots 4 to 7. The data there is 0, 1 and 1: even, so p4 = 0. The codeword is 0110011. Seven bits now carry four: the code rate is 4/7 ≈ 0.571.

    group 4 = slots 4 5 6 7
    
    p4 = slot5 ⊕ slot6 ⊕ slot7
       = 0 ⊕ 1 ⊕ 1
       = 0
    
    codeword  0110011
              p1 p2 d1 p4 d2 d3 d4
    
    rate = data bits / sent bits
         = 4 / 7 = 0.571
  7. The codeword crosses the noise

    All seven bits go out. Noise flips slot 5 again, a data bit this time hidden among parity bits. The receiver holds 0110111, one bit away from what was sent.

    sent      0110011
    flipped   slot 5
    received  0110111
    
    The receiver does not know
    the sent word. It only knows
    the three group rules.
  8. The failed checks name the slot

    The receiver recounts each group. Group 1 is odd: fail. Group 2 is even: pass. Group 4 is odd: fail. Add the failing group numbers: 1 + 4 = 5. That pattern, 101 in binary, is called the syndrome.

    received 0110111
    
    check 1: slots 1 3 5 7 = 0 1 1 1  -> 1 (odd)
    check 2: slots 2 3 6 7 = 1 1 1 1  -> 0 (even)
    check 4: slots 4 5 6 7 = 0 1 1 1  -> 1 (odd)
    
    syndrome c4 c2 c1 = 101 = 5
    
    only slot 5 is in groups 1 and 4
    but not in group 2
    syndrome 000 = no error seen
  9. Flip it back and read the data

    Flip slot 5 back: 0110111 becomes 0110011, the word that was sent. Read the data from slots 3, 5, 6 and 7: 1011. The error was found and fixed with no resend.

    received   0110111
    flip slot 5   ↓
    corrected  0110011
    
    data = slots 3 5 6 7 = 1011  ✓
    
    every single flip, in any of the
    7 slots, gives its own syndrome
    (001 .. 111), so any ONE error
    is found and fixed
  10. What it buys, and its limit

    If each bit flips 1 time in 100, four bare bits arrive wrong 3.9% of the time; Hamming(7,4) fails only when two or more of its seven flip: 0.20%. Two flips fool it: it fixes the wrong slot.

    p = chance one sent bit flips
    
    bare 4 bits:  1 − (1−p)⁴
    Hamming(7,4): 1 − (1−p)⁷ − 7p(1−p)⁶
    
    p = 0.01:  3.9%  vs  0.20%
    
    any two codewords differ in ≥ 3 bits
    (minimum distance 3):
      1 flip  -> corrected
      2 flips -> nearer to a wrong word
    an 8th overall parity bit (SECDED)
    detects the double: ECC memory
  11. The checks are a matrix product

    In Python with numpy, each row of a matrix H marks one group. H @ r % 2 counts each group's ones and keeps odd or even: the syndrome. Wi-Fi 6 and 5G use far longer low-density parity-check (LDPC) codes built on this idea.

    import numpy as np
    H = np.array([[1,0,1,0,1,0,1],
         [0,1,1,0,0,1,1],
         [0,0,0,1,1,1,1]])
    r = np.array([0,1,1,0,1,1,1])
    s = H @ r % 2
    
    >>> s
    array([1, 0, 1])
    >>> int(s @ [1, 2, 4])
    5
    
    row 1 of H = group 1 (slots 1 3 5 7)
    (Python 3 with numpy)
    Next: CRC, for bursts of errors

اختبار يتاح بعد الخطوة الأخيرة.