> 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/eda-gsr/gsr-r-notebooks-peak-detection-and-aggregation.md).

# GSR R-Notebooks: Peak Detection and Aggregation

Run the GSR Peak Detection and Peak Binned Aggregation R notebooks to detect skin conductance peaks as an arousal index and aggregate them across respondents.

Variations in the phasic component of the Galvanic Skin Response (GSR) signal are visible as GSR peaks. This phasic response is sensitive to emotional stimuli and can be considered as an index of autonomic activity. GSR peaks can be indicative of autonomic arousal based on certain stimuli or situations. The aggregation procedure can be used to get insights into the consistency of your respondents' autonomous arousal elicited by the provided stimulus.

This article describes the GSR peak detection and aggregation of GSR peaks across respondents based on our new R-notebook integration.

There are two R-notebooks concerned with (1) **GSR Peak Detection:** the single respondent peak detection and (2) **GSR Peak Binned Aggregation**: the aggregation of the peaks across respondents.

You can:

* Run the notebooks on multiple sensors of the same kind, for example multiple Shimmer sensors.
* Run GSR Peak Binned Aggregation across different GSR sensors.
* Run the notebooks on Empatica E4 GSR data.

## 1. Prerequisites

Supported stimulus types: All

Supported sensors: Shimmer, Biopac, Empatica E4

## 2. How to Analyze in iMotions Desktop

#### As for the GSR Peak Detection R-Notebook, the following steps need to be followed:

