# IMU Processing

Inertial measurement units (IMUs) are electronic devices that measure and report a body's specific force (acceleration and angular velocity) and orientation (roll, pitch, and yaw). IMU signals are often used to measure activity, posture, and gait. In physioKIT, we provide a variety of routines for processing IMU signals.

The snippets below form a walkthrough: later snippets may reuse variables from earlier ones. The plots are saved example outputs. For a standalone runnable example, start with the [quickstart](https://ambiqai.github.io/physiokit/tutorial/quickstart/).

## Compute ENMO

Euclidean Norm Minus One (ENMO) is a measure of activity intensity. ENMO is calculated as the square root of the sum of the squared acceleration values minus one. ENMO is often used to measure activity intensity from IMU signals.

In the following example, we load accelerometer data from a user's wrist when performing the following sequence of tasks for 20s each: sitting, walking, and running.

From this, we can then easily compute the ENMO:

```python
import physiokit as pk

# Load accelerometer data
ax, ay, az = ...

enmo = pk.imu.compute_enmo(x=ax, y=ay, z=az)
```

---

## Compute Tilt Angles

3-axis tilt provides insight into a user's orientation. For example, a 3-axis accelerometer placed on a user's wrist can be used to determine its tilt. The z-angle of the wrist can be extremely useful for both fall and sleep detection applications.

Following the previous example, we compute the tilt angles of the user's wrist.

```python
import physiokit as pk

# Load accelerometer data
ax, ay, az = ...

aax, aay, aaz = pk.imu.compute_tilt_angles(x=ax, y=ay, z=az)
```

## Compute "Counts"

"Counts" is a measure of activity intensity and typically reported by actigraphy watches. Unfortunately, "Counts" is not a standardized measure and is calculated differently by different manufacturers. In physioKIT, we compute counts based on algorithms reported by [ActiGraph](https://doi.org/10.1038/s41598-022-16003-x).

Again, using the 3-axis accelerometer wrist data, we can compute "counts" as follows. The saved example recording is sampled at 500 Hz; set `fs` to your recording's sampling rate.

```python
import numpy as np
import physiokit as pk

fs = 500  # Hz

# Load accelerometer data
ax, ay, az = ...

counts = np.sum(pk.imu.compute_counts(
    data=np.vstack((ax, ay, az)).T,
    sample_rate=fs,
    epoch_len=1
), axis=1)

```

---

## API

[Refer to IMU API for more details](https://ambiqai.github.io/physiokit/api/physiokit/imu/)
