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;
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
具体使用请参考个人博客: 运动想象丨特征提取 MATLAB例程(一):https://blog.csdn.net/MissXy_/article/details/81808579 运动想象丨特征分类 MATLAB例程(二): https://blog.csdn.net/MissXy_/article/details/81809194
资源推荐
资源详情
资源评论
收起资源包目录
Motor Imagery.zip (10个子文件)
Motor Imagery
MydataCSP.mat 4KB
MyTestData1.mat 132.43MB
labels_data_set_iii.mat 336B
run_MyData_FeatureExtract.m 790B
run_FeatureExtract.m 777B
dataCSP.mat 5KB
dataset_BCIcomp1.mat 7.38MB
SVM_Classification.m 362B
learnCSP.m 2KB
extractCSP.m 2KB
共 10 条
- 1
资源评论
- qq_432573382019-03-21里面讲得很详细~~
- sky@dk2019-04-24感觉还可以
MissXy_
- 粉丝: 953
- 资源: 33
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功