.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_gallery/wavefront_elliptic_pupil.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr__gallery_wavefront_elliptic_pupil.py: Wavefront analysis on an elliptic pupil ====================================================================================================== This example measures the aberrations of a wavefront on an off-axis (elliptic) pupil: the wavefront is fitted with the Zernike polynomials, its RMS is obtained from the coefficients, and its slopes are computed exactly from the fitted coefficients. .. GENERATED FROM PYTHON SOURCE LINES 12-19 Synthetic wavefront on an elliptic pupil ----------------------------------------------------------------- For an off-axis field, the effective pupil of a circular aperture is an ellipse, described by its semi-axes :math:`R_x, R_y` and its rotation angle :math:`\alpha`. The wavefront is a sum of defocus, astigmatism and coma (coefficients in the orthonormal basis, ``normalize=True``), evaluated with :func:`pyzernike.xy_zernike_evaluate` and measured with noise on a regular grid. .. GENERATED FROM PYTHON SOURCE LINES 19-35 .. code-block:: Python import numpy import matplotlib.pyplot as plt import pyzernike rng = numpy.random.default_rng(0) pupil = dict(Rx=1.0, Ry=0.7, alpha=numpy.deg2rad(30.0)) order = 4 true_coefficients = numpy.zeros(pyzernike.zernike_polynomials_count(order)) true_coefficients[pyzernike.zernike_order_to_index(n=[2, 2, 3], m=[0, 2, 1])] = [0.30, -0.15, 0.10] # waves x, y = numpy.meshgrid(numpy.linspace(-1, 1, 201), numpy.linspace(-1, 1, 201)) wavefront = pyzernike.xy_zernike_evaluate(true_coefficients, x, y, normalize=True, **pupil) # NaN outside the pupil wavefront += rng.normal(scale=0.01, size=wavefront.shape) .. GENERATED FROM PYTHON SOURCE LINES 36-42 Fit of the Zernike coefficients ----------------------------------------------------------------- :func:`pyzernike.xy_zernike_fit` solves the linear least squares problem on the valid points (the NaN outside the pupil are ignored). As the mapping onto the ellipse is affine, the polynomials stay orthonormal on the pupil, and :func:`pyzernike.zernike_rms` gives the RMS of the wavefront from the coefficients. .. GENERATED FROM PYTHON SOURCE LINES 42-54 .. code-block:: Python coefficients, residuals = pyzernike.xy_zernike_fit(x, y, wavefront, order=order, normalize=True, return_residuals=True, **pupil) n, m = pyzernike.zernike_index_to_order(list(range(len(coefficients)))) for n_j, m_j, c_j in zip(n, m, coefficients): if abs(c_j) > 0.02: print(f"Z_{n_j}^{m_j:+d}: {c_j:+.4f} waves") print(f"RMS from the coefficients: {pyzernike.zernike_rms(coefficients, normalize=True):.4f} waves") print(f"RMS of the measured data: {numpy.nanstd(wavefront):.4f} waves") print(f"RMS of the residuals: {numpy.nanstd(residuals):.4f} waves") .. rst-class:: sphx-glr-script-out .. code-block:: none Z_2^+0: +0.3001 waves Z_2^+2: -0.1500 waves Z_3^+1: +0.1001 waves RMS from the coefficients: 0.3501 waves RMS of the measured data: 0.3501 waves RMS of the residuals: 0.0100 waves .. GENERATED FROM PYTHON SOURCE LINES 55-61 Slopes of the fitted wavefront ----------------------------------------------------------------- The slopes :math:`\partial W / \partial x` and :math:`\partial W / \partial y` (measured for example by a Shack-Hartmann sensor) are computed exactly from the fitted coefficients with :func:`pyzernike.xy_zernike_evaluate`. .. GENERATED FROM PYTHON SOURCE LINES 61-70 .. code-block:: Python slope_x, slope_y = pyzernike.xy_zernike_evaluate(coefficients, x, y, x_derivative=[1, 0], y_derivative=[0, 1], normalize=True, **pupil) fig, axes = plt.subplots(1, 3, figsize=(12, 4)) for axis, image, title in zip(axes, (wavefront, slope_x, slope_y), ("Wavefront", "Slope along x", "Slope along y")): mappable = axis.imshow(image, extent=(-1, 1, -1, 1), origin="lower", cmap="RdBu_r") axis.set_title(title) fig.colorbar(mappable, ax=axis, shrink=0.8) plt.tight_layout() plt.show() .. image-sg:: /_gallery/images/sphx_glr_wavefront_elliptic_pupil_001.png :alt: Wavefront, Slope along x, Slope along y :srcset: /_gallery/images/sphx_glr_wavefront_elliptic_pupil_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.479 seconds) .. _sphx_glr_download__gallery_wavefront_elliptic_pupil.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: wavefront_elliptic_pupil.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: wavefront_elliptic_pupil.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: wavefront_elliptic_pupil.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_