Theory and Implementation of CSP(Common Spatial Pattern) Algorithm

Hiroaki Kubo
3 min readNov 4, 2023

--

Common Spatial Pattern is a technique to analyze multi-channel data based on recordings from two classes (conditions). CSP yields a data-driven supervised decomposition of the signal parameterized by a matrix W(C×C) (C is the number of channels) that projects the signal x(t) in the original sensor space to xCSP​(t), which lives in the surrogate sensor space, as follows:

CSP, like PCA, allows for dimensionality reduction and is especially often used to analyze EEG data. The need for spatial filtering in EEG data analysis can be found here.

How to calculate W

Here is the calculation method based on this paper.
The problem of recovering sources from their linear mixtures without knowledge of the mixing channel has been widely studied. In its simplest form it can be expressed as the problem of identifying the factorization of the N-dimensional observations X into a mixing channel A and M-dimensional sources S,

We consider each column of X and S as a sample in time. In this case we can write Eq(1) as,

We begin by noting that the matrix A explains various cross-statistics of the observations x(t) as an expansion of the corresponding diagonal cross-statistics of the sources s(t). From Eq(3), we can find the follow equation

An obvious example is the time averaged covariance matrix, Rx​=∑tE[x(t)x⊺(t)], where Rs is diagonal if we assume independent or decorrelated sources.

The same process is performed for the other class of data, and using Q, the following equation is obtained

Note that these two conditions alone are already sufficient for source separation. To recover the sources from the observation x(t) we must find an inverse matrix W such that WA=I. In this case we have,

Let us further assume nonzero diagonal values for Qs​. After multiplying Equations (3) and (4) with W and Eq(4) with Qs−1​ we combine them to obtain,

where by assumption, σ=RsQs−1​, is a diagonal matrix.

Eq(8) is obtained by the following calculation procedure.

This constitutes a generalized eigenvalue equation, where the eigenvalues represent the ratio of the individual source statistics measured by the diagonals of Rs​ and Qs​. So we can obtain W by solving the generalized eigenvalue problem with Eq(8).

Implementaion of CSP algorithm with python is here.

--

--

Hiroaki Kubo

Computer vision engineer. 3D computer vision/ Machine learning / Optimization / Graphics, https://www.linkedin.com/in/hiroaki-kubo-2819951ba/