Skip to content
Tutorials Center
My learning

Topics

Lesson 9 of 79 ยท Physical Layer

Line Coding and Modulation

See how bits become signals: NRZ, Manchester, differential Manchester and 4B/5B line codes, clock recovery, and ASK, FSK, PSK and QAM, with live Python.

A wire cannot carry the digit 1. It can carry a voltage, a light pulse or a radio wave, and the receiver has to turn that physical thing back into bits. The rules for that translation are called encoding. When the bits are sent as voltage or light levels over a cable, we speak of line coding. When they ride on top of a sinusoidal carrier wave, as in Wi-Fi, cable and cellular networks, we speak of modulation.

The encoding is a bigger deal than it sounds. A receiver has no shared clock with the sender, so it must work out when each bit starts from the signal alone. A bad code can lose count during a long run of identical bits. In this lesson you build the classic line codes in Python, compare how well they carry timing information, and then see how modulation squeezes several bits into one symbol. It builds on the ideas of bandwidth and noise from Signals, Bandwidth and Transmission Media.

What makes a good line code

A line code is judged on a few practical goals:

  • Clock recovery. The signal should change often enough for the receiver to stay in step with the sender.
  • No DC component. Long stretches at one voltage build up a DC offset that transformers and capacitors in the link block or distort.
  • Bandwidth. Fewer signal changes per second means less bandwidth needed.
  • Error detection. Some codes make certain mistakes visible, such as an illegal pattern.
  • Simplicity. The encoder and decoder should be cheap.

No code wins on all five, which is why several exist.

The classic line codes

All the codes below are shown with two samples per bit (a "half-bit" each), because Manchester needs a mid-bit moment.

NRZ-L (non-return-to-zero, level) is the obvious one: a 1 is a high level, a 0 is a low level. It is simple and uses little bandwidth, but a long run of identical bits produces a flat line with no timing information.

NRZI (non-return-to-zero, inverted) encodes a 1 as a change of level and a 0 as no change. Long runs of ones become a nice square wave, but long runs of zeros are still flat.

Manchester guarantees a transition in the middle of every bit. In the IEEE 802.3 convention used by 10 Mbit/s Ethernet, a 1 is a low-to-high transition and a 0 is a high-to-low transition. The receiver reads the direction of the mid-bit edge. The price is that the signal changes up to twice per bit, so it needs about twice the bandwidth of NRZ.

Differential Manchester always has a mid-bit transition for timing, and uses the presence or absence of a transition at the start of the bit to carry data. In the convention used by Token Ring, a transition at the start means 0 and no transition means 1. Because the data lies in whether the level changes, it does not matter if the two wires are swapped.

flowchart LR
    A["Bits"] --> B["Line encoder"]
    B --> C["Voltage / light levels<br/>on the wire"]
    C --> D["Clock recovery<br/>+ decoder"]
    D --> E["Bits"]

The receiver must rebuild both the bit values and the bit timing from the signal alone.

Encoding and decoding in Python

The program below implements the four codes as functions that return a list of half-bit levels (+1 or -1), draws each waveform as text, and decodes each one back to prove that the round trip works.

Python NRZ, NRZI and Manchester encoders and decoders

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.

Read the drawing column by column. In Manchester, every bit shows a mid-bit edge, upward for 1 and downward for 0. In differential Manchester, every bit also has a mid-bit edge, but whether the line jumped at the start of the bit tells you the data.

Clock recovery and long runs

The receiver's clock is only locked to the sender's while edges keep arriving. Imagine 16 zero bits in NRZ-L: the line sits at one level for 16 bit times, and a receiver whose clock is off by 3% will count 15 or 17 bits. The next program measures the longest stretch with no edge for each code, and also tests a famous fix: 4B/5B.

4B/5B maps every 4 data bits to a 5-bit code word from a table chosen so that no word has more than one leading zero or two trailing zeros. Whatever the data, the stream never contains more than three zeros in a row. The 5-bit words are then sent with NRZI, so every 1 becomes an edge and edges appear at least every four bit times. It costs 25% extra bits, so 100 Mbit/s data needs a 125 Mbaud line, which is exactly what 100BASE-TX and FDDI do.

