Algorithm Search⌘K

Algorithmadvanced

Stolt migration

Also called f-k migration.

Migrate zero offset data in a constant velocity medium by a 2D FFT, a change of variables from frequency to vertical wavenumber, and an inverse FFT.

Time
O(p t x log(p t x) + z x log(z x)) totalO(z x) mapping
Space
O(p t x + z x) extra
Where
p = the padding factor of the time axis; t = the number of time samples; x = the number of traces; z = the number of depth samples
Properties
not depth velocitydeterministicnot lateral velocitysteep dips
Origin
Robert H. Stolt, 1978

Stolt migration images a zero-offset section recorded over a medium of constant velocity. It takes the 2D FFT of the section, moves every sample of the spectrum from the frequency ω\omega to the vertical wavenumber kzk_z by the mapping ω=sgn⁡(kz) v′kx2+kz2\omega = \operatorname{sgn}(k_z)\, v' \sqrt{k_x^2 + k_z^2} with a Jacobian weight, and takes the inverse FFT. The result is the same as phase-shift migration with one constant velocity, but all depth steps are done at once. The omega-k algorithm of synthetic aperture radar uses the same change of variables (it is often called the Stolt mapping there). The difference is in the data: the radar algorithm first multiplies the spectrum by a reference function for the range of the scene center, and its velocity is the speed of light, while seismic data need the half velocity v′=v/2v' = v/2 of the exploding reflector model.

In plain words

A recorded section is a mix of simple waves, each with one frequency and one tilt. In a medium where the speed is the same everywhere, every simple wave comes from one simple pattern in the image, a set of parallel stripes at one depth spacing and one tilt. The rule that links the wave to the stripes is fixed: the frequency of the wave is the speed times the number of stripes per meter. So the image can be built by moving each part of the spectrum of the record to a new place in the spectrum of the image and weighting it. Two fast Fourier transforms and one pass of moving values do the whole job. The new places do not fall on the grid points of the old spectrum, so the method reads values between grid points by interpolation.

The problem

Given a zero-offset section d(t,x)d(t, x) sampled at t=kΔtt = k\Delta t, x=jΔxx = j\Delta x, and the constant velocity vv, find the image m(x,z)m(x, z) on a grid of nzn_z depths with the step Δz\Delta z. This is seismic migration in the exploding reflector model: the section is the upgoing field at z=0z = 0 of the wave equation with the velocity v′=v/2v' = v/2, and the image is the field at t=0t = 0.

The algorithm

