# Slope Integration methods (Zonal and Modal)

Hello ELE Community! Lately I’ve been working a bit on slope integration methods, and am quickly realizing how interesting/complicated they can get. The Zonal methods (like Southwell Integration) work incredibly well, even for strange, non symmetrical apertures. But, lets say you wanted a polynomial fit (Modal). You’re pretty much stuck with Chebyschev (rect apertures) or Zernike vector polynomials (circular apertures) in order to remain orthogonal (as far as I know), then integrating those polynomials to get surface sag. I’ve tested these on other stranger polygonal apertures, and they don’t perfectly match results from Southwell integration (especially in the corners). I’m fairly certain this is due to the aperture shape.
I’m curious if anyone else in the ELE community has used other types of polynomials for slope fitting, or has used them on non symmetric apertures!

2 Likes

Hi @Joel_David_Berkson, I meant to get to this earlier and got caught up in the holidays, so my apologies. The Southwell integration method can have some serious issues particularly for non-symmetric or ‘funny’ shaped apertures. This is strongly apparent in cases were there are a) not a number (NaNs) inside of your aperture, where the aperture has an edge (polygons, rectangles, etc), and c) where there is ‘bad’ data in your slope data. This is because the zone is what is being considered in the integration approach, and this has ‘bleed out’ effects, where one point can impact surrounding data.

As you said, Chebyshev polynomials are particularly convenient for rectangular data, and Zernike polynomials are useful for circular aperture data. The historical limit to these approaches are that is can be really resource intensive to generate enough polynomials to fit high frequency data AND that there can be a risk of overfitting. As an extra ‘and’, you also should be cautious of how the polynomials are being generated. If they are hard-coded for example, did the author check up to whatever order they coded to verify there are no errors? If they are recursively generated, is there any error that may stack on higher orders?

Maham Aftab, as you are familiar with, came up with the recursive Chebyshev Gradient polynomial set which does a great job of generating large numbers of polynomials, a great solution for rectangular apertures. You can also apply these to polygon apertures, but try to consider how this is impacted by the rectangular orthonormality of the base set (http://www.loft.optics.arizona.edu/documents/journal_articles/Aftab2019_Article_ModalDataProcessingForHighReso.pdf). A few comments on this approach, check in the slope domain to see if when you code it there is a scaling issue. The weighting factor referenced in the paper can be dropped when you code it. Recursive generation of these sets using a matrix approach is just stupidly resource intensive when coded in Matlab, I would recommend a vector approach or coding it in some other language.

You can also now find various fast Zernike polynomial set generators for fitting data. See:

Specifically, I think this recent article may be particularly useful to you:

Finally, @bdube has made an excellent open source optics engine called Prysm (https://prysm.readthedocs.io/en/stable/index.html) which offers many general optics tools, including a fast and stable wavefront fitting tool. I leave the technicalities of how this could be usable to you, but the tool offers Zernike, Chebyshev, and Lagrange fitting, all relating back to the Jacobi bases these share ( How Zernike, Legendre, and Chebychev Polynomials relate to Jacobi Polynomials). This is also something we are focused on providing in our toolset as we move forward (we also have suffered through slope fitting ).

TL;DR: Slope fitting is complex, check out Maham’s method and particularly check out this recent paper for a Zernike approach: https://www.osapublishing.org/oe/fulltext.cfm?uri=oe-28-13-18876&id=432516

This is a great question that I hope other members will chime in on. I will plan to write up a guide though in the near future on slope fitting methods!

o/ @Joel_David_Berkson fwiw, in the next release of prysm (pre-alpha now), I simplified and isolated all of the polynomial code (and made it ~5x faster, but I digress) – prysm/prysm/polynomials at v-020-dev · brandondube/prysm · GitHub

There are Hopkins’ Ws, Zernikes, Chebyshevs, Legendres, Jacobi, Qbfs, Qcon, and 2D-Q. You can use the mathematical machinery I am to compute derivatives or sums of derivatives similarly quickly and expressively. See, e.g., appendix of “Robust and fast computation for the polynomials of optics,” G.W. Forbes and “Characterizing the shape of freeform optics.” You can also do the reverse, turn coefficients for the slopes of any orthogonal polynomial basis into coefficients for the polynomials themselves. Then you could either compute the gradients (which are orthogonal for the Qs, but not for the rest) and do least squares, or you could use a nonlinear optimizer (say, BFGS, `scipy.optimize.minimize, method='L-BFGS-B'`) to find the gradient coefficients which is more likely to find the global minimum than OLS for problems that are convex, but not linear.

You can also use the machinery of Forbes’ papers to orthogonalize the core of these polynomials over other apertures, and even compute the coefficients for, say, a hex aperture “hexikes” from a circular aperture (Zernikes).

There are an infinite number of ortho polynomial sets for even 1D functions, but in 2D (or 3D!) I guess that’s infinite^2. For example, you could replace the radial function of the zernikes, which is a particular Jacobi polynomial and change of basis, with Chebyshev polynomials by foregoing the change of variables and shifting `n,m` for the jacobi by -0.5 (first kind) or +0.5 (second kind). Or fit radial chebyshevs, which no one is doing.

Singularity flavored errors (sparse bad pixels) will have minimal impact on fitting modes, even if the modes are non-orthogonal. Discontinuities will ‘break’ everything if the discontinuities exist in ‘sensitive’ regions of the polynomials (e.g., Zernikes blow up at the edge, so error in gradient calculation there will hurt the fit badly).

If you want to deep dive this and submit a PR to prysm to add the gradients of all of those polynomials and the functions to convert poly to slope I’ll accept it It’s all in Forbes’ papers