---
title: Transmission line lab
heading: Reflections, input impedance and bounce diagrams in Python
eyebrow: C10 · Lesson 13 · Transmission line lab
xp: 50
---

::: hook
Lessons 7 to 11 worked reflections, standing waves and input impedance by hand, one case at a time. Engineers don't: they write a few lines of code once and run every case through it. In this lab you'll write that code with complex numbers: the reflection coefficient and VSWR of any load, a line's input impedance, the load voltage after each bounce, the impedance a TDR trace implies, and the length of a stub for any reactance.
:::

::: play
- A step from a source of resistance $R_s$ drives a 50 Ω line ending in R. The staircase is the load voltage as reflections arrive, one round trip apart (time in units of the one-way delay).
- With either end matched, it settles on the first arrival. With both mismatched, it rings toward the DC divider value $R/(R_s + R)$.
- Make both reflections the same sign (both resistances above 50 Ω, or both below): the steps approach from one side. Make them opposite: the voltage overshoots and rings.
:::

```sim plot
view: graph
x: t = 0..16
y: v = 0..2
slider: Rs = 5..200 step 5 = 25
slider: R = 5..1000 step 5 = 100
plot: (50/(Rs + 50))*(1 + ((R - 50)/(R + 50)))*((((R - 50)/(R + 50))*((Rs - 50)/(Rs + 50)))^0*step(t - 1) + (((R - 50)/(R + 50))*((Rs - 50)/(Rs + 50)))^1*step(t - 3) + (((R - 50)/(R + 50))*((Rs - 50)/(Rs + 50)))^2*step(t - 5) + (((R - 50)/(R + 50))*((Rs - 50)/(Rs + 50)))^3*step(t - 7) + (((R - 50)/(R + 50))*((Rs - 50)/(Rs + 50)))^4*step(t - 9) + (((R - 50)/(R + 50))*((Rs - 50)/(Rs + 50)))^5*step(t - 11) + (((R - 50)/(R + 50))*((Rs - 50)/(Rs + 50)))^6*step(t - 13) + (((R - 50)/(R + 50))*((Rs - 50)/(Rs + 50)))^7*step(t - 15)) | samples: 1600 | label: load voltage
plot: R/(Rs + R) | dashed | label: final value
readout: source reflection = (Rs - 50)/(Rs + 50) | digits: 3
readout: load reflection = (R - 50)/(R + 50) | digits: 3
```

::: predict
## A TDR launches 0.5 V into a 50 Ω cable and sees a reflection of +0.1 V from a discontinuity. What impedance does the discontinuity present?

- [ ] 40 Ω
- [ ] 60 Ω
- [x] 75 Ω
- [ ] 100 Ω

::: reveal
75 Ω. Γ = 0.1/0.5 = 0.2, and $Z = Z_0(1 + \Gamma)/(1 - \Gamma) = 50 \times 1.2/0.8 = 75$ Ω: perhaps a section of TV cable spliced in. A negative reflection would mean a lower impedance, such as a connector with too much capacitance.
:::
:::

::: nameit
## The formulas in code

- $\Gamma = (Z_L - Z_0)/(Z_L + Z_0)$, with an open load ($Z_L = \infty$) giving exactly 1. Python's complex numbers handle reactive loads directly: `25 - 50j`.
- $\text{VSWR} = (1 + \lvert\Gamma\rvert)/(1 - \lvert\Gamma\rvert)$, infinite for $\lvert\Gamma\rvert = 1$.
- $Z_\text{in} = Z_0\dfrac{Z_L + jZ_0\tan\beta l}{Z_0 + jZ_L\tan\beta l}$, and for an open load $-jZ_0\cot\beta l$.
- **Bounces**: the launched step is $V_sZ_0/(R_s + Z_0)$; each arrival adds the current wave times $(1 + \Gamma_L)$, and the next wave is the current one times $\Gamma_L\Gamma_S$.
- **TDR**: a reflected step $V_r$ on an incident step $V_i$ means $\Gamma = V_r/V_i$ and $Z = Z_0(1 + \Gamma)/(1 - \Gamma)$.
- **Stubs**: a shorted stub of length l looks like $jZ_0\tan\beta l$, so a reactance X needs $l/\lambda = \arctan(X/Z_0)/2\pi$, taken modulo ½ to keep it positive.
:::

