# LaRaWAPS: A Wide-Area Positioning System Based on LoRa Mesh
## 4 System Design
### LoRa Ranging and Localization
#### 4.3.2 Ranging Algorithm  
The positioning function of LoRaWAPS is based on the ranging result of anchors and targets. Therefore, it is very important to process the ranging data to make the distance estimation more accurate. The Semtech SX1280 transceiver has a built-in ranging engine, which performs time-of-flight measurements between a pair of transceiver radios. The distance between the master node and the slave node can be calculated according to the result of the timer:  
\[d=N\frac{c}{2^{12}B}. \tag{3}\]  
In the equation, \(N\) is the result of the timer, \(B\) is the transmission bandwidth in Hz, and \(c\) is the speed of light. We first perform ranging operation and ranging correction according to Semtech SX1280 datasheet [48]. To improve distance measurement accuracy, ranging is carried out on 40 different channels successively, and timeout re-transmission and exit mechanisms are used to cover as many channels as possible. TOF and RSSI for 40 channels can be obtained. Firstly, we filter the TOF data to eliminate the extreme cases in the data. Then, the RSSI data is filtered and the corresponding data of the channel with large signal attenuation is removed. Secondly, considering the influence of signal refraction and other non-Gaussian noises, the authors propose to use the K-Means clustering algorithm to divide NLOS and Line of Sight (LOS) results. K-Means is a clustering algorithm based on the division of sample sets. We cluster the data of the remaining channels into multiple clusters, in which each cluster corresponds to the data of different channels. The center point of the cluster corresponding to the LOS channel has the minimum TOF value. We then average the results classified as LOS channels to obtain the output. Finally, because the drift of the LoRa hardware clock will lead to the deviation of frequency, some nonlinear results will appear in the close-range measurement, leading to the decrease in distance estimation accuracy, which needs to be corrected [49]. The ranging algorithm is shown in Algorithm 1. First of all, assume the data of the \(t\)-th channel is defined as \(p_{t}=(d_{t},r_{t})\), \(d_{t}\) is the TOF ranging data calculated according to Formula (3), and \(r_{t}\) is RSSI value. \(\boldsymbol{\Gamma}=\left\{p_{1}^{(\boldsymbol{\Gamma})},p_{2}^{(\boldsymbol{ \Gamma})},\ldots,p_{N(\boldsymbol{\Gamma})}^{(\boldsymbol{\Gamma})}\right\}\) represents a set of data and \(p_{n}^{(\boldsymbol{\Gamma})}\) is an element of the set. Filter \(\boldsymbol{H}\) is used for data line filtering. Filter \(\boldsymbol{H}\) can be a median filter, mean filter, or other similar filter. \[\boldsymbol{\Gamma}^{\prime}\leftarrow\boldsymbol{H}\boldsymbol{\Gamma}. \tag{4}\]Secondly, considering the influence of signal multipath propagation and other non-Gaussian noise, K-Means clustering is used to divide the data in the dataset \(\mathbf{\Gamma^{\prime}}\) into \(M\) classes. \(\mathbf{C}_{k}\) represents a cluster resulting from K-Means clustering. \(p_{c}^{k}\) represents the center point of the cluster \(\mathbf{C}_{k}\). \[p_{c}^{k}=E(\mathbf{C}_{k}), \tag{5}\]  
\(d_{c}^{k}\) and \(r_{c}^{k}\) are the TOF ranging data and the RSSI value in the center point \(p_{c}^{k}\). Find the cluster that has the minimum ranging distance \(d_{c}^{k}\), the data corresponding to the line-of-sight channel can be extracted. \[\hat{d}=\underset{k}{min}d_{c}^{k}, \tag{6}\]  
where \(\hat{d}\) is the estimated distance. Finally, the estimated distance is modified to obtain the final result \(\hat{d}\). \[\hat{d}= \begin{cases}\hat{d},&\hat{d}>100\text{ m},\\ \gamma\hat{d}+(1-\gamma)10^{\left(\frac{\gamma_{n}-\hat{d}}{100}\right)},\hat{ d}\leq 100\text{ m}.\end{cases}  \tag{7}\]  
Among them, \(\gamma\) is selected by experimental analysis, \(n\) is the road loss parameter, and \(r_{0}\) represents the RSSI value at a distance of 1 m.  
```
Input: Total number of channels available for ranging \(N\), filter \(\mathbf{H}\), filter \(M\).