gaitmap.trajectory_reconstruction.MadgwickAHRS#
- class gaitmap.trajectory_reconstruction.MadgwickAHRS(beta: float = 0.2, initial_orientation: ndarray | Rotation = cf(array([0., 0., 0., 1.])), memory: Memory | None = None)[source]#
The MadwickAHRS algorithm to estimate the orientation of an IMU.
This method applies a simple gyro integration with an additional correction step that tries to align the estimated orientation of the z-axis with gravity direction estimated from the acceleration data. This implementation is based on the paper [1]. An open source C-implementation of the algorithm can be found at [2]. The original code is published under GNU-GPL.
- Parameters:
- betaflat between 0 and 1
This parameter controls how harsh the acceleration based correction is. A high value performs large corrections and a small value small and gradual correction. A high value should only be used if the sensor is moved slowly. A value of 0 is identical to just the Gyro Integration (see also
gaitmap.trajectory_reconstruction.SimpleGyroIntegrationfor a separate implementation).- initial_orientation
The initial orientation of the sensor that is assumed. It is critical that this value is close to the actual orientation. Otherwise, the estimated orientation will drift until the real orientation is found. In some cases, the algorithm will not be able to converge if the initial orientation is too far off and the orientation will slowly oscillate. If you pass a array, remember that the order of elements must be x, y, z, w.
- memory
An optional
joblib.Memoryobject that can be provided to cache the calls to madgwick series.
- Other Parameters:
- data
The data passed to the estimate method
- sampling_rate_hz
The sampling rate of this data
- Attributes:
orientation_Orientations as pd.DataFrame.
- orientation_object_
The orientations as a single scipy Rotation object
rotated_data_Rotated data.
See also
gaitmap.trajectory_reconstructionOther implemented algorithms for orientation and position estimation
gaitmap.trajectory_reconstruction.StrideLevelTrajectoryApply the method for each stride of a stride list.
Notes
This class uses Numba as a just-in-time-compiler to achieve fast run times. In result, the first execution of the algorithm will take longer as the methods need to be compiled first.
[1]Madgwick, S. O. H., Harrison, A. J. L., & Vaidyanathan, R. (2011). Estimation of IMU and MARG orientation using a gradient descent algorithm. IEEE International Conference on Rehabilitation Robotics, 1-7. https://doi.org/10.1109/ICORR.2011.5975346
Examples
Your data must be a pd.DataFrame with columns defined by
SF_COLS.>>> import pandas as pd >>> from gaitmap.utils.consts import SF_COLS >>> data = pd.DataFrame(..., columns=SF_COLS) >>> sampling_rate_hz = 100 >>> # Create an algorithm instance >>> mad = MadgwickAHRS(beta=0.2, initial_orientation=np.array([0, 0, 0, 1.0])) >>> # Apply the algorithm >>> mad = mad.estimate(data, sampling_rate_hz=sampling_rate_hz) >>> # Inspect the results >>> mad.orientation_ <pd.Dataframe with resulting quaternions> >>> mad.orientation_object_ <scipy.Rotation object>
Methods
clone()Create a new instance of the class with all parameters copied over.
estimate(data, *, sampling_rate_hz, **_)Estimate the orientation of the sensor.
from_json(json_str)Import an gaitmap object from its json representation.
get_params([deep])Get parameters for this algorithm.
set_params(**params)Set the parameters of this Algorithm.
to_json()Export the current object parameters as json.
- __init__(beta: float = 0.2, initial_orientation: ndarray | Rotation = cf(array([0., 0., 0., 1.])), memory: Memory | None = None) None[source]#
- clone() Self[source]#
Create a new instance of the class with all parameters copied over.
This will create a new instance of the class itself and all nested objects
- estimate(data: DataFrame, *, sampling_rate_hz: float, **_) Self[source]#
Estimate the orientation of the sensor.
- Parameters:
- data
Continuous sensor data including gyro and acc values. The gyro data is expected to be in deg/s!
- sampling_rate_hz
The sampling rate of the data in Hz
- Returns:
- self
The class instance with all result attributes populated
- classmethod from_json(json_str: str) Self[source]#
Import an gaitmap object from its json representation.
For details have a look at the this example.
You can use the
to_jsonmethod of a class to export it as a compatible json string.- Parameters:
- json_str
json formatted string
- get_params(deep: bool = True) dict[str, Any][source]#
Get parameters for this algorithm.
- Parameters:
- deep
Only relevant if object contains nested algorithm objects. If this is the case and deep is True, the params of these nested objects are included in the output using a prefix like
nested_object_name__(Note the two “_” at the end)
- Returns:
- params
Parameter names mapped to their values.