You are on page 1of 17

Freescale Semiconductor

Application Note
Document Number: AN4246
Rev. 3, 04/2013
2013 Freescale Semiconductor, Inc. All rights reserved.
Contents
1 Introduction
This application note provides the theory for the in-situ
calibration of a smartphone electronic compass
(eCompass) for hard and soft-iron effects. The
mathematical framework is that developed in AN4248
Implementing a Tilt-Compensated eCompass using
Accelerometer and Magnetometer Sensors and
AN4247 Layout Recommendations for PCBs Using a
Magnetometer Sensor. It is therefore highly
recommended to read these application notes first before
reading this document.
1.1 Key Words
eCompass, Geomagnetic, Magnetometer, Hard Iron, Soft
Iron, Calibration.
Calibrating an eCompass in the
Presence of Hard and Soft-Iron
Interference
by: Talat Ozyagcilar
Applications Engineer
11 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1 Key Words. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2 The Hard and Soft-Iron Model . . . . . . . . . . . . . . . . . . . . . 2
3 Applying Hard and Soft-Iron Corrections to the
Tilt-Compensated eCompass . . . . . . . . . . . . . . . . . . . . . 3
4 Relating the Locus of Magnetometer Measurements
to Calibration Coefficients . . . . . . . . . . . . . . . . . . . . . . . . 5
5 Imposing a Symmetric Constraint onto the Inverse
Soft-Iron Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
6 Full Solution for Four Parameter Calibration . . . . . . . . . 11
7 Calibration Algorithms and Source Code . . . . . . . . . . . 16
Calibrating an eCompass in the Presence of Hard and Soft-Iron Interference, Rev. 3
The Hard and Soft-Iron Model
Freescale Semiconductor 2
1.2 Summary
It is possible for a smartphone eCompass to be calibrated by the owner in the street with no a priori
knowledge of location or the direction of magnetic north.
Magnetometer measurements subject to hard and soft-iron distortions caused from ferromagnetic
materials on the PCB lie on the surface of an ellipsoid which can be accurately modelled by ten
parameters.
The calibration process consists of fitting the ten model parameters to the magnetometer
measurements. Three model the hard-iron offset, six model the soft-iron matrix and one models the
geomagnetic field strength.
When the ten model parameters are known, it is a simple procedure in software to transform the
magnetometer measurements from the surface of the ellipsoid to the surface of a sphere centered
at the origin. This transformation removes the hard and soft-iron interference allowing an accurate
eCompass heading to be computed.
A four parameter calibration, comprising the three hard-iron offsets plus the geomagnetic-field
strength, may be sufficient for circuit boards without strong soft-iron interference. The full solution
and a worked example is included in the text for this case.
Reference C source code for the 10 parameter calibration can be licensed free of charge from
Freescale for use in any product using a Freescale magnetometer. Please contact your Freescale
sales representative for the license agreement.
2 The Hard and Soft-Iron Model
AN4248 equation 4 defines the magnetic field B
p
measured by a smartphone magnetometer in the absence
of hard and soft-iron effects after rotations in yaw , pitch and roll by the rotation matrices R
z
(),
R
y
(), and R
x
() as:
Eqn. 1
B
r
is the local geomagnetic field vector with magnitude B and magnetic inclination at the smartphone
location.
The application note AN4247, section 6, defines the hard-iron offset as resulting from permanently
magnetized ferromagnetic components on the PCB. Since the magnetometer and PCB rotate together, the
hard-iron offset is a simple vector V
PCB
, which adds to the magnetometer reading. AN4247, section 6, also
notes that any zero field offset in the magnetometer factory calibration will appear as a fixed additive
vector V
Sensor
. For convenience, both offsets are normally combined as a single 'hard-iron' vector V
defined as:
Eqn. 2
B
p
R =
x
( )R
y
( )R
z
( )B
r
R
x
( )R
y
( )R
z
( )B
cos
0
sin




