zernike_fit#
- pyzernike.zernike_fit(rho: ndarray, theta: ndarray, values: ndarray, order: Integral, *, weights: ndarray | None = None, use_recursive: bool = False, normalize: bool = False, return_residuals: bool = False) ndarray | Tuple[ndarray, ndarray][source]#
Fit a sampled function with the Zernike polynomials up to a radial order \(N\) by linear least squares.
The function \(f\) sampled at the points \((\rho_k, \theta_k)\) is approximated by:
\[f(\rho, \theta) \approx \sum_{j=0}^{P-1} c_j Z_j(\rho, \theta)\]where \(Z_j\) is the Zernike polynomial of OSA/ANSI index \(j\) (see
pyzernike.zernike_index_to_order()) and \(P = \frac{(N+1)(N+2)}{2}\). The coefficients \(c_j\) minimize the weighted sum of the squared residuals:\[\min_{c} \sum_{k} w_k \left( f_k - \sum_{j} c_j Z_j(\rho_k, \theta_k) \right)^2\]The problem is linear in \(c\), so it is solved directly (no iteration) with an orthogonal decomposition of the design matrix \(A_{kj} = \sqrt{w_k} Z_j(\rho_k, \theta_k)\) (
numpy.linalg.lstsq()), which is more stable than the normal equations \(A^\top A c = A^\top f\).The points where \(\rho\) is outside \([0, 1]\), or where \(\rho\), \(\theta\),
valuesorweightsis not finite, or where the weight is zero, are ignored.Note
The least squares solution does not require the polynomials to be orthogonal on the sampled points: the reconstructed function is the same with or without normalization. The orthogonality on the unit disk only ensures that the coefficients are almost independent of the order \(N\) when the sampling is dense and uniform in area. With
normalize=True, the coefficients are expressed in the orthonormal basis of Noll, and the variance of \(f\) on the disk is then \(\sum_{j > 0} c_j^2\) (seepyzernike.zernike_rms()).See also
pyzernike.xy_zernike_fit()to fit a function sampled in Cartesian coordinates on an elliptic annulus.pyzernike.zernike_evaluate()to evaluate the fitted function and its derivatives from the coefficients.pyzernike.zernike_index_to_order()to convert the OSA/ANSI indices to orders \((n, m)\).pyzernike.zernike_normalization()for the normalization factors of Noll.The page Fitting for the least squares fit and the role of the orthogonality.
- Parameters:
rho (numpy.ndarray) – The radial coordinates \(\rho\) of the samples, with any shape
(...).theta (numpy.ndarray) – The angular coordinates \(\theta\) of the samples in radians, with the same shape as
rho.values (numpy.ndarray) – The sampled values \(f_k\), with the same shape as
rho.order (Integral) – The maximum radial order \(N \geq 0\) of the fit.
weights (Optional[numpy.ndarray], optional) – The weights \(w_k \geq 0\) of the samples, with the same shape as
rho. Default is None (all the weights are equal to 1).use_recursive (bool, optional) – If True, the polynomials are computed with the recurrence relation (recommended for high orders, see
pyzernike.zernike_polynomial_up_to_order()). Default is False.normalize (bool, optional) – If True, the fit uses the polynomials normalized by the factors of Noll \(N_{n}^{m} = \sqrt{(2 - \delta_{m0})(n + 1)}\), and the coefficients are those of the orthonormal basis. Default is False.
return_residuals (bool, optional) – If True, the residuals \(f_k - \sum_{j} c_j Z_j(\rho_k, \theta_k)\) are also returned. Default is False.
- Returns:
coefficients (numpy.ndarray) – The coefficients \(c_j\) with shape \((P,)\), in the OSA/ANSI ordering, as
numpy.float64.residuals (numpy.ndarray) – Only if
return_residualsis True. The residuals with the shape ofrho, asnumpy.float64. They are set tonumpy.nanat the ignored points.
- Raises:
TypeError – If
rho,theta,valuesorweightscan not be converted to a floating point array, iforderis not an integer, or ifuse_recursive,normalizeorreturn_residualsis not a boolean.ValueError – If the arrays do not have the same shape, if
orderis negative, if a weight is negative, or if the number of valid samples is lower than the number \(P\) of polynomials.
- Warns:
RuntimeWarning – If the design matrix is rank deficient (the samples do not determine all the coefficients, for example if they do not cover the disk). The minimum norm solution is then returned.
Examples
Fit a noisy sum of defocus and astigmatism up to the order 4:
import numpy from pyzernike import zernike_fit, zernike_polynomial rng = numpy.random.default_rng(0) rho = numpy.sqrt(rng.uniform(0, 1, 5000)) theta = rng.uniform(0, 2 * numpy.pi, 5000) f = 0.5 * zernike_polynomial(rho, theta, n=2, m=0) - 0.2 * zernike_polynomial(rho, theta, n=2, m=2) f += rng.normal(0, 0.01, f.shape) coefficients = zernike_fit(rho, theta, f, order=4) # coefficients[4] ~ 0.5 (Z_2^0) and coefficients[5] ~ -0.2 (Z_2^2)
Reconstruct the fitted function and get the residuals:
from pyzernike import zernike_evaluate coefficients, residuals = zernike_fit(rho, theta, f, order=4, return_residuals=True) fitted = zernike_evaluate(coefficients, rho, theta) # (5000,)