EEG data preprocessing is critical for removing artifacts and noise that can obscure neural signals. A typical preprocessing pipeline involves several sequential steps, each targeting specific sources of noise or artifact. The order of operations matters: filtering should generally precede re-referencing, bad channel detection should occur before interpolation, and artifact rejection should be performed after epoching.
This vignette demonstrates a complete EEG preprocessing workflow using the PhysioEEG package, from raw continuous data to clean, analysis-ready epochs. We cover filtering, re-referencing, channel interpolation, ICA-based artifact removal, epoching, and automated artifact rejection.
First, load the PhysioEEG package and create example EEG data for demonstration purposes.
library(PhysioEEG)
eeg <- make_eeg(
n_channels = 64,
n_time = 60000,
sampling_rate = 500,
add_noise = TRUE
)The make_eeg() function creates a simulated EEG dataset
with realistic noise characteristics. In practice, you would load your
data using readEDF(), readBrainVision(), or
similar I/O functions.
Filtering removes frequency-specific noise and artifacts. The choice of filter type and cutoff frequencies depends on your research question and the characteristics of your data.
Highpass filters remove slow drifts and DC offsets that can interfere with subsequent processing steps. A cutoff of 0.1 Hz is common for ERP studies, while 1 Hz may be appropriate for resting-state analysis.
Lowpass filters attenuate high-frequency noise, including muscle artifacts and electrical interference above the frequency range of interest. For most cognitive EEG studies, 40-50 Hz is a reasonable cutoff.
Bandpass filters combine highpass and lowpass in a single operation, isolating the frequency band of interest. This is the most common approach for EEG preprocessing.
Notch filters remove narrow-band line noise at 50 Hz (Europe/Asia) or 60 Hz (Americas). Apply notch filtering before bandpass filtering for best results.
The package supports both finite impulse response (FIR) and infinite impulse response (IIR) filters. FIR filters (type = “fir”) have linear phase characteristics and no phase distortion, making them ideal for ERP analysis. IIR filters (type = “butter”, “cheby1”, etc.) are computationally efficient but introduce phase shifts.
eeg_fir <- eegFilter(eeg, lowcut = 1, highcut = 40, type = "fir", order = 100)
eeg_iir <- eegFilter(eeg, lowcut = 1, highcut = 40, type = "butter", order = 4)For most applications, a 4th-order Butterworth filter provides an excellent balance between roll-off characteristics and computational efficiency. Use FIR filters when precise timing is critical (e.g., high-resolution ERPs).
The choice of reference electrode affects the spatial distribution of recorded potentials. Re-referencing transforms data from the original recording reference to a new reference scheme.
Average referencing uses the mean of all electrodes as the reference. This is the most common choice for high-density EEG and is required for source localization methods.
Mastoid (or linked mastoids) referencing is common in clinical applications and sleep studies. Mastoid channels (typically A1 and A2 or TP9 and TP10) are relatively inactive during most cognitive tasks.
Re-referencing to a single electrode (commonly Cz) can be useful when comparing to older literature or for specific experimental designs.
Bad channels can arise from poor electrode contact, excessive impedance, or equipment malfunction. Detecting and interpolating bad channels improves data quality and prevents artifact propagation.
The eegBadChannels() function identifies problematic
channels using multiple criteria: flatness (no signal variation), high
noise levels, and low correlation with neighboring channels.
Once bad channels are identified, spherical spline interpolation reconstructs their signals based on surrounding electrodes. This method assumes that scalp potentials vary smoothly across the head surface.
Interpolation works well for isolated bad channels (up to ~10% of total channels). If more channels are bad, consider rejecting the entire dataset or recording segment.
Independent Component Analysis (ICA) separates EEG signals into statistically independent sources, enabling removal of stereotypical artifacts like eye blinks, eye movements, and muscle activity.
The eegICA() function performs ICA decomposition using
the FastICA or Infomax algorithm. Run ICA on filtered, re-referenced
data before epoching.
Automated detection identifies components likely to represent artifacts based on spatial patterns, temporal characteristics, and spectral properties.
After confirming which components represent artifacts (visual inspection recommended), remove them from the data.
Eye blinks typically appear in 1-2 frontal components with high amplitude and low frequency. Lateral eye movements produce bipolar patterns over frontal electrodes. Muscle artifacts show high-frequency activity and focal topographies. Always visually inspect components before removal.
Epoching segments continuous EEG into time-locked trials around events of interest. This step is essential for event-related potential (ERP) analysis and trial-based statistics.
First, define your experimental events. Events should include onset times (in samples or seconds) and condition labels.
Extract epochs from -200 ms to 800 ms around each event, using the pre-stimulus interval (-200 to 0 ms) for baseline correction.
eeg_epochs <- eegEpoch(
eeg_clean,
events = events,
epoch_limits = c(-0.2, 0.8),
baseline_limits = c(-0.2, 0)
)The epoch_limits parameter defines the time window
around each event (in seconds). The baseline_limits
parameter specifies the interval used for baseline correction, which
removes pre-stimulus voltage differences across trials.
You can also apply baseline correction separately or use different baseline windows for different analyses.
Even after ICA, some trials may contain residual artifacts. Automated artifact rejection identifies and removes contaminated epochs.
The simplest approach rejects epochs where any channel exceeds an absolute voltage threshold (typically ±100 μV for scalp EEG).
Gradient (voltage step) criteria detect rapid voltage changes characteristic of muscle artifacts or electrode pops.
Joint probability computes z-scores for each channel and epoch based on the distribution of values across all trials, rejecting outliers.
You can combine methods by applying them sequentially. Most studies use threshold-based rejection with ±75-100 μV limits.
For standardized processing, eegPreprocess() executes a
complete pipeline in a single function call, applying filtering,
re-referencing, bad channel interpolation, ICA artifact removal,
epoching, and artifact rejection.
eeg_final <- eegPreprocess(
eeg,
filter_lowcut = 1,
filter_highcut = 40,
filter_order = 4,
filter_type = "butter",
notch = 60,
reref_method = "average",
bad_channel_detect = TRUE,
bad_channel_interp = TRUE,
ica_method = "fastica",
ica_components = 30,
ica_artifact_types = c("eye", "muscle"),
events = events,
epoch_limits = c(-0.2, 0.8),
baseline_limits = c(-0.2, 0),
artifact_reject = TRUE,
artifact_method = "threshold",
artifact_threshold = 100
)ERP Studies (cognitive tasks): - Bandpass: 0.1-40 Hz - Average reference - ICA for eye artifacts - Baseline: -200 to 0 ms - Threshold: ±75 μV
Resting-State Analysis: - Bandpass: 1-50 Hz (or analysis-specific, e.g., 8-13 Hz for alpha) - Average reference - ICA for eye, muscle, and cardiac artifacts - No epoching (continuous analysis)
Sleep EEG: - Bandpass: 0.3-35 Hz - Mastoid reference - Minimal ICA (preserve physiological slow waves) - Epoch in 30-second windows for staging
The sequence of preprocessing steps affects final data quality:
Filtering before re-referencing prevents noise propagation. ICA on continuous data captures artifact patterns better than on epoched data. Artifact rejection should be the final step to preserve maximum trials.
Automated preprocessing is powerful but not infallible. Visually inspect your data:
Use plotting functions like plotEEG(),
plotTopomap(), and plotERP() to visualize data
at each stage.
Record all preprocessing parameters in your analysis script and research outputs. Parameter choices significantly impact results and must be reported for reproducibility. The PhysioEEG package stores preprocessing history in the metadata slot of the returned object.
This vignette provides a foundation for EEG preprocessing with PhysioEEG. Adapt these workflows to your specific experimental design and research questions. When in doubt, consult domain-specific literature for parameter recommendations.