Algorithmcore
Hybrid input-output algorithm
Also called HIO, Fienup HIO.
Fienup's phase retrieval iteration: keep the magnitude-projected image on the support and push the old iterate away from it elsewhere. It escapes stagnation but has no convergence proof.
- Solves
- Phase retrieval
- Time
- O(K N log N) worst
- Space
- O(N) extra
- Where
- K = the number of iterations; N = the number of pixels
- Model
- Real RAM
- Properties
- deterministicnot exactnot in placeoffline
- Evidence
- Proof sketchCode in 1 languages
- Origin
- James R. Fienup, 1982
The hybrid input-output algorithm (HIO) solves the phase retrieval problem with a known support. Like error reduction, it first puts the measured Fourier magnitude on the current image. Then it keeps the result on the support, but at the pixels that break the object constraint it does not set zero: it takes the old value and subtracts times the new one. This feedback pushes the iterate away from the stagnation points of error reduction. Fienup (1982) introduced it, and it is still a standard method in coherent diffraction imaging. Its error is not monotone and there is no convergence proof, but every fixed point of HIO gives a solution.
In plain words
Error reduction cleans up a guess by erasing everything outside the box where the object must be. This often gets stuck: the guess stops changing, but it is still wrong.
HIO changes the clean-up step. Inside the box, it keeps the new guess as it is. Outside the box, the new guess should be zero but is not. HIO does not erase it. It looks at how much stuff is there, and it moves the old guess in the opposite direction by almost the same amount. The next round then has a strong push to remove that stuff.
This push lets the guess climb out of places where error reduction gets stuck. The price is that the error can go up for a while. In practice HIO finds the object much more often.
The problem
The input is an array of Fourier magnitudes of an unknown image with pixels, a support mask that holds every nonzero pixel of , and maybe the knowledge that is real and nonnegative. The output is an image that is zero off , nonnegative if required, and whose DFT has magnitude . At best it equals up to the trivial ambiguities. The DFT is unnormalized forward with on the inverse.
The algorithm
HIO(A, S, g₀, K, β)- 1
g ← g₀ - 2
for k ← 1 to K do - 3
G ← DFT(g) - 4
G′_u ← A_u · G_u / |G_u| for each u ▷ A_u when G_u = 0 - 5
g′ ← IDFT(G′) - 6
for each pixel r do - 7
if r ∈ S and g′(r) ≥ 0 then g(r) ← g′(r) ▷ test g′(r) ≥ 0 only with nonnegativity - 8
else g(r) ← g(r) − β · g′(r) - 9
return P_S(g′)
Lines L3 to L5 are the magnitude projection , the same as in error reduction. With nonnegativity the image is real, and means the real part of the inverse DFT. The output of line L9 is with the object constraint put on. The iterate itself is not the answer: off the support it holds the accumulated feedback.
The progress of HIO is usually tracked by the object error , the part of that breaks the object constraint. It is zero exactly when is a solution.
Example
Use the same object as on the error reduction page: the nonnegative block
in the corner of an frame, with the support and nonnegativity. From the random start of seed 1, error reduction stops at the Fourier error 0.617. HIO with from the same start gives these object errors (measurements of the implementation):
| iteration | 0 | 1 | 2 | 5 | 10 | 20 | 50 | 100 | 200 | 299 |
|---|---|---|---|---|---|---|---|---|---|---|
| 6.22 | 5.59 | 3.44 | 1.13 | 0.483 | 0.0374 | 0.0210 | 0.0269 | 0.0100 | 0.00635 |
The error goes up between iterations 50 and 100: HIO is not monotone. After 300 iterations, 20 iterations of error reduction bring the Fourier error to . The result is not but its twin , the object turned by 180 degrees. It has the same Fourier magnitude and fits in the same support, so it is an equally good answer (a conjugate reversal followed by a shift). The start of seed 2 also gives the twin.
Correctness
There is no proof that HIO converges, and its error can go up. Three facts are proved:
- Solutions are fixed points. If fits the magnitude and the object constraint, one HIO step leaves it unchanged.
- Fixed points give solutions. If , then at every pixel outside the set of line L7, , so . So is zero off the support and nonnegative on it, and is a solution. This is the difference from error reduction, whose fixed points can be stagnation points with a positive error.
- Douglas-Rachford. With a support constraint only and , the step is , the Douglas-Rachford iteration for the two sets (Bauschke, Combettes and Luke 2002). The convergence theory of Douglas-Rachford needs convex sets, and the magnitude set is not convex, so this does not give convergence.
The proofs are below.
Proof: Fixed points of HIO give solutions, and HIO with beta = 1 is Douglas-Rachfordfull
The setting is that of the algorithm. Images have pixels. is the set of images whose DFT has magnitude , and is the map of lines L3 to L5: a nearest point of (Lemma 2 of the error reduction proof, which uses only Parseval's identity and the triangle inequality). is the object constraint set: zero off the support and, with nonnegativity, real and nonnegative on . A solution is an image in . The feedback is a fixed number with . Arithmetic is exact.
For an image let (in the nonnegative case, is real and stands for the real part of , as in the implementation). Let be the set of pixels with and, in the nonnegative case, . The HIO map is
as in lines L6 to L8.
There is no theorem that HIO converges, and its error is not monotone. What can be proved is what its fixed points mean, and its link to the Douglas-Rachford method.
Theorem 1 (solutions are fixed points). If , then .
Proof. , so by Lemma 2 of the error reduction proof (a point of is its own nearest point: the map of line L4 sends to when , and sends to ). So . Off , , so . On in the nonnegative case, , so and . Without nonnegativity every is in , with the same result.
Theorem 2 (fixed points give solutions). If , then is a solution: . The algorithm returns at such a point.
Proof. by construction. Take a pixel . Then , so , and since . So is zero at every pixel outside . Every pixel off is outside , so is zero off . In the nonnegative case, a pixel of with is outside , so , which is a contradiction; so on , and is real by the convention above. So . Since , the object projection of line L9 does not change it.
Theorem 2 is the main difference from error reduction. An error reduction fixed point can be a stagnation point with a positive error. A fixed point of HIO always gives a solution. The iterate itself need not be a solution: off the support it holds the accumulated feedback, and is the answer.
Theorem 3 (Douglas-Rachford, Bauschke, Combettes and Luke 2002). Take the case with a support and no nonnegativity, so that (zero off , unchanged on ) is a linear projection, and take . Then for every image ,
The right side is the Douglas-Rachford iteration for the two sets and .
Proof. Without nonnegativity, . Let . On : , so the right side is . Off : , so the right side is with .
What is not claimed. The convergence theorems for Douglas-Rachford need convex sets, and is not convex, so Theorem 3 gives no convergence. The value that Fienup and later users found good is a tuned choice. The test file checks Theorems 1 and 3 and the update rule. It does not check a monotone error, because there is none.
Complexity
Each iteration does one forward and one inverse 2D DFT and other work. With a fast Fourier transform this is per iteration and in total. The extra space is for , and . There is no bound on the number of iterations; typical runs use some hundreds to some thousands.
Properties
- Not monotone. The error can go up. Do not stop at the first increase.
- Fixed points are solutions in the sense of Theorem 2 of the proof.
- Feedback. The value sets the strength of the push. Common values are between 0.5 and 1. The value 0.9 (used, for example, by Marchesini et al. 2003) is a tuned choice, not a result.
- Ambiguities remain. A run can end at the object, its twin, or a shift of either that fits the support.
Variants and improvements
- Input-output and output-output. Fienup (1982) also defined two other steps. The basic input-output step keeps the old value at the pixels of line L7 and uses elsewhere. The output-output step uses at the pixels of line L7 and elsewhere. HIO mixes the two, which gives its name. HIO worked best in his tests.
- HIO then error reduction. Run HIO to get near a solution, then a few error reduction steps, which reduce the error and never increase it.
- Difference map. Elser's family of iterations; one choice of its parameters gives HIO exactly.
- RAAR. A relaxation of the case that behaves better with noisy data.
- Shrinkwrap. HIO with a support that is updated during the run.
- Many starts. Run HIO from many random starts, and keep or average the results with the smallest error.
When to use it
Use HIO for phase retrieval from one far-field intensity with a support, as in coherent diffraction imaging and crystallography of small objects. Start with HIO, end with error reduction, and run several starts. Make sure the data are oversampled, or the support constraint is too weak. With noisy data, compare with RAAR, which is less likely to wander.
Pitfalls
- Returning instead of . The iterate holds feedback off the support. The estimate is .
- Stopping at an increase. The error is not monotone, so an increase is not a sign of failure.
- Nonnegativity for a complex object. The test in line L7 is for a real object. For a complex object, use the support test alone.
- A twin that lives inside the support. For a centrally symmetric support, the twin is always a valid answer, and a run can mix the object and its twin into one stagnation pattern. A support that is not centrally symmetric breaks this tie.
- Comparing with the truth. Align the result for shift, twin and global phase first.
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.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/hybrid-input-output/. The authors are listed in prov/5WGXT1.jsonl.
"""Fienup's hybrid input-output (HIO) algorithm for phase retrieval.
Images are lists of rows. The data are the Fourier magnitudes A = |DFT2(f)|
of an unknown image f (unnormalized DFT, as in imgkit) and a support mask S.
With positive=True the object is also real and nonnegative, and the
iterates are real. One HIO step is
g' = P_M g (put on the Fourier magnitude)
g_next(r) = g'(r) where r is in S (and g'(r) >= 0)
g_next(r) = g(r) - beta g'(r) elsewhere.
There is no monotone error and no convergence proof. The value beta = 0.9
is a tuned choice, not a result.
"""
import math
from imgkit import dft2, idft2
def magnitude_projection(g, amp):
"""P_M g: the nearest image to g whose Fourier magnitude is amp."""
gf = dft2(g) # @line L3
for row_g, row_a in zip(gf, amp): # @line L4
for j, (v, a) in enumerate(zip(row_g, row_a)):
r = abs(v)
row_g[j] = a * v / r if r > 0.0 else complex(a)
return idft2(gf) # @line L5
def object_projection(gp, support, positive=False):
"""P_S g': g' on the support (its nonnegative real part if positive), 0 off it."""
return [[(max(complex(v).real, 0.0) if positive else v) if s else 0.0 for v, s in zip(rv, rs)]
for rv, rs in zip(gp, support)]
def object_error(gp, support, positive=False):
"""E_0(g') = ||g' - P_S g'||, the part of g' that breaks the object constraint."""
proj = object_projection(gp, support, positive)
return math.sqrt(sum(abs(v - p) ** 2 for rv, rp in zip(gp, proj) for v, p in zip(rv, rp)))
def hio_step(g, amp, support, beta=0.9, positive=False):
"""One HIO step. Returns (g_next, g') with g' = P_M g."""
gp = magnitude_projection(g, amp)
if positive:
gp = [[complex(v).real for v in row] for row in gp]
out = []
for row_g, row_p, row_s in zip(g, gp, support): # @line L6
new_row = []
for v, p, inside in zip(row_g, row_p, row_s):
if inside and (not positive or p >= 0.0): # @line L7
new_row.append(p)
else:
new_row.append(v - beta * p) # @line L8
out.append(new_row)
return out, gp
def hio(amp, support, g0, iters, beta=0.9, positive=False):
"""Run HIO. Returns (estimate, g, errors).
errors[k] = E_0(g'_k) for k = 0 .. iters - 1, and estimate = P_S(g') of the
last step, the image that satisfies the object constraint.
"""
g = [list(row) for row in g0] # @line L1
gp = g
errors = []
for _ in range(iters): # @line L2
g, gp = hio_step(g, amp, support, beta, positive)
errors.append(object_error(gp, support, positive))
return object_projection(gp, support, positive), g, errors # @line L9
imgkit.py 322 lines
# 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
test_hybrid_input_output.py 86 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/hybrid-input-output/. The authors are listed in prov/5WGXT1.jsonl.
import math
import random
from hybrid_input_output import hio, hio_step, magnitude_projection, object_projection
from imgkit import dft2
OBJ = [[1, 2, 0, 1], [0, 3, 1, 0], [2, 1, 4, 1], [0, 1, 0, 2]]
def dist(a, b):
return math.sqrt(sum(abs(x - y) ** 2 for ra, rb in zip(a, b) for x, y in zip(ra, rb)))
def page_problem():
f = [[float(OBJ[i][j]) if i < 4 and j < 4 else 0.0 for j in range(8)] for i in range(8)]
twin = [[float(OBJ[3 - i][3 - j]) if i < 4 and j < 4 else 0.0 for j in range(8)] for i in range(8)]
support = [[i < 4 and j < 4 for j in range(8)] for i in range(8)]
amp = [[abs(v) for v in row] for row in dft2(f)]
return f, twin, support, amp
def fourier_error(g, amp):
gf = dft2(g)
n = len(gf) * len(gf[0])
return math.sqrt(sum((abs(v) - a) ** 2 for rv, ra in zip(gf, amp) for v, a in zip(rv, ra)) / n)
def test_twin_has_the_same_magnitudes() -> None:
_, twin, _, amp = page_problem()
assert fourier_error(twin, amp) < 1e-12
def test_beta_one_with_support_only_is_douglas_rachford() -> None:
# Bauschke, Combettes and Luke 2002: x + P_S(2 P_M x - x) - P_M x.
rng = random.Random(1)
_, _, support, amp = page_problem()
x = [[complex(rng.gauss(0, 1), rng.gauss(0, 1)) for _ in range(8)] for _ in range(8)]
pm = magnitude_projection(x, amp)
refl = [[2 * p - v for p, v in zip(rp, rv)] for rp, rv in zip(pm, x)]
ps = object_projection(refl, support)
dr = [[v + s - p for v, s, p in zip(rv, rs, rp)] for rv, rs, rp in zip(x, ps, pm)]
step, _ = hio_step(x, amp, support, beta=1.0)
assert dist(step, dr) < 1e-12
def test_off_support_feedback_rule() -> None:
rng = random.Random(2)
_, _, support, amp = page_problem()
x = [[rng.uniform(-1, 1) for _ in range(8)] for _ in range(8)]
for beta in (0.5, 0.9):
step, gp = hio_step(x, amp, support, beta=beta, positive=True)
for i in range(8):
for j in range(8):
p = gp[i][j]
if support[i][j] and p >= 0:
assert abs(step[i][j] - p) < 1e-15
else:
assert abs(step[i][j] - (x[i][j] - beta * p)) < 1e-15
def test_true_object_is_a_fixed_point() -> None:
f, _, support, amp = page_problem()
for positive in (False, True):
est, g, errors = hio(amp, support, f, 5, 0.9, positive)
assert dist(g, f) < 1e-9
assert dist(est, f) < 1e-9
assert max(errors) < 1e-9
def test_reconstruction_where_error_reduction_stagnates() -> None:
# Measurement, not a theorem: on the example of the page, error reduction from
# these starts stops at an error near 0.6, while 300 HIO steps (beta = 0.9) and
# 20 error reduction steps find the object up to its twin.
f, twin, support, amp = page_problem()
for seed in (1, 2):
rng = random.Random(seed)
g0 = [[rng.uniform(0, 1) for _ in range(8)] for _ in range(8)]
est, _, _ = hio(amp, support, g0, 300, 0.9, True)
g = est
for _ in range(20):
g = object_projection(magnitude_projection(g, amp), support, True)
assert fourier_error(g, amp) < 1e-3
assert min(dist(g, f), dist(g, twin)) < 1e-2
History
Fienup (1978) reconstructed an object from the modulus of its Fourier transform with an iterative method and object domain constraints. In his 1982 comparison he defined error reduction, the gradient methods and the input-output algorithms, among them hybrid input-output, and found that HIO worked best in his tests. Bauschke, Combettes and Luke (2002) showed that HIO for the support constraint with is the Douglas-Rachford algorithm of convex optimization. Elser (2003) and Luke (2005) built the difference map and RAAR on this view.
References
- J. R. Fienup, Phase retrieval algorithms, a comparison, Applied Optics 21(15), 1982, 2758 to 2769, the section on the input-output algorithms, with the hybrid input-output update, and the comparison with error reduction
- J. R. Fienup, Reconstruction of an object from the modulus of its Fourier transform, Optics Letters 3(1), 1978, 27 to 29, the iterative method with object domain constraints
- Heinz H. Bauschke, Patrick L. Combettes and D. Russell Luke, Phase retrieval, error reduction algorithm, and Fienup variants, a view from convex optimization, Journal of the Optical Society of America A 19(7), 2002, 1334 to 1345, HIO with the support constraint and beta = 1 as the Douglas-Rachford algorithm
- S. Marchesini, H. He, H. N. Chapman, S. P. Hau-Riege, A. Noy, M. R. Howells, U. Weierstall and J. C. H. Spence, X-ray image reconstruction from a diffraction pattern alone, Physical Review B 68(14), 2003, 140101, HIO with beta = 0.9 inside the shrinkwrap method
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.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H1 sketch |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag 5WGXT1 Edit this page History Report an error Markdown