Regularized CSP Trainer


  • Plugin name : Regularized CSP Trainer
  • Version : 0.6
  • Author : Jussi T. Lindgren
  • Company : Inria
  • Short description : Computes Common Spatial Pattern filters with regularization
  • Documentation template generation date : Dec 30 2016
  • WARNING : this box has been marked as UNSTABLE by the developer. It means that its implementation may be incomplete or that the box can only work under well known conditions. It may possibly crash or cause data loss. Use this box at your own risk, you've been warned.


The box estimates a filter bank of spatial filters using Common Spatial Patterns (CSP) algorithm. The algorithm tries to find a linear transform of the data that makes the signal conditions (or classes) more distinct, when the data is projected to the matrix of the filters found by the algorithm (i.e. s=Wx, where we assume x is the signal sample, W is the discovered filter bank, and s a lower-dimensional representation of the sample). The spatial filters are constructed for two conditions to maximize the variance of the signals of the first condition while at the same time minimizing it for the second condition. If signal variance contains discriminatory information, CSP filters can be useful in classification situations where designing spatial filter masks such as Laplacians manually is cumbersome or when it is believed that a filter optimized to the current data/user would produce better results than using an one-size-fits all filter.

In principle CSP can be useful in experiments where there is discriminative information in the variance (or power) of the signal conditions, for example motor imagery or SSVEP.

The box implements some of the methods described e.g. by Lotte & Guan [1]. Especially, it allows using the methods called "CSP with Diagonal Loading" and "CSP with Tikhonov Regularization" in the paper. In the approach of the paper, the filters for condition 1 are found by eigenvector decomposition of inv(Sigma1+rho*I)*Sigma2, where Sigma1 and Sigma2 are empirical covariance matrices for the two conditions, rho the amount of Tikhonov regularization and I an identity matrix. For condition 2, the formula is the same with the sigmas swapped. The matrices Sigma1 and Sigma2 may be optionally shrunk towards diagonal matrices.

In situations with more than two conditions, the box implements a one-vs-all approach, where each class gives a Sigma1 in turn, with the covariances of the other classes combined with a class frequency weighted average to obtain Sigma2. Hence, for k classes, k pairings are estimated, and out of each pair, a requested amount of filters is selected separately. This results in k times the amount of filters requested in total.

To avoid caching the whole dataset in the box, the algorithm tries to estimate the required covariances incrementally. The box supports a few different incremental ways to compute the covariances. The 'block average' approach takes an average of all the covariances of the incoming signal chunks, an approach described in [2], whereas the incremental (per sample) method aims to implement Youngs & Cramer algorithm as described in [3]. Which one gives better results may depend on the situation – however, taking an average of covariance matrices is not your usual textbook method for computing covariance over the whole data.

Finally, the box also presents an option to try to compensate for the effect of changes in the average signal power over time. This is can be done by enabling 'Trace Normalization' setting. When trace normalization is enabled, each data chunks contribution to the covariance gets divided by its trace. See miscellaneous notes for details.


1. Stimulations

This stimulus input is needed to indicate the end of a recording session (or end of file). It triggers the training/computation of the CSP filters.

  • Type identifier : Stimulations (0x6f752dd0, 0x082a321e)

2. Signal condition 1

This input expects epoched data for the first condition (e.g. epochs for left hand motor imagery).

  • Type identifier : Signal (0x5ba36127, 0x195feae1)

3. Signal condition 2

This input expects epoched data for the second condition (e.g. epochs for right hand motor imagery).

  • Type identifier : Signal (0x5ba36127, 0x195feae1)


1. Train-completed Flag

The CSP Trainer outputs the stimulation OVTK_StimulationId_TrainCompleted when the training process was successful. No output is produced if the process failed.

  • Type identifier : Stimulations (0x6f752dd0, 0x082a321e)


1. Train Trigger

The stimulus identifier denoting the end of a recording session or end of file, e.g. OVTK_GDF_End_Of_Session or OVTK_StimulationId_ExperimentStop.

  • Type identifier : Stimulation (0x2c132d6e, 0x44ab0d97)
  • Default value : [ OVTK_GDF_End_Of_Session ]

2. Spatial filter configuration

The path and filename of the configuration file in which the computed spatial filters are saved.

  • Type identifier : Filename (0x330306dd, 0x74a95f98)
  • Default value : [ ]

3. Filters per condition

How many spatial filters should be selected per class.

  • Type identifier : Integer (0x007deef9, 0x2f3e95c6)
  • Default value : [ 2 ]

4. Save filters as box config

If true, the file written will be a box configuration override especially for the spatial filter box. Otherwise, it will be an ASCII matrix.

  • Type identifier : Boolean (0x2cdb2f0b, 0x12f231ea)
  • Default value : [ false ]

5. Covariance update

Method to update the covariances

  • Type identifier : Update method (0x59e83f33, 0x592f1dd0)
  • Default value : [ Chunk average ]

6. Trace normalization

Is trace normalization done when appending the per-chunk covariances?

  • Type identifier : Boolean (0x2cdb2f0b, 0x12f231ea)
  • Default value : [ false ]

7. Shrinkage coefficient

The amount of shrinkage, between 0 and 1. It interpolates between the covariance matrices and the diagonal matrix.

  • Type identifier : Float (0x512a166f, 0x5c3ef83f)
  • Default value : [ 0.0 ]

8. Tikhonov coefficient

The amount of Tikhonov regularization, bigger signifies more. This equals parameter rho in the description above. If 0, the method behaves approximately as a regular CSP.

  • Type identifier : Float (0x512a166f, 0x5c3ef83f)
  • Default value : [ 0.0 ]



The suitable amount of regularization may depend on the variance of the data. You may need to try different values to find the one that suits your situation best. Before the CSP training, it may be useful to temporally filter the input data to remove bands which are believed to have no relevant discriminative information.

Note that the usage of the CSP filters before classification training can make the cross-validation results optimistic, unless strictly non-overlapping parts of the data were used to train the CSP and the classifier (disjoint sets for each).

The trace normalization can be found in the literature [2]. The idea is to normalize the scale of each chunk in order to compensate for a possible signal power drift over time during the EEG recording, making each chunks' covariance contribute similarly to the aggregate regardless of the current chunks average power.

To get the "CSP with Diagonal Loading" of Lotte & Guan paper [1], set shrinkage to a positive value and Tikhonov to 0. To get the "CSP with Tikhonov regularization", do the opposite. You can also try a mixture of the two. Note that the Guan & Lotte paper does not appear to use trace normalization.

To get the CSP resembling the one in the Muller-Gerkin paper, set Trace Normalization to True and the Covariance method to Chunk Average, with no regularization. Then, feed the algorithm each trial as a separate chunk (with Stimulation based epoching box). This is also the classic OV way of computing the CSP.

Once the spatial filters are computed and saved, you can load and apply the filters with the Spatial Filter box.


1) Lotte & Guan: "Regularizing common spatial patterns to Improve BCI Designs: Unified Theory and New Algorithms", 2011.

2) Muller-Gerkin & al., "Designing optimal spatial filters for single-trial EEG classification in a movement task", 1999.

3) Chan, Golub & Leveq, "Updating formulae and a pairwise algorithm for computing sample variances", 1979.