Skip to content
Tutorials Center
My learning

Topics

Lesson 45 of 57 ยท Data Communication

Line Coding, Block Coding and Scrambling

Learn how digital data becomes voltage levels on a wire: NRZ, RZ, Manchester, AMI, block codes like 4B/5B and scrambling like B8ZS, with Python waveforms.

Inside a chip a bit is just a voltage that stays high or low for one clock period. Sending bits over a cable, or storing them on a disk, is harder. The receiver has no clock wire to tell it when each bit begins, the cable may block direct current, and long runs of identical bits make the signal look like a flat line. Line coding is the rule that maps bits to voltage levels so that the receiver can still recover both the data and the timing.

This lesson shows the classic line codes (NRZ, RZ, Manchester, differential Manchester and AMI), the properties that decide which one to use, and two ideas that repair their weaknesses: block coding (such as 4B/5B) and scrambling (such as B8ZS). Everything is simulated in Python, with waveforms drawn as text.

What makes a good line code

Four properties matter:

  • Synchronization. The receiver recovers the bit clock from the signal itself, which needs transitions frequently. A long flat stretch gives it nothing to lock to.
  • DC component. If the average voltage is not zero, the signal carries a DC component. Transformers and capacitors in cables and line drivers block DC, so a code with a DC component gets distorted ("baseline wander").
  • Bandwidth. More transitions per bit means a higher signal rate, which needs a wider channel. The baud rate (signal elements per second) can differ from the bit rate: a code that uses two signal elements per bit needs twice the baud rate.
  • Error detection. Some codes make illegal patterns (a rule violation) that reveal damage without extra bits.

No code wins on all four, which is why there are many.

The classic line codes

Code Rule Strength Weakness
Unipolar NRZ 1 = +V, 0 = 0 V simplest strong DC component, no sync
NRZ-L (polar) 1 = +V, 0 = -V simple, DC lower if data balanced no transitions in long runs of equal bits
NRZ-I a 1 flips the level, a 0 keeps it immune to wire polarity swaps; transitions at every 1 long runs of 0s have no transitions
RZ pulse in the first half of the bit, back to 0 in the second half transition in every bit three levels, twice the bandwidth
Manchester a transition in the middle of every bit: one direction means 1, the other means 0 self-clocking, no DC component twice the bandwidth
Differential Manchester always a mid-bit transition; a 0 also has a transition at the start of the bit self-clocking, polarity-immune twice the bandwidth
AMI (bipolar) 0 = 0 V, 1 = alternating +V and -V no DC, error detection (two + pulses in a row is a violation) long runs of 0s lose sync

The word NRZ means "non-return to zero": the level stays for the whole bit and never returns to zero between bits. In polar codes the two levels are positive and negative, in bipolar codes a third level (zero) is also used.

The program below encodes the same 12 bits with seven schemes, drawing each as two columns per bit so that the mid-bit transitions are visible, and then measures three properties: the number of transitions, the longest flat stretch and the average level.

Python Waveforms of the classic line codes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Compare the pictures with the table. NRZ-L has only four transitions, and the data contains a run of four zeros and four ones, so its longest flat stretch is 8 samples. The two Manchester codes never stay flat for more than two samples and have an average level of exactly 0.00, but they use about five times the transitions of NRZ (the cost of the doubled signal rate). NRZ-I changes at each 1, so it survives the run of ones but not the run of zeros. AMI has no DC component over time because the pulses alternate, yet its run of zeros is as flat as NRZ's.

The Manchester convention differs between references: some define a 1 as a low-to-high transition and a 0 as high-to-low, others the opposite. The code above uses the first. Whenever you implement a real standard, read which convention it uses.

Scrambling: fixing long runs of zeros

