Imu gravity removal

From wenmin-wu/ds-skills · View interactive version

Imu gravity removal is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Remove gravity component from raw accelerometer data using quaternion rotation to yield linear acceleration

How to use this skill

Skill file

IMU Gravity Removal

Overview

Raw accelerometer readings include gravitational acceleration (~9.81 m/s^2). Use the device's quaternion orientation to rotate the world-frame gravity vector into sensor frame, then subtract it. Essential preprocessing for any wearable/IMU activity recognition task.

Quick Start

import numpy as np
from scipy.spatial.transform import Rotation as R

def remove_gravity(acc_xyz, quaternions, g=9.81):
    """Remove gravity from accelerometer using quaternion orientation.
    
    Args:
        acc_xyz: (N, 3) raw accelerometer [x, y, z]
        quaternions: (N, 4) orientation [x, y, z, w]
    Returns:
        (N, 3) linear acceleration
    """
    gravity_world = np.array([0, 0, g])
    linear = np.zeros_like(acc_xyz)
    for i in range(len(acc_xyz)):
        rot = R.from_quat(quaternions[i])
        gravity_sensor = rot.apply(gravity_world, inverse=True)
        linear[i] = acc_xyz[i] - gravity_sensor
    return linear

Key Decisions

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.