=
V V
PCB
V
Sensor
+ =
Applying Hard and Soft-Iron Corrections to the
Calibrating an eCompass in the Presence of Hard and Soft-Iron Interference, Rev. 3
Freescale Semiconductor 3
Including these terms in Equation 1 gives:
Eqn. 3
AN4247 defines the soft-iron effect as the interfering magnetic field induced by the geomagnetic field onto
normally unmagnetized ferromagnetic components on the PCB. The assumption is made that the induced
soft-iron field is linearly related to the geomagnetic field measured in the rotated smartphone reference
frame by the 3 by 3 matrix W
Soft
. The linearity assumption is generally accurate but is not strictly true in
the presence of magnetic hysteresis effects present in either the magnetometer or in ferromagnetic
components on the PCB.
The magnetometer is normally calibrated by the supplier to have approximately equal gain in all three
axes. Any remaining differences in gains can be modeled by a diagonal gain matrix W
Gain
.
A final matrix, W
NonOrthog
, can be used to model: i) the rotation of the magnetometer sensor relative to the
smartphone coordinate system and ii) the lack of perfect orthogonality between sensor axes.
For convenience, all three matrices are combined into a single 3 by 3 soft-iron matrix W

defined as:
Eqn. 4
Including the soft-iron matrix W into Equation 3 now defines the magnetometer reading after arbitrary
smartphone rotations as:
Eqn. 5
3 Applying Hard and Soft-Iron Corrections to the
Tilt-Compensated eCompass
The application note AN4248 provides the mathematics and reference C source code for a
tilt-compensated eCompass with correction for hard-iron interference only. This section extends the
mathematics in AN4248 to include the soft-iron interference defined by the matrix W in Equation 5.
Equations 13 and 15 in AN4248 define the roll and pitch angles as a function of the accelerometer
sensor only. These equations are independent of magnetic interference and are therefore valid in the
presence of hard and soft-iron effects.
B
p
R =
x
( )R
y
( )R
z
( )B
cos
0
sin




V +
W W
NonOrthog
W
Gain
W
Soft
=
B
p
WR =
x
( )R
y
( )R
z
( )B
cos
0
sin




V +
Calibrating an eCompass in the Presence of Hard and Soft-Iron Interference, Rev. 3
Applying Hard and Soft-Iron Corrections to the Tilt-Compensated eCompass
Freescale Semiconductor 4
The effects of the soft-iron matrix W can be included in equation 16 in AN4248 by manipulating
Equation 5 to give:
Eqn. 6
Equation 16 in AN4248 and Equation 6 are identical except for the presence of the inverse soft-iron matrix
W
-1
operating on the vector (B
p
- V).
Expanding the components of Equation 6 gives Equations 7 and 8 below, These equations differ from
equations 20 and 21 in AN4248 in that the vector is now the magnetometer reading corrected to
the flat plane = = 0 with both hard and soft-iron effects removed.
Eqn. 7
Eqn. 8
Eqn. 9
The yaw or compass heading angle is determined from the ratio of Equations 7 and 8 to give Equation 10
which is analogous to equation 22 in AN4248:
Eqn. 10
The extension to the C source code listed in AN4248 to incorporate both soft and hard-iron corrections is
equally trivial. In AN4248, section 6.1, the hard-iron vector V alone is subtracted from magnetometer
reading to give B
p
- V:
/* subtract the hard iron offset */
iBpx -= iVx;
iBpy -= iVy;
iBpz -= iVz;
R
z
( )B
cos
0
sin



cos sin 0
sin cos 0
0 0 1





B
cos
0
sin




R
y
( )R
x
( )W
1
B
p
V ( ) = =
B
fx
B
fy
B
fz






B cos cos B
fx
=
sin B cos B
fy
=
B
fx
B
fy
B
fz






R
y
( )R
x
( )W
1
B
p
V ( ) =

B
fy

B
fx
-----------



