I have some problem about the CSP Spatial Filter Trianer BOX. I use it on our own data in OpenViBE, which is really convenient, and get a configuration XML finally. The spatial filter coeffcients results we get are like this (after reshape it)↓：

Then I try to compute spatial filter coeffcients not in OpenViBE, but in Matlab/Python using our own codes. The problem is that we get a different result. ↓(in matlab)

**We don’t know why it is like this.**We use the same data before inputting CSP filter trainer.

Our codes used in matlab and python are as follows↓. Any help would be appreciate.

Code: Select all

```
%BcspProjection.m
function W = BcspProjection(dataIn)
% dataIn: raw EEG data (Ns,Nc,Nt)
% Ns : the number of samples per channel
% Nc: the number of channels
% Nt: the number of trials
[Ns,Nc,Nt]=size(dataIn.x);
ClassLabel=unique(dataIn.y);
nbClasses=length(ClassLabel);
%The covariance of each trial was calculated
for i=1:Nt
E=dataIn.x(:,:,i)';
EE=E*E';
C(:,:,i)=EE./trace(EE);
end
%The mean covariance of all trials was calculated
for i=1:nbClasses
tmpCc(:,:,i)=mean(C(:,:,dataIn.y==ClassLabel(i)),3);
end
%The composite spatial covariance
Cc=tmpCc(:,:,1)+tmpCc(:,:,2);
%
[Uc Dc]=eig(Cc);
eigenvalue=diag(Dc);
[eigenvalue,eigIndex]=sort(eigenvalue,'descend');
Uc=Uc(:,eigIndex);
%The whitening transformation
P=diag(sqrt(1./eigenvalue))*Uc';
%
SL=P*tmpCc(:,:,1)*P';
%
[UB DL]=eig(SL);
eigenvalueS=diag(DL);
[eigenvalueS,eigIndexS]=sort(eigenvalueS,'descend');
UB=UB(:,eigIndexS);
%the projection matrix
W=UB'*P;
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%main.m
W = BcspProjection(EEGSignals);
Wp=W([1:3 (end-3+1):end],:);
```

ZY