---
title: "Optics lab: beams, waveguide modes and angular-spectrum propagation"
heading: Carrying a Gaussian beam's q parameter through lenses and free space in Python, solving a slab waveguide's mode equations by bisection, and propagating a sampled field with the FFT using free space's transfer function, each checked against the closed forms of the earlier lessons
eyebrow: S13 · Lesson 50 · Optics lab
xp: 60
---

::: hook
Three of the course's core calculations become a few lines of NumPy each: the ABCD law for Gaussian beams (lesson 3), the transcendental equations for a slab's modes (lesson 16), and free space as a filter on spatial frequencies (lesson 4). Each is checked against numbers derived by hand. The last one hides a trap that has bitten many simulation codes: a square root with the wrong sign makes evanescent waves grow without limit.
:::

::: predict
## Your angular-spectrum code multiplies each spatial frequency by $e^{-jk_zz}$ with $k_z = 2\pi\sqrt{1/\lambda^2 - \nu^2}$. For ν above 1/λ, NumPy's complex square root returns a positive imaginary part. What happens to those components?

- [ ] They decay, as evanescent waves should
- [x] They grow exponentially, and a fine grating's field explodes
- [ ] They're unaffected
- [ ] They propagate at an angle

::: reveal
With $k_z = +j\kappa$, $e^{-jk_zz} = e^{+\kappa z}$, which grows. Physically the evanescent field must decay away from its source, so $k_z$ must be $-j\kappa$. The lab's test sends a 0.5 µm grating (finer than 633 nm light can carry) 2 µm forward; with the wrong branch its power grows by 10²³.
:::
:::

::: nameit
## Task 1: the q parameter

Start from $q_0 = jz_0$ at a waist and apply $q \to (Aq + B)/(Cq + D)$ for each element; recover $w = \sqrt{-\lambda/(\pi\,\text{Im}(1/q))}$ and $R = 1/\text{Re}(1/q)$.

## Task 2: slab modes

With $V = k_0a\,\text{NA}$, mode m has $u = \kappa a$ in $(m\pi/2, \min((m + 1)\pi/2, V))$; even modes solve $\tan u = \sqrt{V^2 - u^2}/u$ and odd ones $-\cot u = \sqrt{V^2 - u^2}/u$. On each interval the difference is monotonic, so bisection finds every root.

## Task 3: angular spectrum

FFT the sampled field, multiply by $e^{-jk_zz}$ with the decaying branch of $k_z$, inverse FFT. Measure the width by the second moment of intensity: for a Gaussian $|E|^2 \propto e^{-2x^2/w^2}$, $\langle x^2\rangle = w^2/4$.
:::

::: example
## What should task 1 give for a 1 mm, 1550 nm waist focused by a 50 mm lens, and for a 50/100 mm telescope?

::: solution
1. **The focused beam.** Scanning distance after the lens, the smallest width is 24.66 µm at 49.97 mm, lesson 3's numbers.
2. **The telescope.** With a 50 mm lens, 150 mm of space and a 100 mm lens, the beam leaves 2.000 mm wide and almost collimated.
3. **A free-space check.** Starting from the waist, $2z_0$ of free space gives exactly
   $$\begin{aligned} w &= \sqrt{5}\,w_0 \\ R &= 2.5z_0 \end{aligned}$$

**Answer:** the smallest width after the 50 mm lens is 24.66 µm at 49.97 mm, and the 50/100 mm telescope sends the beam out 2.000 mm wide and almost collimated.
:::
:::

::: example
## What should task 2 find for lesson 16's slab and a silicon slab?

::: solution
1. **Lesson 16's slab.** The 10 µm, 1.48/1.46 slab at 1550 nm carries four TE modes, with effective indices
   $$\begin{aligned} &1.478604,\ 1.474485, \\ &1.467940,\ 1.460440 \end{aligned}$$
2. **Thinned to 3 µm.** It's single-mode, at 1.472453.
3. **A silicon slab.** A 0.4 µm silicon slab (3.5 in 1.45) has V = 2.58 and two modes, despite being so thin, because its index contrast is so high.