::: example
## A 1 V step from a 25 Ω source drives a 50 Ω line into 100 Ω. List the load voltage after each of the first three arrivals.

::: solution
1. **Launched step and reflection coefficients.**
   $$\frac{50}{75} \times 1\ \text{V} = 0.667\ \text{V}$$
   $$\Gamma_L = \tfrac13, \quad \Gamma_S = -\tfrac13$$
2. **First arrival.**
   $$\begin{aligned} V_1 &= 0.667 \times \tfrac43 \\ &= 0.889\ \text{V} \end{aligned}$$
3. **Second arrival.** The next wave is $0.667 \times \tfrac13 \times \left(-\tfrac13\right) = -0.0741$ V, and at the load it adds 4/3 of that:
   $$\begin{aligned} V_2 &= 0.889 - 0.0988 \\ &= 0.790\ \text{V} \end{aligned}$$
4. **Third arrival.** The third adds +0.011 V:
   $$V_3 = 0.801\ \text{V}$$
5. **Final value.** It closes on the DC divider value:
   $$\frac{100}{125} = 0.8\ \text{V}$$

**Answer:** the load reads 0.889 V, 0.790 V and 0.801 V, closing on 0.8 V.
:::
:::

::: example
## How long a shorted 50 Ω stub looks like −j50 Ω?

::: solution
1. **Solve for the electrical length.** A shorted stub looks like $jZ_0\tan\beta l$, so set $\tan\beta l = -50/50$:
   $$\arctan(-1) = -\frac{\pi}{4}$$
   which gives $l/\lambda = -0.125$.
2. **Make it a real length.** Adding a half wavelength gives
   $$-0.125\lambda + 0.5\lambda = 0.375\lambda$$
3. **Why so long.** A shorted stub needs to be between a quarter and a half wavelength to look capacitive.
4. **A shorter option.** An open stub 0.125λ long would do the same job in less space.

**Answer:** the shorted stub must be 0.375λ long.
:::
:::

::: check
given: vr = -0.3 | -0.1 | 0.15 | 0.25; vi = 0.5
answer: 50*(1 + vr/vi)/(1 - vr/vi)
unit: Ω
tolerance: 0.5%

## A TDR launches {vi} V into 50 Ω and sees a {vr} V reflection. What impedance is there?

::: right
Right: {answer} Ω.
:::

::: misses
- [any] Not yet. $\Gamma = V_r/V_i$, then $Z = Z_0(1 + \Gamma)/(1 - \Gamma)$.
:::
:::

::: check
kind: python

## Write `gamma(ZL, Z0)` (a complex number; `math.inf` means an open circuit and gives exactly 1) and `vswr(ZL, Z0)` (`math.inf` when |Γ| is 1).

```python starter
import math

def gamma(ZL, Z0):
    return 0j

def vswr(ZL, Z0):
    return 1.0
```

```python solution
import math

def gamma(ZL, Z0):
    if ZL == math.inf:
        return 1 + 0j
    return (ZL - Z0) / (ZL + Z0)

def vswr(ZL, Z0):
    g = abs(gamma(ZL, Z0))
    return math.inf if g >= 1 else (1 + g) / (1 - g)
```

```python tests
import math
assert abs(gamma(100, 50) - 1 / 3) < 1e-12 and abs(vswr(100, 50) - 2) < 1e-12, "100 Ω on 50 Ω: Γ = 1/3, VSWR 2."
g = gamma(25 - 50j, 50)
assert abs(g - (0.076923 - 0.615385j)) < 1e-6, f"25 - j50 Ω on 50 Ω: Γ = 0.077 - j0.615, got {g}."
assert abs(vswr(25 - 50j, 50) - 4.2656) < 1e-3, f"Its VSWR is 4.27, got {vswr(25 - 50j, 50)}."
assert gamma(math.inf, 50) == 1 and vswr(0, 50) == math.inf, "An open reflects +1; a short has infinite VSWR."
```

::: right
Right. Complex arithmetic does all the work.
:::
:::

::: check
kind: python

