dispel.signal.accelerometer module#

Accelerometer functionality for signal processing tasks.

dispel.signal.accelerometer.GRAVITY_CONSTANT = 9.80665#

The gravitational acceleration near Earth’s surface.

dispel.signal.accelerometer.apply_rotation_matrices(rotation_matrices, sensor)[source]#

Apply rotation matrices on a sensor time series.

Parameters:
Returns:

The rotated sensor values based on rotation_matrices.

Return type:

pandas.DataFrame

dispel.signal.accelerometer.compute_quaternion_between_vectors(v_1, v_2)[source]#

Compute quaternion given two vectors.

Parameters:
  • v_1 (ndarray) – An np.ndarray of shape (n_samples, 3) denoting the first vector

  • v_2 (ndarray) – An np.ndarray of shape (n_samples, 3) denoting the second vector

Returns:

  • numpy.ndarray – An array containing the quaternion of shape (n_samples, 4).

  • See proposed solution with pseudocode here

  • https (//stackoverflow.com/questions/1171849/finding-quaternion-)

  • representing-the-rotation-from-one-vector-to-another

Return type:

ndarray

dispel.signal.accelerometer.compute_rotation_matrices(gravity, target_gravity)[source]#

Compute rotation matrices based on gravity time series.

Parameters:
  • gravity (DataFrame) – The gravity time series obtained from the accelerometer sensor.

  • target_gravity (Tuple[float, float, float]) – The unit vector onto which to rotate to

Returns:

A series of rotation matrix objects for the provided gravity entries.

Return type:

pandas.Series

dispel.signal.accelerometer.compute_rotation_matrices_quaternion(gravity, target_gravity)[source]#

Compute rotation matrices from gravity time series (quaternion-based).

Parameters:
  • gravity (DataFrame) – The gravity time series obtained from the accelerometer sensor.

  • target_gravity (Tuple[float, float, float]) – The unit vector onto which to rotate to

Returns:

A series of rotation matrix objects for the provided gravity entries.

Return type:

pandas.Series

dispel.signal.accelerometer.dot_diag_einsum(v_1, v_2)[source]#

Get the diagonal of the dot product of two 2D arrays (fast).

This function is based on np.einsum configured in such a way to provide the dot product diagonal of two 2D arrays. For benchmark example (n_samples=18000), runtime is 2 ms.

Parameters:
  • v_1 (ndarray) – An np.ndarray of shape (n_samples, 3)

  • v_2 (ndarray) – An np.ndarray of shape (n_samples, 3)

Returns:

An array of shape (n_samples, 1) containing the dot product diagonal

Return type:

numpy.ndarray

dispel.signal.accelerometer.dot_diag_list(v_1, v_2)[source]#

Get the diagonal of the dot product of two 2D arrays (slow).

This approach computes the dot product for each time sample of the arrays of shape (n_samples, 3). For benchmark example (n_samples=18000), runtime is 60 ms.

Parameters:
  • v_1 (ndarray) – An np.ndarray of shape (n_samples, 3)

  • v_2 (ndarray) – An np.ndarray of shape (n_samples, 3)

Returns:

An array of shape (n_samples, 1) containing the dot product diagonal

Return type:

numpy.ndarray

dispel.signal.accelerometer.dot_diag_matrix(v_1, v_2)[source]#

Get the diagonal of the dot product of two 2D arrays (very slow).

This approach computes the dot product array of two arrays of shape (n_samples, 3) and then takes the diagonal. For benchmark example (n_samples=18000), runtime is 2 seconds.

Parameters:
  • v_1 (ndarray) – An np.ndarray of shape (n_samples, 3)

  • v_2 (ndarray) – An np.ndarray of shape (n_samples, 3)

Returns:

An array of shape (n_samples, 1) containing the dot product diagonal

Return type:

numpy.ndarray

dispel.signal.accelerometer.orthogonal(v_1)[source]#

Find orthogonal quaternion to a vector provided.

Based on C++ implementation here: PX4/PX4-Matrix (lines 192-224)

Parameters:

v_1 – An np.ndarray of shape (3, )

Returns:

An array of shape (4,) containing the 180 deg quaternion rotation

Return type:

numpy.ndarray

dispel.signal.accelerometer.quaternion_arr_normalize(q_arr)[source]#

Normalize an array of quaternions.

Parameters:

q_arr (ndarray) – A numpy.ndarray of shape (n_samples, 4) containing the quaternions.

Returns:

The unit quaternion in the same format as the input.

Return type:

numpy.ndarray

dispel.signal.accelerometer.quaternion_rotate_vector(q_ba_np, v_a_np)[source]#

Rotate a time series of a 3d vector by a quaternion.

Parameters:
  • q_ba_np (ndarray) – A numpy.ndarray of shape (n_samples, 4) containing the quaternions, expressing the rotation from coordinate frame a to b.

  • v_a_np (ndarray) – A numpy.ndarray of shape (n_samples, 3) containing the vector, expressed in coordinate frame a.

Returns:

A numpy.ndarray of shape (n_samples, 3) containing the vector, expressed in coordinate frame b.

Return type:

numpy.ndarray

dispel.signal.accelerometer.remove_gravity_component(data)[source]#

Remove the gravity component of acceleration.

Based on paper : Two-stage Recognition of Raw Acceleration Signals for 3-D Gesture-Understanding Cell Phones, Cho et al., 2006

Get the linear accelerations - without gravity component - by subtracting the mean acceleration signals, such that \(A_1(t) = A(t) - A_mean where A(t) = [a_x(t),a_y(t),a_z(t)]\)

Parameters:

data (DataFrame) – Input acceleration data

Returns:

A tuple with first, the acceleration data with mean removed and second the mean.

Return type:

Tuple[pandas.DataFrame, float]

dispel.signal.accelerometer.remove_gravity_component_ori(acc_sensor, q_global_sensor, unit='g')[source]#

Remove the gravity component of acceleration based on orientation.

Get the linear accelerations - without gravity component - by converting to the global coordinate frame, subtracting the constant gravity and converting back to the initial sensor frame.

Parameters:
  • acc_sensor – Input acceleration data expressed on the sensor frame.

  • q_global_sensor – Input orientation from sensor to global coordinate frame.

  • unit (str) – The unit in which the accelerometer data is expressed.

Returns:

A tuple with first, the acceleration data with mean removed and second the gravity.

Return type:

Tuple[pandas.DataFrame, pandas.DataFrame]

dispel.signal.accelerometer.transform_ap_ml_acceleration(data)[source]#

Transform accelerometer axis from X,Y,Z to AP,ML, and v.

Parameters:

data (DataFrame) –

Return type:

DataFrame