> For the complete documentation index, see [llms.txt](https://help.imotions.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://help.imotions.com/lab/analysis/biosensor-analysis/r-notebook-accelerometer-movement-detection.md).

# R Notebook: Accelerometer Movement Detection

Run the Accelerometer Movement Detection R notebook to detect movement onsets, offsets, duration and peak acceleration per stimulus in iMotions Lab.

*The Accelerometer Movement Detection R Notebook computes the movement onsets and offsets, the duration of the movement, the magnitude of the acceleration and the peak of each movement. Movement detection can provide information about when a respondent is moving a limb or the whole body in the stimuli, which in combination with other sensors can provide additional insights. For GSR and EEG, this can be used to filter out when data is filled with noise due to movement. For FEA, this can be used to inspect how respondents were reacting while/after doing a specific movement e.g. moving a perfume up to the nose to smell it etc.*

## Accelerometry Overview

#### What is Accelerometry and how are movements defined?

Acceleration is the rate of change of the velocity of an object with respect to time \[1]. This means that an object accelerates when it changes speed or direction or both. Acceleration measured by a triaxial accelerometer is defined as a vector, which indicates the direction and magnitude in 3D space. Therefore raw acceleration is given by 3 signals; acceleration in the X, Y and Z direction all with the unit m/s². More information about how the accelerometer measures acceleration can be found [here.](https://docs.madmachine.io/learn/peripherals/accelerometer)

Here, movements are defined as the magnitude of acceleration being higher than a user defined threshold. This is done by filtering out other forces influencing the acceleration on the object like eg. gravity. Acceleration due to gravity is approximately 9.8 m/s². Taking the magnitude of all 3 acceleration signals will eliminate the direction, but it is redundant as we are only interested in when the movement happened and the magnitude of the movement. As the movement is based on acceleration, this means that constant velocity will not be detected as movement. However, moving the human body is very unlikely to be with constant velocity unless there is no movement, but this should be kept in mind when recording acceleration data for movement detection.

### Prerequisites

**Supported sensors with accelerometer data:** Shimmer GSR, Shimmer EXG, Pupil Invisible, Pupil Labs Neon Glasses, Neuroelectrics Enobio

**Supported iMotions version:** iMotions 10.1.10 and newer

## How to Analyze Accelerometry in iMotions

1\. Create a new analysis for the study that you wish to run the R-Notebook for:

![The Add Analysis button on a study in the Library](https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-0b352d688758cd75a0a20712355835132fb525d1%2Flab_library_add-analysis-button.png?alt=media)

2\. Advance through the steps "Segmentation Settings" and "Segmentation Criteria", with your preferred selections, until you reach the step named "Signal processing".

3\. By clicking "+" next to "Active flows" you can select the algorithms you want to use for signal processing. Select the Accelerometer sensor (for example "Shimmer BE7E GSR") for which you want to apply the Accelerometer Movement Detection R Notebook in the drop-down "Device" menu. Select the algorithms in the drop-down “Algorithm” menu.

![](https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-64e970cd42490f0b1fd7302a162f3a77b90adc0f%2Fb729e619ef_22-1-.webp?alt=media)

4\. You can name the analysis in the "Flow name" menu. Alter the parameters to suit your research needs, then click on “Next”. The parameters will be described in detail in the next section of this article: Algorithm Introduction.

![](https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-9031756d864ffd401ea2d8a1714a48f187803743%2F9c74004ca0_image-T8AFX50V.webp?alt=media)

5\. Select the stimulus you wish to be included in the analysis. Then click the "Add" button to add the analysis and close the dialog.

![](https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-b2021868b5142c8d748ba8c6cff6ad08583779ab%2Fdbb9a1553c_image-1739873012624.webp?alt=media)

### Algorithm Overview

1. **Data Retrieval:** The raw acceleration signals from the sensor are retrieved for a given respondent (expressed in meters per second squared, $$m/s^2$$).
2. **Data Interpolation:** Possible gaps in the signal due to signal drops are identified and linearly interpolated, if the gap is no longer than 4s as default (this is defined in the *Gap interpolation length threshold* parameter). Else, each fragment of signal is analyzed separately.
3. **Filtering:** A first-order high-pass butterworth filter is applied to the three acceleration signals to remove gravity component from the signal. This will isolate the acceleration made from moving the accelerometer. This is the step where the high-pass filter cutoff parameter is used. Default parameter value is 1.5 Hz \[2].
4. **Rectification:** A positive full-wave rectification is performed on the signals to have all detected acceleration in the positive polarity.
5. **Smoothing:** A first-order low-pass butterworth filter is applied to the three acceleration signals to remove noise from the signals and make the signals more smooth. This is the step where the low-pass filter cutoff parameter is used. Default parameter value is 0.8 Hz \[2]. Lowering this value will make the signal even more smooth. This is advantageous when wanting to detect overall movement as one big movement, but will also detect small breaks between movements as movement. Increasing this value will make the signal less smoothed. This is advantageous when wanting to separate all small movement, but can also introduce noise as movements.
6. **Magnitude calculation:** The magnitude is calculated to go from three axis acceleration data to one combined signal. The magnitude is calculated from the smoothed signal using the following formula: $$|a| = \sqrt(a\_x^2 + a\_y^2 + a\_z^2)$$ , where $$|a|$$ is the acceleration magnitude and $$a\_x, a\_y, a\_z$$ are the acceleration along the different axes.
7. **Movements:** A threshold is applied to the acceleration magnitude signal, where onsets and offsets of the movements are detected. Onsets are all the points where the acceleration signal crosses from below the threshold value to above. Offsets are the points where the acceleration signal crosses from above the threshold value to below. The duration of a movement is the time between the onset/offset pair.

   The threshold can be defined manually (using the parameter *Threshold defining onset-offset of movement* or with default value of 0.2 m/s²) or with the Baseline stimulus, which is using the following calculation: $$Threshold = P\_{95} \cdot 1.1$$ , where $$P\_{95}$$ is the 95th percentile of the baseline acceleration signal, which means that the threshold consists of 95th percentile plus 10% of this value. The baseline stimulus is a recording of the acceleration during a posture with no movement e.g. by standing, sitting or lying depending on the study design.

   To deal with the acceleration signal fluctuating around the threshold, movements separated by a time duration inferior to 350 ms as default are merged together (defined by the parameter *Minimum duration between two movements to be considered as separated*). In addition, movements detected due to small artifacts in the signal are removed if the duration is below 70 ms as default (defined by the parameter *Minimum movement duration*).
8. **Movement peaks:** The maximum acceleration value within a movement is defined as the movement peak.

## iMotions Outputs

### Signals in Analysis Replay

In the individual replays of data, you can visualize signals and calculated metrics as well as automated movement onsets/offsets/peaks markers, displayed as follows:

![](https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-664603bc48d1600ceb30866d10c98410a7a1d58d%2F456b56b9b7_image-1739958695247.webp?alt=media)

* **Signals**:
  * *Accelerometer:*
    * **Acceleration X:** Interpolated acceleration along the x-axis.
    * **Acceleration Y:** Interpolated acceleration along the y-axis.
    * **Acceleration Z:** Interpolated acceleration along the z-axis.
    * **Acceleration Magnitude:** The magnitude calculation of acceleration in the three axis (x, y, z).
  * *Movement:*
    * **Movement Detected (binary):** Binary signal, where accelerometry samples falling within the onset and offset of the detected movement are assigned a value of 1. Samples outside movements are assigned a value of 0.
    * **Movement Peak Occurence (binary):** Binary signal, where the time of maximum acceleration of each detected movement is assigned a value of 1. Other samples are assigned a value of 0.
* **Metrics**:
  * **Movement count:** Count of movement.
  * **Total movement time \[ms]**: Total amount of the current stimulus during which the respondent was classified to move.
  * **Total movement percentage \[%]:** Percentage of the current stimulus during which the respondent was classified to move.
  * **Average movement duration \[ms]:** Average duration of all movements classified in the current stimulus.
  * **Average acceleration magnitude \[m/s^2]:** Average total acceleration change of all movements classified in the current stimulus. Magnitude is derived from the change in acceleration on all axis.
  * **Average peak acceleration \[m/s^2]:** Average maximum acceleration of all movements classified in the current stimulus.
  * **Movement threshold \[m/s^2]:** The threshold value used to define movements onsets and offsets.
* **Markers:**

  * **Movement onset:** Markers placed where movement onsets occurred.
  * **Movement offset:** Markers placed where movement offsets occurred.
  * **Movement peak:** Markers placed where movement peaks occurred.

  These markers can be used to automatically create annotations, for example to segment the parts where a movement occurred from where it did not, or to segment a certain duration from the start or end of movement. More information about automatic annotations can be found [here](https://help.imotions.com/lab/analysis/biosensor-analysis/r-notebook-accelerometer-movement-detection.md).

### Exported Files

There are three types of files you can export: Sensor Data files, and two types of R processing files: Movement metrics file (called AccMetrics.csv) and Movement table file (called AccMovementTable.csv).

* **Sensor Data file:** This is the raw sensor data export. It can also include R-processed signals by ticking the "**R analysis Accelerometer Movement Detection**" shown in the screenshot below.

![](https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-bf52618be29a3c262c2347db026e3aad237a1bed%2F2301635274_image-IP5L6ZQK.webp?alt=media)![](https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-1d9a5aa4ac889af8f742561f97312fe38d288b3b%2Fa66bf16f2f_image-1739970301490.webp?alt=media)The SensorData file can have the following layout:

![](https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-58883ca03c107bedebbb6827a1d66553580fa7a6%2Fd4b6a9b88c_image-1739971892516.png?alt=media)

* **Movement metrics file:** This file includes the processed results from R Notebooks across respondents and stimuli. You can access the exported file by (1) clicking on the 'Export' button, (2) under 'R PROCESSING', you can (3) select to have Individual metrics exports (to obtain a separate export file per modality) or a Single multi-modal export (relevant when you use more than one modality), you can click on (4) 'Accelerometry Metrics Export'. Here, when you click the drop down menu, you can visualize further options as illustrated as follows:

![](https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-3a86a3ecbea7726940213e554e4e57e7ab8dce0f%2Fd0e59d70e0_image-1739972877947.webp?alt=media)

When you finally (5) click the blue Export button, you will export the Movement metrics file (AccMetrics.csv), which has the following layout:

![](https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-3234f8bbcbb7df801b535c148bd7b0c0dedd1dd4%2F28cf804555_Screenshot-2025-03-31-101057-1-.png?alt=media)

**Descriptions on the Movement metrics file:**

This file is called AccMetrics file and is exported in .csv format. The file has one row for each respondent and each **stimulus**/**scene**/**scene fragment**/**annotation**/**annotation instance** within the study. Each row contains information about detected movement metrics for this respondent during that stimulus/scene/scene fragment/annotation/annotation instance.

| **Header**                              | **Explanation**                                                                                                                                                     |
| --------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Study Name                              | The name of the study                                                                                                                                               |
| Respondent Name                         | The respondent under a certain stimulus type                                                                                                                        |
| Gender                                  | The gender of the respondent under a certain stimulus type                                                                                                          |
| Age                                     | The age of the respondent under a certain stimulus type                                                                                                             |
| Group                                   | The group variables of the respondent you specified when creating the study                                                                                         |
| Type                                    | Type of the stimulus during which these respiration peaks were experienced: Stimulus, Scene, Scene (Fragment), Annotation, Annotation (Instance)                    |
| Label                                   | The given name of the stimulus, scene, and annotation                                                                                                               |
| Start \[ms]                             | The start time (in ms) of the certain stimulus type relative to the start of the study                                                                              |
| Duration \[ms]                          | The duration (in ms) of the certain stimulus type                                                                                                                   |
| Parent Stimulus                         | The name of the stimulus where the scene was created from; the name of the stimulus/scene where the annotation was created from                                     |
| Movement Count                          | Count of movement in the current interval.                                                                                                                          |
| Time Moving \[ms]                       | Total amount of time during which the respondent was classified to move in the current interval.                                                                    |
| Percentage Moving \[%]                  | Percentage of the current interval during which the respondent was classified to move.                                                                              |
| Average Movement Duration \[ms]         | Average duration of all movements classified in the current interval.                                                                                               |
| Average Acceleration Magnitude \[m/s^2] | <p>Average total acceleration change of all movements classified in the current interval. Magnitude is derived from the change in acceleration on all axis.<br></p> |

* **Movement Table** (called AccMovementTable.csv)

The Movement Table describes the index number, the onset, offset, duration and peak of each Movement per respondent and per stimulus/scene/scene fragment/annotation/annotation instance. In these exports, you will find a column called Truncation type that will describe whether the identified Movement is truncated (incomplete for the given stimulus/scene/annotation) or whole (complete).

![](https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-3d18b3590d329e1cf3481646521d36e774afdcca%2F48d9bc4894_image-1740046924405.png?alt=media)

**Descriptions on the Movement table file:**

This file is called AccMovementTable file and is exported in .csv format. The file has one row for each detected movement within the study. Each row contains information about the event of the detected movement for this respondent during that stimulus/scene/scene fragment/annotation/annotation instance.

| **Header**                               | **Explanation**                                                                                                                                                                               |
| ---------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Study Name                               | The name of the study                                                                                                                                                                         |
| Respondent Name                          | Name of the respondent.                                                                                                                                                                       |
| Respondent Gender                        | The gender of the respondent.                                                                                                                                                                 |
| Respondent Age                           | The age of the respondent.                                                                                                                                                                    |
| Respondent Group                         | The group of the respondent.                                                                                                                                                                  |
| Stimulus Label                           | <p>The name of the stimulus.<br></p>                                                                                                                                                          |
| Stimulus Start \[ms]                     | <p>The start time of the stimulus (relative to recording start).<br></p>                                                                                                                      |
| Stimulus Duration \[ms]                  | <p>The duration of the stimulus.<br></p>                                                                                                                                                      |
| Movement Index                           | <p>Movement number, counting from start of the recording.<br></p>                                                                                                                             |
| Movement Index by Stimulus               | <p>Movement number, counting from start of the stimulus.<br></p>                                                                                                                              |
| Movement Onset \[ms]                     | Start time of the movement (relative to recording start).                                                                                                                                     |
| Movement Offset \[ms]                    | End time of the movement (relative to recording start).                                                                                                                                       |
| Movement Duration \[ms]                  | Duration of the movement.                                                                                                                                                                     |
| Movement Acceleration Magnitude \[m/s^2] | The average acceleration magnitude of the movement.                                                                                                                                           |
| Peak Time \[ms]                          | Time when the movement reached its maximum acceleration (relative to recording start).                                                                                                        |
| Peak Acceleration Magnitude \[m/s^2]     | <p>Maximum acceleration magnitude of the movement.<br></p>                                                                                                                                    |
| Truncation Type                          | <p>Whole: The movement started and ended within the interval; Partial: The movement started or ended outside of the interval. Data was truncated to start or end within the interval.<br></p> |
| Device                                   | <p>Name of the sensor used to collect the acceleration data.<br></p>                                                                                                                          |
| Flow Name                                | <p>User-defined name of the algorithm used to perform the movement analysis.<br></p>                                                                                                          |

### Reports

When you click the **Reports** button (as shown below), you can access the entire description of the signal processing steps done starting from the raw unprocessed acceleration signals.

![](https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-652518f730ba11d593f61a98319a94109d7fbf76%2Ffbede4b69f_image-1740051065126.webp?alt=media)

An example of an Accelerometer Movement Detection report is shown as follows:

![](https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-123ccbab6d7374009e763d9bdcd1cb6622120986%2F31bbb56e8c_image-6FP8GTFD.png?alt=media)

## References

\[1] <https://en.wikipedia.org/wiki/Acceleration>

\[2] Veltink, P.H., Hans B. J. Bussmann, W. de Vries, Wim L. J. Martens, and R. C. Van Lummel. 1996. “Detection of Static and Dynamic Activities Using Uniaxial Accelerometers” 4 (4). <https://ieeexplore.ieee.org/abstract/document/547939>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://help.imotions.com/lab/analysis/biosensor-analysis/r-notebook-accelerometer-movement-detection.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