1. Create a new analysis for the study for which you wish to run the GSR R-Notebook.

   ![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" with your preferred selections and "Segmentation criteria" until you reach the step "Signal processing".

   ![22.png](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)
3. By clicking "+" next to "Active flows", you can select the algorithms you want to use for signal processing. Select the sensor for which you want to apply the GSR R-Notebook in the drop-down "Device" menu. Select the algorithms in the drop-down “Algorithm” menu.

   ***Note:*** *it is possible now (from version 8.1 and onwards), to run the notebook on multiple sensors of the same kind. E.g., you can run the notebook on the second Shimmer device here by adding Active flow with the device you would like to analyze. If you want to compare results from different parameter combinations on the same device, it is possible now from version 9.3.12 and onwards. Just make sure you give a unique "Flow name" for each "Active flow".*

   ![creating\_analysis.png](https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-12350186067ee418b117505cce4cb4f065248bec%2Fa592bef92b_creating_analysis.webp?alt=media)
4. 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.

   ![Parameters.png](https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-466f5ec6de0386e79b05a6107f7c4aff8d2c628e%2Fc4fd1ccb63_parameters.webp?alt=media)
5. Select the stimulus to be included in the analysis. Then click the "Add" button to add the analysis and close the dialog.

   ![Add\_stimulus.png](https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-9f832540b338e69bb1f540adbb033b83fb67eaac%2F56b681fa40_add_stimulus.webp?alt=media)

#### As for the GSR Peak Binned Aggregation R-Notebook, the following steps need to be followed:

***Note: The GSR Peak Binned Aggregation R-Notebook can only be executed together with the execution of the GSR Peak Detection R-Notebook. This means you need to add two R-Notebooks to the analysis if you want to do aggregation - one is GSR Peak Detection R-Notebook, one is GSR Peak Binned Aggregation R-Notebook.***

The first two steps are the same as described above for GSR Peak Detection R-Notebook. In step 3, please make sure to add two R-Notebooks to the analysis. Alter the parameters to suit your research needs, then click on “Next”. The parameters will be described in detail in the next section.

![Aggregation\_creation.png](https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-2ac0d77c48b159714cf1e5ea885529effa9d7b94%2Fa31fa857d9_aggregation_creation.webp?alt=media)

***Note**:it is possible now (from version 8.1 and onwards) to do GSR Peak Binned Aggregation across different GSR devices. By ticking '**Multiple device aggregation**', the GSR Peak Binned Aggregation R-Notebook will give results from multiple GSR devices* ***that have been added to the analysis**. The '**Multiple device aggregation**' is not ticked by default, in this case, the GSR Peak Binned Aggregation R-Notebook will only give results from one GSR device that you chose to run the R-Notebook on.*

*For example, as shown below, the GSR Peak Binned Aggregation R-Notebook will give results from two Shimmer devices that have been added to the analysis under '**Active flows**'.*

![Multi\_device\_aggregation\_creation.png](https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-2e764a13c053fef21bb2feb22e43e40d9032b26b%2F1765242e72_multi_device_aggregation_creation.webp?alt=media)

Next, the same as described above for the GSR Peak Detection R-Notebook, select the stimulus to be included in the aggregation analysis. Then click the "Add" button to add the aggregation analysis and close the dialog.

## 3.GSR Peak Detection and Aggregation Algorithm

Peak detection is accomplished by separating the faster changes in GSR that reflect short arousal responses, the phasic signal, from the slower background level, the tonic signal.

#### The GSR peak detection algorithm:

The algorithm is determined as follows. Additional information about the methods can be found in the following paper [(Benedek and Kaernbach, 2010)](https://doi.org/10.1016/j.jneumeth.2010.04.028).

1\. The calibrated GSR signal from the sensor (Shimmer or BIOPAC EDA100C) is retrieved for the given stimulus and respondent (expressed in microSiemens, µS). 2. The sample rate is determined.

*Shimmer sensor ONLY*: parameter **\[Gap interpolation length threshold \[ms]] is** used to identify possible gaps in the signal due to signal drops. If a gap in the signal is shorter than this threshold, then the missing data is linearly interpolated. Otherwise, the gap is left and each of the signal fragments is processed separately. Option **\[Remove signal discontinuities caused by the sensor switching range setting]** should only be checked when the recorded signal has many discontinuities caused by the auto-range switching setting of Shimmer. 3. The phasic signal is extracted using a median filter over a fixed length time window: parameter **\[Phasic filter length \[ms]]**. The phasic signal is calculated by subtracting the running median from the calibrated signal, smoothing the ends of the signal by using symmetrical medians of subsequently smaller bandwidth, but for the very first and last value where [Tukey's robust end-point rule](#references) is applied. 4. A low-pass [Butterworth filter](https://en.wikipedia.org/wiki/Butterworth_filter) with cutoff frequency: parameter **\[Lowpass filter cutoff frequency \[Hz]]** is applied to the phasic signal (to remove powerline noise). 5. Onsets and offsets are detected on the phasic signal. Onsets are all the points where the phasic signal crosses above the onset threshold: parameter **\[Peak onset threshold \[microSiemens]]**. Offsets are the points where the phasic signal crosses below the offset threshold: parameter **\[Peak offset threshold \[microSiemens]]**. Each onset-offset pair defines a window, and the maximum value attained by the calibrated signal in a window is marked as a possible peak. The amplitude of a possible peak is defined as the difference between the maximum value of the calibrated signal in the window and its value at the onset point. A possible peak is selected if: (1) Its amplitude is above the amplitude threshold: parameter **\[Peak amplitude threshold \[microSiemens]]**, and (2) the time difference between onset and offset is above the duration threshold: parameter **\[Minimum peak duration \[ms]]**.

The selected peaks are returned.

#### The Peak Binned Aggregation algorithm:

Since GSR peaks across respondents might vary in latency and/or amplitude, the aggregation cannot be a simple average of all GSR waveforms across respondents. Instead, GSR aggregation is accomplished based on the binarization of the signal.

iMotions can count the number of respondents that had "a response" (*at least* one GSR peak) in a time window: parameter **\[Time bin length \[ms]]**. The aggregated result therefore relates directly to the audience and the time window, e.g., 10 out of 29 respondents had a response in the time interval 02:00 - 02:09.

1\. Break the continuous data of each respondent during the selected stimulus into bins with a certain length: parameter **\[Time bin length \[ms]]** (5 seconds; default) and overlap: parameter **\[Time bin overlap \[ms]]** (1 second; default). 2. For each respondent, assign a value of "1" to bins that contain *at least* one GSR peak (including the overlap). Assign a value of "0" to bins that contain no GSR peak (including the overlap). Instead of the actual GSR peak amplitudes, the binary values 0 or 1 are used. 3. Sum up the binary scores for a bin across all respondents. For example, if you collected GSR data from 10 respondents and all of them had at least one GSR peak in a certain bin, then the aggregated value for this bin is 10. If none of your respondents had a GSR peak in the bin, then the aggregated value for this bin is 0.

![Aggregation\_alg\_2\_001.png](https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-acdba9e701fbacb878ffde5b0944be3cb6b8bb5a%2F6639513e13_aggregation_alg_2_001.webp?alt=media)

*GSR aggregation example: In the second bin (5s - 10s), 45 respondents had at least one GSR peak (counted from the bin including the overlap: 4.5s - 10.5s).*

## 4. The Outputs

A more general introduction to R-Notebook outputs in the iMotions is documented *here*.

* Desktop replay: R-Notebook results from GSR Peak Detection and GSR Peak Binned Aggregation

(1) From GSR Peak Detection R-notebook: results of each respondent during a certain stimulus/scene.

Example: Metrics from R-Notebook in the replay:

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

Example: Signals from R-Notebook in the replay:

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

**GSR Peak Detection Metrics:**

1\. **\[Peak count]**: Number of detected peaks the respondent had during this stimulus/scene.

2\. **\[Peaks per minute]**: the number of peaks the respondent had total during this stimulus/scene divided by the duration of the stimulus/scene expressed as peaks/min.

3\. **\[Average peak amplitude]**: Average amplitude of detected peaks the respondent had during this stimulus/scene.

**GSR Peak Detection**:

1. **\[GSR Interpolated]**: GSR signal after interpolation process.
2. **\[Tonic Signal]**: a slow-varying component of the GSR signal

3\. **\[Phasic Signal]**: a faster-varying phasic component of the GSR signal

4\. **\[Peak Detected]**: GSR peak samples (GSR values between onset and offset) are assigned a value of "1". Other samples outside the peak duration are assigned a value of "0".

5\. **\[Peak Amplitude]**: GSR amplitude at the peak minus GSR amplitude at the onset.

6\. **\[Peak Occurrences]**: The time of the detected GSR peak.

7\. **\[Signal Interpolation]**: GSR samples obtained by interpolation are assigned a value of "1". Other samples that are not obtained by interpolation are assigned a value of "0".

**Analysis Markers:**

Markers generated from the signal processing algorithm "GSR Peak Detection" for a given analysis flow (such as GSR Peak Onset, GSR Peak, GSR Peak Offset) are shown in the replay. Automated annotations can efficiently be placed by defining rule-based GSR peak markers in your study.

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

(2) From GSR Peak Binned Aggregation R-Notebook: aggregated results across respondents during a certain stimulus/scene. Can be aggregated results of one chosen device or across multiple devices.

Example: aggregated results, one from one chosen device, the other one from two Shimmer devices

![Aggregated\_replay.png](https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-14667fa5eb8bc93ba8feee3eff466da4e735162a%2F7582153417_aggregated_replay.webp?alt=media)

* *Exported files*: Please refer to the article *here* on how to access the exported files from R processing. There are two types of files you can access: **Sensor Data files** and files from R processing (**GSR Summary Scores, Peaks Per Respondent** and**Moment To Moment Per Respondent**).
  1. **Sensor Data file**: raw sensor data export. Can also include processed signals by ticking"R Analysis (Active flows name given by the user)". You can also click "Show signal descriptions" for more descriptions of each signal.

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

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

2\. **GSR Summary Scores, Peaks Per Respondent,** and **Moment To Moment Per Respondent:** Processed results from R-Notebooks. Please refer to ***here*** for a detailed description of each of the exported files. You can access the exported file by (1) clicking on the 'Export' button, (2) under 'R PROCESSING', you can (3) click on 'GSR Metrics Export', and then (4) click on 'Export' button to generate the file.

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

Please refer to the section [*Peak Binned Aggregation algorithm*](#the-peak-binned-aggregation-algorithm) for a detailed explanation of parameters **\[Time bin length \[ms]] and**\[Time bin overlap \[ms]]**.**

* *HTML reports*:

  By clicking the "Reports" button a one-page readout is generated, along with presenting the actual R Code used to calculate the results, descriptions of each of the analysis steps, and links to the articles that were referred to when writing the algorithm’s script.

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

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

## References

Benedek, Mathias, and Christian Kaernbach. 2010. “A Continuous Measure of Phasic Electrodermal Activity.”*Journal of Neuroscience Methods* 190 (1): 80–91.<https://doi.org/10.1016/j.jneumeth.2010.04.028>.

Tukey's robust end-point rule: <https://stat.ethz.ch/R-manual/R-devel/library/stats/html/runmed.html>


---

# 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/eda-gsr/gsr-r-notebooks-peak-detection-and-aggregation.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.