= tan
Relating the Locus of Magnetometer Measurements to
Calibrating an eCompass in the Presence of Hard and Soft-Iron Interference, Rev. 3
Freescale Semiconductor 5
Both hard and soft-iron interference are removed by computing W
-1
(B
p
- V) from the magnetometer
reading B
p
. The inverse soft-iron matrix is assumed stored in Q15 fractional integer format with entries in
the range -32768 to +32767 representing the range -1. to 0.9999.
/* 32 bit scratch integers */
int iSumx, iSumy, iSumz;
/* subtract the hard iron offset */
iBpx -= iVx;
iBpy -= iVy;
iBpz -= iVz;
/* multiply by the inverse soft iron offset */
iSumx = (InvW[0, 0] * iBpx) + (InvW[0, 1] * iBpy) + (InvW[0, 2] * iBpz);
iSumy = (InvW[1, 0] * iBpx) + (InvW[1, 1] * iBpy) + (InvW[1, 2] * iBpz);
iSumz = (InvW[2, 0] * iBpx) + (InvW[2, 1] * iBpy) + (InvW[2, 2] * iBpz);
/* return the resulting vector invW*(Bp-V) back into 16 bit Bp */
iBpx = (short)(iSumx >> 15);
iBpy = (short)(iSumy >> 15);
iBpz = (short)(iSumz >> 15);
Incorporating both hard and soft-iron corrections into the tilt-compensated eCompass algorithm is
therefore trivial if these calibration coefficients are known. Note that the geomagnetic field strength B and
inclination angle cancel in going from Equations 7 and 8 to Equation 10. The only calibration values
used in the code sample above are the components of the hard-iron vector V and the inverse soft-iron
matrix W
-1
.
The next sections discusses how V and W
-1
can be computed from measurements made by the smartphone
in the complete absence of any a priori information about the direction and strength of the geomagnetic
field vector.
4 Relating the Locus of Magnetometer Measurements to
Calibration Coefficients
Application note AN4247 introduces the concept of the locus of magnetometer measurements from an
experimental perspective. This allows the PCB designer to visually determine the level of hard and
soft-iron interference prior to final PCB fabrication.
The same locus of magnetometer measurements represents the primary information available to the
calibration algorithms to determine the hard and soft-iron calibration V and W
-1
. Under arbitrary rotation
of the smartphone by its owner, the magnetometer readings lie on a surface which can be derived from
Equation 5 to be:
Eqn. 11 W
1
B
p
V ( )



T
W
1
B
p
V ( ) R
x
( )R
y
( )R
z
( )B
cos
0
sin









T
R
x
( )R
y
( )R
z
( )B
cos
0
sin




B
2
= =
Calibrating an eCompass in the Presence of Hard and Soft-Iron Interference, Rev. 3
Relating the Locus of Magnetometer Measurements to Calibration Coefficients
Freescale Semiconductor 6
The general expression defining the locus of the vector R lying on the surface of an ellipsoid with center
at R
0
is (where A must be a symmetric matrix):
Eqn. 12
Equations 11 and 12 are similar since it can be easily proved that the matrix A = {W
-1
}
T
W
-1
is symmetric:
Eqn. 13
In general therefore, the locus of raw magnetometer measurements forms the surface of the ellipsoid
defined by Equations 11 and 12. The ellipsoid is centered at the hard-iron offset R
0
= V, has shape
determined by the matrix A (equal to transposed square of the inverse soft-iron matrix W
-1
) and has the
overall size defined by the geomagnetic field strength B.
Figure 1 shows Equation 11 visually using real magnetometer data measured in a strong hard and soft-iron
environment. The shaded ellipsoid shows the optimum fit of the matrix A, the hard-iron vector V and the
geomagnetic field strength B to the data points. The excellent fit between the measurements and the
optimum ellipsoid validates the linear magnetic model of Equation 11.
R R
0
( )
T
A R R
0
( ) const =
A
T
W
1



T
W
1



T
W
1



T
W
1



T



T
W
1



