A real-time Simulink S-Function that performs Artifact Subspace Reconstruction (ASR) on multichannel EEG signals. The block calibrates from clean EEG data, estimates a baseline covariance model, and continuously attenuates high-variance artifact subspaces while preserving underlying neural activity.
The S-Function implements a lightweight C++ version of the Artifact Subspace Reconstruction (ASR) algorithm designed for real-time execution inside Simulink. During calibration, the block learns the covariance structure of clean EEG data. During runtime, it monitors the covariance of a sliding window and attenuates principal components whose variance exceeds statistically derived thresholds.
Unlike hard artifact rejection, this implementation performs soft subspace attenuation, reducing artifact energy while preserving as much physiological EEG activity as possible.
| Property | Value |
|---|---|
| Processing | Real-time |
| Input | N-channel EEG (double) |
| Output | Cleaned N-channel EEG (double) |
| Calibration | Built into block |
| Window | User configurable |
| Algorithm | Artifact Subspace Reconstruction (ASR) |
The figure above illustrates the complete real-time EEG acquisition, processing, artifact removal, and visualization pipeline.
The Simulink model performs the complete online EEG processing chain.
Receives raw EEG samples from the OpenBCI Cyton board through a serial (COM) port.
Converts the incoming EEG samples into the desired units before further processing.
Removes DC drift and high-frequency noise while preserving the EEG frequency band of interest.
Suppresses 50 Hz power-line interference.
The ASR block performs real-time artifact suppression.
During Calibration Mode, the algorithm learns the covariance structure of clean EEG data.
During Run Mode, it continuously analyzes the covariance of a sliding window and attenuates principal components whose variance exceeds the learned baseline thresholds. This effectively suppresses eye blinks, muscle activity, and motion artifacts while preserving the underlying EEG.
The processed data are transmitted to the NeuroScope application using Inter-Process Communication (IPC).
Both the original filtered EEG and the artifact-cleaned EEG are streamed simultaneously for real-time visualization and comparison.
The NeuroScope application displays both signal streams simultaneously.
- Channels CH1–CH4 display the original filtered EEG signals.
- Channels CH5–CH8 display the corresponding EEG channels after Artifact Subspace Reconstruction (ASR).
Displaying the original and cleaned signals together makes it easy to evaluate the effectiveness of artifact removal during live acquisition. Large transient disturbances such as eye blinks or movement artifacts visible in the upper traces are significantly attenuated in the corresponding lower traces while preserving the underlying EEG activity.
- Real-time EEG artifact removal
- Automatic calibration from clean EEG
- Supports arbitrary channel counts
- Sliding covariance estimation
- Soft subspace attenuation
- Internal Jacobi eigenvalue decomposition
- No external linear algebra libraries required
- Designed for real-time Simulink execution
- IPC interface for live NeuroScope visualization
- MATLAB / Simulink (tested on R2023a+)
- Microsoft Visual Studio
- MATLAB MEX compiler configured (
mex -setup)
Compile the S-Function from MATLAB:
mex -O main.cpp -output sfun_asraddpath('path\to\project')Verify MATLAB can locate
| File | Purpose |
|---|---|
sfun_asr.mexw64 |
ASR Simulink S-Function |
open('Simulink_ASR.slx')The ASR block exposes four parameters.
| Parameter | Description |
|---|---|
| Window Size | Sliding covariance window size |
| Threshold Sigma | Artifact detection threshold |
| Calibration Screen Factor | Rejects large calibration outliers |
| Minimum Clean Fraction | Minimum accepted clean calibration samples |
The second input selects the operating mode.
| Mode | Description |
|---|---|
1 |
Calibration |
0 |
Artifact Removal |
For calibration:
- Feed clean EEG into the block.
- Set the mode input to 1.
- Collect several seconds of artifact-free EEG.
- Switch the mode input to 0.
- The block automatically computes the baseline covariance and begins artifact suppression.
| Port | Description |
|---|---|
| Input 1 | Raw EEG (N×1) |
| Input 2 | Mode (1 = Calibration, 0 = Run) |
| Port | Description |
|---|---|
| Output | Cleaned EEG (N×1) |
The implementation follows the standard Artifact Subspace Reconstruction workflow.
- Collect calibration EEG.
- Compute per-channel RMS values.
- Reject calibration outliers.
- Estimate the baseline covariance matrix.
- Compute eigenvectors using an internal Jacobi eigenvalue decomposition.
- Compute variance thresholds for each principal component.
- Build a sliding covariance window during runtime.
- Compare runtime variances against calibration thresholds.
- Softly attenuate artifact-related principal components.
- Reconstruct the cleaned EEG sample.
Unlike binary rejection methods, this implementation attenuates artifact components instead of completely removing them, preserving more physiological EEG information.
| File | Description |
|---|---|
main.cpp |
Complete Simulink S-Function implementation |
AsrUserData |
Persistent calibration and runtime state |
LocalMatrixMath |
Internal Jacobi eigenvalue decomposition |
The implementation is designed specifically for deterministic real-time execution inside Simulink.
Highlights include:
- Dynamic channel count
- Circular sliding window
- Mean-centered covariance estimation
- Symmetric Jacobi eigenvalue decomposition
- Soft attenuation floor (5%) to prevent signal collapse
- No LAPACK or external numerical libraries
- Persistent state stored using Simulink
PWork
This project is licensed under the GNU General Public License v3.0.
