The document describes an automatic calibration algorithm for a three-axis magnetic compass module. The algorithm has two stages:
1) The first stage characterizes the magnetic environment and corrects for distortions using an upper triangular soft iron matrix and hard iron offset vector. This makes the magnetometer measurements orthogonal and gain matched.
2) The second stage refines the soft iron matrix estimation by determining a rotation matrix to align the magnetometer coordinate system with the accelerometer coordinate system, improving heading accuracy. Given successful calibration, heading accuracies of 2 degrees or better can be achieved.