Python Longest silence per code, and the 4B/5B table

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 4B/5B result is the interesting one: even for the worst possible input, the line never goes quiet for more than three bit times. Because the table has 32 possible words but only 16 are needed for data, the leftover words become control symbols for things like start and end of a frame, which also helps error detection: a receiver can notice an illegal word.

From levels to waves: modulation

Line coding works for a cable you own. A radio channel, a telephone line or a TV cable carries only a limited band of frequencies, so the bits are impressed onto a carrier, a sine wave at the channel's frequency. Three properties of the sine can be changed:

  • Amplitude (ASK, amplitude-shift keying): a loud carrier means 1, a quiet one means 0.
  • Frequency (FSK): two nearby frequencies stand for 0 and 1. It resists noise well and was used in early modems.
  • Phase (PSK): the wave is shifted. BPSK uses two phases (0 and 180 degrees) for one bit per symbol; QPSK uses four phases for two bits.

Analog radio does the same with voices: AM and FM vary the amplitude and frequency of the carrier continuously instead of in steps.

The key idea is the symbol: the smallest unit the receiver can tell apart. If a symbol can take one of M values, it carries log2(M) bits. The symbol rate is measured in baud, and the bit rate is the baud rate times the bits per symbol. QAM (quadrature amplitude modulation) combines amplitude and phase, so a 16-QAM symbol carries 4 bits and 256-QAM carries 8. Wi-Fi, cable modems and DSL all use it.

The catch is noise. Packing more points into the same space makes them closer together, so a smaller disturbance flips one into its neighbour. That is why you see 256-QAM only on strong, clean links, and why a Wi-Fi card drops to a simpler scheme when you walk away from the router. This trade-off is exactly the one Shannon's formula describes.

Python A 16-QAM constellation with Gray coding

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.

Notice the Gray coding in the grid: moving one step to a neighbouring point changes only one bit. When noise pushes a symbol into a neighbour, it usually causes a single bit error rather than several, which makes the job of error correction easier. The second noise example shows the limit: if the disturbance is larger than half the gap between points (1.0 here), the receiver picks the wrong point.

Do not confuse baud with bits per second. They are the same only when each symbol carries one bit. A 64-QAM link running at 5 million baud moves 30 million bits per second.

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?

    Without transitions the receiver has nothing to synchronize its clock to, so a small clock difference can make it miscount bits.
  2. 2. In IEEE 802.3 Manchester coding, how is a 1 sent?

    In the 802.3 convention a 1 is a low-to-high mid-bit transition and a 0 is high-to-low. The edge in every bit carries both the clock and the data.
  3. 3. What does 4B/5B coding guarantee?

    The 5-bit words were chosen to limit runs of zeros. With NRZI each 1 is an edge, so the line is never silent for long. The cost is 25% extra bits.
  4. 4. A link uses 64-QAM at 5 million symbols per second. What is its bit rate?

    64-QAM has 64 points, so each symbol carries log2(64) = 6 bits. 5 million symbols per second times 6 bits is 30 Mbit/s.
  5. 5. Why do Wi-Fi devices switch to simpler modulation when the signal gets weak?

    More points in the same signal space means smaller distances between them, so less noise is needed to turn one symbol into another.

Summary

  • Line coding turns bits into voltage or light levels; modulation puts bits on a carrier wave.
  • A receiver must recover the clock from the signal, so codes need frequent edges.
  • NRZ-L is simple but has no timing guarantee; NRZI helps with runs of ones only.
  • Manchester has an edge in every bit, which gives clock and data at the cost of double bandwidth; differential Manchester carries data in whether the level changes.
  • 4B/5B plus NRZI limits runs of zeros to three, at 25% overhead.
  • ASK, FSK and PSK change amplitude, frequency or phase; QAM combines amplitude and phase to send several bits per symbol.
  • Bit rate equals baud rate times bits per symbol; denser constellations need cleaner channels.

Next: Multiplexing and Physical Devices