T
W
1
A = = = =
Relating the Locus of Magnetometer Measurements to
Calibrating an eCompass in the Presence of Hard and Soft-Iron Interference, Rev. 3
Freescale Semiconductor 7
Figure 1. Locus of raw magnetometer measurements in a strong hard and soft-iron environment with the
optimum ellipsoidal fit superimposed.
The ellipsoid fit provides the matrix A and hard-iron vector R
0
= V whereas the calculation of the
calibrated magnetic field measurements W
-1
(B
p
- V) requires the inverse soft-iron matrix W
-1
. Although it
is trivial to compute the ellipsoid fit matrix A from a given inverse soft-iron matrix W
-1
using Equation 13,
there is no unique solution to the inverse problem of computing W
-1
from the matrix A. This problem and
its solutions are discussed in the next section.
Calibrating an eCompass in the Presence of Hard and Soft-Iron Interference, Rev. 3
Imposing a Symmetric Constraint onto the Inverse Soft-Iron Matrix
Freescale Semiconductor 8
5 Imposing a Symmetric Constraint onto the Inverse
Soft-Iron Matrix
Equation 4 defines the linear soft-iron model as derived from the product of three independent
3 by 3 matrices resulting in nine independent elements in the matrix W. Using the magnetometer
measurement locus in Equation 11 results in the soft-iron matrix appearing as the symmetric product
matrix A = {W
-1
}
T
W
-1
with only six independent coefficients. The mathematical process by which three
degrees of freedom are lost is simple to follow in Equation 11. Physically, it can be understood as a result
of the loss of angle information in the construction of the ellipsoidal measurement locus in Equation 11
since this is a function of the magnetometer measurements only.
The simplest solution is to impose the constraint that the inverse soft-iron matrix W
-1
also be symmetric
with six degrees of freedom. The argument for this constraint is discussed next.
Equation 5 relates each magnetometer measurement to the smartphone orientation, to the true hard-iron
offset V and the true soft-iron matrix W. If the readings B
p
are corrected by an estimated hard-iron offset
V
Cal
and an estimated soft-iron matrix W
Cal
, then the resulting corrected magnetometer reading B
c
is given
by:
Eqn. 14
If the calibration algorithm correctly estimates the hard and soft-iron coefficients, , V
Cal
= V,
then the corrected locus of readings lies on the surface of a sphere centered at the origin:
Eqn. 15
The converse is not the case. An arbitrary rotation can be included in the estimated inverse soft-iron matrix
and this would still result in the corrected locus of measurements lying on the surface of a sphere
centred at the origin. This is because a sphere is still a sphere under arbitrary rotation. Mathematically, if
, where R() is an arbitrary rotation matrix by angle , then (using the result that the
transpose of any rotation matrix R()
T
is identical to rotation by the inverse angle R(-)), the corrected
measurement locus still forms the surface of a sphere with radius B:
Eqn. 16
B
c
W
1
Cal
B
p
V
Cal
( ) W
1
Cal
WR
x
( )R
y
( )R
z
( )B
cos
0
sin





V V
Cal
( ) +





= =
W
1
Cal
W I =
B
c
( )
T
B
c
R
x
( )R
y
( )R
z
( )B
cos
0
sin










T
R
x
( )R
y
( )R
z
( )B
cos
0
sin





B
2
= =
W
1
Cal
W
1
Cal
W R ( ) =
B
c
( )
T
B
c
R ( )R
x
( )R
y
( )R
z
( )B
cos
0
sin










T
R ( )R
x
( )R
y
( )R
z
( )B
cos
0
sin





