Skip to main content
. 2025 Jul 25;25(15):4609. doi: 10.3390/s25154609
Algorithm 1: Pseudo-code of the vector calibration algorithm
Input:
vlocal: A raw 3D vector to be calibrated (e.g., magnetic field vector)
araw: The raw 3D vector from the accelerometer
ψyaw: The current yaw angle of the device
ψref: The application-specific reference yaw angle
Output:
vcalib The calibrated 3D vector
Constant:
α = 0.8(The smoothing coefficient)
Persistent State:
afiltered_prev: The filtered acceleration vector from the previous time step
1 // Stage1: Pitch & Roll Correction
2
afiltered_current =α  afiltered_prev + (1α)  araw
3
g=afiltered_current// g= gx, gy,gzT
4
afiltered_prev = afiltered_current
5
θ = atan2(gx,sqrt(gy^2+gz^2))// roll angle
6
ϕ = atan2(gy, gz)// pitch angle
7
Rx =rotation_matrix_x(ϕ)
8
Ry =rotation_matrix_y(θ)
9
vtilt = Ry  Rx  vlocal
10
11 // Stage2: Yaw Correction
12
Δψ = ψyawψref
13
Rz =rotation_matrix_z(Δψ)
14
vcalib = Rz  vtilt
15
return vcalib