## Write `zin(ZL, Z0, bl)`: the input impedance of a line of electrical length `bl` radians ending in ZL (which may be `math.inf`).

```python starter
def zin(ZL, Z0, bl):
    return ZL
```

```python solution
import math

def zin(ZL, Z0, bl):
    t = math.tan(bl)
    if ZL == math.inf:
        return -1j * Z0 / t
    return Z0 * (ZL + 1j * Z0 * t) / (Z0 + 1j * ZL * t)
```

```python tests
import math
assert abs(zin(100, 50, math.pi / 4) - (40 - 30j)) < 1e-9, f"100 Ω through λ/8 of 50 Ω: 40 - j30, got {zin(100, 50, math.pi / 4)}."
assert abs(zin(25, 50, math.pi / 2 - 1e-9) - 100) < 1e-4, "A quarter wave inverts 25 Ω to 100 Ω."
assert abs(zin(0, 50, math.pi / 4) - 50j) < 1e-9, "A shorted λ/8 stub is +j50."
assert abs(zin(math.inf, 50, math.pi / 4) + 50j) < 1e-9, "An open λ/8 stub is -j50."
```

::: right
Right. One formula covers every load and every length.
:::
:::

::: check
kind: python

## Write `bounce(Vs, Rs, Z0, RL, n)`: the load voltage after each of the first n arrivals when a step of Vs from a source of resistance Rs drives a line of Z0 into a resistive load RL.

```python starter
def bounce(Vs, Rs, Z0, RL, n):
    return [Vs * RL / (Rs + RL)] * n
```

```python solution
def bounce(Vs, Rs, Z0, RL, n):
    v = Vs * Z0 / (Rs + Z0)
    gs, gl = (Rs - Z0) / (Rs + Z0), (RL - Z0) / (RL + Z0)
    out, total = [], 0.0
    for k in range(n):
        total += v * (1 + gl)
        out.append(total)
        v *= gl * gs
    return out
```

```python tests
b = bounce(1, 25, 50, 100, 4)
want = [0.888889, 0.790123, 0.801097, 0.799878]
assert all(abs(x - y) < 1e-6 for x, y in zip(b, want)), f"Expected {want}, got {b}."
b = bounce(1, 10, 50, 1e12, 3)
assert all(abs(x - y) < 1e-6 for x, y in zip(b, [1.666667, 0.555556, 1.296296])), f"A 10 Ω driver into an open line rings: 1.67, 0.56, 1.30; got {b}."
assert all(abs(x - 0.5) < 1e-12 for x in bounce(1, 50, 50, 50, 3)), "Matched at both ends, it's 0.5 V at once and stays."
```

::: right
Right. That's a lattice diagram, done by multiplication instead of drawing.
:::
:::

::: check
kind: python

## Write `stub_length(Z0, X)`: the length, in wavelengths between 0 and ½, of a shorted stub that looks like jX.

```python starter
def stub_length(Z0, X):
    return 0.25
```

```python solution
import math

def stub_length(Z0, X):
    return (math.atan2(X, Z0) / (2 * math.pi)) % 0.5
```

```python tests
assert abs(stub_length(50, 50) - 0.125) < 1e-12, "+j50 on 50 Ω: λ/8."
assert abs(stub_length(50, -50) - 0.375) < 1e-12, f"-j50 on 50 Ω: 3λ/8, got {stub_length(50, -50)}."
assert abs(stub_length(50, 25) - 0.073792) < 1e-6, "+j25 on 50 Ω: 0.0738λ."
assert abs(stub_length(75, 1e9) - 0.25) < 1e-6, "A huge reactance, an open, takes λ/4."
```

::: right
Right. Shorted stubs cover every reactance within half a wavelength.
:::
:::

::: explain
## Why does a line's input impedance repeat every half wavelength, rather than every full wavelength?

::: model
The input impedance depends on the ratio of the reflected wave to the incident wave at the input. The reflected wave makes a round trip, out to the load and back, so moving the input by a distance l shifts its phase relative to the incident wave by 2βl. Moving by half a wavelength shifts it by $2\pi$, a full cycle, which brings the ratio, and so the impedance, back to where it started. That's also why a Smith chart goes round once per half wavelength of line, and why stubs never need to be longer than λ/2.
:::
:::