B
2
= =
Imposing a Symmetric Constraint onto the Inverse Soft-Iron
Calibrating an eCompass in the Presence of Hard and Soft-Iron Interference, Rev. 3
Freescale Semiconductor 9
If the constraint is applied that W
-1
is symmetric, then it is impossible for any spurious rotation angle and
error in compass heading to be introduced by the calibration process since any rotation matrix must be
anti-symmetric.
A further understanding of the advantage of a symmetric inverse soft-iron matrix can be obtained by
relating the eigenvectors of the matrix A to the eigenvectors of the inverse soft-iron matrix W
-1
. The
eigenvectors X
i
and eigenvalues e
i
of the inverse soft-iron matrix W
-1
are defined by:
Eqn. 17
Given that W
-1
is symmetric then: i) the eigenvectors of the matrix A are identical to the eigenvectors X
i

of W
-1
and ii) the eigenvalues of A are the square of the eigenvalues e
i
of W
-1
:
Eqn. 18
Since the eigenvectors of the matrix A represent the principal axes of the magnetometer measurement
ellipsoid (the Principal Axis Theorem in geometry), then constraining the inverse soft-iron matrix W
-1
to
be symmetric shrinks the ellipsoid into a sphere along the principal axes of the ellipsoid without applying
any additional spurious rotation.
Comparing Equations 12 and 13 and using the symmetry property of W
-1
, the hard and soft-iron magnetic
calibration parameters are related to the magnetometer ellipsoid locus by:
Eqn. 19
Eqn. 20
Figure 2 shows the measured magnetometer data of Figure 1 after application of Equation 16 to compute
the calibrated measurement B
c
. The inverse soft-iron matrix W
-1
was computed from the square root of the
ellipsoid fit matrix A using Equation 20. The data obviously lies on the surface of the sphere with radius
B centered at the origin as predicted in Equation 16. These calibrated measurements can now be passed to
the tilt-compensated eCompass algorithm for calculation of the compass heading.
W
1
X
i
e
i
X
i
=
AX
i
W
1



T
W
1
X
i
W
1



T
e
i
X
i
e
i
W
1
X
i
e
i
2
X
i
= = = =
V R
0
=
W
1



T
W
1
W
1
W
1
A = W
1
A
1
2
---
= =
Calibrating an eCompass in the Presence of Hard and Soft-Iron Interference, Rev. 3
Imposing a Symmetric Constraint onto the Inverse Soft-Iron Matrix
Freescale Semiconductor 10
Figure 2. Locus of the magnetometer measurements of Figure 1 after calibration for
hard and soft-iron effects.
Figure 3 shows the data from Figures 1 and 2 combined on the same plot. The calibration process can be
summarized as i) computing an optimal ellipsoid fit A, R
0
= V to the raw measured data shown in red
ii) computing the inverse soft-iron matrix from the square root of the ellipsoid matrix A and iii) applying
the hard and inverse soft-iron correction which translates the red measurement ellipsoid to the origin and
then shrinks it down its principal axes to produce the blue calibrated measurements.
Full Solution for Four Parameter Calibration
Calibrating an eCompass in the Presence of Hard and Soft-Iron Interference, Rev. 3
Freescale Semiconductor 11
Figure 3. Combination plot showing Figures 1 and 2 showing uncalibrated and
calibrated magnetometer measurements.
6 Full Solution for Four Parameter Calibration
The magnetic calibration problem permits a particularly simple solution for the case where the hard-iron
offset dominates and soft-iron effects can be ignored. This simplification results in just four calibration
parameters being determined: the three components of the hard-iron vector V plus the geomagnetic field
strength B. The soft-iron matrix W is assumed to be the identity matrix.
With these assumptions, equation 11 simplifies to:
Eqn. 21
Eqn. 22
B
p
V ( )
T
B
p
V ( ) B
2
=
B
p

