# Technical Explanation – Spherical Calibration

#### Theoretical description

What is it?

Few, if any, biologging tags produce accurately-calibrated sensor data that is ready to use – it is up to you to calibrate and check the data. Calibration generally involves measuring the sensor output when a known input is applied, e.g., the pressure sensor output when the tag is submerged to 100 m depth. For most sensors this requires special test equipment but for triaxial accelerometers and magnetometers, you get the calibration signal for free! Accelerometers and magnetometers measure the Earth’s gravity and magnetic fields, respectively, both of which are, at least locally, constant. If a tag is outside and still, the magnitude of the accelerometer and magnetometer vectors (i.e., the square-root of the sum of the x, y, and z axes squared) should equal the corresponding field strength of the Earth, no matter how the tag is oriented, if the sensor is correctly calibrated.

Spherical calibration uses the known field strength to infer the calibration values for each axis of the accelerometer or magnetometer. The input data is simply a recording made by the tag in several different orientations. The tool uses this to compute the calibration values, i.e., the offset and gain of each axis, that give the best fit to the known field strength. You don’t need the tag to be in specific orientations, you just need it to be in a few different ones. The easiest way to ensure this is to rotate the tag around all three axes slowly and smoothly. Moving the tag smoothly is important to minimise specific acceleration which would interfere with the calibration. It is also essential to make the calibration recording outside and away from any electrical wiring, metal and machinery. This is because magnetic fields generated by power wiring or retained by metal can be much larger than the Earth’s own magnetic field and will swamp the measurement.

How is it measured?

The tool `spherical_cal()` needs two inputs: (i) the accelerometer or magnetometer data that you recorded while rotating the tag outside, and (ii) the field strength. For the accelerometer, the gravity field strength is very close to 9.81 m/s2 anywhere that you are likely to calibrate a tag. The magnetic field varies regionally but there are accurate online calculators such as this one by NOAA. The units you use for the field strength determine the units of the acceleration and magnetic field data when you apply the calibration values. We recommend that you use m/s2 for acceleration and µT (micro-Tesla) for magnetic field.

`spherical_cal()` uses a search algorithm called simplex to find the calibration values that give the best fit to the field strength. A third input to `spherical_cal()` tells the tool what kind of calibration to perform. Usually we want the gain and offset of each axis (this is the ‘gain’ option). A different option, can be used with the magnetometer if the tag contains any iron or nickel (e.g., steel bolts). Ferromagnetic metals warp the magnetic field passing through the tag. To correct for this so-called ‘soft iron’ effect, use the ‘cross’ option if needed when calibrating the magnetometer.

`spherical_cal()` reports the residual error and the axial balance of the data after fitting which let you know if you did a good job when rotating the tag. A low residual e.g., <5% indicates that the data can be calibrated well and there is not much noise whereas a high residual error means that you did not move the tag smoothly enough. The axial balance indicates whether the movement in sensor data stream `X` is suitable for data-driven calibration. If the movement covers all directions fairly equally, the axial balance will be high. A low axial balance (<20%) means that you didn’t rotate the tag thoroughly enough (e.g., you only rotated around one or two of the three axes) and may lead to unreliable calibration. For bench calibrations, a high axial balance is achieved by rotating the sensor through the full 3-dimensions.

What is it good for?

Spherical calibration is a simple and reliable way to make an initial calibration of the accelerometer and magnetometer. If the temperature of the tag is similar during calibration and deployment, the calibration values may be close enough to correct the entire deployment. But even if the tag experiences different temperatures or the sensors drift during a deployment, the spherical calibration values are a good starting point for data-driven calibration. Spherical calibration is also a quick way to check that the sensors are working properly, in particular to catch if the magnetometer has saturated or if any metal in the tag has acquired a magnetic field. It’s a good idea to keep a record of the calibration values for each sensor and re-do the calibration from time-to-time.

#### Technical description

Usage of tool

In this vignette you will learn to use `spherical_cal()` to deduce the calibration constants for a triaxial field sensor, such as an accelerometer or magnetometer, based on movement data. This can be used to do a ‘bench’ calibration of a sensor.

MATLAB
```[Y,G] = spherical_cal(X,n,method);
```
R
```[Y,G] <- spherical_cal(X,n,method)
```
###### Inputs:
• `X`: is the segment of triaxial sensor data to calibrate. It must be a 3-column matrix. `X` can come from any triaxial field sensor and can be in any unit and any frame.
• `n`: is the target field magnitude e.g., 9.81 for accelerometer data using m/s2 as the unit.
• `method`: is an optional string selecting the type of calibration. The default is to calibrate for offset and scaling only. Other options are: `'gain'`: adjust gain of axes 2 and 3 relative to 1. `'cross'`: adjust gain and remove cross-axis correlations.
###### Outputs:
• `Y`: is the matrix of converted sensor values. These will have the same units as for input argument `n`. If `n` is not specified, the units of `Y` are the same as for the input data. The size of `Y` is the same as the size of `X` and it has the same frame and sampling rate.
• `G`: is the calibration structure containing fields:
• `G.poly` is a matrix of polynomials. The first column of `G.poly` is the three scale factors applied to the columns of `X`. The second column is the offset added to each column of `X` after scaling.
• `G.cross` is a `3x3` matrix of cross-factors. If there are no cross-terms, this is the identity matrix. Off-axis terms correct for cross-axis sensitivity.

This function uses a Simplex search for optimal calibration parameters and so can be slow if the data size is large. For this reason it is most suitable for bench calibrations rather than field data. This function is only usable for field sensors. It will not work for gyroscope data.