**Answer:** lesson 16's slab gives four TE modes (1.478604, 1.474485, 1.467940 and 1.460440), one mode at 1.472453 when thinned to 3 µm, and the silicon slab gives two modes.
:::
:::

::: check
kind: python

## Step 1. Write `propagate_q` and `beam`: Gaussian beams through ABCD systems.

```python starter
import numpy as np

def free(d): return np.array([[1.0, d], [0.0, 1.0]])
def lens(f): return np.array([[1.0, 0.0], [-1.0/f, 1.0]])

def propagate_q(q, mats):
    # Apply each 2x2 ABCD matrix in mats, in order, to the complex beam parameter q:
    # q -> (A q + B)/(C q + D). Return the final q.
    return q

def beam(q, lam):
    # From q and the wavelength, return (w, R): the beam radius from Im(1/q) = -lam/(pi w^2)
    # and the wavefront radius R = 1/Re(1/q) (np.inf when Re(1/q) is zero).
    return 0.0, np.inf

lam, w0 = 1.55e-6, 1e-3
q0 = 1j*np.pi*w0**2/lam
print(beam(propagate_q(q0, [lens(0.05), free(0.04997)]), lam))
```

```python solution
import numpy as np

def free(d): return np.array([[1.0, d], [0.0, 1.0]])
def lens(f): return np.array([[1.0, 0.0], [-1.0/f, 1.0]])

def propagate_q(q, mats):
    for M in mats:
        A, B, C, D = M[0, 0], M[0, 1], M[1, 0], M[1, 1]
        q = (A*q + B)/(C*q + D)
    return q

def beam(q, lam):
    inv = 1/q
    w = np.sqrt(-lam/(np.pi*inv.imag))
    R = np.inf if inv.real == 0 else 1/inv.real
    return w, R

lam, w0 = 1.55e-6, 1e-3
q0 = 1j*np.pi*w0**2/lam
print(beam(propagate_q(q0, [lens(0.05), free(0.04997)]), lam))
```

```python tests
lam, w0 = 1.55e-6, 1e-3
z0 = np.pi*w0**2/lam
q0 = 1j*z0
w, R = beam(propagate_q(q0, [free(2*z0)]), lam)
assert abs(w/(w0*np.sqrt(5)) - 1) < 1e-12 and abs(R/(2*z0*(1 + 0.25)) - 1) < 1e-12, f"Free space 2z0 from the waist: w = √5 w0, R = 2.5 z0; got w = {w*1e3:.6f} mm, R = {R:.6f} m."
zs = np.linspace(0.0499, 0.0500, 2001)
ws = [beam(propagate_q(q0, [lens(0.05), free(z)]), lam)[0] for z in zs]
k = int(np.argmin(ws))
assert abs(ws[k] - 24.6615e-6) < 1e-9 and abs(zs[k] - 0.0499696) < 1e-7, f"A 50 mm lens focuses the 1 mm beam to 24.66 µm at 49.97 mm; your q gives {ws[k]*1e6:.4f} µm at {zs[k]*1e3:.4f} mm."
w, R = beam(propagate_q(q0, [lens(0.05), free(0.15), lens(0.1)]), lam)
assert abs(w/2e-3 - 1) < 1e-3 and abs(1/R) < 1e-2, f"A 50 mm / 100 mm telescope (spacing 150 mm) doubles the beam and leaves it nearly collimated: w ≈ 2 mm; got {w*1e3:.4f} mm, 1/R = {1/R:.4f} /m."
```

::: right
Right. Your q parameter reproduces the waist, curvature and focus of lessons 2 and 3.
:::
:::

::: check
kind: python

## Step 2. Write `slab_te_modes`: every TE mode by bisection.

