PSGpower: A MATLAB toolbox for analyzing sleep EEG data
Ahren B. Fitzroy, Rebecca M.C. Spencer

TL;DR
PSGpower is a MATLAB toolbox that streamlines and standardizes the analysis of sleep EEG data using various processing workflows.
Contribution
The novel contribution is a unified and extensible MATLAB toolbox for sleep EEG data analysis across different acquisition systems and software.
Findings
PSGpower supports data import from both legacy and modern sleep recording systems.
The toolbox integrates existing and custom algorithms for microstructure analysis of sleep EEG data.
PSGpower allows for the addition of new workflows using shared preprocessing and sleep staging code.
Abstract
Sleep science has seen a surge in discoveries fueled by enhanced data processing approaches to sleep physiology recordings. PSGpower is a MATLAB toolbox designed to make these processing steps more efficient and standardized. PSGpower imports sleep polysomnography data recorded using legacy and modern acquisition systems, and sleep-staged using a variety of software packages, for processing in a number of microstructure analysis workflows. Workflows include existing algorithms from EEGLAB and FieldTrip and custom algorithms. PSGpower is extensible, and new workflows can be added that take advantage of the common data importing, sleep stage marking, and preprocessing code.
Genes, proteins, chemicals, diseases, species, mutations and cell lines named across the full text — each resolved to its canonical identifier and authoritative record.
Click any figure to enlarge with its caption.
Figure 1
Figure 2
Figure 3
Figure 4
Figure 5
Figure 6Peer Reviews
No public reviews on file for this paper yet. If you reviewed it on a platform where reviews are public (OpenReview, ICLR, NeurIPS, ICML), you can paste yours below so the community can read it here.
Videos
No videos yet. Explain this paper in a talk, walkthrough, or lecture? Add one.
Taxonomy
TopicsEEG and Brain-Computer Interfaces · Sleep and Wakefulness Research · Sleep and Work-Related Fatigue
Motivation and significance
Several high-quality open source toolboxes exist for analyzing electroencephalogram (EEG) data, including EEGLAB [1], ERPLAB [14], FieldTrip [17], Brainstorm [20], MNE [8], and SPM [7]. However, although these toolboxes are able to analyze EEG data collected from both waking and sleeping participants, these toolboxes are not optimized for the analysis of EEG data recorded during sleep. A primary issue is that sleep EEG data are routinely classified into discrete sleep stages at regular intervals as an initial analysis step, forming a second time axis (“stage time”) often of essential analytical interest instead of, or in complement to, chronological time. While it is possible to annotate sleep stages and use those annotations to guide analyses in the currently available EEG software packages, this process requires several manual steps of epoching and reconcatenation to perform common simple sleep EEG analysis tasks, such as isolating non-rapid eye movement (NREM) stage 3 sleep for spectral analysis. Moreover, existing EEG analysis toolboxes are not capable of automatically importing sleep stage notations created using clinical or open-source sleep EEG software, so stage notations need to be manually converted into a format acceptable by the chosen toolbox before they can be used to inform analyses.
PSGpower aims to solve these issues by considering sleep stage notation metadata throughout the analysis process. PSGpower automatically imports stage notations from multiple specialized sleep EEG software output formats and converts them to a common EEGLAB-based tag marker structure, then allows simple specification of stage time restrictions in a graphical user interface (GUI; Fig. 1) prior to running analyses. This facilitates performing analyses in EEGLAB, FieldTrip, or custom algorithms that flexibly consider stage time, without requiring extensive coding experience of the user. Importantly, PSGpower is built to be extensible, allowing the easy addition of stage-time aware analysis “modules” implementing any desired custom MATLAB workflow that accepts EEGLAB-format sleep EEG data with stage notation metadata attached. Further, import functions for additional EEG recording systems or stage notation software can be easily added as required. This common analysis framework has benefitted researchers in our lab, and we believe it would be of utility to the broader community of sleep EEG/polysomnography (PSG) researchers as well.
Thus far our lab has implemented stage-time aware modules to quantify and compare spectral amplitude using cluster-based permutation analyses, to quantify and compare time-domain amplitude using the filter-Hilbert method or wavelet-based decomposition, and to analyze the temporal and topographical dynamics of multichannel amplitude envelopes. These modules have been applied by multiple researchers in our group, resulting in publications based on developmental [3,12,13,16], young adult [9–11,18], and aging [4–6] population sleep EEG data. Additionally, we have wrapped the code underlying a sleep spindle detection algorithm previously published by another research group [2] as a module, demonstrating the easy extensibility of PSGpower; we believe this provides a useful solution for standardizing custom processing algorithms implemented across sleep physiology research groups, enhancing efficiency of research, and thus facilitating discoveries in sleep science.
Software description
Software architecture
2.1.
PSGpower is a MATLAB application built using the AppDesigner framework, and can be run directly within MATLAB or installed into the MATLAB Apps tab. Once launched, the user uses a GUI (Fig. 1) to select their EEG data files, indicate the sleep stage notation metadata format, select and configure an analysis module, indicate stage time and/or frequency band processing restrictions, set general processing options, and launch the analysis module. Once an analysis module is launched, dialogs may guide the user through additional run-time choices, then the graphical and/or statistical output of the module is displayed on screen and/or saved to disk.
The PSGpower GUI is broadly organized into four “Steps”, which are designed to guide the user sequentially through module selection and configuration. In Step 1, the user selects the directory containing their sleep EEG recordings and associated stage notation files, provides basic information about these files, and can flexibly select a subset of the files available in the specified folder to process. In Step 2, the user selects an analysis module from the dropdown list or tab group headings, then configures processing and analysis parameters specific to that module. In Step 3, the user indicates which frequency bands the chosen analysis module should be looped over, which sleep stages should be included within each frequency band, and what artifact detection thresholds should be used for each frequency band. In Step 4, the user chooses general preprocessing settings including how to re-reference the EEG data, whether or not to perform bad channel interpolation (BCI) and specifying which channels to interpolate if so, and whether or not to perform an independent components analysis (ICA) decomposition of the data. Additionally, Step 4 provides the opportunity to load in custom preprocessing code, allowing the user to perform any desired preprocessing steps not typically performed by PSGpower as part of automated batch processing.
Software functionalities
2.2.
Once all configuration choices have been made in the GUI, the user clicks the Analyze button to launch the selected analysis module. PSGpower then processes each participant one at a time, starting with a series of module-agnostic data processing steps collectively referred to as the “common preprocessing” (Fig. 2). After the common preprocessing completes, the module-specific data processing and analysis is performed separately for each requested frequency band within each participant. After all participants have been processed individually, module-specific group level processing and analyses are performed, if applicable. For full details regarding the common preprocessing, module-specific individual processing, and module-specific group-level processing and analysis, please see the PSGpower documentation or open source code, which are both distributed with the application. These steps are also summarized in brief here.
The common preprocessing steps are, in order: 1) import EEG data (.edf or .vhdr/.vmrk/.eeg format) into EEGLAB; 2) downsample the EEG if requested; 3) import stage notation metadata in Hume [19], RemLogic (Natus Medical Incorporated, Middleton, WI), or TWin (Grass Technologies, Middleton, WI) format, convert to embedded EEGLAB event markers indicating sleep stage every 30 s, and save to disk in EEGLAB . set/.fdt format; 4) determine and attach channel location information to the EEG; 5) run any custom preprocessing code supplied by the user; 6) re-reference the EEG; 7) run independent components analysis (ICA) on the EEG and save the weights to disk if requested; 8) perform bad channel interpolation (BCI) on the EEG if requested; 9) filter the EEG to the requested frequency band using sleep-EEG-optimized filters; and 10) apply any requested stage time restrictions to the EEG. After these steps are completed, module-specific processing and analysis steps are performed. The filtering, stage time restriction, and module-specific steps are repeated for each requested frequency band before moving on to the next participant (Fig. 2).
The initial release of PSGpower includes seven analysis modules: Hypnogram, Spectra (EEGLAB), Spectra (FieldTrip), Power (Hilbert), Power (newtimef), Spindle Detection (Ferrarelli), and Envelope Viewer. The basic function of each module is transparent from the name: Hypnogram plots the stage time axis (i.e., hypnogram) for a recording, the two Spectra modules allow stage-time aware quantification and comparison of spectral amplitude using the tools built into EEGLAB and FieldTrip, the two Power modules allow stage-time aware quantification and comparison of time-domain amplitude or power using the filter-Hilbert method or wavelet-based time-frequency decomposition, the Spindle Detection module uses an adapted version of an algorithm previously published by Ferrarelli et al. [2] to automatically detect and quantify sleep spindles, and Envelope Viewer plots and quantifies amplitude or power envelopes generated by the Power (Hilbert) module. At the group level, Spectra (EEGLAB) and Spectra (FieldTrip) create and save group-averaged spectral amplitude plots, and Spectra (FieldTrip) can compare spectral amplitude between two groups using cluster-based permutation analysis over the frequency and topography dimensions [15,17]. Envelope Viewer can also compare time-domain amplitude or power using cluster-based permutation analysis over the topography dimension. Note that Envelope Viewer omits the common preprocessing steps, as they have already been performed on the output of Power (Hilbert).
Illustrative examples
Figs. 3, 4, 5, and 6 show the primary graphical output of individual participant processing by the seven modules included in PSGpower for one young adult overnight recording. For full descriptions of all displayed or saved output from each module, please see the documentation. The young adult overnight recording used is taken from a project examining the effects of sleep on memory reconsolidation [9], and is one of the young adult data points included in analyses in this manuscript.
Impact
The primary impact of PSGpower is making stage-time aware analyses of sleep EEG data simpler to perform. This enables sleep researchers to interrogate information-rich sleep EEG datasets in new ways more rapidly, and expands access to advanced command-line driven EEG tools to sleep researchers who do not have extensive programming experience. We anticipate this will allow sleep research groups to move faster, and potentially expand the use of more involved sleep EEG analysis methods like time-frequency representation analysis and cluster-based permutation analysis. Moreover, the extensibility of PSGpower can facilitate the rapid and stable sharing of “home-grown” sleep EEG analysis methods across research groups, as well as the expansion of existing analysis modules to additional legacy or new sleep EEG or stage metadata formats, which we believe could help the sleep research field as a whole move forward more rapidly and cohesively.
In our internal usage PSGpower has thus far had the desired impact, decreasing the friction of sleep EEG analyses and allowing rapid sharing of methods across research projects. We have used PSGpower as the primary means for analyzing sleep microstructure in eight peer-reviewed manuscripts [4–5,9–11,13,16] and three peer-reviewed published abstracts [3,12,18]. Methodological improvements from each experiment feed back into the software for better results in future work. For example, as part of our older adult work we iterated through several different filter designs to sufficiently isolate theta-band activity from delta-band activity during deep sleep. Nap theta activity isolated using the final Chebyshev II filter design (described first in [5]) showed robust relationships with motor sequence learning over the nap that were not observed for delta or sigma activity, which is an uncommon finding. Because this filter is now implemented as the default theta filter in PSGpower, we can easily apply it in our other work to test for further involvement of theta activity in sleep-dependent memory consolidation that may have been missed with suboptimal filtering.
Another benefit afforded by the structure of PSGpower is a common analysis interface for data from many sources. This benefit was illustrated in our internal usage when the global COVID-19 pandemic caused our work and data collection to become fully remote, including the use of several different portable EEG recording systems. Although each of these portable EEG systems came with their own proprietary software for recording and analysis, once staged and exported to .edf format our researchers were able to analyze these data using the same PSGpower tools they were already familiar with, rather than having to learn multiple new software interfaces. This ability to use familiar analysis tools reduced the productivity costs of adapting to multiple new recording systems. This anecdotal report speaks to the utility of a common analysis framework with extensible input and analysis modules; our intention in releasing PSGpower to the community is that with multiple research groups contributing input and analysis modules around this common framework, methodological gains can be shared more rapidly and overall progress and experimental rigor will increase.
Conclusions
PSGpower facilitates the flexible analysis of sleep EEG microstructure by considering sleep stage metadata throughout EEG processing and analyses, in EEGLAB-based, FieldTrip-based, and custom workflows. The structure of PSGpower is modular and extensible, allowing the addition and sharing of custom analysis workflows within and across labs, and the application of existing analysis workflows to new EEG and sleep stage data formats as needed. This common framework approach has improved the sharing, speed, and reliability of sleep EEG microstructure analyses in our laboratory, and we are releasing these tools publicly because we believe these improvements will scale to the wider sleep EEG research community with broader use.
The reference list from the paper itself. Each links out to its DOI / PubMed record.
- 1Delorme A, Makeig S. EEGLAB: an open source toolbox for analysis of single-trial EEG dynamics including independent component analysis. J Neurosci Methods 2004;134(1):9–21. 10.1016/j.jneumeth.2003.10.009.15102499 · doi ↗ · pubmed ↗
- 2Ferrarelli F, Huber R, Peterson MJ, Massimini M, Murphy M, Riedner BA, Watson A, Bria P, Tononi G. Reduced sleep spindle activity in schizophrenia patients. Am J Psychiatry 2007;164(3):483–92. 10.1176/ajp.2007.164.3.483.17329474 · doi ↗ · pubmed ↗
- 3Fitzroy AB, Cremone A, Spencer RM. Characterizing the effects of sleep extension on overnight EEG dynamics in children with Attention-Deficit/Hyperactivity disorder. Sleep 2018;41(suppl_1):A 101. 10.1093/sleep/zsy 061.260. · doi ↗
- 4Fitzroy AB, Jones BJ, Kainec KA, Seo J, Spencer RMC. Aging-related changes in cortical sources of sleep oscillatory neural activity following motor learning reflect contributions of cortical thickness and pre-sleep functional activity. Front Aging Neurosci 2022;13. https://www.frontiersin.org/article/10.3389/fnagi.2021.787654.10.3389/fnagi.2021.787654 PMC 878673735087393 · doi ↗ · pubmed ↗
- 5Fitzroy AB, Kainec KA, Seo J, Spencer RMC. Encoding and consolidation of motor sequence learning in young and older adults. Neurobiol Learn Mem 2021;185:107508. 10.1016/j.nlm.2021.107508.34450244 PMC 8595672 · doi ↗ · pubmed ↗
- 6Fitzroy AB, Kainec KA, Spencer RMC. Ageing-related changes in nap neuroscillatory activity are mediated and moderated by grey matter volume. Eur J Neuroscience 2021;54(9):7332–54. 10.1111/ejn.15468.PMC 880947934541728 · doi ↗ · pubmed ↗
- 7Friston KJ, Ashburner JT, Kiebel SJ, Nichols TE, Penny WD. Statistical parametric mapping: the analysis of functional brain images. Elsevier; 2007.
- 8Gramfort A, Luessi M, Larson E, Engemann D, Strohmeier D, Brodbeck C, Parkkonen L, Hämäläinen M. MNE software for processing MEG and EEG data. Neuroimage 2014;86:446–60. 10.1016/j.neuroimage.2013.10.027.24161808 PMC 3930851 · doi ↗ · pubmed ↗
