# Finding perfect focus with V-Curve fitting based on a hyperbolic function

This article is about finding the best possible telescope focus by approximating a V-Curve with a hyperbolic function. A V-Curve is the result of moving the focus of a telescope from an outside focus position into the focus and then further into the same direction out of focus again. As probably not surprising this curve is called V-Curve since it is similar to a V. The focus can be measured using different measures - for example the "Half-Flux Diameter" (HFD) or the "Full width at half maximum" (FWHM). In this case the HFD is used. This way a V-Curve comes into existence where the focus position is shown on the x-axis and the focus measure (HFD) on the y-axis (right figure).

From this V-Curve (or a set of those V-Curves) one wants to determine the ideal focus position. In order to do so, more abstract criteria like slopes and / or local minimum need to be extracted from the individual data points. This can be achieved by approximating a function to the data points as good as possible and then taking the respective curve parameters from the resulting function as such criteria. With the implementation presented below the result shown in the left figure can be achieved. What may be the advantages of a hyperbolic curve fitting?

• Compared to the approach when fitting with two straight lines, the problem of deciding which data points should be assigned to the "left" and which to the "right" straight line no longer exists.
• Because of the round shape at the bottom fitting two straight lines, is problematic since the program has to decide which data points close to the best focus position should still be part of the fitting and which should not. This problem no longer exists when using a hyperbolic curve.
For a V-Curve as shown above there could be different ways to approximate it. They all have different advantages and disadvantages. For example one could try to use a parable or straight lines. In this approach I want to show the approximation to the V-Curve with a hyperbolic curve.

Consider the following explicit function:  It describes a hyperbolic curve.  is the variable and , ,  and  are parameters which influence position and shape of the curve. The following image illustrates an example of how this hyperbolic curve could look like for different parameter sets.

The figure above helps to understand the effect of the different function parameters:  &  define the "opening angle" of the hyperbolic curve,  shifts the curve to the left or right on the x-axis and  shifts the curve up- or down along the y-axis.

As one can see this is quite close to how a typical V-Curve of a telescope focusing run looks like. To the left and to the right the slope is constant and in the center it looks a little like a parable. The left and the right tangent can also be easily calculated by calculating the limiting values:

• Left: 
• Right: 
The two tangents are also shown in the pictures above (green and turquoise).

For a given set of  data points (in this context focuser position and HFD value) now the parameters , ,  and  of the hyperbolic curve have to be determined so that the resulting curve is as close to the data points as possible. In order to achieve this the Levenberg-Marquart algorithm is used. To avoid reinventing the wheel an implementation of this algorithm from the GNU Scientific Library (GSL) is used.

This algorithm requires the partial derivative of  to each parameter. The different partial derivatives make then up the Jacobi matrix . The partial derivatives are listed below. A repeating expression is substituted with  to simplify the expressions in the end.  will be also reflected in the code below as a function named phi().









This leads to the Jacobi matrix   which will be used in the C++ program below to calculate an approximated hyperbolic curve.

In addition the first and the second derivative of  to  is required:





One important thing to note is that the shape of the V-Curve more or less characterizes the optical system consisting of focus, telescope and camera. This is independent of the current, absolute best focus position. That also means: Knowing the V-Curve of a system allows a quite good prediction of the best absolute focus position by just having a few data points measured. This can be used to quickly focus a system and move the more time consuming procedure of recording a whole V-Curve to the very beginning. Once such a V-Curve exists for a system, it can be reused and one can save a lot of time during the focusing process.

With those thoughts we can now start to implement something:
Executing the program fits the hyperbolic curve to the V-Curve data: The result of the execution are the curve parameters , , , and . Those parameters have been entered into the following gnuplot script: The script can be downloaded here. The overall result is shown below. As one can see the calculated hyperbolic curve fits the data points quite good. The red line indicates the theoretically optimal focus position which is expressed by parameter . Last updated: June 18, 2018 at 13:44 pm