```python starter
import numpy as np

def slab_te_modes(n1, n2, d, lam):
    # Return the effective indices of all TE modes of a symmetric slab (thickness d, core n1,
    # cladding n2), highest first. With a = d/2, k0 = 2 pi/lam, V = k0 a sqrt(n1^2 - n2^2):
    # mode m has u in (m pi/2, min((m + 1) pi/2, V)); even m solve tan u = sqrt(V^2 - u^2)/u,
    # odd m solve -cot u = sqrt(V^2 - u^2)/u. Then n_eff = sqrt(n1^2 - (u/(k0 a))^2).
    return []

print(slab_te_modes(1.48, 1.46, 10e-6, 1.55e-6))
```

```python solution
import numpy as np

def slab_te_modes(n1, n2, d, lam):
    k0 = 2*np.pi/lam
    a = d/2
    V = k0*a*np.sqrt(n1**2 - n2**2)
    out = []
    m = 0
    while m*np.pi/2 < V:
        lo, hi = m*np.pi/2 + 1e-12, min((m + 1)*np.pi/2, V) - 1e-12
        if m % 2 == 0:
            f = lambda u: np.tan(u) - np.sqrt(V*V - u*u)/u
        else:
            f = lambda u: -1/np.tan(u) - np.sqrt(V*V - u*u)/u
        for _ in range(200):
            mid = (lo + hi)/2
            if f(mid) < 0:
                lo = mid
            else:
                hi = mid
        u = (lo + hi)/2
        out.append(np.sqrt(n1**2 - (u/(k0*a))**2))
        m += 1
    return out

print(slab_te_modes(1.48, 1.46, 10e-6, 1.55e-6))
```

```python tests
m = slab_te_modes(1.48, 1.46, 10e-6, 1.55e-6)
assert len(m) == 4, f"V = 4.91 allows 4 TE modes; you found {len(m)}."
want = [1.478604035, 1.474484973, 1.467940330, 1.460440166]
assert all(abs(a - b) < 1e-8 for a, b in zip(m, want)), f"Effective indices should be {want}; got {[round(x, 9) for x in m]}."
m = slab_te_modes(1.48, 1.46, 3e-6, 1.55e-6)
assert len(m) == 1 and abs(m[0] - 1.472452862) < 1e-8, f"A 3 µm slab is single-mode (V < π/2) with n_eff = 1.4724529; got {m}."
m = slab_te_modes(3.5, 1.45, 0.4e-6, 1.55e-6)
assert len(m) == 2 and all(1.45 < x < 3.5 for x in m), f"A 0.4 µm silicon slab in oxide carries two TE modes, between the two indices; got {m}."
```

::: right
Right. All the modes, with effective indices to nine digits.
:::
:::

::: check
kind: python

## Step 3. Write `angular_spectrum`: free space as a filter, by FFT.

```python starter
import numpy as np

def angular_spectrum(E, dx, lam, z):
    # Propagate a sampled 1D field E (spacing dx) a distance z: FFT, multiply each spatial
    # frequency nu by exp(-j kz z) with kz = 2 pi sqrt(1/lam^2 - nu^2), inverse FFT. Use
    # np.fft.fftfreq for nu. Where nu > 1/lam the root is imaginary: take its imaginary part
    # negative, so evanescent components decay instead of growing.
    return E

lam, w0 = 633e-9, 50e-6
N, dx = 4096, 1e-6
x = (np.arange(N) - N/2)*dx
E = np.exp(-x**2/w0**2)
I = abs(angular_spectrum(E, dx, lam, np.pi*w0**2/lam))**2
print("width after z0, µm:", 2*np.sqrt(np.sum(x**2*I)/np.sum(I))*1e6)
```

```python solution
import numpy as np

def angular_spectrum(E, dx, lam, z):
    nu = np.fft.fftfreq(len(E), dx)
    kz = 2*np.pi*np.sqrt((1/lam**2 - nu**2).astype(complex))
    kz = kz.real - 1j*abs(kz.imag)
    return np.fft.ifft(np.fft.fft(E)*np.exp(-1j*kz*z))

lam, w0 = 633e-9, 50e-6
N, dx = 4096, 1e-6
x = (np.arange(N) - N/2)*dx
E = np.exp(-x**2/w0**2)
I = abs(angular_spectrum(E, dx, lam, np.pi*w0**2/lam))**2
print("width after z0, µm:", 2*np.sqrt(np.sum(x**2*I)/np.sum(I))*1e6)
```

