# GNSS Based Low-Cost Magnetometer Calibration
## 3 Experimental Verification
### Data Processing  
We processed the measured data with python scripts using the pandas and NumPy 1.20.2 packages. From the GPS points, we have calculated the direction vectors for each point using Equations (7) and (8) and determined the theoretical axial components of the magnetic vector in the local coordinates of the vehicle in Equation (16). The vectors were calculated only when the vehicle was moving faster than 5 km/h. The faster the vehicle moved, the more accurate estimate of the direction vector was obtained. Calibration is hence not possible when the vehicle is stationary. Based on the obtained error of individual points \(E\) in Equation (12), we tried three different algorithms for finding calibration constants. The first was the arithmetic average of the error in the given axis, which allows calculating only the bias. Another method was linear regression, based on which we obtained both bias and gain. As the last one, we tried calibration by searching for a calibration matrix that should be able to calibrate the values between the individual axes and Equations (14)-(16). \[B=C\cdot V \tag{14}\]  
\[B\cdot V^{T}=C\cdot\left(V\cdot V^{T}\right) \tag{15}\]  
\[C=\left(B\cdot V^{T}\right)\cdot\left(V\cdot V^{T}\right)^{-1} \tag{16}\]  
where \(B\) is the magnetic field vector from the GPS direction [3 \(\times\) 1], \(V\) is the measured vector from the magnetometer [3 \(\times\) 1], and \(C\) is the calibration matrix [3 \(\times\) 4]. The matrix \(C\) could be of dimension [3 \(\times\) 3], but the calculation would not consider the bias of the sensor. During the data processing, we discovered a strong influence of hard iron in the passenger car cabin. The absolute value of the measured vector was many times greater than the value of the Earth's magnetic field. This fact can also be seen in Figure 4, where the orange color shows where the points on a certain track should have been, and the blue color shows where they were measured. In the picture, we can also notice certain anomalies that are significantly shifted compared to other measurements. After analysis, we found that these points were created when crossing the iron bridge. In other measurements, they were probably created by the traction lines of railways, trolleybuses, or when passing trucks. As mentioned, the magnetic field is influenced strongly by the environment. Figure 4: Measured data after a certain path (blue color), calculated data (orange color). During the calibration phase (1, 2, 3, 4, 5...min), we obtained the error vector \(E\), and then, based on the calibration constants, we adjusted the data from the magnetometer for the duration of the journey, which took 4 min. During this evaluation phase, we predicted the position of the vehicle based on the direction from the magnetometer and the speed from the GNSS to show how accurately the data is calibrated. We calculated the difference between the predicted direction vector (azimuth) and the real one from the GNSS during the prediction period. The progress of the error during the prediction can be seen in Figure 5. The error does not contain drift. The total error was reflected in the RMSE values, which for this case, were as follows  
* without calibration: 33.6\({}^{\circ}\),
* averaging error (AVG): 6.0\({}^{\circ}\),
* linear regression (LinReg): 4.39\({}^{\circ}\)
* and calibration matrix estimation (Matrix): 4.33\({}^{\circ}\). The prediction of the position, with different types of search for calibration constants, can be seen in Figure 6, where the magnetometer was calibrated for 4 min, and then the position of the vehicle was predicted for 4 min. The direction of the uncalibrated data (red color) was almost always in the same direction. This phenomenon was caused by the car's hard ferromagnetic material. The absolute value of the measured vector was around 120 uT, while the intensity of the magnetic field in the measurement locations was 49.5 uT. At the end of each measurement, we placed a compass at the location of the sensor and recorded its value.