---
title: "Nonlinear and ultrafast lab: phase matching, FFTs, chirp and sidebands"
heading: Second-harmonic growth with and without quasi-phase matching, a radix-2 FFT built from scratch, the frequency chirp that self-phase modulation writes on a pulse, and the Bessel-function sidebands of an electro-optic phase modulator
eyebrow: S13 · Lesson 44 · Nonlinear and ultrafast lab
xp: 60
---

::: hook
Lessons 40 to 43 described modulators, nonlinear optics and ultrafast pulses with formulas valid in tidy limits: phase matching with sinc², a chirp proportional to the intensity's slope, a phase modulator's sidebands. In this lab you'll compute each directly: integrate the second harmonic along a periodically poled crystal, build the fast Fourier transform that every pulse simulation relies on, measure the chirp self-phase modulation writes on a pulse, and find a phase modulator's sideband powers from Bessel functions. The fiber-pulse project (lesson 51) then uses the FFT for split-step propagation.
:::

::: play
- The curves are the second-harmonic field's magnitude against distance, in units of the coherence length $L_c$, without phase matching (bounded, oscillating) and with quasi-phase matching that flips the nonlinear coefficient every $L_c$ (growing steadily at $2/\pi$ of the perfectly matched rate).
- Unmatched, the harmonic generated in one coherence length is converted back in the next.
- Quasi-phase matching only reverses the sign before that happens, so the harmonic keeps growing.
:::

```sim plot
view: graph
x: z = 0..10
y: E = 0..8
plot: abs(2*sin(pi*z/2)/pi) | label: unmatched
plot: 2/pi*z | dashed | label: quasi-phase-matched (average growth)
plot: z | label: perfectly matched
```

::: predict
## An electro-optic phase modulator is driven harder and harder. At what modulation index does the optical carrier disappear from the spectrum entirely?

- [ ] Never: the carrier always remains
- [ ] At π
- [x] At about 2.405
- [ ] At π/2

::: reveal
A sinusoidally phase-modulated field $e^{jm\sin\Omega t}$ splits into sidebands at multiples of Ω with amplitudes $J_k(m)$, so the carrier's power is $J_0^2(m)$, which first vanishes at m = 2.405, the first zero of $J_0$. All the power is then in the sidebands; the first pair peaks at m = 1.84, with 34% each. Task 4 finds both numerically.
:::
:::

::: nameit
## What the lab computes

- **Second-harmonic growth**: in the undepleted, slowly varying limit, the harmonic's field after length L is proportional to $\int_0^Ld(z)e^{j\Delta kz}\,dz$; with d constant it is bounded by $2/\Delta k$, while flipping d every coherence length $L_c = \pi/\Delta k$ makes it grow as $(2/\pi)L$.
- **The FFT**: the radix-2 Cooley–Tukey algorithm computes the N-point DFT in $N\log_2N$ operations by splitting it into even and odd halves recursively.
- **Self-phase modulation's chirp**: a pulse of intensity profile I(t) picks up a phase $\phi(t) = \phi_\max I(t)/I_0$, so its instantaneous frequency shifts by $-d\phi/dt$. For a Gaussian intensity $e^{-t^2/T_0^2}$ the largest shift is $\sqrt2e^{-1/2}\phi_\max/T_0 = 0.858\,\phi_\max/T_0$, and the spectrum broadens in proportion.
- **Phase-modulation sidebands**: $e^{jm\sin\Omega t} = \sum_kJ_k(m)e^{jk\Omega t}$ (the Jacobi–Anger expansion), so sideband k carries $J_k^2(m)$ of the power, with $J_k(m) = \frac1\pi\int_0^\pi\cos(k\tau - m\sin\tau)\,d\tau$.
:::

::: check
kind: python

## Write `shg_field(dk, L, Lc, qpm, n=20000)`: the magnitude of $\int_0^Ld(z)e^{j\Delta kz}dz$ by the midpoint rule, with d = 1, or flipping sign every Lc when qpm is true.

```python starter
def shg_field(dk, L, Lc, qpm, n=20000):
    return L
```

```python solution
import cmath

def shg_field(dk, L, Lc, qpm, n=20000):
    dz = L / n
    s = 0
    for i in range(n):
        z = (i + 0.5) * dz
        d = (1 if int(z / Lc) % 2 == 0 else -1) if qpm else 1
        s += d * cmath.exp(1j * dk * z) * dz
    return abs(s)
```

```python tests
import math
dk = math.pi
assert abs(shg_field(dk, 100, 1, True) / 100 - 2 / math.pi) < 1e-4, "Quasi-phase matching grows at 2/π of the matched rate."
assert abs(shg_field(dk, 100.5, 1, False) - 2 * math.sin(dk * 100.5 / 2) / dk) < 1e-4, "Unmatched, it stays bounded: 0.45 here."
assert abs(shg_field(0, 10, 1, False) - 10) < 1e-9, "Perfect matching grows linearly."
```

::: right
Right. Flipping the sign every coherence length keeps the harmonic from converting back, at a cost of $2/\pi$ in field.
:::
:::

::: check
kind: python