```python tests
lam, w0 = 633e-9, 50e-6
z0 = np.pi*w0**2/lam
N, dx = 4096, 1e-6
x = (np.arange(N) - N/2)*dx
E = np.exp(-x**2/w0**2)
for r in (1, 2, 5):
    I = abs(angular_spectrum(E, dx, lam, r*z0))**2
    w = 2*np.sqrt(np.sum(x**2*I)/np.sum(I))
    want = w0*np.sqrt(1 + r**2)
    assert abs(w/want - 1) < 1e-4, f"After {r} z0 a Gaussian beam's width should be {want*1e6:.3f} µm; got {w*1e6:.3f} µm."
P0 = np.sum(abs(E)**2)
P1 = np.sum(abs(angular_spectrum(E, dx, lam, 3*z0))**2)
assert abs(P1/P0 - 1) < 1e-9, f"Free-space propagation conserves power; it changed by {P1/P0 - 1:.2e}."
x2 = np.arange(1000)*0.1e-6
E2 = np.cos(2*np.pi*x2/0.5e-6)
P2 = np.sum(abs(angular_spectrum(E2, 0.1e-6, lam, 2e-6))**2)
assert P2 < 1e-10*np.sum(abs(E2)**2), f"A 0.5 µm grating is finer than the 633 nm wavelength, so its field is evanescent and should vanish within a few µm; {P2/np.sum(abs(E2)**2):.2e} of its power remains. Take the complex square root."
```

::: right
Right. Gaussian spreading to parts per million, power conserved, and evanescent detail gone.
:::
:::

::: explain
## Why does the angular-spectrum method reproduce Gaussian beam spreading so exactly?

::: model
The method is exact for the scalar wave equation: it decomposes the field into plane waves, which free space doesn't change except by a phase, and recombines them. Nothing in it assumes a Gaussian shape or a paraxial angle. The Gaussian beam formulas of lesson 2 are the paraxial solution, valid when the beam's angles are small, which for a 50 µm waist at 633 nm (a 4 mrad divergence) they are, so the two agree to a few parts per million. The remaining differences come from the sampling, the finite window and the tiny nonparaxial terms the exact method keeps and the approximate formula drops.
:::
:::

::: practice

::: check
given: w = 0.5 | 2; l = 0.633 | 1.55; f = 20 | 100
answer: w*1000/sqrt(1 + (pi*(w*1e-3)^2/(l*1e-6)/(f*1e-3))^2)
unit: µm
tolerance: 0.5%

## Focused waist: {w} mm at {l} µm, f = {f} mm?

::: misses
- [any] $w_0/\sqrt{1 + (z_0/f)^2}$.
:::
:::

::: check
given: n = 1.46 | 3.5; d = 0.02 | 0.5; t = 0.3 | 5; l = 1.31 | 1.55; let b = n - d
answer: 2*pi/l*(t/2)*sqrt(n^2 - b^2)
tolerance: 0.5%

## Slab V: core {n}, cladding {b}, {t} µm, {l} µm?

::: misses
- [any] $k_0a\,\text{NA}$.
:::
:::

::: check
given: l = 0.4 | 1.6; r = 0.3 | 0.8; let p = l*r
answer: 1/sqrt(1/(p*1e-6)^2 - 1/(l*1e-6)^2)*1e9/(2*pi)
unit: nm
tolerance: 0.5%

## A grating of period {p} µm is lit by {l} µm light, so its field is evanescent. Over what distance does that field fall by 1/e?

::: misses
- [any] $1/\kappa$ with $\kappa = 2\pi\sqrt{1/p^2 - 1/\lambda^2}$.
:::
:::
:::
