Fast Synthetic Fields with Orthogonal Polynomials

Sampling smooth random fields and binary masks with controllable topology, using tensor products of orthogonal polynomials.

Why sample smooth fields?

We want a cheap, reproducible supply of smooth random fields on a grid — and, by thresholding, binary masks with controllable topology. Our use case: synthetic perturbations for training a topology-aware segmentation model (see the demo). Orthogonal polynomials make this easy.

Four polynomial bases

Build a 2D field by tensoring two 1D bases, $f(x, y) = \sum_{ij} c_{ij}\, P_i(x)\, P_j(y)$, with random, seeded coefficients. Each basis gives a different texture — a few samples each, expand for more:

Legendre

Legendre random-coefficient fields
More Legendre examples
More Legendre fields

Chebyshev

Chebyshev random-coefficient fields
More Chebyshev examples
More Chebyshev fields

Hermite (probabilist’s, $He_n$)

Probabilist Hermite random-coefficient fields
More Hermite examples
More probabilist Hermite fields

Physicist’s Hermite ($H_n$)

Physicist Hermite random-coefficient fields
More physicist's Hermite examples
More physicist Hermite fields

A fresh $300 \times 300$ field takes about 14 ms (≈ 70 per second) on a laptop, so a large batch is cheap.

From fields to masks

Threshold a field to get a binary mask. Raising the order folds the level set more, adding more connected components and holes.

Binary masks from thresholded Chebyshev fields
Binary masks from thresholded Chebyshev fields (order 6); raising the order adds components and holes.

How to use

import numpy as np
from polysynth import Legendre, TensorPolynomial, random_coefficients

x = np.linspace(-1, 1, 300)
X, Y = np.meshgrid(x, x)

f = TensorPolynomial(Legendre(), random_coefficients((6, 6), normalize="l1", seed=0))
Z = f(X, Y)                                # smooth random field
mask = f.mask(X, Y, threshold=0.5)         # binary topology perturbation

Swap Legendre() for Chebyshev(), Hermite(), or PhysicistsHermite() to change the texture; raise the order for more topology.