# Real-Time Indoor Positioning Approach Using iBeacons and Smartphone Sensors
## 3 Research Method
### BLE-Based Method
\begin{bmatrix}\frac{x_{0}-x^{1}}{\rho_{0}^{1}},\frac{ y_{0}-y^{1}}{\rho_{0}^{1}},\frac{z_{0}-z^{1}}{\rho_{0}^{1}}\\ \frac{x_{0}-x^{2}}{\rho_{0}^{2}},\frac{y_{0}-y^{2}}{\rho_{0}^{2}},\frac{z_{0}- z^{2}}{\rho_{0}^{2}}\\ \dots\\ \frac{x_{0}-x^{n}}{\rho_{0}^{n}},\frac{y_{0}-y^{n}}{\rho_{0}^{n}},\frac{z_{0}- z^{n}}{\rho_{0}^{n}}\end{bmatrix} ,X_{0}= \begin{bmatrix}x_{0}\\ y_{0}\\ z_{0}\end{bmatrix}  \tag{7}\]  
In Equation (7), \((x^{n},y^{n},z^{n})\) refers to the coordinates of beacon \(n\), \(d^{n}\) represents the measured distance between beacon \(n\) and the current location, and \(\rho_{0}^{n}\) stands for the initial guess of this distance. Accordingly, the solution of residuals is presented in Equation (8). The \(n\times n\) matrix \(P\) is the weight matrix, which reflects the importance of each distance adopted. These distances are all derived from the related RSSI values (always negative), and we consider that a RSSI with a high value contains high reliability. Thus, we input the initial \(P\) as a diagonal matrix whose diagonal elements are set to \(\frac{1}{RSSI^{2}}\). \[\hat{X}=(B^{T}PB)^{-1}B^{T}Pl \tag{8}\]  
After getting the residuals of the coordinates, we can readily derive the coordinates of the current location by adding up the residuals to the initial values (Equation (9)). The vector \([x,y,z]^{T}\) indicates the final estimate of the current location. \[ \begin{bmatrix}x\\ y\\ z\end{bmatrix} =X_{0}+\hat{X} \tag{9}\]  
It should be noted that the calculation process in Equation (8) is iterative. Considering the non-convergence issue of iteration, we set thresholds for the iterated \([x,y,z]^{T}\) as the termination condition. For instance, the iteration would stop when two adjacent calculation results \(x^{\prime}\) and \(x^{\prime\prime}\) contain a large difference over than the threshold (e.g., 2 m). In this way, we ensure this iteration can stop and the derived coordinates are not exaggerated. **Fingerprinting**. Fingerprinting is another RSS-based localization technique adopted in this paper. This method is introduced in Section 2. Here, we aim to use the NLOS information in the database (radio map) to correct trilateration results. We select a deterministic matching method for positioning, that is, the comparison of measured RSSI vector and reference RSSI vector to rank the correlation degree. The implementation of fingerprinting consists of several steps. First, in offline phase, we set up predefined reference points to collect the database (i.e., the RSSI vector of iBeacons on each point). Second, in online phase, we compare the current RSSI vector with the database and calculate the location coordinates by weighted K Nearest Neighbor (WKNN) method. As the RSS values of an iBeacon always fluctuate, we do not select the minimum Euclidean distance between RSSI vectors as the primary criterion of location matching. The criterion of minimum Euclidean distance is more prone to wrong estimate due to data noises and sensor differences of smartphones. \[cos(\theta)=\frac{V_{i}\bullet V_{j}}{\|V_{i}\|\|V_{j}\|} \tag{10}\]  
More specifically, we use _cosine similarity_ to determine the minimum closeness of RSSI vectors regarding locations (Equation (10)). When two RSSI vectors share the same size, their cosine similarity is calculated by their dot product and modules (\(\|V_{i}\|\) and \(\|V_{j}\|\)). In this way, we can check whether the trend of these RSSI vectors is consistent, and include these similar RSSI vectors to reckon the location. Compared to the minimum Euclidean distance, cosine similarity is less sensitive to RSS variations. It can provide more candidates around the genuine location and thus avoid the outliers even with the minimum Euclidean distance. In addition, distinct smartphone sensors often receive different RSS values of the same iBeacon. In this case, we can perceive the overall difference via the cosine similarity between the RSSI vector of a phone and that of the database.