# Human Activity Recognition Based on Deep Learning Regardless of Sensor Orientation
## 3 Methodology
### Madgwick Algorithm  
The Madgwick algorithm implementation incorporates magnetic distortion and gyroscope bias drift compensation [24]. This algorithm is based on quaternions, which effectively addresses the problem of attitude estimation in nonlinear systems while maintaining low computational demands and good real-time performance [44]. An object's orientation is represented using a quaternion, denoted as  
\[q=[q_{0},q_{1},q_{2},q_{3}]. \tag{1}\]  
The unit quaternion is usually initialized as \([1,0,0,0]\). The attitude adjustment gain parameter, denoted as \(\beta\), is used to control the fusion rate and noise compensation. Typically, it is set to a small positive value, e.g., 0.1.  
To address the problem of device orientation change, data preprocessing was necessary. We preprocessed the accelerometer (acc) data and gyroscope (gyr) data, which included unit conversion and coordinate system adjustments. In addition, we aligned them with the coordinate system of the orientation quaternion \(q\). The preprocessing of the magnetometer (mag) data, which we did not collect, made no difference.  
The main steps of the Madgwick algorithm are as follows:  
1. Calculate the attitude update time step (\(\Delta t\)), which depends on the update frequency of the attitude fusion.
2. Update the orientation quaternion based on gyroscope measurements: \[q=\frac{1}{2}q\otimes\begin{pmatrix}0\\ \text{gyro}_{x}\\ \text{gyro}_{y}\\ \text{gyro}_{z}\end{pmatrix},\] (2) where \(\otimes\) denotes quaternion multiplication and \(\text{gyro}_{x}\), \(\text{gyro}_{y^{\prime}}\), \(\text{gyro}_{z}\) represent the gyroscope's 3-axial measurements.
3. Correct the orientation quaternion using accelerometer and magnetometer measurements.  
For this, first, the accelerometer and magnetometer measurements must be normalized to ensure that they are unit vectors:  
\[acc_{\text{norm}}=\frac{acc}{\|acc\|},\quad mag_{\text{norm}}=\frac{mag}{\| mag\|} \tag{3}\]  
where \(||\cdot||\) denotes the vector's magnitude (length).  
Next, the gravity's reference direction needs to be calculated, which is derived from the current orientation quaternion \(q\):  
\begin{table}
\begin{tabular}{c c c c} \hline \hline  & **Our Dataset** & **WISDM** & **UCI HAR** \\ \hline Sample rate (Hz) & 200 & 20 & 50 \\ Subjects & 31 & 29 & 30 \\ Catagories & 7 & 6 & 6 \\ Window size & 300 & 80 & 256 \\ Stride & 150 & 40 & 128 \\ Overlap rate (\%) & 50 & 50 & 50 \\ \hline \hline \end{tabular}
\end{table}
Table 1: Datasets setup.  
\[\text{gravity}=\begin{bmatrix}0\\ 2(q_{0}q_{2}-q_{1}q_{3})\\ 2(q_{1}q_{2}+q_{0}q_{3})\\ q_{0}^{2}-q_{1}^{2}-q_{2}^{2}+q_{3}^{2}\end{bmatrix}. \tag{4}\]  
Then, the reference direction for the accelerometer and magnetometer is derived from the current orientation quaternion \(q\) and the normalized accelerometer and magnetometer measurements:  
\[\text{ref}=\begin{bmatrix}0\\ 2m_{\text{n}_{y}}(0.5-q_{1}^{2}-q_{2}^{2})+2m_{\text{n}_{z}}(q_{1}q_{3}-q_{0}q _{2})\\ +2m_{\text{n}_{y}}(q_{1}q_{2}-q_{0}q_{3})+2m_{\text{n}_{z}}(q_{0}q_{1}+q_{2}q _{3})\\ +2m_{\text{n}_{y}}(q_{0}q_{2}+q_{1}q_{3})+2m_{\text{n}_{z}}(0.5-q_{1}^{2}-q_{2 }^{2})\end{bmatrix}. \tag{5}\]  
Next, it is necessary to calculate the error term error  
\[e=\text{ref}\otimes\text{gravity}^{*}, \tag{6}\]  
where \({}^{*}\) denotes the quaternion's conjugate.  
Then, the cross-coupling term between the gyroscope and accelerometer is calculated:  
\[h=\dot{q}\cdot\beta \tag{7}\]  
along with the correction term correction:  
\[c=e\cdot h. \tag{8}\]  
Finally, the correction term correction is used to correct the rate of change of the orientation quaternion \(\dot{q}\):  
\[\dot{q}-=c. \tag{9}\]  
d. Normalize the orientation quaternion \(q\) to ensure that it has a unit length of  
\[q_{\text{norm}}=\frac{q}{\|q\|}. \tag{10}\]  
The above steps are repeated while continuously updating the orientation quaternion \(q\) based on real-time sensor data in order to achieve attitude estimation.