How to Fit Spherical Harmonics to a 3D Grid
At a glance
- 1. Choose a radius
Spherical harmonics describe angular variation on a sphere, so select a shell or define a radial model.
- 2. Build the basis
Evaluate the harmonics at each grid point’s direction, using a stated normalization and phase convention.
- 3. Fit with weights
Use surface-area-aware weights and solve a weighted least-squares problem for the harmonic coefficients.
- 4. Compare residuals
For a single best harmonic, compare each candidate’s weighted fit error; for an expansion, retain modes as needed.
A spherical harmonic is a function of direction on a sphere. A 3D field generally depends on both distance from the origin and direction, so there is no single spherical-harmonic fit until you decide how to handle the radius. The usual approach is to fit the angular pattern separately on one or more spherical shells.
This also clarifies “nearest”: it can mean the best single harmonic, or a truncated sum of harmonics that approximates the field. The fitting method below covers both.
Separate radial and angular dependence
Write a scalar field as an angular expansion whose coefficients may vary with radius:
Here is distance from the chosen origin, is the polar angle, and is the azimuth. This article uses the complex spherical harmonics normalized so that
where the star denotes complex conjugation and . Other normalizations and real-valued bases are common; use one convention consistently.
For a continuous field at fixed , the coefficient is
On a 3D Cartesian grid, choose a shell around the radius of interest and use the field values in that shell. Repeat for several radii if you need the radial dependence of the coefficients.
Fit grid samples by weighted least squares
For each selected grid point , calculate
The origin has no defined direction, so do not use it in an angular fit. At each remaining point, evaluate the chosen basis functions . Collect the field values into a vector and the basis values into a matrix , with one row per sample and one column per retained harmonic. Estimate the coefficient vector by minimizing
In matrix form, solve , where is diagonal with entries . Use a stable least-squares solver such as QR or singular-value decomposition rather than explicitly forming a matrix inverse.
The weights matter. A regular Cartesian grid does not sample directions uniformly: some parts of a shell may have more grid points than others. Give samples weights that approximate their represented solid angle. For small voxels in a thin shell, a useful approximation is proportional to voxel volume divided by ; the radial thickness is then effectively constant. A purpose-built spherical quadrature grid can instead use its supplied solid-angle weights. Simply giving every Cartesian point equal weight can bias the fit.
If the samples are well-distributed quadrature nodes and the weights approximate , you can estimate a coefficient directly with . For a general Cartesian subset, least squares is usually safer because the sampled basis functions may not be orthogonal.
Decide which harmonic is nearest
To find the best single harmonic, fit each candidate mode separately and compare its weighted residual sum of squares. Choose the mode with the smallest residual. When the sampling and weights make the basis orthonormal, this is equivalent to choosing the mode with the largest coefficient magnitude . For nonuniform or incomplete sampling, that shortcut need not hold.
To approximate the field with a sum of harmonics, fit all modes through some maximum degree , then increase until the residual is sufficiently small. Higher degrees represent finer angular variation, but they can also fit noise and become poorly determined if there are too few well-distributed samples. A useful practical check is to compare residuals on held-out samples or across successive values of .
Worked example: a constant plus a dipole
Suppose the field on a unit sphere is
Under the normalization above, and . Therefore
The only nonzero coefficients are and . If you require one harmonic only, compare their squared magnitudes: the constant mode has , while the dipole mode has . Thus is the better single-mode fit in the continuous, uniformly weighted spherical sense. It captures of the field’s total squared norm. A grid fit should reach the same result approximately when its shell sampling and weights represent the sphere well.
Common mistakes
- Fitting a volume as if it were one sphere. Keep radius fixed, fit separate shells, or explicitly model radial dependence.
- Using unweighted Cartesian samples. Uneven directional coverage changes the effective fit and can favor some angles.
- Comparing coefficients from incompatible conventions. Normalization and phase conventions affect coefficient values; state and keep the convention fixed.
- Using too many modes for the data. There are complex modes through degree . Sparse or clustered samples can make the fit unstable.
- Ignoring the origin or coordinate orientation. The origin has no angular coordinates, and changing the chosen axis changes the mode labels and coefficients.
References
- NIST Digital Library of Mathematical Functions, §14.30: Spherical and Spheroidal Harmonics
- Numerical Recipes: The Art of Scientific Computing, Third Edition, William H. Press et al.
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.