# A Novel Method of Adaptive Kalman Filter for Heading Estimation Based on an Autoregressive Model
## 5 The Detection of a QS Magnetic Field  
The geomagnetic field suffers severe perturbations in indoor environments, causing the magnetic field magnitude to not be constant. However, locations are possible where these magnetic field magnitudes are constant, and these situations can be considered as QS [38]. The heading calculated by magnetic field intensity in QS can achieve an accurate result. In this paper, the change rate of the magnetic field intensity is used to detect QS. The detection model based on the change rate of magnetic field magnitude is expressed as  
\[y_{k}^{m}=o_{k}+v_{k} \tag{40}\]  
where \(o_{k}=\left\|\overrightarrow{n_{k}}\right\|\), \(\left\|\overrightarrow{n_{k}}\right\|\) is the change rate of magnetic field magnitude and \(v_{k}\) is noise. The magnetic field can be considered as QS if \(o_{k}=0\). Therefore, the detection of QS can be expressed as a binary hypothesis test. Here, the two hypotheses \(H_{0}\) and \(H_{1}\) are  
\[ \begin{array}{ll}H_{0}:\exists k\in\Gamma_{n}&s.t.&o_{k}\
eq 0\\ H_{1}:\forall\in\Gamma_{n}&then&o_{k}=0\end{array}  \tag{41}\]  
where \(\Gamma_{n}=\left\{l\in N:n\leq l\leq n+N-1\right\}\). The probability density function (PDF) of Equation (40) based on hypothesis \(H_{0}\) can be given by  
\[f(y;o_{k},H_{0})=\prod_{k\in\Gamma_{n}}\frac{1}{\sqrt{2\pi o_{o_{k}}^{2}}}\exp \left(\frac{-1}{2\pi o_{o_{k}}^{2}}\left(y_{k}^{m}-o_{k}\right)^{2}\right) \tag{42}\]where \(\sigma_{\alpha_{k}}^{2}\) is the noise variance of \(\alpha_{k}\). We assume that the maximum likelihood estimate (MLE) of \(\alpha_{k}\) is \(\hat{\alpha}_{k}\), which can be obtained by the mean of samples as  
\[\delta_{k}=\frac{1}{N}\sum_{k\in\Gamma_{n}}y_{k}^{m} \tag{43}\]  
Therefore, the PDF of \(H_{0}\) can be changed to  
\[f(y;\alpha_{k},H_{0})=\prod_{k\in\Gamma_{n}}\frac{1}{\sqrt{2\pi\sigma_{\alpha_ {k}}^{2}}}\exp\left(\frac{-1}{2\pi\sigma_{\alpha_{k}}^{2}}\left(y_{k}^{m}- \delta_{k}\right)^{2}\right) \tag{44}\]  
For the hypothesis \(H_{1}\), \(\alpha_{k}=0\), so the PDF of \(H_{1}\) can be obtained by  
\[f(y;H_{1})=\prod_{k\in\Gamma_{n}}\frac{1}{\sqrt{2\pi\sigma_{\alpha_{k}}^{2}}} \exp\left(\frac{-1}{2\pi\sigma_{\alpha_{k}}^{2}}\left(y_{k}^{m}\right)^{2}\right) \tag{45}\]  
The detection of QS based on the generalized likelihood ratio test (GLRT) can be expressed as  
\[\wedge(y)=\frac{f(y;\|\hat{\alpha}_{k}\|,H_{0})}{f(y;H_{1})}<\chi_{m} \tag{46}\]  
Substituting Equations (44) and (45) into Equation (46), we can obtain  
\[\wedge(y)=\prod_{k\in\Gamma_{n}}\exp\left(\frac{1}{2\sigma_{\alpha_{k}}^{2}} \left(y_{k}^{m}\right)^{2}-\frac{1}{2\sigma_{\alpha_{k}}^{2}}\left(y_{k}^{m}- \delta_{k}\right)^{2}\right)<\chi_{m} \tag{47}\]  
Taking the natural logarithm on both sides of Equation (47), we get the test statistics as follows. \[\left|\frac{1}{\sqrt{N}}\right|\sum_{k\in\Gamma_{n}}y_{k}^{m}\right|<\chi_{m}^ {\prime} \tag{48}\]  
where \(\chi_{m}^{\prime}=\sqrt{2\sigma_{\alpha_{k}}^{2}\ln(\chi_{m})}\) is the threshold and \(N\) is the window size. If Equation (48) is true, the magnetic field is considered as QS. Then the heading calculated by Equation (31) can be corrected with the heading updated by Equation (15). Similarly, the roll and pitch can also be corrected with the solution estimated by KF. The following algorithm is tabulated for expression of the proposed heading estimation. ```
Step 0. Given \(S\), \(L\) with \(S\geq L+2\). We set \(S=4\), \(L=1\). Step 0.1 Initial state: \(x_{0}=\left[ \begin{array}{cccc}z_{0}&z_{-1}&z_{-2}&z_{-3}\end{array} \right]^{T}\), where \(z\) is the measurement heading calculated by Equation (32). Step 0.2 Initial state variance matrix \(P_{0}\), state noise covariance \(Q_{0}\) and measurement noise covariance \(R_{0}\). Step 1. Calculation of coefficients matrix: Calculate the coefficient of an AR model by Equation (20), and calculate the state transition matrix by Equation (10). Step 2. KF prediction: Performance the KF time update of Equations (11) and (12). Step 3.