B825 of 29
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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, sinc2(asin⁡θ/λ). The solid one adds up m 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 m until the false peaks leave the range −1≤sin⁡θ≤1. How does that m compare with a/λ?

Predict

You model a slit 20 wavelengths wide with 10 point sources. Where does the first false peak appear?

Name it

Huygens's principle as a sum

Put m point sources at positions yj across the opening, each with the opening's transmission tj. In direction θ, the source at yj is ahead by a path yjsin⁡θ, so the far field is

E(θ)∝∑jtjei2πyjsin⁡θ/λ

and the intensity is |E|2, normalized to 1 straight ahead. The sources must be closer than a wavelength, or the sum itself becomes a grating with false orders at sin⁡θ=λ/Δy.

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 sin⁡πxπx, 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 fx=sin⁡θ/λ. Sample the aperture at spacing Δx over n points and the FFT (A10) computes the sum for n directions at once:

  • spatial frequencies from np.fft.fftfreq(n, dx), so sin⁡θ=λfx;
  • steps of Δ(sin⁡θ)=λnΔx, so a wider window gives finer angles;
  • np.fft.fftshift puts 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
  1. How many sources. The sources must be less than a wavelength apart, so m>aλ=40
  2. With 200 sources. They're 0.1 µm apart, so the first false peak would be at sin⁡θ=0.50.1=5 far outside the range.
  3. Accuracy. The sum matches sinc2 to within about 10−5 for |sin⁡θ|≤0.3.

Answer it needs more than 40 sources. With 200 there's no false peak in range, and the sum matches sinc2 to within about 10−5.

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
  1. Window width. 4096×0.5μm=2048μm
  2. Angular step. Δ(sin⁡θ)=0.52048=2.4×10−4
  3. Range. The highest frequency is 12Δx, so sin⁡θ runs to ±λ2Δx=±0.5
  4. A 20 µm slit. Its first null, at sin⁡θ=0.025, sits 102 steps out from the center, so the main lobe is well resolved.

Answer the result covers sin⁡θ from −0.5 to +0.5, in steps of 2.4×10−4.

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 |sin⁡θ|≤1?

Check

An aperture sampled at 0.25 µm over 1024 points is transformed at 1.55 µm. How big is each step in sin⁡θ?

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 sin⁡θ 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 sinc2.

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): N slits of width a, centers d apart. Add the N slits' phasors ei2πndsin⁡θ/λ, divide by N, square, and multiply by one slit's envelope sinc2(asin⁡θ/λ).

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 sin⁡θ in increasing order and the intensity normalized to 1 at sin⁡θ=0. 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?

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.