# Zernikes¶

Prysm supports two flavors of Zernike polynomials; the Fringe set up to the 49th term, and the Noll set up to the 36th term. They have identical interfaces, so only one will be shown here.

Zernike notations are a subclass of Pupil, so they support the same arguments to __init__,

[1]:

%matplotlib inline
from prysm import FringeZernike, NollZernike
p = FringeZernike(samples=123, dia=456.7, wavelength=1.0, opd_unit='nm', mask='dodecagon')


There are additional keyword arguments for each term, and the base (0 or 1) can be supplied. With base 0, the terms start at Z0 and range to Z48. With base 1, they start at Z1 and range to Z49 (or Z48, for Standard Zernikes). The Fringe set can also be used with unit variance via the norm keyword argument. Both notations also have nice print statements,

[ ]:

p2 = FringeZernike(Z4=1, Z9=1, Z48=1, base=0, norm=True)
p2


Notice that the RMS value is equal to sqrt(1^2 + 1^2 + 1^2) = sqrt(3) = 1.732 ~= 1.722. The difference of ~1% is due to the array sizes used by prysm by default, if increased, e.g. by adding samples=1204 to the constructor, the value would converge to the analytical one.

A Zernike pupil can also be initalized with an iterable (list, tuple…) of coefficients,

[ ]:

import numpy as np
terms = np.random.rand(49)  # 49 zernike coefficients, e.g. from a wavefront sensor
fz3 = FringeZernike(terms)


Zernike instances have a truncate method which discards terms with indices higher than n. For example,

[ ]:

fz3.truncate(16)


this is less efficient, however, than simply slicing the coefficient vector,

[ ]:

fz4 = FringeZernike(terms[:16])


and this slicing alternative should be used when one is sensitive to performance.

The top few terms may be extracted,

[ ]:

fz4.top_n(5)


or the terms listed by their pairwise magnitudes and clocking angles,

[ ]:

fz4.magnitudes


These things may be (bar) plotted;

[ ]:

fz4.barplot(orientation='h', buffer=1, zorder=3)
fz4.barplot_magnitudes(orientation='h', buffer=1, zorder=3)
fz4.barplot_topn(n=5, orientation='h', buffer=1, zorder=3)


orientation controls the axis on which the terms are enumerated. h results in vertical bars, v is also accepted, as are horizontal and vertical. buffer is the number of terms’ worth of spaces left on each side. The default of 1 leaves a one bar margin. zorder is passed to matplotlib – the default of 3 places the bars above any gridlines, which is an aesthetic choice. Matplotlib has a general default of 1.

If you would like direct access to the underlying functions, there are two paths. prysm._zernike contains functions for the first 49 (Fringe ordered) Zernike polynomials, for example

[ ]:

from prysm._zernike import defocus


each of these takes arguments of (rho, phi). They have names which end with _x, or _00 and _45 for the terms which have that naming convention.

Zernike functions are cached by prysm’s internal routines,

[ ]:

from prysm.zernike import zcache
# 8 is the index into prysm.zernike.zernikes, which loosely follows the Fringe ordering
zcache(8, norm=True, samples=128)
zcache.clear()  # you should never have to do this unless you want to release memory


This cache instance is used internally by prysm, if you modify the returned arrays in-place, you probably won’t like the result. You can create your own independent instance,

[ ]:

from prysm.zernike import ZCache
my_zcache = ZCache()


See Pupils for information about general pupil functions.