Skip to content

Accelerometers

labda.accelerometers.AutoCalibrate dataclass

Automated gravitational calibration of tri-axial accelerometer data.

This class corrects gain (scale) and offset errors by identifying stationary periods in the data where the magnitude of the acceleration vector should theoretically be exactly 1 g.

Attributes:

Name Type Description
window str | timedelta

The window size for resampling and stationarity checks (e.g., "10s"). Defaults to "10s".

tolerance float

Maximum standard deviation (in g) allowed for a window to be considered stationary. Defaults to 0.015.

samples int

Minimum number of stationary windows required to attempt calibration. Defaults to 50.

calib_cube float

Coverage threshold. Ensures stationary points exist at least calib_cube g away from the origin on all axes (both positive and negative) to prevent overfitting to a single orientation. Defaults to 0.3.

max_iter int

Maximum number of optimization iterations.

improv_tol float

Minimum relative error improvement required to continue optimization iterations.

err_tol float

Target mean error (in g) for successful calibration.

compute

compute(df: DataFrame) -> tuple[pd.DataFrame, dict]

Applies calibration to the provided DataFrame.

If sufficient stationary data is found and the algorithm converges, the returned DataFrame will contain corrected values. If calibration fails (e.g., not enough stationary periods or poor coverage), the original data is returned, and a warning is logged.

Parameters:

Name Type Description Default
df DataFrame

Input dataframe containing raw accelerometer data. Must contain columns: acc_x, acc_y, acc_z.

required

Returns:

Type Description
tuple[DataFrame, dict]

tuple[pd.DataFrame, dict]: A tuple of: - A DataFrame. Its column set and dtype are guaranteed ONLY when status == "successful": a new frame with exactly acc_x, acc_y, acc_z, cast to float32. On "already_ok" and "failed", this is the literal input df returned unchanged (same object, same columns, same dtype) -- calibration was not applied, so there is nothing to reconstruct. - An info dict with the fitted coefficients and outcome: offset (np.ndarray, shape (3,)), scale (np.ndarray, shape (3,)), error (float), and status (one of "successful", "already_ok", "failed"). On "failed", offset is zeros(3), scale is ones(3) (the identity transform, not a fit), and error is NaN. On "already_ok", offset/scale are the same hardcoded identity -- NOT a small genuine fit, the data was simply judged close enough already -- and error is the real init_error that was measured to make that judgment. On "successful", all four are the fitted values. Either way, the coefficients can always be applied via offset + raw * scale, so callers never need to branch on status just to decide whether they may use them.