T
B
p
2B
p
T
V V
T
V B
2
0 = +
Calibrating an eCompass in the Presence of Hard and Soft-Iron Interference, Rev. 3
Full Solution for Four Parameter Calibration
Freescale Semiconductor 12
The residual error r[i] for the i-th measurement is then:
Eqn. 23
where V
x
, V
y
and V
z
are the components of the hard-iron vector V:
Eqn. 24
and where B
px
, B
py
, B
pz
are the components of the magnetometer measurement B
p
:
Eqn. 25
The objective of the calibration is to take a series of magnetometer measurements and fit the four model
parameters V
x
, V
y
, V
z
and B to minimize the fit error to Equation 23 in a least squares sense.
Equation 23 can be written more conveniently in matrix form as:
Eqn. 26
If M measurements are used to compute the calibration (where M must be greater than, or equal to four
since there are four unknowns to be determined), Equation 26 can be expanded to:
Eqn. 27
r i [ ] B
px
= i [ ]
2
B
py
i [ ]
2
B
pz
i [ ]
2
2B
px
i [ ]V
x
2B
py
i [ ]V
y
2B
pz
i [ ]V
z
V
x
2
V
y
2
V
z
2
B
2
+ + + + +
V
V
x
V
y
V
z






=
B
B
px
B
py
B
pz






=
r i [ ] B
px
i [ ]
2
B
py
i [ ]
2
B
pz
i [ ]
2
+ +


B
px
i [ ]
B
py
i [ ]
B
pz
i [ ]
1







T
2V
x
2V
y
2V
z
B
2
V
x
2
V
y
2
V
z
2










=
r 0 [ ]
r 1 [ ]
...
r M 1 [ ]







B
px
0 [ ]
2
B
py
0 [ ]
2
B
pz
0 [ ]
2
+ +
B
px
1 [ ]
2
B
py
1 [ ]
2
B
pz
1 [ ]
2
+ +
...
B
px
M 1 [ ]
2
B
py
M 1 [ ]
2
B
pz
M 1 [ ]
2
+ +










B
px
0 [ ] B
py
0 [ ] B
pz
0 [ ] 1
B
px
1 [ ] B
py
1 [ ] B
pz
1 [ ] 1
... ... ... 1
B
px
M 1 [ ] B
py
M 1 [ ] B
pz
M 1 [ ] 1








2V
x
2V
y
2V
z
B
2
V
x
2
V
y
2
V
z
2










=
Full Solution for Four Parameter Calibration
Calibrating an eCompass in the Presence of Hard and Soft-Iron Interference, Rev. 3
Freescale Semiconductor 13
Equation 27 can be further abstracted to:
Eqn. 28
where Y is the vector of the known dependent variables:
Eqn. 29
X is the matrix of known magnetometer measurements:
Eqn. 30
is the solution vector from which the four calibration parameters can be easily determined:
Eqn. 31
and r is the vector of the error residuals for each of the M magnetometer measurements:
Eqn. 32
The performance function P to be minimized by optimizing the calibration fit is simply defined to be the
sum of the squares of the components of the error residual vector r:
Eqn. 33
r Y X =
Y
B
px
0 [ ]
2
B
py
0 [ ]
2
B
pz
0 [ ]
2
+ +
B
px
1 [ ]
2
B
py
1 [ ]
2
B
pz
1 [ ]
2
+ +
...
B
px
M 1 [ ]
2
B
py
M 1 [ ]
2
B
pz
M 1 [ ]
2
+ +










=
X
B
px
0 [ ] B
py
0 [ ] B
pz
0 [ ] 1
B
px
1 [ ] B
py
1 [ ] B
pz
1 [ ] 1
... ... ... 1
B
px
M 1 [ ] B
py
M 1 [ ] B
pz
M 1 [ ] 1








=

3








2V
x
2V
y
2V
z
B
2
V
x
2
V
y
2
V
z
2










= =
r
r 0 [ ]
r 1 [ ]
...
r M 1 [ ]






