The 8/S shaped pattern is used to calibrate magnetometers in mobile phones and other devices.
Background
Typical mobile phone era magnetometers measure the magnetic field strength along three orthogonal axes, e.g.:
m=mxı^+myȷ^+mzk^m=mxı^+myȷ^+mzk^
With the magnitude of the field given by,
∥m∥=m2x+m2y+m2z−−−−−−−−−−−−√‖m‖=mx2+my2+mz2
and the angle of rotation from each axis as
θk=cos−1mk∥m∥, where k∈[x,y,z]θk=cos−1mk‖m‖, where k∈[x,y,z]
Calibration
Since the magenetic field of the earth is relatively constant, the magnitude of the as field measured by the magnetometer should also be constant, regardless of the orientation of the sensor. i.e. if one were to rotate the sensor around and plot mxmx, mymy, and mzmz in 3D, the paths should plot out the surface of a sphere with constant radius.
Ideally it should look somthing like this:
However due to hard and soft iron effects and other distortions, it ends up looking like a deformed sphere:
This is because the magnitude of the magnetic field as measured by the sensor is changing with orientation. The result being that the direction of the magnetic field when calculated according to the formulas above is different from the true direction.
Calibration must be performed to adjust each of the three axis readings so that the magnitude is constant regardless of orientation - you can think of it as the deformed sphere must be warped into a perfect sphere. The LSM303 application note has lots of detailed instructions on how to perform this.
So what about the figure 8 pattern!?
Performing the figure 8 pattern 'traces out' part of the deformed sphere above. From the coordinates obtained, the deformation of the sphere can be estimated, and the calibration coefficients obtained. A good pattern is one that traces through the greatest range of orientations and therefore estimates the greatest deviation from the true constant magnitude.
To estimate the shape of the deformed sphere, least squares ellipse fitting can be used. The LSM303 application note also has information on this.
A simple method for a basic calibration
According to the app note if you assume no soft-iron distortion, the deformed sphere will not be tilted. Therefore a simple method for a basic calibration may be possible:
rk=12(max(mk)−min(mk))rk=12(max(mk)−min(mk))
zk=max(mk)−rkzk=max(mk)−rk
m′k=mk−zkrkmk′=mk−zkrk
This is based off the code found here.
Solving using least squares
MATLAB code to solve using least squares is shown below. The code assumes a variable mag
where the columns are the x y z values.
H = [mag(:,1), mag(:,2), mag(:,3), - mag(:,2).^2, - mag(:,3).^2, ones(size(mag(:,1)))];
w = mag(:,1).^2;
X = (H'*H)\H'*w;
offX = X(1)/2;
offY = X(2)/(2*X(4));
offZ = X(3)/(2*X(5));
temp = X(6) + offX^2 + X(4)*offY^2 + X(5)*offZ^2;
scaleX = sqrt(temp);
scaleY = sqrt(temp / X(4));
scaleZ= sqrt(temp / X(5));
To do a dynamic figure 8 calibration, you could run the least squares routine with every new reading and terminate when the offset and scale factors have stabilised.