AMI is popular on telephone trunks because it has no DC component, but a long run of 0 bits sends no pulses at all, and the receiver clock drifts. Scrambling changes the pattern only when the dangerous sequence appears, without adding any extra bits. The trick is to substitute the run of zeros with a special pattern that violates the alternating rule, which the receiver recognizes and replaces by zeros again.

  • B8ZS (bipolar with 8-zero substitution, used on T1 lines in North America) replaces eight consecutive zeros with 000VB0VB, where V is a pulse with the same polarity as the previous pulse (a violation) and B is a normal alternating pulse.
  • HDB3 (high-density bipolar 3-zeros, used on E1 lines in Europe) replaces four zeros with 000V or B00V, chosen so that the violations themselves alternate and keep the DC component at zero.
Python B8ZS: substitute eight zeros and recover them

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The scrambled signal contains the violation pattern -+0+- (and its mirror image) where the plain AMI line was silent for 8 samples. The longest silent stretch drops from 8 to 4 and the receiver, which watches for same-polarity pulses, restores the exact original data. No bit was added, so the line rate stays the same.

Block coding: adding structure before the line code

A different approach changes the bits first and then applies a simple line code. Block coding replaces every group of m data bits by a group of n bits (n > m), choosing the code words so that they have good properties. In 4B/5B, four data bits become a five-bit code. There are 16 data groups but 32 five-bit words, so only the 16 best words are used, the ones that never produce more than three zeros in a row. The other 16 words are free for control symbols (idle, start and end of frame) or are invalid and therefore reveal errors.

Combined with NRZ-I, which makes a transition for every 1, a guarantee of "at most three zeros in a row" means a transition at least every four bit times, which is enough for clock recovery. The price is 25% overhead (5 bits sent for 4 bits of data), far cheaper than Manchester's 100%. 4B/5B with NRZ-I was used by FDDI, and Fast Ethernet (100BASE-TX) follows 4B/5B with MLT-3, a three-level cousin of NRZ-I. Other block codes are 8B/10B (Gigabit Ethernet fiber, SATA, PCI Express before version 3) and 64b/66b (10 Gigabit Ethernet).

Python 4B/5B and NRZ-I

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The program first checks the guarantee on all pairs of codes (the longest run of zeros is 3), then encodes a run of 24 zero bits. The raw data would have no transitions at all under NRZ-I (the L line is flat), but the coded data has a zero run of at most 1 here and transitions all along. The invalid word 00000 is not in the table, so a receiver can flag it as a transmission error.

Choosing a code

Need Typical choice
Simplicity between chips on one board NRZ-L with a separate clock
Self-clocking, no DC, bandwidth not a problem Manchester (10 Mbit/s Ethernet over twisted pair)
Telephone trunks, DC-free, error detection AMI with B8ZS or HDB3
High speed with limited bandwidth block code plus NRZ-I or a multilevel code

Whichever code is chosen, the next problem is getting the signal across a real channel. That is the topic of the next lesson on modulation and transmission impairments.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why is a long run of identical bits a problem for NRZ-L?

    NRZ keeps the level for the whole bit. With a long run of equal bits the line is flat and gives the receiver no timing edges.
  2. 2. Which line code has a transition in the middle of every bit and therefore carries its own clock?

    Manchester always changes level mid-bit. The price is a signal rate twice the bit rate.
  3. 3. What does B8ZS do?

    Scrambling substitutes the long run of zeros with a recognizable violation pattern, keeping the line active without adding bits.
  4. 4. What is the overhead of 4B/5B block coding?

    Five bits are sent for every four data bits, so 25% more bits are transmitted.
  5. 5. Why does a code with a DC component cause trouble on many cables?

    Many links are AC-coupled, so a non-zero average level drifts (baseline wander) and bits can be misread.

Summary

  • Line coding maps bits to voltage levels; a good code gives the receiver frequent transitions for clock recovery, a zero DC component, modest bandwidth and, ideally, error detection.
  • NRZ is simple but loses sync on long runs; Manchester and differential Manchester are self-clocking but need twice the signal rate; AMI has no DC component but needs help on runs of zeros.
  • Scrambling (B8ZS, HDB3) replaces long zero runs by violation patterns without adding bits.
  • Block codes such as 4B/5B map data groups to code words with limited zero runs, at 25% overhead, and leave spare words for control symbols and error detection.

Next: Modulation and Transmission Impairments