實現腦電訊號的情緒分類
兩個階段:
- 特徵提取方法:自迴歸(Autoregression,AR),公共空間模式(Common Spatial Pattern ,CSP),離散小波變換(Discrete Wavelet Transform,DWT)和功率譜密度( Power Spectral Density,PSD);
- 特徵分類方法:Bagging、Boosting、AdaBoost
一、特徵提取
共空間模式 Common Spatial Pattern (CSP)
main_CSP
% Extract Common Spatial Pattern (CSP) Feature
close all; clear; clc;
load dataset_BCIcomp1.mat
EEGSignals.x=x_train;
EEGSignals.y=y_train;
Y=y_train;
classLabels = unique(EEGSignals.y);
CSPMatrix = learnCSP(EEGSignals,classLabels);
nbFilterPairs = 1;
X = extractCSP(EEGSignals, CSPMatrix, nbFilterPairs);
EEGSignals.x=x_test;
T = extractCSP(EEGSignals, CSPMatrix, nbFilterPairs);
save dataCSP.mat X Y T
color_L = [0 102 255] ./ 255;
color_R = [255, 0, 102] ./ 255;
pos = find(Y==1);
plot(X(pos,1),X(pos,2),'x','Color',color_L,'LineWidth',2);
hold on
pos = find(Y==2);
plot(X(pos,1),X(pos,2),'o','Color',color_R,'LineWidth',2);
legend('Left Hand','Right Hand')
xlabel('C3','fontweight','bold')
ylabel('C4','fontweight','bold')
函式1:learnCSP
作用:學習CSP生成投影矩陣
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
covMatrices{c} = mean(trialCov(:,:,EEGSignals.y == classLabels(c)),3); %EEGSignals.y==classLabels(c) returns the indeces corresponding to the class labels
end
%the total covariance matrix
covTotal = covMatrices{1} + covMatrices{2};
%whitening transform of total covariance matrix
[Ut Dt] = eig(covTotal); %caution: the eigenvalues are initially in increasing order
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
transformedCov1 = P * covMatrices{1} * P';
%EVD of the transformed covariance matrix
[U1 D1] = eig(transformedCov1);
eigenvalues = diag(D1);
[eigenvalues egIndex] = sort(eigenvalues, 'descend');
U1 = U1(:, egIndex);
CSPMatrix = U1' * P;
函式2:extractCSP
作用:特徵提取
function features = extractCSP(EEGSignals, CSPMatrix, nbFilterPairs)
%
%extract features from an EEG data set using the Common Spatial Patterns (CSP) algorithm
%
%Input:
%EEGSignals: the EEGSignals from which extracting the CSP features. 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)
%CSPMatrix: the CSP projection matrix, learnt previously (see function learnCSP)
%nbFilterPairs: number of pairs of CSP filters to be used. The number of
% features extracted will be twice the value of this parameter. The
% filters selected are the one corresponding to the lowest and highest
% eigenvalues
%
%Output:
%features: the features extracted from this EEG data set
% as a [Nt * (nbFilterPairs*2 + 1)] matrix, with the class labels as the
% last column
%
%initializations
nbTrials = size(EEGSignals.x,3);
features = zeros(nbTrials, 2*nbFilterPairs+1);
Filter = CSPMatrix([1:nbFilterPairs (end-nbFilterPairs+1):end],:);
%extracting the CSP features from each trial
for t=1:nbTrials
%projecting the data onto the CSP filters
projectedTrial = Filter * EEGSignals.x(:,:,t)';
%generating the features as the log variance of the projected signals
variances = var(projectedTrial,0,2);
for f=1:length(variances)
features(t,f) = log(1+variances(f));
end
end
原始資料集:3為通道,140為試驗trails
生成的特徵資料
特徵散點圖:
AR係數
相關文章
- Python 超簡單實現人類面部情緒的識別Python
- 電腦科學的學科分類
- 利用訊號量實現執行緒順序執行執行緒
- 電商左側商品分類選單實現
- 腦機介面例項二:腦電訊號CSP處理腦機介面
- 常見的電腦電源型別區分 電源種類有哪些?型別
- Linux中訊號量的實現Linux
- Pytorch實現分類器PyTorch
- Bert文字分類實踐(一):實現一個簡單的分類模型文字分類模型
- Semaphore-訊號量的實現分析
- 電腦序列號怎麼查詢 電腦序列號的查詢方法介紹
- 通用mapper和分類實現APP
- ML.NET 示例:二元分類之使用者評論的情緒分析
- iOS 類知乎”分頁”效果的實現?iOS
- 使用jupyter實現貓和狗的分類
- 選單的無限極分類實現
- 提高工作效率超實用電腦技巧,電子記事本如何分類管理?
- 匿名內部類方式實現執行緒的建立執行緒
- 概率分類之樸素貝葉斯分類(垃圾郵件分類python實現)Python
- Mac電腦如何對檔案進行分類和整理Mac
- Mac電腦如何將檔案進行分類和整理?Mac
- 電腦常用埠號作用
- PHP實現無限極分類PHP
- 基於MindSpore實現BERT對話情緒識別情緒識別
- 破解垃圾分類難題,智慧分類如何實現最優解?
- [kotlin]帶分類的RecyclerView通用實現新思路KotlinView
- C++11 實現簡易的訊號槽。C++
- Java多執行緒併發工具類-訊號量Semaphore物件講解Java執行緒物件
- 情緒管理:如何照顧好自己的情緒
- Thread類實現買電影案例thread
- 膝上型電腦連結投影儀報訊號不支援
- 【QT】子類化QThread實現多執行緒QTthread執行緒
- 二分查詢實現符號表符號
- Laravel 框架實現無限極分類Laravel框架
- mysql 無限級分類實現思路MySql
- 基於Pytorch實現貓狗分類PyTorch
- Python執行緒專題5:訊號量與有邊界的訊號量Python執行緒
- win10 新買的電腦分割槽_電腦分盤怎麼分win10Win10