> 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/eeg/r-notebook-eeg-power-spectral-density-psd.md).

# R Notebook: EEG Power Spectral Density (PSD)

Run the EEG Power Spectral Density R notebooks to get delta, theta, alpha, beta and gamma band power per respondent and aggregated across respondents.

*This* *Help Center* *article* *describes* *the EEG Power Spectral Density and EEG Power Spectral Density Aggregation R Notebooks* *for* *EEG.*

The Power Spectral Density (PSD) R Notebook runs a signal processing protocol for obtaining the PSD data in standard frequency bands: delta (default to 1-3 Hz), theta (default to 4-7 Hz), alpha (default to 8-12 Hz), beta (default to 13-25 Hz) and gamma (default to 26-40 Hz). The PSD scores can be used as an EEG-based metric for further statistical analysis or feature extraction.

The decomposition of EEG signal into PSD data is often the first step toward deriving EEG-based metrics (e.g., attention, motivation, workload scores). An example of that is the Frontal Alpha Asymmetry (FAA) score (see also: [R Notebook (EEG): Frontal Asymmetry](https://help.imotions.com/lab/analysis/eeg/r-notebook-eeg-frontal-asymmetry-fa.md)).

## What is Power Spectral Density (PSD)

The idea behind decomposing the (raw) EEG data into PSD data is that different brain processes contribute to the EEG activity at different frequencies – for instance, the processes related to a person 'being alert' or 'being engaged' in a task. Some processes contribute to the slower frequencies of the EEG signal, while other processes contribute to the faster frequencies component. Attention processes, for example, have often been related to activity in the alpha range (i.e., relatively slow activity in a frequency range from around 8 to 12 Hz), while processes related to cognitive workload have often been related to activity in the theta range (i.e., even slower activity in a frequency range from 4 to 8 Hz).

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

To derive PSD data, EEG researchers use mathematical procedures for decomposing the (raw) EEG signal into standard waveforms (e.g., sinusoidal waves); these are waveforms that oscillate at faster and slower frequencies. The Pink Floyd album cover illustrates this mathematical procedure: the EEG signal (white light) is decomposed (prism) into waveforms of different frequencies (rainbow colors). For instance, if your research focuses on attention processes, the method would allow you to isolate only the alpha activity.

## How to add the PSD R Notebook to your analysis

The power spectral density R Notebook can be added to an analysis during the creation process of a new analysis, or later, by returning to the signal processing page within an existing analysis.

To add the R Notebook to the analysis:

1. Navigate to the Signal Processing page, and click the '+' button to add an active flow
2. In the 'Devices' drop-down menu, select the correct EEG device
3. In the 'Algorithms' drop-down menu, select the *EEG Power Spectral Density* R Notebook
4. Set the parameters (see also: [*Parameters*](#parameters))
5. Click 'Next'

![PSD\_adding\_nb.png](https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-6c75937ea812c8b30af06dd93dbb5bf893ac36c5%2Fd69210a3f9_psd_adding_nb.webp?alt=media)

If you also wish to compute an 'aggregation' of the PSD values (i.e., the *average* across all respondents in your analysis), you need to add the PSD aggregation R Notebook.

To add the PSD aggregation R Notebook to the analysis:

1. At the Signal Processing page, click the '+' button to add another active flow
2. In the 'Devices' drop-down menu, select the EEG device
3. In the 'Algorithms' drop-down menu, select the *EEG Power Spectral Density Aggregation* R Notebook
4. Click 'Next'

***Note**: In order to run EEG Power Spectral Density Aggregation R Notebook, you have to run EEG Power Spectral Density R Notebook on an individual level first.*

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

## Details about the algorithm

The Power Spectral Density (PSD) R Notebook includes two methods for deriving the spectral power in different power bands: a legacy method inspired by the Advanced Brain Monitoring (ABM) built-in algorithm, and the Welch method (standard method in the scientific literature).

The pre-processing of the EEG data is identical for the two methods, and includes:

**(1)** a zero phase-lag band-pass filter (Butterworth)

**(2)** a zero phase-lag notch filter (Butterworth)

**(3)** *for BrainProducts EEG only*, an additional step may be added before applying the artefact threshold: re-referencing of the data (allowing for re-referencing the EEG data, for instance, to the mastoids).

**(4)** an artifact threshold (rejecting data-points whose absolute values exceed a preset voltage threshold)

*The steps are applied in this order.*

After pre-processing, the EEG data is chunked into short time-windows and the Power Spectral Density is computed using Fast Fourier Transform (FFT). This is where the legacy method and Welch method start deviating.

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

For the **legacy method**, the EEG data is split into 2-second time windows with an overlap of 50%. These 2-second time windows are then further subdivided into (three) 1-second time windows with an overlap of 50%. These 1-second chunks of EEG data are submitted to the FFT, resulting in three separate power spectra per 1-second time window; these power spectra are then averaged to derive a single (averaged) power spectrum per 2-second time window. If the artifact threshold is hit in any of the contributing time-windows, the power spectrum is discarded from the average; if the artifact threshold is hit in all three contributing time-windows, this leads to ‘missing data’ for the 2-second time window. Ultimately, the legacy method results in one power spectrum per second, whereby each power spectrum relates to two seconds of EEG data.

***Note**: For legacy method, the window duration forwarded to FFT computation is always fixed at 1 second. And the legacy method uses **square** time-windows.*

For the **Welch method**, the EEG data is split into 1-second time-windows (by default) with an overlap of 50%. These 1-second chunks of EEG data are submitted to the FFT, resulting in one power spectrum per 0.5 seconds (if the time-window is 1-second). If the artifact threshold is hit within that period, the power spectrum is discarded. Ultimately, the Welch method results in one power spectrum per 0.5 seconds (if the time-window is 1-second), whereby each power spectrum relates to 1-second of EEG data (if the time-window is 1-second).

***Note**: For Welch method, the window duration forwarded to FFT computation in R Notebook is available within 1-second, 0.5-second or 2-second, the overlap rate is always fixed at 50% though. The welch method uses **hamming** time-windows.*

The resulting power spectra range from 0 Hz to Fn, and with Fn being the Nyquist frequency (i.e., 0.5 \* sampling rate). Finally, the delta, theta, alpha, beta and gamma power are computed by averaging the Power Spectral Density within the following standard power bands: delta (default to 1-3 Hz), theta (default to 4-7 Hz), alpha (default to 8-12 Hz), beta (default to 13-25 Hz), and gamma (default to 26-40 Hz). As a last step, a logarithm transform is added to display the values in [Decibels (dB)](https://en.wikipedia.org/wiki/Decibel).

***Note***: *Any averaging computation across electrodes, time and participants is performed before applying any logarithmic transform to the data.*

**Power Spectral Density** of all available channels will be computed, in addition, you also have the possibility to compute the **Average PSD of selected electrode cluster**. It is computed by averaging the spectral power across selected channels (E.g., Ch1, Ch2, Ch3 ...). The values are transformed into the unit of Decibel (dB) after the averaging.

**EEG Metrics (PSD)** for the respondent and stimulus that are selected in the analysis is computed by averaging the spectral power over the entire stimulus duration and then log transformed into the unit of Decibel (dB).

**Spectral Power within the frequency range of 0 - 45 HZ** for each available channel is also plotted, but *only presented in HTML report (please see the* [*section*](#report) *below regarding how to access the report)*. The values are in the unit of Decibel (dB).

Only if the *EEG* *Power Spectral Density Aggregation R Notebook* is added:

The **Aggregated Power Spectral Density** is computed by averaging the spectral power out of *EEG Power Spectral Density* R Notebook across respondents. The aggregated values are then log transformed into Decibel (dB). The aggregation is done for both individual available channel and selected cluster.

**Spectral Power within the frequency range of 0 - 45 HZ** for each available channel is also aggregated and plotted, but *only presented in HTML report (please see the section below regarding* [*how to access the report*](#report)*)*. The values are in the unit of Decibel (dB).

## Parameters

The R Notebook can be altered by changing the following parameters. Changing these parameters will change **(1)** the pre-processing **(2)** the method used for computing the PSD data **(3)** which power bands and electrode cluster to be used for PSD computation.

**(1) Power calculation method:**

* **PSD computation method:** Choose to run the ‘legacy method’ or ‘Welch method’ (standard method in the scientific literature).
* **Window length for Welch PSD \[in second]:** Window length used for applying Fast Fourier Transform.

***Notes on Window length parameter:** 1. Welch PSD method only. For legacy method a window size of 1 second will always be used. 2. The chosen window size affects the frequency resolution i.e. the number of frequency bins reported. Only change this parameter if you know what you are doing.*

**Available options and defaults values:**

<table data-header-hidden><thead><tr><th></th><th></th><th></th><th></th></tr></thead><tbody><tr><td>PSD computation method</td><td>"3 windows average (legacy method)" and "Welch PSD (50% overlap)". Default is "Welch PSD (50% overlap)"</td><td colspan="2"><br></td></tr><tr><td>Window length for Welch PSD [in second]</td><td>0.5, 1, 2. Default is 1 second</td><td colspan="2"><br></td></tr></tbody></table>

**(2) Pre-processing:**

* **Band-pass filter frequencies \[in Hz]:** The band-pass filter is used to preserve power within a specified frequency range.
* **Notch filter frequency:** The Notch filter is used to remove line noise introduced in the signal by electrical equipment in the recording environment. In the US: 60 Hz in Europe: 50 Hz

***Notes on Band-pass filter frequencies:** The lowpass cutoff frequency (by default 100 Hz) should be set lower than Nyquist frequency (i.e., 0.5 \* sampling rate). If it exceeds the Nyquist frequency, the algorithm will use Nyquist frequency as the actual lowpass cutoff frequency.*

***Notes on Notch filter frequency:** Make sure you choose the right Notch filter frequency. If you choose the incorrect Notch filter frequency and set your bandpass frequency range big, it could result in line noise being kept in EEG signal. This may give you a lot of NA values later because of the subsequent artifact rejection.*

* **Reference channel (BrainProducts only):** The re-referencing option allows for re-referencing the EEG signal to a custom channel (e.g., Cz, Mastoid(s), tip of the nose).
* **Artefact threshold \[in µV]:** The artifact threshold is a simple voltage threshold that marks the parts of the EEG signal where the absolute value of the data exceeds the threshold. These parts of the EEG signal will be excluded according to the method specified above (i.e., legacy method, Welch method; see [*Details about the algorithm*](#details-about-the-algorithm)). Note that EEG signals would typically fluctuate within a range of -100 to 100 microvolts around the mean (larger fluctuations may reflect artifacts).

**Available options and defaults values:**

| Band-pass filter frequencies \[in Hz]                                             | <p>Default range: 0.5 - 100 Hz<br><em><strong>Note</strong>: Please always use "-" as a separator</em></p> |
| --------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------- |
| Notch filter frequency                                                            | 50 Hz, 60 Hz. Default is 60 Hz                                                                             |
| Artefact threshold \[in µV]                                                       | Default is 120 µV                                                                                          |
| \*Reference channel 1                                                             | Default is: 'Ref'                                                                                          |
| \*Reference channel 2, leave empty for re-referencing based on only one electrode | Default is empty                                                                                           |

*\* only available for BrainProducts (ActiChamp)*

**(3) Powerbands:**

* **Powerbands options:** The available powerbands for computation. Please note that you can choose one or multiple powerbands in one analysis.
* **Delta band frequencies \[in Hz]:** Frequency range for delta band
* **Theta band frequencies \[in Hz]:** Frequency range for theta band
* **Alpha band frequencies \[in Hz]:** Frequency range for alpha band
* **Beta band frequencies \[in Hz]:** Frequency range for beta band
* **Gamma band frequencies \[in Hz]:** Frequency range for gamma band

**Available options and defaults values:**

| Powerbands options              | Delta, Theta, Alpha, Beta, Gamma. Default is all selected |
| ------------------------------- | --------------------------------------------------------- |
| Delta band frequencies \[in Hz] | Default range: 1 - 3 Hz                                   |
| Theta band frequencies \[in Hz] | Default range: 4 - 7 Hz                                   |
| Alpha band frequencies \[in Hz] | Default range: 8 - 12 Hz                                  |
| Beta band frequencies \[in Hz]  | Default range: 13 - 25 Hz                                 |
| Gamma band frequencies \[in Hz] | Default range: 26 - 40 Hz                                 |

***Note**: Please always use "-" as a separator*

**Electrode cluster selection:**

* **Electrode cluster for averaging PSD:** Set one or more electrodes for the calculation of averaged PSD
* **LSL Electrode cluster for average PSD (comma-separated). Leave empty to select ALL**: Only applicable to LSL devices. Input one or more electrodes for the calculation of averaged PSD. Use "," to separate channels. Leave it empty if you want to select all channels. Case-insensitive. Please note that manually providing the LSL channel names will now work even if the user forgot to create a profile / created the wrong profile for their device.

***Notes on Electrode cluster selection:** please note that the naming of electrode locations is different for Enobio than other EEG headset. For Enobio, channel names are reported as Ch1, Ch2, Ch3...You will have to find the original channel names in the Enobio software.*

**Available options and defaults values:**

| Electrode cluster for average PSD                                                  | Default is all selected |
| ---------------------------------------------------------------------------------- | ----------------------- |
| LSL Electrode cluster for average PSD (comma-separated). Leave empty to select ALL | Default is all selected |

***Tip:*** It is possible to change these default values and save them for later, by clicking ‘Save as default’ on the Signal processing page; at any time, you can restore the default values by clicking ‘Factory defaults’.

![save\_as\_default.png](https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-45abf8c3612763fe8ffadce37a92e6af9d9d20fa%2Fd76383efea_save_as_default.webp?alt=media)

## Is it possible to use the ABM ‘decontaminated’ data?

Users of the ABM system may wonder whether it is possible to use the ‘decontaminated’ data provided by ABM, rather than the ‘raw’ data. The answer is No. You can only apply R Notebook on ABM raw data. Please make sure that you select the ABM '*raw*' data (B-Alert EEG) instead of ABM '*decontaminated*' data (B-Alert Decontaminated EEG) in the drop-down menu under 'Devices' (See also: [*How to add the PSD R Notebook to your analysis*](#how-to-add-the-psd-r-notebook-to-your-analysis)). If you choose the ABM '*decontaminated*' data (B-Alert Decontaminated EEG), there will be no available EEG R Notebooks shown.

## Is it possible to use the power band data from Emotiv?

Users of the Emotiv system may note that Emotiv already provides power band information directly. The most salient difference between the power band information computed by Emotiv and by iMotions, is that slightly different frequency bands are used: theta (4-8 Hz), alpha (8-12 Hz), low beta (12-16 Hz), high beta (16-25 Hz) and gamma (25-45 Hz). However, the algorithms developed by Emotiv are proprietary, and therefore, it can be assumed that the methods deviate from the methods described in this Help Center article. Users of the Emotiv system are able to choose to run the EEG Power Spectral Density R Notebook from the ‘*raw*’ EEG data, or from the '*power band*' EEG data provided by Emotiv directly. This is done by either selecting Emotiv '*raw*' data or Emotiv '*powerbands*' data in the drop-down menu Devices (See also: [*How to add the PSD R Notebook to your analysis*](#how-to-add-the-psd-r-notebook-to-your-analysis)).

If you choose to run the Power Spectral Density R Notebook from the raw EEG data, the computations happen exactly as described in this Help Center article.

If you choose to run the Power Spectral Density R Notebook from the powerbands data provided by Emotiv, the only step applied by the R Notebook, is to compute the logarithm on the powerbands data (the final step of the R Notebook's PSD computation).

## Is it possible to run EEG R Notebooks on data collected from a device connected via LSL?

Yes. With the help of "Adding a Device profile" *before* your data collection, it is possible to run EEG R Notebooks on your collected data. Please refer to [*How to collect EEG data with an LSL device in iMotions*](https://help.imotions.com/lab/sensor-setup/eeg/how-to-collect-eeg-data-with-an-lsl-device-in-imotions.md) to learn more about LSL device profiles and how to choose one before data collection.

## What the R Notebook delivers: replay, export files, report

The EEG Power Spectral Density R Notebook returns:

**(1)** the pre-processed (band-pass filtered, notch filtered, re-referenced (Actichamp only), artifact rejected) EEG signal

**(2)** up to five additional data channels per EEG electrode and for selected electrode cluster – representing the delta, theta, alpha, beta, gamma power over time. These data are often the basis of EEG based metrics (e.g., attention, motivation, workload scores); an example of that is the Frontal Alpha Asymmetry (FAA) metric, a measurement of approach and withdrawal motivation, which is computed as the difference in alpha activity between the left and right frontal regions.

Please go to [R Notebook (EEG): Frontal Asymmetry](https://help.imotions.com/lab/analysis/eeg/r-notebook-eeg-frontal-asymmetry-fa.md) for more details on computation of FAA.

**(3)** the mean PSD per stimulus, scene and annotation placed in the analysis.

These data can be visualized in the Individual Replay, Aggregate Replay and exported as part of the sensor data export file or as a separate summary metrics file.

**(4)** a one-page report with graphs, algorithm descriptions and embedded R-code.

#### Individual Replay

The R Notebook adds three sections to the signals page in the Replay: 'Preprocessed EEG (PSD)', ‘Power Spectral Density’ and ‘EEG Metrics (PSD)’:

1. The section **'Preprocessed EEG (PSD)'** returns the pre-processed EEG signal of each available channel.
2. The section **‘Power Spectral Density’** returns multiple data channels: the log transformed spectral power averaged within selected powerband(s) for each available channel and selected electrode cluster. Note that results from more than one selected powerbands can be displayed at the same time.
3. The section **‘EEG Metrics (PSD)’** further provides quick summary statistics for each selected powerband(s), namely, the mean PSD for the respondent and stimulus currently displayed.

#### Aggregate replay

*If you choose to also add the PSD Aggregation R Notebook*, in the Aggregate Replay you may find the section **'Aggregated Power Spectral Density'**. This section contains multiple new data channels, representing the aggregated PSD scores per time unit (across all respondents included in the analysis) of each selected powerbands, for each available channel and for selected electrode cluster.

#### Metrics export

The R Notebook further produces an export file, which, for *every* respondent included in the analysis and for *every* stimulus, scene, or annotation presented in the study, computes the mean PSD (E.g., Mean PSD Ch1 Theta (dB), Mean PSD Ch2 Alpha (dB)).

Please see [R-Processed: EEG Export Files (Frontal Asymmetry and Power Spectral Density)](https://help.imotions.com/lab/analysis/eeg/r-processed-eeg-export-files.md) for a detailed description on the exported summary metrics file.

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

#### Sensor data export

It is further possible to export the PSD data as part of the sensor data export file. This is done by ticking the option: "Processed and imported data" in the sensor data export editor.

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

#### Report

By clicking the "Reports" button it is also possible to retrieve an .html report with the R-code embedded and a step-by-step description of the algorithm. Here in the report, you will also find information/messages on troubleshooting if something is going wrong.

***Note**: You can find this .html file for both individual PSD R Notebook and aggregated PSD R Notebook.*

*In the .html file you will also find graphs of spectral power within the frequency range of 0 - 45 HZ for each available channel. Please note that this type of graph only exists in .html file.*

![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)

## References

Allen, John J. B., James A. Coan, and Maria Nazarian. 2004. “Issues and Assumptions on the Road from Raw Signals to Metrics of Frontal EEG Asymmetry in Emotion.” *Biological Psychology* 67 (1-2): 183–218. <https://doi.org/10.1016/j.biopsycho.2004.03.007>.


---

# 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/eeg/r-notebook-eeg-power-spectral-density-psd.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.
