Bits become levels
A cable carries voltages, not ones and zeros. A line code is the rule that turns each bit into a voltage pulse lasting one bit time, Tb. Here are 8 bits to send: 1 0 1 1 1 1 0 0.
The simplest rule: a 1 is +A volts, a 0 is 0 volts, held for the whole bit. NRZ means non-return-to-zero: the level does not drop back to 0 in mid-bit. The encoder sends one pulse per bit.
Polar NRZ sends a 1 as +A and a 0 as −A. The gap between the two levels doubles, so noise must be twice as big to flip a bit. With equally many 1s and 0s it averages to zero.
What goes wrong
Many cables pass through a transformer or capacitor that blocks DC, a steady voltage. On-off NRZ has a steady part of A/2 (here 0.625 A), and polar NRZ's spectrum peaks at 0 Hz. Blocked, that part is lost and the baseline drifts.
There is no clock wire. The receiver keeps time by watching the level change. Bits 3 to 6 are all 1, so polar NRZ stays flat for 4 bit times. A slightly fast or slow receiver clock can count 3 or 5 ones.
Better codes
Manchester splits each bit in two halves. In the IEEE 802.3 rule a 1 goes low then high, a 0 high then low. Every bit has an edge in its middle, so the clock never starves, and every bit averages to zero.
Half-bit pulses change twice as fast, so Manchester needs more bandwidth. Its spectrum's main hump (lobe) ends at the first null, a frequency with zero power: 2R, against R for polar NRZ. 10BASE-T Ethernet sends 10 Mbit/s in Manchester: that null is at 20 MHz.
AMI sends a 0 as 0 volts and each 1 with the opposite sign to the last 1. The pulses cancel in pairs, so no DC builds up, and its main hump still ends at R. Two same-sign 1s in a row reveal an error.
Back to bits
The receiver locks its clock to the mid-bit edges, then measures each half of every bit. Second half higher than the first: a 1. Lower: a 0. The 8 bits come back as 1 0 1 1 1 1 0 0.
In Python with numpy: 2*b − 1 maps bits to ±1 (polar NRZ). np.repeat doubles each value into two halves, and multiplying by the pattern −1, +1 flips the first half. That is Manchester, 16 half-bits.
The working
# step 1/10 · bits bits to send: 1 0 1 1 1 1 0 0 bit time Tb (seconds per bit) bit rate R = 1 / Tb (bits per second) line code = the rule bit -> voltage for one Tb levels are drawn in units of A, the pulse height in volts
Levels are drawn in units of A, the pulse height. The spectra assume long streams of random, equally likely, independent bits with full-width rectangular pulses, normalised by A²·Tb; every curve is computed from the formulas in the model and checked against a DFT of simulated bits. The motion is slowed down a great deal: at 10 Mbit/s one bit lasts 100 ns.