Compansating hard-iron, soft-iron and scaling effects in magnetic sensors

Hi guys,

i want to share my results about compensation of  unwanted magnetic effects such as hard-iron, soft-iron and scaling/calibration effects.

As we know that, since earth magnetic field is locally constant , the locus of all data points taken while a 3D magnetic sensor is rotated around every orientation, must be a sphere centered at origin. But in reality, it is generally a arbitrary rotated ellipsoid centered at some offset from origin. Hard-iron effects, scaling errors and soft-iron effects  are reasons for the offset, the ellipsoidal shape and rotation of the ellipsoid around some arbitrary axis, respectively.

Today, i rotated my ardumega card with hand and meanwhile collected raw magnetic data from Diydrones magnetic sensor using logs of AP Mission Planner (after setting magnetic offsets to zero initially). What i found is my sphere was depressed on polar caps in the direction of z axis. I wrote a matlab function (see attachment) to find an ellipsoid least square fit to the raw magnetic data. My results are below (See attachment for details).

Output parameters of matlab function (ellipsoid_fit2magnetic_data.m)  are offsets and a transformation matrix (inverse W). After translation of all data points using offsets to the origin and then applying transformation matrix (inverse W) , all magnetic raw data lying on the ellipsoidal surface can be transformed to a sphere centered at origin eliminating hard-iron, soft-iron and scaling effects.

Actually what i described here is not limited with 3D magnetic field sensors , it can be also applied to 3D accelerometers for calculating sensor offsets since gravitational field of earth is also constant locally. I also experienced scale errors for 3D accelerometers before. But data acquisition for compensation of accelerometer hard,soft and scale effects will be difficult unless eliminating inertial accelerations during the recording of accel data with different orientations.

Best all,


For documentation


Views: 4573


Reply to This

Replies to This Discussion

Nice work!!  I left off in doing this sort of thing many months ago when I ran into snags with the soft iron calibration and then ran into bigger issues that led to ditching a magnetometer for my rover. I never got it really figured out. Glad to see you got this sorted out and can't wait to take a look!

The problem is that we don't do any real calibration at all.

It's like you're trying to describe the best way to tune a jet engine to a grease monkey auto mechanic that didn't graduate high school.

You'd be better off just writing a calibration algorithm and telling us it's mo' better cause it gots the secret sauce. 

Thanks Bot, you are welcome...


Great Work,

I tried your code and seems to be working fine.

                                 Fig 1. before calibration, (Mx)

                                   Fig 2: after calibration


I am new to compass and took some time to get till here. My only concern is, have you implemented it in hardware and has it worked as it has in the papers? I want to get my compass calibrated soon, do you have any special concerns or suggestion??

I first use my matlab code to calculate offsets and inverseW matrix  from offline mag data. Then i use the calculated offsets and inverseW matrix in autopilot software directly. I have not experienced any problems in implementing theory so far. I have also suggested this algorithm for APM planner before.

Locate your compass as far as possible for all current carrying wires. Record calibration data in a magnetically clean environments (generally outdoor). Make sure you have independent enough number of magnetic data  by rotating the drone around at least three orthogonal axises.



Reply to Discussion



Season Two of the Trust Time Trial (T3) Contest 
A list of all T3 contests is here. The current round, the Vertical Horizontal one, is here

© 2019   Created by Chris Anderson.   Powered by

Badges  |  Report an Issue  |  Terms of Service