# Detect compass disturbance

I am looking for a way to determine wether the compass of a phone is showing inaccurate north heading by looking at magnetometer values.

Right now I have been looking at total magnetic field strength: Math.sqrt((x * x) + (y * y) + (z * z));. When disturbing the compass of the phone with metal - and when seeing the compass needle pointing the wrong way - I look at the teslameter and it deviates +- 8μT or more.

Is it a correct assumption I can detect a faulty compass heading by comparing the total magnetic strength. And would it be better to look at the vectors independently and compare them instead. If so how?

It can be noted I am using calibrated magnetic field which is supposed to give readings without device bias mentioned here. And that I have checked out raw device magnetometer data of which was not recommended and hard to understand.

Update: These stack overflow answers and this AHRS algorithm suggest it is possible.

Cellphone magnetometers are operated as ratiometric devices; the cell phone produces a bearing and possibly a magnetic dip by applying trigonometry to ratios of the measurements. This means that there is no strong demand for any absolute accuracy in the magnetometer chips output. Manual calibration is used to recalibrate both the relative gains of each of the three 1D magnetometers, and their offsets.

Is it a correct assumption I can detect a faulty compass heading by comparing the total magnetic strength.

The short answer is No, at least not easily.

Let's look at what can cause a faulty compass heading:

1. bad or out-of-date calibration
2. "soft-iron" errors (refers to nearby ferromagnetic materials that interact with the Earth's field and produce a local distortion that affects your compass)
3. "hard-iron" errors (refers to nearby magnetized materials (or other sources of DC magnetic fields) that adds to the Earth's field and affects your compass)

A bad or out-of-date calibration might be detectable if you measure at many orientations (as shown below) and determine that $$S$$ is not constant. But an easier way to detect that would be to simply do the calibration again and see if the heading changes.

"Soft-iron" and "hard iron" errors would be difficult to detect for the following reason. The local geomagnetic field strength is not very predictable. The Earth's average field at your coordinates can be looked up somewhere, but local geolocial effects can perturb it substantially. See Is there a “submerged object” in Australia that causes a magnetic deviation of 20 degrees?

If you think about it, if the local Earth's field is 1.0 in the $$x$$ direction, and you have a perturbing field in the $$y$$ direction, the vector sum can change by tens of degrees while the magnitude only changes a few percent.

Compare the magnitude $$\sqrt{1 + a^2}$$ with the angle $$\arctan{a}$$:

  a      magnitude    deviation (degs)
-----    ---------    ----------------
0        1.00              0.0
0.1       1.02             11.3
0.2       1.04             16.7
0.3       1.08             21.8
0.4       1.12             26.6


I don't see any way that you can reliably use a magnetic strength inferred from calibrated data from a cellphone magnetometer to detect faulty compass headings reliably. It might work once in a while, but for most of the causes of unreliable readings, it wont be very helpful.

## About cellphone magnetometer data calibration, and verification

If you are using uncalibrated data, you must calibrate it. If you believe you are using calibrated data then you can still use the equation below to get an idea how well calibrated it is.

You would have to degauss and calibrate your cellphone's magnetometer chip carefully before even starting to believe the raw data values!

Calibration, automatic or manually by yourself, assumes at a minimum that each of the three channels has a gain and offset that need to be handled. The circular motion we make with our phones to calibrate the magnetometer will match the three gains, but leave the absolute scale uncalibrated, since an electronic compass will only use the ratios.

If you look at the plots at the end, you can see that without calibration adding the three components in quadrature while doing a 3D rotation does not produce a constant magnitude, in fact it's way off. After a calibration, the magnitude is more constant, but the absolute value can not be calibrated this way. You'd need to find a better instrument to get the magnitude.

To do this, you could minimize $$S$$, the deviation of the amplitude from some constant value:

$$S = \sum_{j=1}^n \left( \sqrt{ \left(a_x\left(ADC_{x,j}-b_x \right) \right)^2 + \left(a_y\left(ADC_{y,j}-b_y \right) \right)^2 + \left(a_z\left(ADC_{z,j}-b_z \right) \right)^2 } -B_0 \right)^2$$

You can't even look up the geomagnetic field to calibrate because local geomagnetic fields can vary quite a bit depending on what is inside the Earth locally. For more on that see all the answers to Is there a “submerged object” in Australia that causes a magnetic deviation of 20 degrees?

A more advanced calibration might assume non-orthoginality between the three axes, and so there would be a first order mixing between, making the calibration a 3x4 augmented matrix with a form similar to the one I describe in Why is the notation (R|RT) for “first apply translation then rotation”? where the 3x3 rotations there are gains here, and the 3 translations there are offsets here.

For the type of magnetoresistive magnetometers found in cellphones, see:

## 1. Degauss...

From Wikipedia's Degaussing; Magnetic data storage media:

Erasure via degaussing may be accomplished in two ways: in AC erasure, the medium is degaussed by applying an alternating field that is reduced in amplitude over time from an initial high value (i.e., AC powered); in DC erasure, the medium is saturated by applying a unidirectional field (i.e., DC powered or by employing a permanent magnet). A degausser is a device that can generate a magnetic field for degaussing magnetic storage media.

## 2. Calibrate out the fixed magnetizations...

When I use the compass on my phone it instructs me to "calibrate" it first by doing a weird swirly pattern. Internally it continuously digitizes the three magnetometers (and possibly my phone's gyros) during the motion in order to find a best fit to a spherical rotation of a constant vector. It doesn't know if the vector comes from the Earth's field alone or has other external contributions (magnetized or ferromagnetic materials nearby) but it will calibrate out any fixed magnetizations that are in my phone - that rotate along with the magnetometer and add constant offsets.

A random Arduino illustration from ancient history:

• note: I'm entering the equation in MathJax right now, please don't yell at me for having an equation in an image, thanks! – uhoh May 7 '19 at 11:09
• But is this when working with raw data? I was not clear with that but I am using CMCalibratedMagneticField data since I could don't understand the raw data: forums.developer.apple.com/thread/116222. Here is a summary of the available ways to access magnetometer data on iOS: stackoverflow.com/questions/15380632/…. – lolelo May 7 '19 at 11:17
• @lolelo ah, that's good to know! Why don't you go add that information back into your original question so that others will see it for sure. In the mean time I will continue to add to this answer. I'll ping you again when I think it's finished. Thanks for your speedy response! – uhoh May 7 '19 at 11:21
• @lolelo I've added more, have a look, and have fun! If you get some interesting data or results it would be interesting to see. :-) – uhoh May 7 '19 at 11:43