=
P r 0 [ ]
2
r 1 [ ]
2
... r M 1 [ ]
2
r
T
r Y X ( )
T
Y X ( ) = = + + + =
Calibrating an eCompass in the Presence of Hard and Soft-Iron Interference, Rev. 3
Full Solution for Four Parameter Calibration
Freescale Semiconductor 14
The solution to Equation 33 which minimizes P is derived in Freescale application note AN4399 to be:
Eqn. 34
Since both the matrix X and the vector Y are known from the series of M magnetometer measurements,
Equation 34 allows to be determined through simple matrix algebra.
In practice, it is more elegant to compute the 4 x 4 matrix X
T
X and the 4 x 1 vector X
T
Y directly from the
magnetometer measurements. This eliminates the storage overhead, which can be significant for large M,
of the M by 4 matrix X and the M by 1 vector Y.
Eqn. 35
Eqn. 36
Once is determined, Equation 31 gives three components of the hard-iron vector and the geomagnetic
field strength B as:
Eqn. 37
One important caveat to engineers is that the magnetometer measurements used to construct the
measurement matrix of Equation 30 should be taken at different orientation angles to prevent
magnetometer noise dominating the solution. While a detailed analysis of sensitivity to sensor noise is
beyond the scope of this discussion, it should be apparent that ten magnetometer measurements made at
the same orientation will be identical apart from sensor noise and will not lead to a quality solution. The
standard approach is to use the accelerometer sensor to select magnetometer measurements for calibration
taken at significantly different roll and pitch angles.
X
T
X ( )
1
X
T
Y =
X
T
X
M 1

i 0 =
B
px
i [ ]
2
B
px
i [ ]B
py
i [ ] B
px
i [ ]B
pz
i [ ] B
px
i [ ]
B
px
i [ ]B
py
i [ ] B
py
i [ ]
2
B
py
i [ ]B
pz
i [ ] B
py
i [ ]
B
px
i [ ]B
pz
i [ ] B
py
i [ ]B
pz
i [ ] B
pz
i [ ]
2
B
pz
i [ ]
B
px
i [ ] B
py
i [ ] B
pz
i [ ] 1










=
X
T
Y
M 1

i 0 =
B
px
i [ ] B
px
i [ ]
2
B
py
i [ ]
2
B
pz
i [ ]
2
+ + ( )
B
py
i [ ] B
px
i [ ]
2
B
py
i [ ]
2
B
pz
i [ ]
2
+ + ( )
B
pz
i [ ] B
px
i [ ]
2
B
py
i [ ]
2
B
pz
i [ ]
2
+ + ( )
B
px
i [ ]
2
B
py
i [ ]
2
B
pz
i [ ]
2
+ +










=

1
2B
2
---------
P
M
---- - =
Full Solution for Four Parameter Calibration
Calibrating an eCompass in the Presence of Hard and Soft-Iron Interference, Rev. 3
Freescale Semiconductor 15
Worked example 1
What is the least squares solution for the hard-iron vector and the geomagnetic field strength using the six
magnetometer measurements below (all in units of T)?
Substituting these measurements into Equations 29 and 30 gives the vector Y and measurement matrix X
to be:
Eqn. 38
Eqn. 39
Table 1.
B
p
0 [ ]
167.4
242.4
91.7





=
B
px
0 [ ]
2
B
py
0 [ ]
2
B
pz
0 [ ]
2
95189.4 = + +
B
p
1 [ ]
140.3
221.9
86.8





=
B
px
1 [ ]
2
B
py
1 [ ]
2
B
pz
1 [ ]
2
76457.9 = + +
B
p
2 [ ]
152.4
230.4
0.6





=
B
px
2 [ ]
2
B
py
2 [ ]
2
B
pz
2 [ ]
2
76310.3 = + +
B
p
3 [ ]
180.3
270.6
71.0





=
B
px
3 [ ]
2
B
py
3 [ ]
2
B
pz
3 [ ]
2
110773.5 = + +
B
p
4 [ ]
190.9
212.4
62.7





=
B
px
4 [ ]
2
B
py
4 [ ]
2
B
pz
4 [ ]
2
85487.9 = + +
B
p
5 [ ]
192.9
242.4
17.1





