## 5 Statistical Algorithm for S Turns
### Statistical Algorithm  
Using the first layer as an example to illustrate the S-turn statistical algorithm for each sublayer signal, for the lower half signal \(\Psi_{11}\), based on the theory in Section 3.2, solve the  
Figure 5: Layering algorithm. set \(\hat{T}_{0}^{11}=\{\hat{t}_{1},\hat{t}_{2},\cdots,\hat{t}_{i},\cdots,\hat{t}_{i_{0}}\}\), which is the set of de-redundant median-crossing marks. The subscript or superscript \"\(11\)\" represents the number of the solved sublayer and \(\hat{n}_{0}\) is the length of \(\hat{T}_{0}^{11}\). It should be noted that for the lower \(\Psi_{11}\) of \(\Psi_{0}\), there are hovering and S-turning marks in set \(\hat{T}_{0}^{11}\) which have already been counted by set \(H\) and set \(S\). That is to say, even \(\hat{T}_{0}^{11}\) after removing redundant median-crossing points with yaw angle deviations below threshold \(c\) for the sublayer sequence, there are still redundant points that have been counted by the previous layers, as shown in Figure 6. Therefore, further redundancy removal algorithms are needed for \(\hat{T}_{0}^{11}\). Before designing the algorithm, we noticed that an S turn has paired rising and falling edges; therefore, one can mark an S turn using only the median-crossing point of its rising edge, as shown in Figure 7.  
For \(\forall\hat{t}_{i}\in\hat{T}_{0}^{11}\), if the yaw angle at \(\hat{t}_{i}\) is in the rising edge, \(\hat{t}_{i}\) needs satisfy the following inequality:  
\[\Psi_{11}(\hat{t}_{i}+1)-\Psi_{11}(\hat{t}_{i}-1)>0. \tag{7}\]  
Then, we can determine whether median-crossing mark \(\hat{t}_{i}\) of the rising edge is a redundant point. From Figure 6, it can be seen that if \(\hat{t}_{i}\) is a redundant point, then, in the open interval (\(\hat{t}_{i}\),\(\hat{t}_{i+1}\)), there exists one or more points of which yaw angle equals the median of the previous layer, and because \(\Psi_{11}\) is the lower half signal of the previous layer, for the angle segment \(\Psi_{si}^{11}=\{\Psi_{11}(\hat{t}_{i}+1),\Psi_{11}(\hat{t}_{i}+2),\cdots,\Psi _{11}(\hat{t}_{i+1}-1)\}\), it must satisfy the following:  
\[\max(\Psi_{si}^{11})=\overline{\Psi}_{0}. \tag{8}\]  
Figure 6: The redundant median-crossing points in a sublayer. Figure 7: The rising edge on a sublayer. For example, \(\hat{t}_{1}\) in Figure 8 will be removed from \(\hat{\mathcal{T}}_{0}^{11}\). Upon removing the redundant elements in set \(\hat{\mathcal{T}}_{0}^{11}\), and retaining only the elements that satisfy Equation (7) but not Equation (8), a set of median-crossing points can be obtained by removing the marks related to the previous layer. The new set is annotated as \(S_{11}\) if the length of \(S_{11}\) is \(N_{S}^{11}\); then, \(N_{S}^{11}\) is the number of S turns in the current sublayer. For the upper half signal \(\Psi_{12}\), in order to use the above algorithm of the lower \(\Psi_{11}\), let \({\Psi_{12}}^{\prime}=-\Psi_{12}\); then, similar to \(\Psi_{11}\), the number \(N_{S}^{12}\) of S turns on \({\Psi_{12}}^{\prime}\) can be obtained as well. Because \({\Psi_{12}}^{\prime}=-\Psi_{12}\), \(N_{S}^{12}\) is the number of S turns in the upper half signal \(\Psi_{12}\). After obtaining the number of S turns of the lower \(\Psi_{11}\) and the upper \(\Psi_{12}\), the total number of S turns in the first layer can be expressed by \(N_{S}^{1}=N_{S}^{11}+N_{S}^{12}\).