function CSPMatrix = learnCSP(EEGSignals,classLabels)
%
%Input:
%EEGSignals: the training EEG signals, composed of 2 classes. These signals
%are a structure such that:
% EEGSignals.x: the EEG signals as a [Ns * Nc * Nt] Matrix where
% Ns: number of EEG samples per trial
% Nc: number of channels (EEG electrodes)
% nT: number of trials
% EEGSignals.y: a [1 * Nt] vector containing the class labels for each trial
% EEGSignals.s: the sampling frequency (in Hz)
%
%Output:
%CSPMatrix: the learnt CSP filters (a [Nc*Nc] matrix with the filters as rows)
%
%See also: extractCSPFeatures
%check and initializations
nbChannels = size(EEGSignals.x,2); % 通道
nbTrials = size(EEGSignals.x,3); % 实验次数
nbClasses = length(classLabels); % 类别
if nbClasses ~= 2
disp('ERROR! CSP can only be used for two classes');
return;
end
covMatrices = cell(nbClasses,1); %the covariance matrices for each class
%% Computing the normalized covariance matrices for each trial
%% 为每个试验计算标准化的协方差矩阵。
trialCov = zeros(nbChannels,nbChannels,nbTrials);
for t=1:nbTrials
E = EEGSignals.x(:,:,t)'; %note the transpose
EE = E * E';
trialCov(:,:,t) = EE ./ trace(EE);
end
clear E;
clear EE;
%computing the covariance matrix for each class
for c=1:nbClasses
%EEGSignals.y==classLabels(c) returns the indeces corresponding to the class labels
covMatrices{c} = mean(trialCov(:,:,EEGSignals.y == classLabels(c)),3);
end
%the total covariance matrix
covTotal = covMatrices{1} + covMatrices{2};
%whitening transform of total covariance matrix
%caution: the eigenvalues are initially in increasing order注意:特征值最初是递增的
[Ut Dt] = eig(covTotal);
eigenvalues = diag(Dt);
[eigenvalues egIndex] = sort(eigenvalues, 'descend');
Ut = Ut(:,egIndex);
P = diag(sqrt(1./eigenvalues)) * Ut';
%transforming covariance matrix of first class using P
%用P变换第一类协方差矩阵
transformedCov1 = P * covMatrices{1} * P';
%EVD of the transformed covariance matrix 变换协方差矩阵的EVD
[U1 D1] = eig(transformedCov1);
eigenvalues = diag(D1);
[eigenvalues egIndex] = sort(eigenvalues, 'descend');
U1 = U1(:, egIndex);
CSPMatrix = U1' * P;
MissXy_
- 粉丝: 953
- 资源: 33
最新资源
- SparkSQL进阶操作相关数据
- java制作的小游戏,作为巩固java知识之用.zip
- Java语言写的围棋小游戏 半成品A Go game written in golang(Semi-finished).zip
- 基于Java-swing的俄罗斯方块游戏:源码+答辩文档+PPT.zip
- florr map详细版
- shiahdifhiahfiqefiwhfi weifwijfiwqufiqweefijeq0jfe
- registry-2.8.3<arm/amd>二进制文件
- Kotlin接口与抽象类详解及其应用
- 51单片机加减乘除计算器系统设计(proteus8.17,keil5),复制粘贴就可以运行
- lv_0_20241114231223.mp4
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