=
B
px
5 [ ]
2
B
py
5 [ ]
2
B
pz
5 [ ]
2
96260.6 = + +
Y
95189.4
76457.9
76310.3
110773.5
85487.9
96260.6









=
X
167.4 242.4 91.7 1
140.3 221.9 86.8 1
152.4 230.4 0.6 1
180.3 270.6 71.0 1
190.9 212.4 62.7 1
192.9 242.4 17.1 1









=
Calibrating an eCompass in the Presence of Hard and Soft-Iron Interference, Rev. 3
Calibration Algorithms and Source Code
Freescale Semiconductor 16
Substituting into Equation 34 gives the solution vector as:
Eqn. 40
Eqn. 41
Substituting Equation 41 into Equation 35 gives the least squares fit to the hard-iron vector as:
Eqn. 42
And finally, substituting into Equation 36 gives the geomagnetic field strength B as:
Eqn. 43
7 Calibration Algorithms and Source Code
This document has defined the calibration problem as the transformation of the locus of magnetometer
measurement from the surface of an ellipsoid displaced from the origin to the surface of a sphere located
at the origin. The algorithms and reference C source code which implement the calibration process are
available for license from Freescale for use free of charge in any product using a Freescale magnetometer.
Please contact your Freescale sales representative for details.

167.4 242.4 91.7 1


140.3 221.9 86.8 1
152.4 230.4 0.6 1
180.3 270.6 71.0 1
190.9 212.4 62.7 1
192.9 242.4 17.1 1









T
167.4 242.4 91.7 1
140.3 221.9 86.8 1
152.4 230.4 0.6 1
180.3 270.6 71.0 1
190.9 212.4 62.7 1
192.9 242.4 17.1 1




















1
167.4 242.4 91.7 1
140.3 221.9 86.8 1
152.4 230.4 0.6 1
180.3 270.6 71.0 1
190.9 212.4 62.7 1
192.9 242.4 17.1 1









T
95189.4
76457.9
76310.3
110773.5
85487.9
96260.6









=

311.5
478.2
91.7
81303.5






=
V
x
V
y
V
z






1
2
---



0

2






155.7
239.1
45.8





T = =
B
3
V
x
2
V
y
2
V
z
2
+ + + 2205.4 47.0T = = =
Document Number: AN4246
Rev. 3
04/2013
Information in this document is provided solely to enable system and software
implementers to use Freescale products. There are no express or implied copyright
licenses granted hereunder to design or fabricate any integrated circuits based on the
information in this document.
Freescale reserves the right to make changes without further notice to any products
herein. Freescale makes no warranty, representation, or guarantee regarding the
suitability of its products for any particular purpose, nor does Freescale assume any
liability arising out of the application or use of any product or circuit, and specifically
disclaims any and all liability, including without limitation consequential or incidental
damages. Typical parameters that may be provided in Freescale data sheets and/or
specifications can and do vary in different applications, and actual performance may
vary over time. All operating parameters, including typicals, must be validated for each
customer application by customers technical experts. Freescale does not convey any
license under its patent rights nor the rights of others. Freescale sells products pursuant
to standard terms and conditions of sale, which can be found at the following address:
freescale.com/salestermsandconditions.
How to Reach Us:
Home Page:
freescale.com
Web Support:
freescale.com/support
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware,
Energy Efficient Solutions logo, Kinetis, mobileGT, PowerQUICC, Processor Expert,
QorIQ, Qorivva, StarCore, Symphony, and VortiQa are trademarks of Freescale
Semiconductor, Inc., Reg. U.S. Pat. & Tm. Off. Airfast, BeeKit, BeeStack, ColdFire+,
CoreNet, Flexis, MagniV, MXC, Platform in a Package, QorIQ Qonverge, QUICC
Engine, Ready Play, SafeAssure, SMARTMOS, TurboLink, Vybrid, and Xtrinsic are
trademarks of Freescale Semiconductor, Inc. All other product or service names are
the property of their respective owners.
2013 Freescale Semiconductor, Inc.

You might also like