# Real-Time Indoor Positioning Based on BLE Beacons and Pedestrian Dead Reckoning for Smartphones
## 3 Methodology
### PDR Positioning
#### 3.3.3 Heading Direction Estimation  
We assume it is most likely that the user holds the smartphone to check the navigation while walking. Thus, the user holds the smartphone in a portrait orientation, pointing to the heading direction with the screen up. So, the user's heading direction is the same as that of the smartphone. In this paper, the heading direction is estimated with the combineddata from the magnetometer, accelerometer, and gyroscope using a complementary filter, as shown in Figure 2. It is given by  
\[\varphi=\varepsilon\cdot\varphi_{m}+(1-\varepsilon)\varphi_{g}, \tag{8}\]  
where \(\varphi_{m}\) is the heading angle obtained from the magnetometer and accelerometer, \(\varphi_{g}\) is the heading angle obtained from the gyroscope, and \(\varepsilon\) is the weight coefficient of the complementary filter, depending on the accuracy of the two components. The rationale of this method is as follows. On the one hand, the heading angle estimated by the magnetometer and accelerometer is unstable over a short time due to the disturbance from the indoor magnetic field. On the other hand, a gyroscope can provide 3-axis angular velocities with high accuracy in a short interval, but it incurs accumulative error in the long run. Therefore, using a complementary filter to combine the heading angle from both components can improve accuracy and stability. In the following, we elaborate on how to obtain \(\varphi_{m}\) and \(\varphi_{g}\), respectively. First, we discuss how to get \(\varphi_{m}\) based on the sensing data from the magnetometer and accelerometer--noting that the coordinate system of the smartphone sensors is based on the device coordinate system (DCS), which changes with the phone's attitude. Therefore, we have to transform the DCS-based sensing data to the earth coordinate system (ECS) to ensure accuracy. The Euler angle is defined as the angle between the three axes of DCS and ECS, including pitch, roll, and yaw. The pitch, roll, and yaw represent rotation around the \(x\), \(y\), and \(z\) axes, often denoted by \(\theta\), \(\gamma\), and \(\varphi\), respectively. The relationship between the 3-axis magnetic field intensity under DCS and that under ECS is as follows:  
\[ \begin{bmatrix}H_{x}\\ H_{y}\\ H_{z}\end{bmatrix} = \begin{bmatrix}\cos\theta&\sin\gamma\sin\theta&\cos\gamma \sin\theta\\ 0&\cos\gamma&-\sin\theta\\ -\sin\theta&\sin\gamma\cos\theta&\cos\gamma\cos\theta\end{bmatrix}\begin{bmatrix} h_{x}\\ h_{y}\\ h_{z}\end{bmatrix} , \tag{9}\]  
where \( \begin{bmatrix}H_{x}&H_{y}&H_{z}\end{bmatrix} ^{\text{T}}\) is the magnetic field intensity under ECS, and \( \begin{bmatrix}h_{x}&h_{y}&h_{z}\end{bmatrix} ^{\text{T}}\) is the magnetic field intensity under DCS provided by the magnetometer of the smartphone. The pitch \(\theta\) and roll \(\gamma\) are obtained by measurements from the accelerometer:  
\[\theta=-\arctan\left(\frac{a_{x}}{\sqrt{a_{y}^{2}-a_{z}^{2}}}\right), \tag{10}\]  
\[\gamma=\arctan\left(\frac{a_{y}}{a_{z}}\right). \tag{11}\]  
Figure 2: The process of heading direction estimation using complementary filter. Then the estimated heading angle \(\varphi_{m}\) can be calculated using  
\[ \begin{split}\varphi_{m}&=\arctan\Bigg{(}\frac{H_{y}}{ H_{x}}\Bigg{)}\\ &=\arctan\Bigg{(}\frac{h_{y}\cos\gamma-h_{z}\sin\gamma}{h_{x}\cos \theta+h_{y}\sin\gamma\sin\theta+h_{z}\cos\gamma\sin\theta}\Bigg{)}.\end{split}  \tag{12}\]  
Second, we discuss how to get \(\varphi_{g}\) based on the data from the gyroscope. The rotation matrix \(\mathbf{C}_{n}^{b}\) is introduced, representing the motion process of the device rotation from DCS to ECS, defined as follows:  
\[\mathbf{C}_{n}^{b}= \begin{bmatrix}\cos\theta\cos\varphi&-\cos\theta\sin \varphi+\sin\gamma\sin\theta\cos\varphi&\sin\gamma\sin\varphi+\cos\gamma\sin \theta\cos\varphi\\ \cos\theta\sin\varphi&\cos\theta\sin\varphi+\sin\gamma\sin\theta\cos\varphi&-\sin \gamma\sin\varphi+\cos\gamma\sin\theta\cos\varphi\\ -\sin\theta&\sin\gamma\cos\theta&\cos\gamma\cos\theta\end{bmatrix} .