Reading a Compass in a Haunted Room
The cheapest magnetic instrument in the world is in your pocket, it works, and almost every attempt to compute anything from it gets one specific thing wrong.
Your phone contains a three-axis magnetometer. A web page is not allowed to read it directly — the raw sensor was closed off to browsers on both major platforms years ago, for fingerprinting reasons that have nothing to do with any of this — which is why no website can honestly show you a field strength in microtesla, and why any that does is showing you a number it invented.
What a browser can read is the compass heading. And a heading is more useful than it looks, because it is a magnetometer reading that has already been through the interpretation, and it carries the interference with it.
What a heading is actually telling you
A compass reports the direction of the total magnetic field where it is. In open ground that is essentially the earth's field, and the heading is stable to a fraction of a degree. Indoors it is the earth's field plus everything else: steel in the structure, reinforcement in a slab, wiring, motors, transformers, magnets, and whatever is in your other pocket.
When the local field is disturbed, the heading does not simply move — it stops being able to settle. It jitters. The scatter in it over a second or two is a real, physical measurement of how disturbed the field around the device is, and you can verify the whole thing in about four seconds by walking a phone toward a speaker.
Two useful things follow.
It is strongly sensitive to permanent magnets, which a mains-band coil meter is nearly blind to. Speakers, magnetic catches, headphones, magnetic phone mounts and the steel in a fire door all move a heading a long way and move a K-II hardly at all. That is a genuine complementarity and not a defect in either instrument.
It is directional for free. You are already measuring a bearing, so every reading you take comes with the direction you were facing attached, which is the basis of a bearing survey and the thing that makes "point your phone at the dark corner" mean something specific.
The arithmetic that catches everybody
Headings wrap. 359° and 1° are two degrees apart. Feed a sequence like 359, 0, 1, 358, 2 into an ordinary mean and you get 144°, which is not merely wrong but points in a direction nobody was facing. Feed it into an ordinary standard deviation and you get an enormous number.
The consequence in a real application is unmistakable once you know to look for it: the instrument goes off every time the user faces north, and only when they face north, and it looks exactly like the room responding to a direction.
The fix is standard and is called circular statistics. Turn each heading into a unit vector —
(cos θ, sin θ) — average the vectors, and take the direction of the result as the mean and its
length as a measure of concentration. The scatter figure is then 1 − R, where R is the length
of the mean vector: zero when every reading agrees, approaching one when they are spread all the
way round. North is not special to any of it, because a unit circle has no seam.
| Mean | atan2 of the summed sines and cosines. Never the arithmetic mean of the degrees. |
|---|---|
| Scatter | 1 − R, where R is the length of the mean unit vector. Bounded 0 to 1 and has no discontinuity. |
| Difference | Wrap to ±180°. ((a − b + 540) mod 360) − 180 does it in one line. |
| Binning | Sectors of a circle, not buckets of a number line. A sector at 350–10° is one sector. |
| Never | Standard deviation of degrees, a min/max range, or a moving average over raw heading values. |
Using a phone compass properly
Calibrate first. The figure-of-eight motion every phone asks for is genuine: it lets the software estimate and subtract the hard-iron offset from the device's own magnets. Do it away from metal, and do it again if you have had the phone in a magnetic mount or beside a laptop.
Take the case off, or know what is in it. A very large number of phone cases contain magnets, and a magnetic wallet or car mount will dominate every reading you take for the rest of the night.
Hold it flat and hold it still. Heading is computed from a tilt-compensated projection, and tilting the device introduces error that has nothing to do with the room. Movement of your own hand is indistinguishable from movement of the field.
Take a floor before you take anything else. Ninety seconds in a position you have marked. A scatter figure with nothing to compare it against is not a reading, and the same room will give you quite different numbers on a wooden table and on a steel-framed desk.
Watch for iOS and Android differing. They expose heading through different mechanisms, with different rates and different smoothing, and on iOS you also get an accuracy estimate that is genuinely useful. Do not compare a figure from one handset with a figure from another and expect them to match; compare each against its own floor.
What ours does with it
Instrument 01 samples heading many times a second and computes exactly the circular dispersion described above, then normalises it against a floor and a ceiling established during calibration. The needle is deliberately not linear against that figure — the bottom of the range is expanded, so that a quiet room still produces a needle that sits somewhere and moves rather than lying flat on the pin. A panel reading of 14 beside a sigma of 0.1 is correct and the methodology page explains why.
It also bins the magnitude by real compass heading into thirty-six sectors, which gives you a map of the room by direction. A sector nobody has pointed at is drawn as an empty mark rather than as a reading of zero, because unmeasured and measured-quiet are different facts, and it refuses to name a peak bearing off fewer than five swept sectors. One high reading taken in passing is not a direction.
On a device with no north reference — which includes most laptops — the rose still works and is labelled relative, rather than printing compass letters it would be guessing at.
Questions
- Can a phone compass detect magnetic interference?
- Yes, and quite well. A compass heading is a magnetometer reading that has already been interpreted, and it carries the interference with it: when the local field is disturbed the heading stops being able to settle. The scatter in the heading is a genuine physical measurement of that disturbance.
- Why does a compass spin near a speaker?
- A loudspeaker contains a large permanent magnet, often producing a field at close range far stronger than the earth's. The compass reports the direction of the total field it is in, which near a speaker is mostly the speaker.
- Why does an app read wrong near north?
- Because headings are circular. 359° and 1° are two degrees apart, not 358. Ordinary averages and standard deviations treat them as far apart and report an enormous figure every time the user happens to face north. The fix is to compute with unit vectors instead.