This vignette provides a basic introduction to spectral analysis of Electroencephalography (EEG) signal from a sleep record file.
EEG refers to all the methods of recording, analysis and interpretation of the electrical activity of the brain. In clinical EEG, multiple electrodes are usually placed on the scalp, measuring its superficial activity over time. Electrodes are typically arranged using the standardized International 10-20 system , in order to enhance analysis reproducibility.
EEG is a major component in sleep analysis. Sleep stages, such as slow wave sleep or paradoxical sleep are partly defined over visual EEG characteristics . Many sleep related disorders can be identified in EEG data. Polysomnography (PSG), the gold standard exam in sleep medicine, includes EEG along many other physiological signals .
Sleep records are usually stored using European Data Format (EDF)
. The R package
.edf files. Reading an
.edf file takes
two steps: First reading the headers of the file, then reading the
selected signals. The following spectral analysis will be performed on a
single channel of the EEG, the
spectrogram() plots the
spectrogram of the signal. But first, install
version from Github.
rsleep reads this particular format with the
read_events_noxturnal function. The
plot_hypnogram function then plots the hypnogram.
The hypnogram show sleep stages over time using consecutive 30 seconds epochs. This record was manually scored by well-trained technicians according to the American Academy of Sleep Medicine manual . Five sleep stages can be observed:
Visual scoring is an empirical science requiring a considerable amount of knowledge and training. Alternative methods like spectral estimations techniques such as the Fourier transform must be used to quantify information carried in the physiological signals .
Epoching is the first step of sleep analysis. Physiological signal, such as EEG, is splitted into consecutive epochs of a discrete duration. Epochs usually start at lights turnoff, when the patient or subject starts the night.
As the example record already has a hynogram, the EEG signal can be
splitted using these scored epochs. The
rsleep package split the signal according to these
parameters. It returns a list of signal vectors.
The Fourier transform (FT) may be the most important function in signal analysis. It decomposes the signal into its constituent frequencies and computes its power spectral densities (PSD). However, EEG signals also carry a lot of noise. This noise is easily intereprted by the FT and can jam the results. To solve this problem, Welch’s method split the signal into overlapping segments to average power spectral densities from the Fourier transform.
rsleep computes a
periodogram using Welch’s method. The following example computes and
plot the periodogram of the 120th epoch. This epoch has been scored N2,
or light sleep, by the expert.
This epoch periodogram shows high PSD in lower frequencies of the
spectrum. As values are normalized using
log, PSD are
To compute average periodograms by stage, hypnogram and epochs can be
iterated simultaneously using the
Periodograms can be filtered at this step to discard values over 30
mapply returns a list that can be coerced to a
rbind combined to
Once coerced to a
dataframe, raw periodogram values can
be averaged by stage.
Aggregated periodograms can then be plotted using
Each sleep stage show a distinct average periodogram. If the
N3 stage averages higher PSD values in the lower spectrum,
it show way lower PSD in the upper frequencies compared to other
The traditional way to simplify the EEG periodogram is to cut the frequencies of the spectrum into bands or ranges :
3.5 Hertz, the Delta
band is associated with slow-wave sleep in adults subjects.
7.5 Hertz, the Theta band is associated with drowsiness in
adults and teens subjects.
13 Hertz, the Alpha band is associated with a relaxed state
and eyes closed.
30 Hertz the Beta band is associated with active thinking,
focus, high alert or anxiousness.
30 Hertz the Gamma band
is mostly used for animal EEG analysis.
Bands can be computed using the
bands_psd of the
rsleep package. Those bands can be normalized by the
spectrum range covered by the bands.
lapply returns a list, results must be reshaped in
order to obtain a dataframe object.
Stages can be retreived from the hypnogram.
Now that the epochs bands PSD and their corresponding stages are
stored in a dataframe, they can easily be plotted using boxplots from
bands_df_long <- reshape2::melt(bands_df, "stage") palette <-c("#F98400", "#F2AD00", "#00A08A", "#FF0000", "#5BBCD6") ggplot(bands_df_long, aes(x=stage,y=value,color=stage)) + geom_boxplot() + facet_grid(rows = vars(variable),scales = "free") + scale_colour_manual(name = "stage", values = palette) + theme_bw() + xlab("") + ylab("PSD") + theme(legend.position = "none")