# A Novel Indoor Localization Method Based on Image Retrieval and Dead Reckoning
## 3 Pedestrian Dead Reckoning
### Heading Calculation  
The heading calculation module is the most important part of PDR. The inherent defect of PDR algorithm is the cumulative error, which is largely caused by the cumulative error of integrating the angular velocity measured by the gyroscope. In the actual walking process, accompanied by swings, the postures of the sensors do not always represent the forward direction of the pedestrian, which, if not corrected, would introduce additional error into the heading calculation. To solve this problem, PDR algorithm integrates the outputs of gyroscope, accelerometer and magnetometer to determine the posture of sensors and to correct the heading of pedestrians. Kalman filtering [36] and particle filtering [37] are often used in the PDR system as the fused heading calculation algorithms. In this paper, we propose a recursively weighted fusion heading calculation algorithm with small computation. Firstly, the gyroscope is used to get three postures, which are named as angle of pitch, angle of yaw and angle of roll. Then, the acceleration is used to correct the pitch angle and the roll angle, and the magnetometer outputs are used to correct the yaw angle, which is the heading of the pedestrian. The values of correction parameters in this algorithm are directly related to the accuracy of the sensors. Our algorithm uses quaternions to settle the attitude of AHRS (Attitude and Heading Reference System) and get the attitude information of the sensors. According to the mechanism of PDR, the  
Figure 2: The waveform of accelerations. angular rate and the attitude quaternion at step \(k-1\) are known, thus the updated attitude quaternion at step \(k\) can be obtained by solving the gradient of the attitude quaternion (Equation (4)). \[\left\{ \begin{array}{l}q_{\omega,k}=q_{\omega,k-1}+q_{\omega,k}\Delta t\\ q_{\omega,k}=\frac{1}{2}q_{\omega,k-1}\bigotimes\omega_{k}\end{array} \right. \tag{4}\]  
The attitude angles can be calculated using Equation (5). \[\left\{ \begin{array}{l}\varphi=\arctan\left[\left(2q_{1}q_{2}-2q_{0}q_{3} \right)\right./\left.\left(2{q_{0}}^{2}+2{q_{2}}^{2}-1\right)\right]\\ \theta=\arcsin\left(2q_{2}q_{3}+2q_{0}q_{1}\right)\\ \gamma=-\arctan\left[\left(2q_{1}q_{3}-2q_{0}q_{2}\right)\right./\left.\left(2 {q_{0}}^{2}+2{q_{3}}^{2}-1\right)\right]\end{array} \right. \tag{5}\]  
where \(\varphi\) is the yaw angle, \(\theta\) is the pitch angle and \(\gamma\) is the roll angle. In the navigation coordinate system, the gravity is a constant value, and, if the attitude matrix calculated by angular rate is accurate, it should be equal to the acceleration in the carrier coordinate system when projected onto it. Therefore, the inaccurate calculation of attitude matrix would inevitably lead to inconsistency of the outputs in the two coordinate systems. Thus, the gradient descent method can be adopted to minimize this inconsistency. When the loss function is zero, the error of the rotation matrix has been eliminated and an accurate attitude quaternion is obtained. The loss function of the attitude correction process is defined in Equation (6). \[f(q,a)=\left\|q_{a}\times g-a_{m}\right\|^{2} \tag{6}\]  
Thus, to solve this problem, we get Equation (7)  
\[\
abla f(q,a)=\mathcal{J}(q,a)\cdot e(q,a) \tag{7}\]  
where \(\mathcal{J}(q,a)\) is the Jacobian matrix of quaternions and \(e(q,a)\) is the error function. \[\left\{ \begin{array}{l}\mathcal{J}(q,a)=\partial f/\partial q\\ e(q,a)=(q_{a}\cdot g-a_{m})^{T}\end{array} \right. \tag{8}\]  
The quaternion iteration equation represented by acceleration is shown in Equation (9). \[\left\{ \begin{array}{l}q_{a,k+1}=q_{a,k}-\mu_{a,k}\frac{\
abla f(q,a)}{\left\| \
abla f(q,a)\right\|}\\ \mu_{a,k}=\alpha_{k}\left\|q_{\omega}\right\|\Delta t\end{array} \right. \tag{9}\]  
Similar to the attitude correction by accelerations, we also adopt gradient descent method to achieve the purpose of correcting yaw angle of the attitude by minimizing the error between the yaw angle calculated by magnetic values and the yaw angle calculated by angular rates.