The phase-shift image is m(x,z)=(2π)−2∬D(ω,kx) eikzz+ikxx dω dkxm(x, z) = (2\pi)^{-2}\iint D(\omega, k_x)\, e^{i k_z z + i k_x x}\, d\omega\, dk_x with kz=sgn⁡(ω)ω2/v′2−kx2k_z = \operatorname{sgn}(\omega)\sqrt{\omega^2/v'^2 - k_x^2}. For a fixed kxk_x, change the variable from ω\omega to kzk_z. The inverse map is ω(kz,kx)=sgn⁡(kz) v′kx2+kz2\omega(k_z, k_x) = \operatorname{sgn}(k_z)\, v'\sqrt{k_x^2 + k_z^2}, and dω=J dkzd\omega = J\, dk_z with J=v′∣kz∣/kx2+kz2J = v'|k_z|/\sqrt{k_x^2 + k_z^2}. So the image is the inverse 2D Fourier transform of

M(kz,kx)=v′∣kz∣kx2+kz2 D(ω(kz,kx),kx).M(k_z, k_x) = \frac{v'|k_z|}{\sqrt{k_x^2 + k_z^2}}\, D\big(\omega(k_z, k_x), k_x\big).
  1. STOLT-MIGRATION(d, Δt, Δx, Δz, n_z, v, p) ▷ constant velocity v, padding factor p
  2. 1pad d with zeros to p · n_t time samples
  3. 2D ← DFT2(d)
  4. 3for every pair (i, j) of the n_z × n_x image spectrum do
  5. 4r ← sqrt(k_x,j² + k_z,i²); ω ← sgn(k_z,i) v' r; J ← v' |k_z,i| / r ▷ v' = v / 2; J ← 0 if r = 0
  6. 5if J = 0 or ω is outside the frequency band of D then M[i][j] ← 0 and go to the next pair
  7. 6D_ω ← linear interpolation of D[·][j] at the frequency ω
  8. 7M[i][j] ← (Δt / Δz) · J · D_ω
  9. 8m ← IDFT2(M)
  10. 9return Re m

Line L1 pads the time axis so that the spectrum is sampled finely in ω\omega; this makes the interpolation of line L6 accurate. Line L2 is one 2D FFT. Lines L3 to L7 fill the spectrum of the image: for every image wavenumber pair (kz,i,kx,j)(k_{z,i}, k_{x,j}), line L4 finds the data frequency that maps to it and the Jacobian, and line L6 reads the data spectrum there. The factor Δt/Δz\Delta t/\Delta z in line L7 converts the sums of the DFT to the integrals of the formula: the forward DFT in time is the integral divided by Δt\Delta t, and the inverse DFT in depth is the inverse integral divided by Δz\Delta z. Line L5 drops the point kz=0k_z = 0 and the frequencies at the edge of the band, where the interpolation has no neighbor. Lines L8 and L9 are one inverse 2D FFT.

Example

The mapping by hand. Take v=2000v = 2000 m/s, so v′=1000v' = 1000 m/s, and the image wavenumbers kz=0.03k_z = 0.03 and kx=0.04k_x = 0.04 rad/m. Then r=0.05r = 0.05, ω=1000⋅0.05=50\omega = 1000 \cdot 0.05 = 50 rad/s and J=1000⋅0.03/0.05=600J = 1000 \cdot 0.03/0.05 = 600 m/s. For kz=−0.03k_z = -0.03 the frequency is −50-50 rad/s and the Jacobian is again 600. For kx=0k_x = 0 the mapping is ω=v′kz\omega = v' k_z and J=v′J = v': a plain stretch from time to depth, z=v′tz = v' t.

A flat event. With Δz=v′Δt\Delta z = v'\Delta t and a padded length that is a multiple of nzn_z, every frequency that line L4 asks for at kx=0k_x = 0 is a grid frequency, so line L6 is exact. A section that is the same on every trace then maps sample by sample to depth: in the test, every image row equals the Ricker trace at the matching time within 10−610^{-6} (the difference is the band edge removed by line L5).

A point diffractor. In the test case of phase-shift migration (velocity 3000 m/s, point at x0=160x_0 = 160 m and z0=192z_0 = 192 m, Δz=6\Delta z = 6 m), the image peaks within one sample of row 32, column 16. The relative difference to the phase-shift image depends on the padding (a measurement on this case):

padding factor pp 1 2 4 8
relative difference to phase shift 0.84 0.27 0.094 0.067

Without padding the interpolation error is as large as the image itself. The code uses p=4p = 4.

Correctness

The full proof is in proofs/correctness.md.

  • The change of variables (theorem). For fixed kxk_x, the map ω↦kz\omega \mapsto k_z is a smooth bijection from each branch ω>v′∣kx∣\omega > v'|k_x| and ω<−v′∣kx∣\omega < -v'|k_x| onto kz>0k_z > 0 and kz<0k_z < 0. On both branches dω/dkz=v′∣kz∣/kx2+kz2>0d\omega/dk_z = v'|k_z|/\sqrt{k_x^2 + k_z^2} > 0, so the integral keeps its orientation and the formula for MM holds.
  • The discrete scale (theorem). For band-limited samples, the DFT sums equal the integrals times 1/(Δt Δx)1/(\Delta t\,\Delta x) and 1/(Δz Δx)1/(\Delta z\,\Delta x), which gives the factor Δt/Δz\Delta t/\Delta z of line L7.
  • What the code computes (theorem). The output is the inverse transform of the discrete Stolt mapping with D(ω)D(\omega) replaced by its linear interpolant. Where the requested frequencies fall on the DFT grid, it is exact.
  • The interpolation error (heuristic, with a measurement). Linear interpolation of the spectrum is the only approximation. Its effect is shown in the table above.
Proof: Correctness of Stolt migrationfull

Setting. The model is the exploding reflector model in the constant velocity vv: the zero-offset section d(t,x)d(t, x) is the upgoing field at z=0z = 0 of the wave equation with the half velocity v′=v/2v' = v/2, and the image is the field at t=0t = 0. Transforms use the sign of the area, D(ω,kx)=∬d(t,x) e−iωt−ikxx dt dxD(\omega, k_x) = \iint d(t, x)\, e^{-i\omega t - i k_x x}\,dt\,dx, and the inverse carries the factor (2π)−2(2\pi)^{-2}. On the non-evanescent set ∣kx∣<∣ω∣/v′|k_x| < |\omega|/v' put kz(ω,kx)=sgn⁡(ω)ω2/v′2−kx2k_z(\omega, k_x) = \operatorname{sgn}(\omega)\sqrt{\omega^2/v'^2 - k_x^2}.

Step 1 (the image as an integral over ω\omega). By the phase-shift lemma (the field at depth zz is D(ω,kx) eikzzD(\omega, k_x)\, e^{i k_z z}; see the proof of phase-shift migration), the image is

m(x,z)=1(2π)2∬∣kx∣<∣ω∣/v′D(ω,kx) eikz(ω,kx)z+ikxx dω dkx,m(x, z) = \frac{1}{(2\pi)^2} \iint_{|k_x| < |\omega|/v'} D(\omega, k_x)\, e^{i k_z(\omega, k_x) z + i k_x x}\,d\omega\,dk_x,

because the factor eiωte^{i\omega t} of the inverse time transform is 1 at t=0t = 0.

Step 2 (the change of variables). Fix kxk_x. On the branch ω>v′∣kx∣\omega > v'|k_x|, the map ω↦kz=ω2/v′2−kx2\omega \mapsto k_z = \sqrt{\omega^2/v'^2 - k_x^2} is a smooth bijection onto (0,∞)(0, \infty) with inverse ω=v′kx2+kz2\omega = v'\sqrt{k_x^2 + k_z^2} and

dωdkz=v′kzkx2+kz2>0.\frac{d\omega}{dk_z} = \frac{v' k_z}{\sqrt{k_x^2 + k_z^2}} > 0.

On the branch ω<−v′∣kx∣\omega < -v'|k_x|, the map ω↦kz=−ω2/v′2−kx2\omega \mapsto k_z = -\sqrt{\omega^2/v'^2 - k_x^2} is a bijection onto (−∞,0)(-\infty, 0) with inverse ω=−v′kx2+kz2\omega = -v'\sqrt{k_x^2 + k_z^2} and

dωdkz=−v′kzkx2+kz2=v′∣kz∣kx2+kz2>0.\frac{d\omega}{dk_z} = \frac{-v' k_z}{\sqrt{k_x^2 + k_z^2}} = \frac{v'|k_z|}{\sqrt{k_x^2 + k_z^2}} > 0.

Both branches keep the orientation, so with ω(kz,kx)=sgn⁡(kz) v′kx2+kz2\omega(k_z, k_x) = \operatorname{sgn}(k_z)\, v'\sqrt{k_x^2 + k_z^2} the substitution gives

m(x,z)=1(2π)2∬M(kz,kx) eikzz+ikxx dkz dkx,M(kz,kx)=v′∣kz∣kx2+kz2 D(ω(kz,kx),kx).m(x, z) = \frac{1}{(2\pi)^2} \iint M(k_z, k_x)\, e^{i k_z z + i k_x x}\,dk_z\,dk_x, \qquad M(k_z, k_x) = \frac{v'|k_z|}{\sqrt{k_x^2 + k_z^2}}\, D(\omega(k_z, k_x), k_x).

The set kz=0k_z = 0 has measure zero. So mm is the inverse 2D Fourier transform of MM: the Stolt mapping. Note the absolute value. The factor v′kz/kx2+kz2v' k_z/\sqrt{k_x^2 + k_z^2} without it is correct only for kz≥0k_z \ge 0; with it, M(−kz,−kx)=M(kz,kx)‾M(-k_z, -k_x) = \overline{M(k_z, k_x)} for real data, so the image is real. □\square

Step 3 (the discrete scale). Let D[k][j]=∑ℓ,rd(ℓΔt,rΔx) e−iωkℓΔt−ikx,jrΔxD[k][j] = \sum_{\ell, r} d(\ell\Delta t, r\Delta x)\, e^{-i\omega_k \ell\Delta t - i k_{x,j} r\Delta x} be the DFT of line L2. For a section that is band limited and zero outside the record, D[k][j]=D(ωk,kx,j)/(Δt Δx)D[k][j] = D(\omega_k, k_{x,j})/(\Delta t\,\Delta x) (the sum is the Riemann sum of the integral, which is exact for band-limited samples on a grid finer than the Nyquist rate). The inverse DFT of an nz×nxn_z \times n_x array M[i][j]M[i][j] gives 1nznx∑M[i][j] ei(kz,iz+kx,jx)\frac{1}{n_z n_x}\sum M[i][j]\, e^{i(k_{z,i} z + k_{x,j} x)}, the Riemann sum of (2π)−2∬Mei(⋅) dkz dkx(2\pi)^{-2}\iint M e^{i(\cdot)}\,dk_z\,dk_x times Δz Δx\Delta z\,\Delta x, since ΔkzΔkx=(2π)2/(nzΔz nxΔx)\Delta k_z \Delta k_x = (2\pi)^2/(n_z \Delta z\, n_x \Delta x). So the discrete image equals the continuous one when

M[i][j]=M(kz,i,kx,j)Δz Δx=ΔtΔz⋅v′∣kz,i∣kx,j2+kz,i2⋅D[ω(kz,i,kx,j)][j],M[i][j] = \frac{M(k_{z,i}, k_{x,j})}{\Delta z\,\Delta x} = \frac{\Delta t}{\Delta z}\cdot\frac{v'|k_{z,i}|}{\sqrt{k_{x,j}^2 + k_{z,i}^2}}\cdot D[\omega(k_{z,i}, k_{x,j})][j],

which is line L7, with DD at the frequency ω(kz,i,kx,j)\omega(k_{z,i}, k_{x,j}). □\square

Theorem (what the pseudocode computes). Lines L3 to L7 visit every pair (i,j)(i, j) once. For each, line L4 computes ω(kz,i,kx,j)\omega(k_{z,i}, k_{x,j}) and the Jacobian of Step 2. Line L5 leaves M[i][j]=0M[i][j] = 0 when the Jacobian is 0 (the point kz=0k_z = 0) or when ω\omega lies outside the open band of the DFT frequencies of the padded record. Line L6 is the linear interpolation of D[⋅][j]D[\cdot][j] between the two DFT frequencies around ω\omega, and line L7 stores the value of Step 3 with this interpolated DD. Line L8 is the inverse 2D DFT, and line L9 returns its real part. So the output is the inverse transform of the discrete Stolt mapping of Step 3, with D(ω)D(\omega) replaced by its linear interpolant. The loops have nznxn_z n_x passes, so the algorithm stops. ■\blacksquare

Corollary (exact cases). If every ω(kz,i,kx,j)\omega(k_{z,i}, k_{x,j}) that is used falls on a frequency of the padded DFT, the interpolation in line L6 is exact and the result equals the discrete form of Step 2. This holds for kx=0k_x = 0 when Δz=v′Δt\Delta z = v'\Delta t and the padded length is a multiple of nzn_z: then ω=v′kz,i\omega = v' k_{z,i} is the DFT frequency p ip\,i of the padded record with p=ntpad/nzp = n_t^{\text{pad}}/n_z. A section that is constant in xx is then mapped sample by sample from time to depth, z=v′tz = v' t, up to the frequencies removed at line L5.

What is approximate. In general ω(kz,kx)\omega(k_z, k_x) falls between DFT frequencies. Linear interpolation of a spectrum is a low-order approximation, and its error is the main error of the method (a heuristic statement, supported by a measurement: for the point diffractor of the tests, the relative difference to phase-shift migration was 0.84 without padding, 0.27 with padding by 2, 0.094 with padding by 4 and 0.067 with padding by 8). Both the DFT and the image are periodic, so events near the edges wrap around.

Complexity

  • Real-ram, with FFTs. Line L2 costs O(pntnxlog⁡(pntnx))O(p n_t n_x \log(p n_t n_x)), lines L3 to L7 cost O(nznx)O(n_z n_x) (one interpolation per image sample), and line L8 costs O(nznxlog⁡(nznx))O(n_z n_x \log(n_z n_x)).
  • Against phase shift. Phase shift with nzn_z steps costs O(nzntnx)O(n_z n_t n_x), so Stolt is faster by about nz/log⁡(ntnx)n_z/\log(n_t n_x), at the price of the interpolation error and of a single velocity.
  • Space. The padded spectrum holds pntnxp n_t n_x complex numbers.

Properties

  • All dips. The mapping is exact for every dip up to 90 degrees in the one-way model.
  • One velocity only. A single constant velocity is assumed. Stolt (1978) also gave an approximate stretch of the time axis for a velocity that changes slowly with depth.
  • Periodic edges. As with every DFT method, energy wraps around in tt, xx and zz. Pad in xx as well when events reach the edges.
  • Real image. Because JJ uses ∣kz∣|k_z|, the mapped spectrum keeps the conjugate symmetry M(−kz,−kx)=M(kz,kx)‾M(-k_z, -k_x) = \overline{M(k_z, k_x)}, and the image of real data is real.

Variants and improvements

  • Better interpolation. Sinc interpolation with a short window, or a longer padding, reduces the error of line L6 at a higher cost.
  • The omega-k algorithm. The same mapping focuses synthetic aperture radar data after a reference function multiply.
  • Prestack Stolt migration. A mapping in four wavenumbers images data with offset in constant velocity.
  • Stolt stretch. A change of the time variable makes data from a velocity v(z)v(z) close to constant-velocity data, with a factor that controls the error.
  • Phase-shift migration. For a velocity v(z)v(z), give up the single mapping and step down in depth.

When to use it

Use Stolt migration for a fast first image when one velocity is a fair model, for velocity scans (migrate with many trial velocities and keep the best focus), and as a building block of the radar omega-k algorithm. Do not use it when the velocity changes strongly: the error grows with the depth and the dip. Then use phase-shift migration for v(z)v(z), or split-step Fourier migration and reverse-time migration for v(x,z)v(x, z).

Pitfalls

  • The Jacobian without the absolute value. Writing J←v′kz/rJ \leftarrow v' k_z/r in line L4 gives a negative weight for kz<0k_z < 0, breaks the conjugate symmetry, and leaves a large imaginary part. Use v′∣kz∣/rv'|k_z|/r, as in line L4.
  • No padding. Skipping line L1 makes the interpolation of line L6 coarse; in the example the error rises from 0.094 to 0.84. Pad by 4 or more, as in line L1.
  • The full velocity. Using vv instead of v′=v/2v' = v/2 in line L4 doubles every depth. Zero-offset data need the half velocity.
  • The missing scale. Leaving out Δt/Δz\Delta t/\Delta z in line L7 gives the right shape but the wrong amplitude. Keep the factor of line L7.

Implementations

Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.

imgkit.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Shared helper of the imaging and inverse problems pages of https://algorithm.tamnd.com/. The authors are listed in prov/.
"""Small images and signals for the imaging and inverse problems pages.

Conventions:
- A signal is a list of numbers. An image is a list of rows, img[i][j], with
  row i from the top and column j from the left.
- An n x n image covers the square [-1, 1]^2. The center of pixel (i, j) is
  x_j = -1 + (2 j + 1) / n and y_i = 1 - (2 i + 1) / n, so y goes up.
- DFT: X_k = sum_j x_j exp(-2 pi i j k / n), unnormalized; the inverse has
  the factor 1 / n. Index k stands for frequency k for k < n / 2 and k - n
  otherwise. fftshift moves the zero frequency to position n // 2.
- Periodic convolution kernels are stored with their center at index 0
  (wrapped around), so a delta kernel gives the identity and no shift.
"""

import cmath
import math

# ------------------------------------------------------------------ images


def new_image(n1, n2, value=0.0):
    """An n1 x n2 image (n1 rows, n2 columns) filled with value."""
    return [[value] * n2 for _ in range(n1)]


def img_add(a, b):
    """The pixelwise sum a + b."""
    return [[x + y for x, y in zip(ra, rb)] for ra, rb in zip(a, b)]


def img_scale(a, c):
    """The image c a."""
    return [[c * x for x in row] for row in a]


def img_dot(a, b):
    """<a, b> = sum conj(a_ij) b_ij."""
    return sum(x.conjugate() * y for ra, rb in zip(a, b) for x, y in zip(ra, rb))


def img_norm(a):
    """The Euclidean (Frobenius) norm of an image."""
    return math.sqrt(sum(abs(x) ** 2 for row in a for x in row))


def flatten(img):
    """Row-major vector of an image."""
    return [x for row in img for x in row]


def unflatten(v, n1, n2):
    """The n1 x n2 image of a row-major vector."""
    return [list(v[i * n2:(i + 1) * n2]) for i in range(n1)]


# ------------------------------------------------------------------ DFT


def _is_pow2(n):
    return n > 0 and n & (n - 1) == 0


def _fft_pow2(x):
    """Iterative radix-2 FFT, unnormalized forward transform."""
    n = len(x)
    a = [complex(v) for v in x]
    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]
    size = 2
    while size <= n:
        half = size // 2
        tw = [cmath.exp(-2j * math.pi * k / size) for k in range(half)]
        for start in range(0, n, size):
            for k in range(half):
                u = a[start + k]
                v = a[start + k + half] * tw[k]
                a[start + k] = u + v
                a[start + k + half] = u - v
        size *= 2
    return a


def dft(x):
    """X_k = sum_j x_j exp(-2 pi i j k / n). Radix-2 FFT when n is a power of 2."""
    n = len(x)
    if n == 0:
        return []
    if _is_pow2(n):
        return _fft_pow2(x)
    w = [cmath.exp(-2j * math.pi * t / n) for t in range(n)]
    return [sum(x[j] * w[(j * k) % n] for j in range(n)) for k in range(n)]


def idft(xf):
    """x_j = (1/n) sum_k X_k exp(2 pi i j k / n)."""
    n = len(xf)
    if n == 0:
        return []
    y = dft([complex(v).conjugate() for v in xf])
    return [v.conjugate() / n for v in y]


def _rows_then_cols(img, f):
    rows = [f(row) for row in img]
    cols = [f(list(col)) for col in zip(*rows)]
    return [list(r) for r in zip(*cols)]


def dft2(img):
    """2D DFT: X_kl = sum_jq x_jq exp(-2 pi i (j k / n1 + q l / n2))."""
    return _rows_then_cols(img, dft)


def idft2(img):
    """Inverse 2D DFT, with the factor 1 / (n1 n2)."""
    return _rows_then_cols(img, idft)


def _shift1(x, s):
    return list(x[s:]) + list(x[:s])


def fftshift(x):
    """Move the zero frequency to position n // 2 (1D, or both axes of an image)."""
    if x and isinstance(x[0], (list, tuple)):
        rows = [fftshift(r) for r in x]
        return _shift1(rows, (len(rows) + 1) // 2)
    return _shift1(x, (len(x) + 1) // 2)


def ifftshift(x):
    """The inverse of fftshift (1D, or both axes of an image)."""
    if x and isinstance(x[0], (list, tuple)):
        rows = [ifftshift(r) for r in x]
        return _shift1(rows, len(rows) // 2)
    return _shift1(x, len(x) // 2)


def circ_conv(x, h):
    """Periodic convolution y_i = sum_j h_j x_{(i - j) mod n}, kernel centered at index 0."""
    n = len(x)
    nz = [(j, hj) for j, hj in enumerate(h) if hj != 0]
    return [sum(hj * x[(i - j) % n] for j, hj in nz) for i in range(n)]


def circ_conv2(img, h):
    """Periodic 2D convolution with the kernel h centered at index (0, 0)."""
    n1, n2 = len(img), len(img[0])
    nz = [(p, q, h[p][q]) for p in range(len(h)) for q in range(len(h[0])) if h[p][q] != 0]
    out = new_image(n1, n2)
    for i in range(n1):
        for j in range(n2):
            out[i][j] = sum(v * img[(i - p) % n1][(j - q) % n2] for p, q, v in nz)
    return out


# ------------------------------------------------------------------ phantoms


def pixel_centers(n):
    """(xs, ys): xs[j] = -1 + (2 j + 1) / n and ys[i] = 1 - (2 i + 1) / n."""
    xs = [-1.0 + (2 * j + 1) / n for j in range(n)]
    ys = [1.0 - (2 * i + 1) / n for i in range(n)]
    return xs, ys


# Rows: rho, A, B, x0, y0, phi (degrees). A is the semi-axis along the
# direction phi (counterclockwise from the x axis), B the other semi-axis.
# Geometry and densities of Shepp and Logan (1974), as reproduced in Kak and
# Slaney, Principles of Computerized Tomographic Imaging, Table 3.1. The value
# at the center (0, 0) is 2 - 0.98 = 1.02.
SHEPP_LOGAN = [
    (2.0, 0.92, 0.69, 0.0, 0.0, 90.0),
    (-0.98, 0.874, 0.6624, 0.0, -0.0184, 90.0),
    (-0.02, 0.31, 0.11, 0.22, 0.0, 72.0),
    (-0.02, 0.41, 0.16, -0.22, 0.0, 108.0),
    (0.01, 0.25, 0.21, 0.0, 0.35, 90.0),
    (0.01, 0.046, 0.046, 0.0, 0.1, 0.0),
    (0.01, 0.046, 0.046, 0.0, -0.1, 0.0),
    (0.01, 0.046, 0.023, -0.08, -0.605, 0.0),
    (0.01, 0.023, 0.023, 0.0, -0.605, 0.0),
    (0.01, 0.046, 0.023, 0.06, -0.605, 90.0),
]

# The same ellipses with the gray levels of Toft (1996), which give more
# contrast. The value at the center is 1 - 0.8 = 0.2.
MODIFIED_SHEPP_LOGAN = [
    (rho, a, b, x0, y0, phi)
    for rho, (_, a, b, x0, y0, phi) in zip(
        (1.0, -0.8, -0.2, -0.2, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1), SHEPP_LOGAN
    )
]


def ellipse_value(x, y, table):
    """The phantom value at the point (x, y): the sum of rho over the ellipses that hold it."""
    total = 0.0
    for rho, a, b, x0, y0, phi in table:
        c, s = math.cos(math.radians(phi)), math.sin(math.radians(phi))
        dx, dy = x - x0, y - y0
        u = dx * c + dy * s
        v = -dx * s + dy * c
        if (u / a) ** 2 + (v / b) ** 2 <= 1.0:
            total += rho
    return total


def ellipse_phantom(n, table):
    """The n x n image of an ellipse table, sampled at the pixel centers."""
    xs, ys = pixel_centers(n)
    return [[ellipse_value(x, y, table) for x in xs] for y in ys]


def disk_phantom(n, radius):
    """1 at pixel centers with x^2 + y^2 <= radius^2, 0 elsewhere."""
    xs, ys = pixel_centers(n)
    r2 = radius * radius
    return [[1.0 if x * x + y * y <= r2 else 0.0 for x in xs] for y in ys]


def spikes_1d(n, positions, amplitudes):
    """A signal of length n with the given amplitudes at the given positions."""
    out = [0.0] * n
    for p, a in zip(positions, amplitudes):
        out[p] += a
    return out


# ------------------------------------------------------------------ projector


def parallel_beam_matrix(n, angles, nbins):
    """Pixel-driven discrete Radon transform of an n x n image on [-1, 1]^2.

    Each pixel center (x, y) projects to s = x cos(theta) + y sin(theta). Its
    value times the pixel area over the bin width is split linearly between
    the two nearest bin centers. The bins cover [-sqrt(2), sqrt(2)]. Row
    a * nbins + b holds angle a and bin b; column i * n + j holds pixel (i, j).
    Returns the sparse rows, each a list of (column, weight) pairs.
    Pixel-driven projection has a ripple: with a bin width close to the pixel
    size, a disk projection is off by up to about 10 percent in some bins. A
    bin of about twice the pixel size (nbins = 23 for n = 32) keeps it near
    1 percent.
    """
    xs, ys = pixel_centers(n)
    smax = math.sqrt(2.0)
    width = 2.0 * smax / nbins
    scale = (2.0 / n) ** 2 / width
    rows = []
    for theta in angles:
        c, s = math.cos(theta), math.sin(theta)
        acc = [dict() for _ in range(nbins)]
        for i, y in enumerate(ys):
            for j, x in enumerate(xs):
                t = (x * c + y * s + smax) / width - 0.5
                b0 = math.floor(t)
                frac = t - b0
                col = i * n + j
                for b, w in ((b0, 1.0 - frac), (b0 + 1, frac)):
                    if 0 <= b < nbins and w > 0.0:
                        acc[b][col] = acc[b].get(col, 0.0) + w * scale
        rows.extend(sorted(d.items()) for d in acc)
    return rows


def sp_matvec(rows, x):
    """y = A x for sparse rows of (column, weight) pairs."""
    return [sum(w * x[c] for c, w in row) for row in rows]


def sp_rmatvec(rows, y, ncols):
    """x = A^H y for sparse rows of (column, weight) pairs and ncols columns."""
    out = [0.0] * ncols
    for row, yi in zip(rows, y):
        if yi == 0:
            continue
        for c, w in row:
            out[c] += w.conjugate() * yi
    return out


# ------------------------------------------------------------------ noise


def gauss_noise(rng, shape, sigma):
    """Independent N(0, sigma^2) values: a list for an int shape, an image for (n1, n2)."""
    if isinstance(shape, int):
        return [rng.gauss(0.0, sigma) for _ in range(shape)]
    n1, n2 = shape
    return [[rng.gauss(0.0, sigma) for _ in range(n2)] for _ in range(n1)]


def _poisson_small(rng, lam):
    """Knuth's product method, exact for a mean lam <= 30."""
    limit = math.exp(-lam)
    k, p = 0, rng.random()
    while p > limit:
        k += 1
        p *= rng.random()
    return k


def poisson_sample(rng, lam):
    """An exact Poisson(lam) sample: a sum of Knuth samples with means at most 30."""
    if lam < 0:
        raise ValueError("negative mean")
    total = 0
    while lam > 30.0:
        total += _poisson_small(rng, 30.0)
        lam -= 30.0
    if lam > 0.0:
        total += _poisson_small(rng, lam)
    return total
stolt_migration.py 90 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/stolt-migration/. The authors are listed in prov/HTV0W8.jsonl.
"""Stolt (f-k) migration of a zero-offset section in constant velocity.

Conventions (the imaging and inverse problems pages):
- data[k][j] is the zero-offset sample at the two-way time t = k * dt and the
  surface position x = j * dx. image[i][j] is the image at depth z = i * dz.
- U(omega) = sum u(t) exp(-i omega t), and the x transform uses exp(-i kx x).
  Index k of a DFT of length n stands for k for k < n / 2 and k - n otherwise.
- Exploding reflector model: the data is a one-way field in the velocity
  v' = v / 2. Downward continuation multiplies by exp(+i kz z).
- Stolt mapping: the image transform at (kz, kx) is
      M(kz, kx) = (dt / dz) * v' |kz| / sqrt(kx^2 + kz^2) * D(omega, kx),
  with omega = sgn(kz) v' sqrt(kx^2 + kz^2), and D is read from the data
  transform by linear interpolation along omega. The factor dt / dz turns the
  sums of the DFT into the integrals of the continuous formula.

Cost: one 2D FFT of the data, O(nz * nx) interpolations, and one inverse 2D
FFT of the image: O(nt nx log(nt nx) + nz nx log(nz nx)) with radix-2 FFTs.
"""

import math

from imgkit import dft2, idft2


def signed_index(k, n):
    """The signed frequency index of DFT index k: k for k < n / 2, else k - n."""
    return k if 2 * k < n else k - n


def stolt_map(kz, kx, vhalf):
    """omega = sgn(kz) v' sqrt(kx^2 + kz^2) and the Jacobian v' |kz| / sqrt(kx^2 + kz^2)."""
    r = math.hypot(kx, kz)
    if r == 0.0:
        return 0.0, 0.0
    return math.copysign(vhalf * r, kz), vhalf * abs(kz) / r


def stolt_migration(data, dt, dx, dz, nz, vel, pad=4):
    """Migrate a zero-offset section in the constant velocity vel (m/s).

    data: nt x nx real samples. Returns the nz x nx real image. Precondition:
    dt, dx, dz, vel > 0. The time axis is padded with zeros to pad * nt
    samples, so that linear interpolation along omega is accurate. Frequencies
    outside the band of the data (|omega| near the Nyquist frequency) are set
    to zero.
    """
    nx = len(data[0])
    padded = [list(row) for row in data] + [[0.0] * nx for _ in range((pad - 1) * len(data))]  # @line L1
    nt = len(padded)
    vhalf = vel / 2.0
    d = dft2(padded)  # @line L2
    dw = 2.0 * math.pi / (nt * dt)
    dkx = 2.0 * math.pi / (nx * dx)
    dkz = 2.0 * math.pi / (nz * dz)
    m = [[0j] * nx for _ in range(nz)]
    for i in range(nz):  # @line L3
        kz = signed_index(i, nz) * dkz
        for j in range(nx):
            kx = signed_index(j, nx) * dkx
            omega, jac = stolt_map(kz, kx, vhalf)  # @line L4
            u = omega / dw
            k0 = math.floor(u)
            frac = u - k0
            if jac == 0.0 or not (-nt // 2 < k0 and k0 + 1 < (nt + 1) // 2):  # @line L5
                continue  # the zero wavenumber or outside the band: M stays 0
            val = (1.0 - frac) * d[k0 % nt][j] + frac * d[(k0 + 1) % nt][j]  # @line L6
            m[i][j] = (dt / dz) * jac * val  # @line L7
    img = idft2(m)  # @line L8
    return [[v.real for v in row] for row in img]  # @line L9


def ricker(t, f0):
    """The Ricker wavelet (1 - 2 a) exp(-a) with a = (pi f0 t)^2, peak frequency f0."""
    a = (math.pi * f0 * t) ** 2
    return (1.0 - 2.0 * a) * math.exp(-a)


def diffractor_section(nt, nx, dt, dx, x0, z0, vel, f0):
    """Zero-offset data of one point diffractor at (x0, z0) in constant velocity.

    The event is a Ricker wavelet along t(x) = (2 / vel) sqrt(z0^2 + (x - x0)^2).
    """
    out = [[0.0] * nx for _ in range(nt)]
    for j in range(nx):
        t0 = 2.0 * math.hypot(z0, j * dx - x0) / vel
        for k in range(nt):
            out[k][j] = ricker(k * dt - t0, f0)
    return out
test_stolt_migration.py 84 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/stolt-migration/. The authors are listed in prov/HTV0W8.jsonl.
import cmath
import math
import unittest

from imgkit import dft2, idft
from stolt_migration import diffractor_section, ricker, signed_index, stolt_map, stolt_migration

NT, NX, DT, DX = 64, 32, 0.004, 10.0
VEL = 3000.0
DZ = VEL * DT / 2.0


def phase_shift_oracle(data, dt, dx, dz, nz, vel):
    """Constant velocity phase-shift migration, used as an independent reference."""
    nt, nx = len(data), len(data[0])
    p = dft2(data)
    vh = vel / 2.0
    img = []
    for _ in range(nz):
        row = [sum(p[k][j] for k in range(nt)) / nt for j in range(nx)]
        img.append([v.real for v in idft(row)])
        for k in range(nt):
            w = 2.0 * math.pi * signed_index(k, nt) / (nt * dt)
            for j in range(nx):
                kx = 2.0 * math.pi * signed_index(j, nx) / (nx * dx)
                a = (w / vh) ** 2 - kx * kx
                if w == 0.0 or a <= 0.0:
                    p[k][j] = 0j
                else:
                    p[k][j] *= cmath.exp(1j * math.copysign(math.sqrt(a), w) * dz)
    return img


def argmax2(img):
    return max((img[i][j], i, j) for i in range(len(img)) for j in range(len(img[0])))[1:]


class TestStoltMigration(unittest.TestCase):
    def test_flat_event_maps_time_to_depth(self):
        # A flat event at t0 = 0.2 s goes to z = v' t0 = 300 m, row 50.
        data = [[ricker(k * DT - 0.2, 25.0)] * 8 for k in range(NT)]
        img = stolt_migration(data, DT, DX, DZ, 64, VEL)
        for j in range(8):
            for i in range(64):
                self.assertAlmostEqual(img[i][j], ricker(i * DT - 0.2, 25.0), delta=1e-6)

    def test_point_diffractor_focuses(self):
        data = diffractor_section(NT, NX, DT, DX, 160.0, 192.0, VEL, 25.0)
        img = stolt_migration(data, DT, DX, DZ, 64, VEL)
        i, j = argmax2(img)
        self.assertLessEqual(abs(i - 32), 1)
        self.assertEqual(j, 16)

    def test_agrees_with_phase_shift(self):
        data = diffractor_section(NT, NX, DT, DX, 160.0, 192.0, VEL, 25.0)
        a = stolt_migration(data, DT, DX, DZ, 48, VEL)
        b = phase_shift_oracle(data, DT, DX, DZ, 48, VEL)
        num = sum((a[i][j] - b[i][j]) ** 2 for i in range(48) for j in range(NX))
        den = sum(b[i][j] ** 2 for i in range(48) for j in range(NX))
        self.assertLess(math.sqrt(num / den), 0.2)

    def test_stolt_map(self):
        vh = 1000.0
        w, jac = stolt_map(0.03, 0.04, vh)
        self.assertAlmostEqual(w, 50.0, places=9)
        self.assertAlmostEqual(jac, 0.6 * vh, places=9)
        w, jac = stolt_map(-0.03, 0.04, vh)
        self.assertAlmostEqual(w, -50.0, places=9)
        self.assertAlmostEqual(jac, 0.6 * vh, places=9)
        self.assertEqual(stolt_map(0.0, 0.0, vh), (0.0, 0.0))
        # At kx = 0 the map is the plain time-to-depth stretch omega = v' kz.
        w, jac = stolt_map(0.02, 0.0, vh)
        self.assertAlmostEqual(w, 20.0, places=9)
        self.assertAlmostEqual(jac, vh, places=9)

    def test_zero_data_gives_zero_image(self):
        img = stolt_migration([[0.0] * 4 for _ in range(16)], DT, DX, DZ, 8, VEL)
        self.assertEqual(max(abs(v) for row in img for v in row), 0.0)


if __name__ == "__main__":
    unittest.main()

History

Stolt (1978) derived the migration of constant-velocity data by a change of variables in the Fourier domain, and gave the stretch for depth-varying velocity in the same paper. Gazdag (1978) published the phase-shift method in the same year. The mapping became a standard tool of seismic processing for its speed. Radar engineers later took the same mapping into the omega-k (wavenumber domain) algorithm for synthetic aperture radar.

References

  1. R. H. Stolt, Migration by Fourier transform, Geophysics 43(1), 1978, 23 to 48, the change of variables from frequency to vertical wavenumber for constant velocity
  2. Jeno Gazdag, Wave equation migration with the phase-shift method, Geophysics 43(7), 1978, 1342 to 1351, the phase-shift form of the same downward continuation
  3. Oz Yilmaz, Seismic Data Analysis, Society of Exploration Geophysicists, 2001, Chapter 4 (migration), frequency-wavenumber migration

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH1 sketch
  • a full proof of time.mapping
  • a full proof of time.total
  • a full proof of space.extra
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • a scene preset for the example