## 2 Principle
### Modified K-Means Clustering Algorithm  
The k-means algorithm is one of the most commonly used clustering methods due to its simplicity and efficiency. The basic idea is to partition data samples into k clusters based on the calculated distance between each element and centroid [22; 23; 24]. As the practical FSO channels suffer from many factors such as ambient light noise and multi-path dispersion [20], this method is able to optimize the decision threshold of the time-varying received signal and reduce the error rate in this noisy environment. Furthermore, as analyzed in Section 2.1, a notable clustering feature is observed in terms of the element distribution of optical Morse signals, which can be effectively differentiated using the k-means clustering algorithm. The detailed procedure in this case is as follows:  
Let \(X\) denote the sampled voltage data set, and initialize \(k\) cluster centroids \(C=[C_{1},C_{2},\ldots C_{k}]\) randomly from \(X\), compute the Euclidean distance between each voltage sample and centroid to find the positions of the clusters' centroids minimizing the cost function [21], and then assign each sample to its closest cluster centroid, defined as  
\[j=\underset{1\leq i\leq k}{\arg(\min}\|X-C_{i}(m)\|^{2}), \tag{2}\]  
Figure 3: Time domain of optical Morse signal with additive white Gaussian noise (AWGN). Figure 2: Histogram of (**a**) space and (**b**) dot–dash duration distributions. where \(\|\bullet\|\) represents the Euclidean distance, \(C_{i}(m)\) is the \(i\)-th cluster centroid during the \(m\)-th iteration, \(X\)\(\in\)\(S_{j}(m)\), and \(S_{j}(m)\) denotes the cluster with the centroid of \(C_{j}(m)\). Then, update the centroid of each cluster by calculating the mean as follows:  
\[C_{i}(m+1)=\frac{1}{n_{i}}\sum_{X\in S_{i}(m)}X,\ (i=1,2,\ldots,k), \tag{3}\]  
where \(C_{i}(m+1)\) is the \(i\)-th cluster centroid during the \((m+1)\)-th iteration, \(n_{i}\) stands for the number of data samples in the \(i\)-th cluster, \(S_{j}(m)\) denotes the cluster with the centroid of \(C_{i}(m)\), and \(X\) represents all the data samples in cluster \(S_{j}(m)\). If the updated centroids remain the same, it indicates that convergence is reached and then stops, described as  
\[\text{abs}\left(\left[C_{i}(m\ +\ 1)\ -\ C_{i}(m)\right]\right)<\text{abs}( \left[C_{i}(m)\ -\ C_{i}(m\ -\ 1)\right])\times 2\%,\ (i=1,\ 2,\ \ldots,\ k). \tag{4}\]  
Otherwise, repeat the step until it reaches convergence. It is noted that there are also another enhanced clustering method and modified version of the Euclidean distance that yield good performance for high-dimensional datasets [25; 26; 27]. The data to be processed by the proposed automatic recognition algorithm include voltage value and level duration, both of which are one-dimensional datasets. Thus, for the ease of implementation in the embedded system, the simplest k-means method and Euclidean distance are chosen in this work, which can also effectively meet the requirement. In order to adapt this algorithm to the application scenario, we propose several modifications to the clustering process to improve the recognition accuracy. The optimization of the k-means clustering algorithm is as follows:  
1. Cluster Centroid Initialization: The digital filter will cause an amplitude level hopping effect in the head and tail of the passing signal, leading to significant deviation. Thus, this part of the signal is designed to be abandoned in this case while selecting the cluster centroid during the iteration. 2. Selection Sort-Assisted (Figure 4): It can be easily known that the obtained \(k\) cluster centroids are in an unordered state. As the purpose of cluster analysis is to distinguish the signals with different durations in the Morse decoding, it is necessary to sort the final \(k\) centroids so that the device can effectively identify different clusters. Considering the small quantity of centroids and easy implementation in this case, we exploit the widely used selection sort algorithm to rearrange the centroids in a certain order [28].