xy_zernike_fit#

pyzernike.xy_zernike_fit(x: ndarray, y: ndarray, values: ndarray, order: Integral, *, Rx: Real | None = 1.0, Ry: Real | None = 1.0, x0: Real | None = 0.0, y0: Real | None = 0.0, alpha: Real | None = 0.0, h: Real | None = 0.0, weights: ndarray | None = None, use_recursive: bool = False, normalize: bool = False, return_residuals: bool = False) → ndarray | Tuple[ndarray, ndarray][source]#

Fit a function sampled in Cartesian coordinates on an elliptic annulus with the Zernike polynomials up to a radial order \(N\) by linear least squares.

../_images/elliptic_annulus_domain.png

The parameters of the elliptic annulus domain.#

The function \(f\) sampled at the points \((x_k, y_k)\) is approximated by:

\[f(x, y) \approx \sum_{j=0}^{P-1} c_j Zxy_j(x, y)\]

where \(Zxy_j(x, y) = Z_j(\rho_{eq}(x, y), \theta_{eq}(x, y))\) is the Zernike polynomial of OSA/ANSI index \(j\) on the elliptic annulus of parameters \((R_x, R_y, x_0, y_0, \alpha, h)\) (see pyzernike.xy_zernike_polynomial()) 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 Zxy_j(x_k, y_k) \right)^2\]

The points are converted to the equivalent polar coordinates \((\rho_{eq}, \theta_{eq})\) (see pyzernike.cartesian_to_elliptic_annulus()), and the problem is solved as in pyzernike.zernike_fit(). The parameters of the annulus are fixed: their estimation is a non-linear problem (see pyzernike.xy_zernike_least_squares()).

The points outside the elliptic annulus, or where \(x\), \(y\), values or weights is not finite, or where the weight is zero, are ignored.

Note

For a full ellipse (\(h = 0\)), the mapping onto the unit disk is affine, so the normalized polynomials stay orthonormal for the inner product averaged over the area of the ellipse: with normalize=True and a sampling dense and uniform in area, the variance of \(f\) on the ellipse is \(\sum_{j > 0} c_j^2\). For \(h > 0\), the polynomials are orthonormal only for a non-uniform weight, and this relation is approximate (see the page Fitting).

See also

Parameters:
  • x (numpy.ndarray) – The x Cartesian coordinates of the samples, with any shape (...).

  • y (numpy.ndarray) – The y Cartesian coordinates of the samples, with the same shape as x.

  • values (numpy.ndarray) – The sampled values \(f_k\), with the same shape as x.

  • order (Integral) – The maximum radial order \(N \geq 0\) of the fit.

  • Rx (Optional[Real], optional) – The semi-axis \(R_x\) of the ellipse along its x axis. Must be finite and strictly positive. Default is 1.0.

  • Ry (Optional[Real], optional) – The semi-axis \(R_y\) of the ellipse along its y axis. Must be finite and strictly positive. Default is 1.0.

  • x0 (Optional[Real], optional) – The x coordinate \(x_0\) of the center of the ellipse. Must be finite. Default is 0.0.

  • y0 (Optional[Real], optional) – The y coordinate \(y_0\) of the center of the ellipse. Must be finite. Default is 0.0.

  • alpha (Optional[Real], optional) – The rotation angle \(\alpha\) of the ellipse in radians. Must be finite. Default is 0.0.

  • h (Optional[Real], optional) – The ratio \(h\) of the inner to the outer semi-axes, in \([0, 1)\). Default is 0.0 (full ellipse).

  • weights (Optional[numpy.ndarray], optional) – The weights \(w_k \geq 0\) of the samples, with the same shape as x. 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.xy_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 normalized basis. Default is False.

  • return_residuals (bool, optional) – If True, the residuals \(f_k - \sum_{j} c_j Zxy_j(x_k, y_k)\) are also returned. Default is False.

  • ellipse (None is accepted for all the parameters of the)

  • value. (and means the default)

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_residuals is True. The residuals with the shape of x, as numpy.float64. They are set to numpy.nan at the ignored points.

Raises:
  • TypeError – If x, y, values or weights can not be converted to a floating point array, if order is not an integer, if a parameter of the ellipse is not a real number, or if use_recursive, normalize or return_residuals is not a boolean.

  • ValueError – If the arrays do not have the same shape, if order is negative, if a parameter of the ellipse is not finite, if Rx or Ry is not strictly positive, if h is not in \([0, 1)\), 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 annulus). The minimum norm solution is then returned.

Examples

Fit a wavefront measured on an off-axis (elliptic) pupil up to the order 4:

import numpy
from pyzernike import xy_zernike_fit, xy_zernike_polynomial

pupil = dict(Rx=1.0, Ry=0.7, alpha=numpy.deg2rad(30.0))
X, Y = numpy.meshgrid(numpy.linspace(-1, 1, 200), numpy.linspace(-1, 1, 200))

W = 0.3 * xy_zernike_polynomial(X, Y, n=2, m=0, **pupil) - 0.1 * xy_zernike_polynomial(X, Y, n=3, m=1, **pupil)
coefficients = xy_zernike_fit(X, Y, W, order=4, **pupil)  # NaN outside the pupil are ignored
# coefficients[4] ~ 0.3 (Z_2^0) and coefficients[8] ~ -0.1 (Z_3^1)

Evaluate the fitted wavefront and its slopes:

from pyzernike import xy_zernike_evaluate

fitted = xy_zernike_evaluate(coefficients, X, Y, **pupil)
dW_dx, dW_dy = xy_zernike_evaluate(coefficients, X, Y, x_derivative=[1, 0], y_derivative=[0, 1], **pupil)