# A Real-Time BLE/PDR Integrated System by Using an Improved Robust Filter for Indoor Position
## 3 Materials and Methods
### An Improved Mahony Complementary Filter Based on the Motion States
\right)_{t} \tag{11}\]  
where the Equation (11) is an expansion of the Equation (10), \(Q[t]\) refers to the quaternion vector of time \(t\), \(\Delta t\) represents tiny time and is often assigned of sampling cycles, \(\Omega_{w}[t]\) is the corresponding matrix as shown in Equation (11), which is made up of gyroscope raw data at time \(t\). As a result, we can obtain the latest quaternion vector by utilizing the gyroscope data based on the Runger-Kutta method. However, as time goes on, it will suffer a drift problem, resulting in huge cumulative error for long-time orientation. To reduce the drift problem, the acceleration and magnetometer field are used to compensate for the cumulative error. Supposed that the gyroscope error correction is \(e=\left[e_{x},e_{y},e_{z}\right]^{T}\), it can be defined as:  
\[e=e_{a}+e_{m} \tag{12}\]  
where \(e_{a}\), \(e_{m}\) are the error correction items calculated by accelerometer and magnetometer readings, respectively. They are expressed as \(e_{a}=\left[e_{ax},e_{ay},e_{az}\right]^{T}\) and \(e_{m}=\left[e_{mx},e_{my},e_{nz}\right]^{T}\), and can be obtained by Equation (13):  
\[\left\{ \begin{array}{c}e_{a}=a_{n}^{b}\times a\\ e_{m}=m_{n}^{b}\times m\end{array} \right. \tag{13}\]  
where \(a\), \(m\) are the normalized accelerometer and magnetometer readings. \(a_{n}^{b}\), \(m_{n}^{b}\) are the normalized vectors of gravity acceleration and magnetic field after matrix \(C_{n}^{b}\) conversion. The symbol \"\(\times\)\" represents the vector cross product. After getting the error correction, the compensated gyroscope value \(\omega^{\prime}=\left(\omega_{x}^{\prime},\omega_{y}^{\prime},\omega_{z}^{ \prime}\right)\) can be calculated based on the proportional-integral (PI) method as follows in Equation (14):  
\[\omega^{\prime}=\omega+K_{p}\cdot e+K_{i}\cdot\int e \tag{14}\]  
where \(K_{p}\), \(K_{i}\) are the proportional and integral control parameters, respectively. The compensated gyroscope value \(\omega^{\prime}\) is plugged into the quaternion differential Equation (11) and the quaternion is updated. Then the attitude is obtained by Equation (9) based on the updated quaternion. In general, \(K_{p}\), \(K_{i}\) are fixed empirical values without considering the impact of pedestrian motion status. In our research, we proposed an improved MCF to change the control parameter \(K_{p}\) and \(K_{i}\) adaptively based on the pedestrian motion status. For simplicity, the pedestrian motion status can be divided into three types including static, walking, and running which can be judged by the standard deviation of triaxial acceleration modulus. The triaxial acceleration modulus can be calculated by Equation (15):  
\[acc_{mod}=\|acc\|=\sqrt{acc_{x}^{2}+acc_{y}^{2}+acc_{z}^{2}} \tag{15}\]  
where \(acc\) and \(\|acc\|\) refer to the acceleration vector and the corresponding modulus, respectively. Figure 5 shows the performance of the acceleration modulus on different motion statuses. The dotted green line is an artificial boundary between different motion states. The acceleration modulus is collected for 1 min at the frequency of 50 Hz. People in the first 20 s are in a static state, people in the middle 20 s are in a walking state, and people in the last 20 s are in a running state. We set up a sliding window to store the acceleration modulus and calculate the standard deviation in the window as shown in Equations (16) and (17). \[\mu_{acc}=\frac{1}{n}\cdot\sum_{i=1}^{n}acc_{mod_{i}} \tag{16}\]  
\[\sigma_{acc}=\sqrt{\frac{1}{n}\cdot\sum_{i=1}^{n}\bigl{(}acc_{mod_{i}}-\mu_{ acc}\bigr{)}^{2}} \tag{17}\]  
where \(\mu_{acc}\) is the average acceleration modulus of the sliding window in size \(n\), \(\sigma_{acc}\) refers to the standard deviation of the sliding window. Then, a moving average filter (MAF) is applied to process the \(\sigma_{acc}\) value to classify the motion state. Figure 6 demonstrates that the standard deviation of the acceleration modulus is smoother and easier to distinguish after MAF.