# Application of Deep Convolutional Neural Networks and Smartphone Sensors for Indoor Localization
## 3 Materials and Methods
### Location Estimation
#### 3.4.2 Magnetic Localization  
Localization is done using the proposed mKNN approach. Algorithm 1 is proposed which takes the recognized scene information and magnetic samples from user phone and estimates the user's current location. The process followed in mKNN is described as well in Algorithm 1. We conduct the experiment in a three-story building, so the first task is to identify the floor of the user. This task is achieved with the recognized scene \(S_{r}\) using CNN classifier. Once a specific floor is identified, the magnetic database \(M\textsubscript{F}_{l}^{db}\) is loaded for the localization process. The second task of the recognized scene is to narrow down the search space. The recognized scene is employed to set the starting (\(db_{S}\)), and ending (\(db_{E}\)) of the magnetic database to match user magnetic samples (\(MF_{S}\)) against the database. Magnetic samples occasionally contain random noise and slight fluctuations which are removed using a low pass filter. Euclidean distance is calculated between the processed magnetic sample and magnetic database using:  
\[d_{E}=\frac{1}{n}\sum_{i=1}^{n}\sqrt{(MF_{S_{i}}-M\textsubscript{F}_{l}^{db})^ {2}} \tag{3}\]  
where \(d_{E}\) represents Euclidean distance between magnetic samples \(MF_{S_{1},\ldots,n}\) and magnetic database \(M\textsubscript{F}_{l,\ldots,n}^{db}\). Later, \(K_{1}\) number of location estimates (\(L_{E}\)) are selected with the lowest \(d_{E}\), where \(K_{1}\) is set to 11. The selected location estimates \(L_{E}\) have two parameters: Calculated Euclidean distance and location in longitude and latitude dimension and are denoted as \(L_{E_{loc,dis}}\). Initially selected location estimates are shown in Figure 6a, where the \(x\) and the \(y\) axis represent longitude and latitude, respectively, while the size of displayed locations shows the distance(error). The normal procedure of using KNN is to consider \(K\) neighbors and then calculate their centroid which serves as the predicted location. The neighbors are considered based on their distance(error) from the user sample. However, the neighbors can be very distant which increases the localization error. For example, if we look at Figure 6b we can see that the selected candidates based on distance do not form a spatial group and are separated distantly. The calculated centroid will be far away from the ground truth in this case. ```
Input: Recognized scene information (\(S_{R}\)) & magnetic samples (\(MF_{S}\))

Output: User's estimated location (\(L_{p}\))
```
1: identify floor using \(S_{r}\)
2: load magnetic database \(MF_{l}^{db}\)
3: set \(db_{S}\), and \(db_{E}\) // Set the search space for database
4:for\(h\longleftarrow\) 1 to 2 do
5:for\(i\longleftarrow\)\(db_{S}\) to \(db_{E}\)do
6:\(d_{E}\longleftarrow\)\(calEucDis(MF_{S},MF_{l}^{db}\);
7:endfor
8:\((L_{Euc,ds})\longleftarrow\)\(findCandidates(K_{1},d_{E})\) // \(K_{1}\) denotes number of neighbors
9:for\(j\longleftarrow\) 1 to \(length(k_{1})\)do
10:\(W_{L_{E}}\longleftarrow\)\(calWeight(L_{Euc,ds})\);
11:endfor
12:\(f_{L_{E}}\longleftarrow\)\(fillLocEst(K_{2},W_{L_{E}})\);
13:\(L_{p}\longleftarrow\)\(mean(f_{L_{E}})\);
14:endfor ```  
**Algorithm 1**Find user location  
We follow a different approach and calculate weights (\(W_{L_{E}}\)) for each \(L_{E}\) which has initially been selected based on the distance(error) alone. The weight \(W_{L_{E}}\) is calculated using:  
\[W_{L_{Ei}}=0.5*d_{Ei}+\frac{0.5}{min(d_{Si})} \tag{4}\]  
Figure 6: Location estimates and prediction using K nearest neighbor (KNN) and modified KNN. where:  
\[d_{Si}=||L_{Ei}-L_{Ei+1,\ldots,n-1}|| \tag{5}\]  
The term \(d_{Si}\) denotes the spatial distance of \(L_{Ei}\) to all other \(L_{E}\) which are initially selected. Equation (4) shows that the inverse proportional of \(d_{Si}\) is taken which implies that the closer the two location candidates are the higher the weight is and vice versa. So, we consider the Euclidean distance of location candidates from the user sample, as well as, the spatial closeness of location candidates.