## 3 Algorithm  
The numerical reconstruction of the all-in-focus height map is performed in four steps, as follows:  
1. Temporal phase-shifting-determination of the global phase shifts between single images induced by the piezoactuator and the calculation of the complex wave [39; 40; 41].
2. Propagation of the complex wave \(C_{\lambda}(x,y)\) and calculation of the synthetic wavefronts [39].
3. Shape-from-focus (described in this section): 1. Maximum filter (Section 3.1) [38] 2. Gaussian fit (Section 3.2)
4. Iterative combination of focus criterion and finer artificial wavelengths [15; 38].  
We propose a method to calculate an extended focus image based on the phase noise, and combine the respective synthetic wavefronts using this output of the algorithm.  
As input data, the phase information at arbitrary (even multiple) wavelengths can be processed. A pre-filter with radius 2 has been found to effectively reduce camera noise on the complex holograms. Firstly, the phase noise \(\Delta_{x}\varphi\) and \(\Delta_{y}\varphi\) in \(x\) and \(y\) direction of a synthetic wavefront at reconstruction distance \(z\), is calculated.  
\[\Delta_{x}\varphi\ =\ (\varphi_{x+1}-\varphi_{x})\ mod\ 2\pi \tag{2}\] \[\Delta_{y}\varphi\ =\ (\varphi_{y+1}-\varphi_{y})\ mod\ 2\pi\]  
The gradient vector  
\[\
abla\varphi\ =\ \left(\begin{array}{c}\Delta_{x}\varphi\\ \Delta_{y}\varphi\end{array}\right) \tag{3}\]can be normalized as follows:  
\[\hat{\
abla}\varphi\ =\ \frac{\
abla\varphi}{\left|\
abla\varphi\right|}\ =\ \frac{ \left(\begin{array}{c}\Delta_{x}\varphi\\ \Delta_{y}\varphi\end{array}\right)}{\sqrt{\left(\Delta_{x}\varphi\right)^{2} +\left(\Delta_{y}\varphi\right)^{2}}} \tag{4}\]  
With a moving average filter (e.g., radius \(r=8\)), the amplitude of the phase smoothness \(s_{z}(x,y)\) for each pixel at reconstruction distance \(z\) is used as a focus criterion, as follows:  
\[s_{z}(x,y)\ =\ \left|\begin{array}{c}\\ i\ =\ -r\ldots r\\ k\ =\ -r\ldots r\end{array}\hat{\
abla}\varphi(x+i,y+k)\frac{1}{(2r+1)^{2}} \right| \tag{5}\]  
Figure 2 illustrates the principle of the algorithm graphically, as follows: Panel (a) shows a synthetic phase map with linearly increasing noise from left to right. Panel (b) shows the anisotropic normalized phase gradient \(\hat{\
abla}\varphi\) for the low-noise (green) and high-noise (orange) areas, taken from the synthetic data in panel (a), respectively. Areas of low noise are characterized by a uniform phase gradient direction, whereas noisy areas appear as random phase vectors. In the following, we use a filtered sum of these gradient vectors \(\sum\hat{\
abla}\varphi\), shown in panel (c), as our phase noise or rather, the phase smoothness criteria. It can be interpreted as the filtered information inside the moving average kernel. A large gradient vector sum indicates an area of smooth phase and thus focused pixel. In out-of-focus areas, the gradient vectors are noisy and the \(\sum\hat{\
abla}\varphi\) is shorter.  
Figure 3 shows the output \(s_{z}\) of the algorithm at a single point \((x,y)\), and different reconstruction distances \(z\) for a gear sample. The graph clearly shows a maximum at around \(z=6\) mm.  
To find the in-focus distances within the stack of images at propagation distances \(z\), a minimum phase-noise value of the stack of calculated propagation distances \(z\) for each pixel coordinate \((x,y)\) has to be found. In the following, two methods for the synthesis of a phase map with extended depth of field are proposed.  
Figure 2: Algorithm principle from phase map input to phase noise output. (a) Simulated phase map with linearly increasing noise from left to right; (b) phase gradient direction for low-noise and high-noise points in a \(6\times 6\) px\({}^{2}\) area; and (c) sum of gradient direction vectors \(\sum\hat{\
abla}\varphi\) for low-noise (top) and high-noise (bottom) points. A long sum vector indicates an area of smooth phase.