## Write `fft(a, inverse=False)`: a radix-2 FFT for a list whose length is a power of 2 (the inverse divides by N).

```python starter
def fft(a, inverse=False):
    return list(a)
```

```python solution
import cmath, math

def fft(a, inverse=False):
    n = len(a)
    a = list(a)
    j = 0
    for i in range(1, n):
        bit = n >> 1
        while j & bit:
            j ^= bit
            bit >>= 1
        j |= bit
        if i < j:
            a[i], a[j] = a[j], a[i]
    length = 2
    while length <= n:
        w = cmath.exp((2j if inverse else -2j) * math.pi / length)
        for i in range(0, n, length):
            wn = 1
            for k in range(length // 2):
                u = a[i + k]
                v = a[i + k + length // 2] * wn
                a[i + k] = u + v
                a[i + k + length // 2] = u - v
                wn *= w
        length <<= 1
    if inverse:
        a = [x / n for x in a]
    return a
```

```python tests
import cmath, math, random
rnd = random.Random(3)
x = [complex(rnd.random(), rnd.random()) for _ in range(64)]
X = fft(x)
direct = [sum(x[n] * cmath.exp(-2j * math.pi * k * n / 64) for n in range(64)) for k in range(64)]
assert max(abs(a - b) for a, b in zip(X, direct)) < 1e-9, "Matches the direct DFT."
assert max(abs(a - b) for a, b in zip(fft(X, True), x)) < 1e-12, "The inverse undoes it."
```

::: right
Right. The FFT's N log N cost is what makes split-step simulation of long fibers practical.
:::
:::

::: check
kind: python

## Write `spm_chirp(phimax, T0, n=4001)`: for a Gaussian intensity $e^{-t^2/T_0^2}$ sampled on t from −4T₀ to 4T₀, compute φ(t) and the largest magnitude of its time derivative by central differences, returning that largest frequency shift in rad per unit time.

```python starter
def spm_chirp(phimax, T0, n=4001):
    return phimax / T0
```

```python solution
import math

def spm_chirp(phimax, T0, n=4001):
    t = [-4 * T0 + 8 * T0 * i / (n - 1) for i in range(n)]
    phi = [phimax * math.exp(-(x / T0) ** 2) for x in t]
    dt = t[1] - t[0]
    return max(abs(phi[i + 1] - phi[i - 1]) / (2 * dt) for i in range(1, n - 1))
```

```python tests
import math
w = spm_chirp(1.0, 1.0)
assert abs(w - math.sqrt(2) * math.exp(-0.5)) < 1e-5, "The largest shift is 0.858 φmax/T0."
assert abs(spm_chirp(3.0, 2.0) / spm_chirp(1.0, 1.0) - 1.5) < 1e-5, "Three times the phase over twice the width: 1.5 times the chirp."
```

::: right
Right. The leading edge shifts to lower frequencies and the trailing edge to higher, which is what anomalous dispersion can compress.
:::
:::

::: check
kind: python

## Write `sideband_power(m, k)`: the fraction of a phase-modulated field's power in sideband k, $J_k^2(m)$, using Bessel's integral with 2000 points (negative k has the same power), and `carrier_zero()`: the first m > 0 where the carrier's power vanishes, by bisection between 2 and 3.

```python starter
def sideband_power(m, k):
    return 1.0 if k == 0 else 0.0

def carrier_zero():
    return 2.0
```

```python solution
import math

def bessel(n, x):
    N = 2000
    return sum(math.cos(n * (i + 0.5) * math.pi / N - x * math.sin((i + 0.5) * math.pi / N)) for i in range(N)) / N

def sideband_power(m, k):
    return bessel(abs(k), m) ** 2

def carrier_zero():
    lo, hi = 2.0, 3.0
    for _ in range(50):
        mid = (lo + hi) / 2
        if bessel(0, lo) * bessel(0, mid) <= 0:
            hi = mid
        else:
            lo = mid
    return (lo + hi) / 2
```

```python tests
assert abs(carrier_zero() - 2.4048) < 1e-4, "The carrier vanishes at m = 2.405."
assert abs(sideband_power(1.841, 1) - 0.3386) < 1e-4, "The first sidebands peak at 34% each, at m = 1.84."
total = sum(sideband_power(1.5, k) for k in range(-12, 13))
assert abs(total - 1) < 1e-9, "The sidebands' powers add up to all of it: phase modulation changes no power."
```

::: right
Right. Phase modulation only redistributes power among the carrier and sidebands, which is why it's used to broaden a laser's spectrum without changing its intensity.
:::
:::

::: explain
## Why does self-phase modulation broaden a pulse's spectrum without changing its shape in time?

::: model
The Kerr effect makes the refractive index follow the instantaneous intensity, so each part of the pulse picks up a phase in proportion to its own intensity: most at the peak, less on the wings. The intensity profile itself is untouched, so in time the pulse keeps its shape. But a phase that changes with time is a shift of the instantaneous frequency: the rising edge, where the phase is increasing, is shifted one way and the falling edge the other. The pulse now contains frequencies it didn't have before, so its spectrum widens in proportion to the peak phase, and dispersion can then reshape the pulse in time, compressing or spreading it.
:::
:::
