FWHM minimization
From a series of images, the FWHM of a single star is determined. The focus is then the position of minimal FWHM. Propbaly the easiest method to calculate the FWHM of a star (or a measure close enough to it), is by assuming a Gaussian point-spread-function (PSF):
psf(r)=I0·exp(-r2/2σ2), summing psf(r) as Σ and psf2(r) as ΣΣ over the entire range and simply writing
FWHM=√(2ln(2)/π)·Σ2/ΣΣ
Outliers should be rejected to stabilize the search, an initial focal position can be derived from a temperture-depending model.
Measuring the FWHM is strongly affected by varying seeing conditions, thus a parabola is fitted through the points. Outlier removal is advisable (in the example the measurement with FWHM≈14px, f≈43.25 has to be discarded) . The minimum of the fitting parabola is called the optimal focus.