Complete MATLAB toolkit for automated EEG preprocessing using EEGLAB - Designed for hospital EEG data with flexible channel montage support (19-128 channels), featuring comprehensive processing, validation, and comparative analysis.
- MATLAB (R2018b or later recommended)
- EEGLAB toolbox (latest version)
- ICLabel plugin (required for Model 3 ICA functionality)
- Signal Processing Toolbox (for advanced analysis)
- Place EEG files in
eeg_files/folder (supports CNT, EDF, GDF formats) - Configure settings at the top of the script (see Configuration Options below)
- Choose processing model:
- Model 2: Run
model2_eeg_prep.mfor basic preprocessing (7 steps) - Model 3: Run
model3_eeg_prep.mfor advanced ICA preprocessing (11 steps)
- Model 2: Run
- Processed files saved to
output/folder:- Model 2:
[filename]_model2_preprocessed.edf - Model 3:
[filename]_model3_preprocessed.edf
- Model 2:
- Processing logs saved to
logs/folder
- Use Model 2 for: Quick preprocessing, baseline processing, when ICA is not required
- Use Model 3 for: Research-grade preprocessing with artifact removal, high-quality clean data requirements
- Use ASR Optimization Tool for: Finding the best ASR parameters for your specific dataset
- Use Comparison Tool for: Validating script vs manual processing, evaluating ICA effectiveness
Model2-EEG-Pipeline/
├── model2_eeg_prep.m # Basic preprocessing script (7 steps)
├── model3_eeg_prep.m # Advanced ICA preprocessing script (11 steps)
├── optimize_asr_parameters.m # ASR parameter optimization tool
├── compare_models.m # Automated comparison analysis tool
├── prepare_external_data.m # External data standardization script
├── eeg_files/ # Input for all EEG files
├── output/ # Output for all processed files
├── logs/ # Processing logs and reports
├── compare_models/ # Comparison analysis folder
│ ├── script/ # Script-processed files
│ ├── manual/ # Manually-processed files
│ ├── results/ # Comparison plots and analysis
│ └── logs/ # Comparison logs
└── README.md # This file
7-step basic preprocessing pipeline without ICA:
- Import with configurable time range 1b. Clean channel labels (remove reference suffixes like -AA, -Ref)
- Remove unwanted channels (configurable: reference, EOG, EMG, ECG)
- Downsample to target sample rate
- Bandpass filter
- Clean raw data (artifact removal with ASR)
- Re-reference to average
- Save as preprocessed EDF
11-step advanced preprocessing pipeline with automated ICA artifact removal:
- Import with configurable time range (Steps 1-4 same as Model 2)
- Clean raw data (artifact removal with ASR)
- Add channel locations (Dynamic montage detection: 19/21/25/32/64/128 channels)
- Run ICA decomposition (Extended Infomax)
- Classify components using ICLabel
- Remove artifact components (configurable brain threshold, default ≥70%)
- Re-reference to average
- Save as preprocessed EDF
Systematic parameter testing tool to find optimal ASR settings:
- Tests multiple combinations of Burst and Window criteria
- Balances high data retention with signal quality (SNR, Kurtosis)
- Provides per-file breakdown and actionable recommendations
- Usage: Run
optimize_asr_parametersfrom MATLAB command line
Automated comparison analysis for systematic evaluation:
- Interactive menu system for easy comparison selection
- Multiple comparison types: Script vs Manual, Model 2 vs Model 3
- Statistical significance testing with p-values
- Enhanced visualizations and reporting
Utility script to standardize external EEG datasets to the required montage.
- Automatically renames channels (e.g., T7 -> T3) based on map
- Strips channel suffixes
- Saves standardized files to
output/with_prepared.edfsuffix
Each script features a centralized USER CONFIGURATION section at the top.
IMPORT_TIME_RANGE_SEC = 180; % Import first N seconds (set to Inf for all data)
TARGET_SAMPLE_RATE_HZ = 125; % Target sample rate after downsampling
FILTER_LOW_HZ = 0.5; % Bandpass filter low cutoff (Hz)
FILTER_HIGH_HZ = 40; % Bandpass filter high cutoff (Hz)BRAIN_THRESHOLD = 0.7; % ICLabel: minimum brain probability to keep componentASR_BURST_CRITERION = 23; % Threshold for burst removal (lower = more aggressive)
ASR_WINDOW_CRITERION = 0.25; % Proportion of bad channels to trigger window rejection
ASR_WINDOW_TOLERANCES = [-Inf 7]; % Tolerance range for window rejectionSTRIP_CHANNEL_SUFFIXES = true; % Remove reference suffixes from channel names
CHANNEL_SUFFIX_PATTERN = '-(AA|Ref)$'; % Regex pattern for suffixes to removeINPUT_FOLDER = fullfile(pwd, 'eeg_files'); % Input data location
OUTPUT_FOLDER = fullfile(pwd, 'output'); % Output data location
LOG_FOLDER = fullfile(pwd, 'logs'); % Log file locationBoth models support flexible channel removal configuration:
REMOVE_REFERENCE_CHANNELS = true; % A1, A2, M1, M2, TP9, TP10, etc.
REMOVE_EOG_CHANNELS = true; % VEOG, HEOG, EOG1, EOG2, etc.
CHANNELS_TO_NEVER_REMOVE = {'Fp1', 'Fp2', 'F3', ...}; % Safety protectionThe optimize_asr_parameters.m tool runs a sweep of parameter combinations to find the best balance for your specific data.
Configuration:
BURST_VALUES = [20, 30, 40, 50]; % Values to test
WINDOW_VALUES = {0.25, 0.5, 0.75, 'off'}; % Values to test
TARGET_RETENTION = 0.90; % Target data retention (e.g., 90%)
MAX_FILES_TO_TEST = 10; % Test subset of files (or 'all')Output:
- Results table with retention, SNR, kurtosis, and quality scores
- Per-file breakdown showing which files pass/fail
- Recommendation for optimal settings
- CSV report saved to
logs/asr_optimization_results.csv
Tip
The tool's recommendation is a statistically optimal starting point. We recommend checking values slightly above/below the recommendation (e.g., Burst +/- 1) using visual inspection in EEGLAB, as manual validation may yield even better results for specific noise profiles.
Execute compare_models.m to start the interactive tool.
- Set up comparison folders:
compare_models/manual/model2/&model3/- Manually-processed filescompare_models/script/model2/&model3/- Script-processed files
- Select comparison type from the menu.
- View Results: Saved in
compare_models/results/with unique filenames.
Analysis Features:
- Signal quality assessment: Amplitude, variability, SNR improvements
- Power spectral density analysis: Frequency band changes
- Statistical significance testing: Wilcoxon signed-rank tests
- Channel correlation analysis: Signal preservation assessment
- Brain Component Retention: ≥70% brain probability threshold (Model 3)
- Power Change Validation:
- Normal: <15 dB change
- Substantial: 15-30 dB change
- Warning: >30 dB change (likely error)
- Signal Reduction Warnings: Amplitude >20x or Variability >200x reduction flagged
- Component classification confidence: ICLabel probability scores
- Retained vs removed component breakdown: Detailed count of artifact types
- Component separation quality: Information entropy measures for classification clarity
- Dynamic channel location assignment: Supports 19-128 channel montages with automatic fallbacks
- ICLabel requires channel locations: System automatically selects appropriate montage file
- Memory issues: Reduce
IMPORT_TIME_RANGE_SECif running out of RAM - GUI not showing: Set
SHOW_EEGLAB_GUI = trueto debug visually
- Graceful failure handling: Failed files are logged and skipped
- Memory management: Automatic cleanup (
onCleanup) prevents memory leaks - Log file safety: Logs are guaranteed to close even on error
- Process test files with both Model 2 and Model 3
- Run comparison analysis to validate effectiveness
- Review quality metrics in generated logs
- Adjust thresholds based on findings using the Optimization Tool