Main Content

magcal

Magnetometer calibration coefficients

Description

[A,b,expmfs] = magcal(D) returns the coefficients needed to correct uncalibrated magnetometer data D.

To produce the calibrated magnetometer data C, use equation C = (D-b)*A. The calibrated data C lies on a sphere of radius expmfs.

example

[A,b,expmfs] = magcal(D,fitkind) constrains the matrix A to be the type specified by fitkind. Use this syntax when only the soft- or hard-iron effect needs to be corrected.

Examples

collapse all

Generate uncalibrated magnetometer data lying on an ellipsoid.

c = [-50; 20; 100]; % Ellipsoid center
r = [30; 20; 50]; % Semiaxis radii

[x,y,z] = ellipsoid(c(1),c(2),c(3),r(1),r(2),r(3),20);
D = [x(:),y(:),z(:)];

Correct the magnetometer data so that it lies on a sphere. The option for the calibration is set by default to 'auto'.

[A,b,expmfs] = magcal(D); % Calibration coefficients
expmfs % Display the expected magnetic field strength in uT
expmfs = 
31.0723
C = (D-b)*A; % Calibrated data

Visualize the uncalibrated and calibrated magnetometer data.

figure(1)
plot3(x(:),y(:),z(:),"LineStyle","none","Marker","X","MarkerSize",8)
hold on
grid(gca,"on")
plot3(C(:,1),C(:,2),C(:,3),"LineStyle","none","Marker","o", ...
      "MarkerSize",8,"MarkerFaceColor","r")
axis equal
xlabel("uT")
ylabel("uT")
zlabel("uT")
legend("Uncalibrated Samples","Calibrated Samples","Location","southoutside")
title("Uncalibrated vs Calibrated" + newline + "Magnetometer Measurements")
hold off

Figure contains an axes object. The axes object with title Uncalibrated vs Calibrated Magnetometer Measurements, xlabel uT, ylabel uT contains 2 objects of type line. One or more of the lines displays its values using only markers These objects represent Uncalibrated Samples, Calibrated Samples.

Input Arguments

collapse all

Input matrix of raw magnetometer data, specified as a N-by-3 matrix. Each column of the matrix corresponds to the magnetometer measurements in the first, second and third axes, respectively. Each row of the matrix corresponds to a single three-axis measurement.

Data Types: single | double

Matrix type for output A. The matrix type of A can be constrained to:

  • 'eye' – identity matrix

  • 'diag' – diagonal

  • 'sym' – symmetric

  • 'auto' – whichever of the previous options gives the best fit

Output Arguments

collapse all

Correction matrix for the soft-iron effect, returned as a 3-by-3 matrix.

Correction vector for the hard-iron effect, returned as a 3-by-1 array.

Expected magnetic field strength, returned as a scalar.

More About

collapse all

References

[1] Ozyagcilar, T. "Calibrating an eCompass in the Presence of Hard and Soft-iron Interference." Freescale Semiconductor Ltd. 1992, pp. 1-17.

Extended Capabilities

expand all

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Version History

Introduced in R2019a

expand all

See Also

Classes

Objects