B8 · Lesson 25 · Diffraction lab
Computing diffraction patterns in NumPy
The sinc² pattern of a slit and the sharp lines of a grating came from formulas (lessons 10 and 11). Real apertures, like a lens with a central obstruction, a grating with uneven lines or a phased array with a dead element, rarely have tidy formulas. But Huygens's principle gives a recipe a computer can follow for any shape: treat each point of the opening as a source, add up their waves with the right phases, and square. In this lab you'll do exactly that, check it against the formulas, and then do it much faster with the FFT.
- The dashed curve is a slit's exact pattern, . The solid one adds up point sources spread evenly across the slit.
- With a few sources the sum looks right near the center but grows false peaks farther out: evenly spaced sources are a grating.
- Raise until the false peaks leave the range . How does that compare with ?
Predict
You model a slit 20 wavelengths wide with 10 point sources. Where does the first false peak appear?
At . Ten sources across 20 wavelengths are 2 wavelengths apart, a grating with , which has a first-order peak at . To push it past the sources must be less than a wavelength apart: more than 20 of them.
Name it
Huygens's principle as a sum
Put point sources at positions across the opening, each with the opening's transmission . In direction θ, the source at is ahead by a path , so the far field is
and the intensity is , normalized to 1 straight ahead. The sources must be closer than a wavelength, or the sum itself becomes a grating with false orders at .
In NumPy
import numpy as np
s = np.linspace(-0.3, 0.3, 601) # sin(theta) values
y = np.linspace(-1e-5, 1e-5, 200) # source positions (m)
phase = 2 * np.pi / 0.5e-6 * np.outer(s, y) # one row per angle, one column per source
E = np.exp(1j * phase).sum(axis=1) # add the sources for every angle at once
I = np.abs(E) ** 2 / np.abs(E).max() ** 2
np.outer builds the whole table of phases at once, so no Python loop is needed. NumPy's np.sinc(x) is , so a slit's exact pattern is np.sinc(a * s / lam) ** 2.
With the FFT
The sum has the form of a Fourier transform: the far field is the transform of the aperture's transmission, evaluated at spatial frequency . Sample the aperture at spacing over points and the FFT (A10) computes the sum for directions at once:
- spatial frequencies from
np.fft.fftfreq(n, dx), so ; - steps of , so a wider window gives finer angles;
np.fft.fftshiftputs the zero frequency in the middle, in increasing order.
This is how optics software computes far fields for arbitrary apertures, and how antenna engineers compute the patterns of phased arrays.
Worked example
How many Huygens sources does a 20 µm slit need at 0.5 µm? What happens with 200?
Show the solution
- How many sources. The sources must be less than a wavelength apart, so
- With 200 sources. They're 0.1 µm apart, so the first false peak would be at far outside the range.
- Accuracy. The sum matches to within about for .
Answer it needs more than 40 sources. With 200 there's no false peak in range, and the sum matches to within about .
Worked example
An aperture is sampled at 0.5 µm over 4096 points and transformed with the FFT, at λ = 0.5 µm. What directions does the result cover, and how finely?
Show the solution
- Window width.
- Angular step.
- Range. The highest frequency is , so runs to
- A 20 µm slit. Its first null, at , sits 102 steps out from the center, so the main lobe is well resolved.
Answer the result covers from −0.5 to +0.5, in steps of .
Check
A slit 40 µm wide is modeled with 10 evenly spaced sources at 0.633 µm. At what angle is the first false peak?
Check
What is the fewest evenly spaced sources that model a slit 20 µm wide at 0.45 µm without any false peak in the range ?
Check
An aperture sampled at 0.25 µm over 1024 points is transformed at 1.55 µm. How big is each step in ?
Check
A slit 40 µm wide is sampled at 0.5 µm over 2048 points. How many FFT steps lie between the center and the first null?
Check
Write slit_pattern(s, a, lam, m=200): the intensity at each in the array s from m sources spread evenly across a slit of width a, at the centers of m equal strips, normalized to 1 straight ahead. The test compares it with .
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write grating_pattern(s, N, d, a, lam): slits of width a, centers d apart. Add the slits' phasors , divide by , square, and multiply by one slit's envelope .
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write fft_pattern(aperture, dx, lam): given an aperture's transmission sampled at spacing dx, return (s, I), the values of in increasing order and the intensity normalized to 1 at . Use np.fft.fft, np.fft.fftfreq and np.fft.fftshift.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why can the FFT compute a diffraction pattern, and what limits the directions it covers?
The far field is a sum over the aperture of each point's transmission times a phase that grows linearly with the point's position, which has the same form as a discrete Fourier transform, with the spatial frequency playing the role of frequency. The FFT computes that sum for evenly spaced spatial frequencies at once. The sample spacing sets the highest frequency it can represent, so only reaches ; the total window width sets how finely those directions are spaced, . Finer sampling covers wider angles, and a longer window gives finer angular detail.
Practice
Keep practicing
7 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.