Skip to content

neural-bit/Simulink-ASR

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Artifact Subspace Reconstruction (ASR) – Simulink S-Function

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.

ASR Simulink Pipeline


Overview

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)

Example Workflow

The figure above illustrates the complete real-time EEG acquisition, processing, artifact removal, and visualization pipeline.

Left: Simulink Processing Pipeline

The Simulink model performs the complete online EEG processing chain.

1. OpenBCI Acquisition (sfun_openbci)

Receives raw EEG samples from the OpenBCI Cyton board through a serial (COM) port.

2. Signal Scaling

Converts the incoming EEG samples into the desired units before further processing.

3. Band-Pass Filter (BP)

Removes DC drift and high-frequency noise while preserving the EEG frequency band of interest.

4. Notch Filter

Suppresses 50 Hz power-line interference.

5. Artifact Subspace Reconstruction (sfun_asr)

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.

6. IPC Streaming (sfun_neuroscope)

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.


Right: NeuroScope Visualization

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.


Features

  • 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

Prerequisites

Software

  • MATLAB / Simulink (tested on R2023a+)
  • Microsoft Visual Studio
  • MATLAB MEX compiler configured (mex -setup)

Getting Started

1 — Build the MEX File

Compile the S-Function from MATLAB:

mex -O main.cpp -output sfun_asr

2 — Add the MEX File to the MATLAB Path

addpath('path\to\project')

Verify MATLAB can locate

File Purpose
sfun_asr.mexw64 ASR Simulink S-Function

3 — Open Your Simulink Model

open('Simulink_ASR.slx')

4 — Configure the Block

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

5 — Calibration

The second input selects the operating mode.

Mode Description
1 Calibration
0 Artifact Removal

For calibration:

  1. Feed clean EEG into the block.
  2. Set the mode input to 1.
  3. Collect several seconds of artifact-free EEG.
  4. Switch the mode input to 0.
  5. The block automatically computes the baseline covariance and begins artifact suppression.

Inputs

Port Description
Input 1 Raw EEG (N×1)
Input 2 Mode (1 = Calibration, 0 = Run)

Output

Port Description
Output Cleaned EEG (N×1)

Algorithm

The implementation follows the standard Artifact Subspace Reconstruction workflow.

  1. Collect calibration EEG.
  2. Compute per-channel RMS values.
  3. Reject calibration outliers.
  4. Estimate the baseline covariance matrix.
  5. Compute eigenvectors using an internal Jacobi eigenvalue decomposition.
  6. Compute variance thresholds for each principal component.
  7. Build a sliding covariance window during runtime.
  8. Compare runtime variances against calibration thresholds.
  9. Softly attenuate artifact-related principal components.
  10. Reconstruct the cleaned EEG sample.

Unlike binary rejection methods, this implementation attenuates artifact components instead of completely removing them, preserving more physiological EEG information.


Source Files

File Description
main.cpp Complete Simulink S-Function implementation
AsrUserData Persistent calibration and runtime state
LocalMatrixMath Internal Jacobi eigenvalue decomposition

Implementation Notes

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

License

This project is licensed under the GNU General Public License v3.0.

About

A real-time Simulink S-Function implementing Artifact Subspace Reconstruction (ASR) for online EEG artifact removal.

Topics

Resources

Stars

9 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages