This example aims to design a classification system that can classify all 5 mental tasks from EEG signals. Although three mental task classifier has been designed in previous example, the accuracy rate for new data-set only achieves around 76%. As the result, the same design for five mental task classier will result in poorer success rate.  

In order to increase the accuracy of five mental task classification system, new feature extraction technique combines with optimal Bayesian Neural Network structure might be the solution. Inn this example, Independent Component Analysis (ICA) is applied to separate blind sources from auto-regression (AR) coefficients to obtain significant features and reduce data-set dimension.

Data preparation and feature extraction

The process of utilizing ICA to extract features from AR coefficients is shown in following Figure.


Figure 4.33: Feature extraction for five mental tasks using AR and ICA combination.

For given mental task, first raw EEG data is segmented into 0.5 second of time domain segments. Auto-regression technique will be applied to fit segments into AR model (order 6) using Burg method. Each electrode channel will produce 6 AR coefficients that are corresponding to its AR model. After combining 6 electrode channels, 36 AR coefficient vector will form a data sample. The ICA will be used to extract signification features from this AR vector to form 6-element ICA vector that can be used as an input for a Neural Network classifier.

1. Main Matlab program for feature extraction

%% 1. Get EEG mental task data

clear all;

load eegdata;

Fs =125;            % Using the window size = 0.5 second (125 samples)

baseline_task =[];



rotation_task =[];

counting_task =[];

%% 2. Extract EEG data from the subject 1

for i=1:length(data)

  subjectID = data{i}{1};

 if(subjectID=='subject 1')    

     taskname = data{i}{2};

     switch taskname

         case 'baseline',


         case 'multiplication',


          case 'letter-composing',


          case 'rotation',


          case 'counting',







%% 3. Calculate Autogressive model coeficients using burg method with order of 6

[N,M] = size(multiplication_task);

L = round(M/Fs);

for i=1:N

     temp= reshape(baseline_task(i,:),Fs,L)' ;

     baselineTask{i}= ARCoefs(temp,Fs);


     temp= reshape(multiplication_task(i,:),Fs,L)' ;

     multiplicationTask{i}= ARCoefs(temp,Fs);


     temp= reshape(letter_composing_task(i,:),Fs,L)' ;

     lettercomposingTask{i}= ARCoefs(temp,Fs);


     temp= reshape(rotation_task(i,:),Fs,L)' ;

     rotationTask{i}= ARCoefs(temp,Fs);


     temp= reshape(counting_task(i,:),Fs,L)' ;

     countingTask{i}= ARCoefs(temp,Fs);



%% 4. Combine coeffients from 6 EEG channels into a single source

baselineBands           = CombineChannels(baselineTask);

multiplicationBands     = CombineChannels(multiplicationTask);

lettercomposingBands    = CombineChannels(lettercomposingTask);

rotationBands           = CombineChannels(rotationTask);

countingBands           = CombineChannels(countingTask);


%% Apply ICA

icabaselineBands            = ApplyICA(baselineBands);

icamultiplicationBands      = ApplyICA(multiplicationBands);

icalettercomposingBands     = ApplyICA(lettercomposingBands);

icarotationBands            = ApplyICA(rotationBands);

icacountingBands            = ApplyICA(countingBands);


%% 5. Construct the mental task data-set for baseline and multiplication tasks

% Map targets to mental tasks

baselineTargets         = [1 0 0 0 0];

multiplicationTargets   = [0 1 0 0 0];

lettercomposingTargets  = [0 0 1 0 0];

rotationTargets         = [0 0 0 1 0];

countingTargets         = [0 0 0 0 1];


% Adding targets

baselineData        = AddTargets(icabaselineBands,baselineTargets);

multiplicationData  = AddTargets(icamultiplicationBands,multiplicationTargets);

lettercomposingData = AddTargets(icalettercomposingBands,lettercomposingTargets);

rotationData        = AddTargets(icarotationBands,rotationTargets);

countingData        = AddTargets(icacountingBands,countingTargets);


% Forming data-set

MentalTaskData =[baselineData;multiplicationData;lettercomposingData;rotationData;countingData];

%% 6. Seperate data-set into training set and testing set, and save them into files

[~,inputLength] =size(icabaselineBands);

newdata-set = shuffleRow(MentalTaskData);

[N, ~] = size(newdata-set);

M = floor(0.7*N);

trainingset = newdata-set(1:M,:);

testset = newdata-set(M+1:end,:);


input = trainingset(:,1:inputLength);

target = trainingset(:,inputLength+1:end);

edata =[input target];

[N,ip] = size(input);

[N,op] = size(target);

textHeader= getTextHeader(ip,op);

%write header to file

fid = fopen('MentalTaskTrainingICAAR.csv','w');





input = testset(:,1:inputLength);

target = testset(:,inputLength+1:end);


edata =[input target];

[N,ip] = size(input);

[N,op] = size(target);

textHeader= getTextHeader(ip,op);

%write header to file

fid = fopen('MentalTaskTestingICAAR.csv','w');





2. Utility functions used in main script

function icaData = ApplyICA(dataTask)

      Mdl = rica(dataTask,6,'IterationLimit',100);

     icaData = transform(Mdl,dataTask);



Load data

After feature extraction procedure, the obtained EEG features will be used to form five mental task training data-set (MentalTaskTrainingICAAR.csv) and test data-set (MentalTaskTestingICAAR.csv). The training data-set will be further divided into three subset defined in training data ratio in Step 2: training set is used for training procedure, validation set is used for preventing over-fitting issue, and test set is used for evaluating the training Neural Network.  This training data-set (MentalTaskTrainingICAAR.csv)  is only used in design and evaluation stage (Step 1 to Step 4). After the trained Neural Network is verified via evaluation procedure, the test data-set (MentalTaskTestingICAAR.csv) will be loaded to test how the trained Neural Network responds to completely new data-set. As a result, the test data-set is completely different to the test set, a fraction of the training data-set, used in design procedure in Step 1 to Step 4. In other words, the test data-set refers to completely new data-set (MentalTaskTestingICAAR.csv), and test set refers to a fraction of the training data-set with the fraction ratio = 0.5*(100-training data ratio) in percentages defined in the Step 2.

Figure 4.34: Load five mental task training data-set into ANNHUB

Configure Neural Network classifier

Figure 4.35: Configure five mental task classifier in ANNHUB

In this example, the Bayesian Neural Network is configured to classify five mental tasks’ problem. Since the data-set contains only 1000 data samples, and this data is divided into training data-set (MentalTaskTrainingICAAR.csv) containing 700 samples and test data-set (MentalTaskTestingICAAR.scv) containing 300 samples. The training data-set is further divided into training set (75%*700 = 525 samples) for training, and 175 samples for both validation set and test set. As a result, the training data-set is small, and it could be not sufficient to be used in training procedure. The Bayesian Neural Network equipped with excellent generalization property, so it does not require validation set to avoid over-fitting issue. It is the best candidate to be used in the case when data-set size is small. As shown in Figure 4.35, the Bayesian Neural Network is configured so that 525 samples of training data-set will be used for training, and 175 samples of the training data-set will be used for testing (test set).  

Get optimal structure for Neural Network

Figure 4.36: Determine optimal number of hidden nodes for five mental task classifier in ANNHUB

When designing a Neural Network classifier, it is important to know what is the optimal structure that could lead to best classification result. Usually, a design relies on trial and error method to determine the number of hidden nodes, and this method cannot guarantee the best classification result. Thanks to evidence framework based on probability theory, the optimal hidden nodes could be obtained via scanning through possible neural network structures and compare their log evidences. Figure 4.36 shows that the five mental tasks’ classifier with 17 hidden nodes would be the best structure for this application.  

Train Neural Network classifier

Figure 4.37: Train five mental task classifier in ANNHUB

Since the Bayesian Regularization training algorithm is used, early stopping technique is not required to avoid over-fitting issue. Figure 4.37 shows that after two training attempts, the best performance index is achieved with 50 training epochs.

Evaluate the trained Neural Network classifier

Figure 4.38: Train five mental task classifier in ANNHUB

The confusion matrix and ROC curves are used to evaluate trained Neural Network as shown in Figure 4.38, and the classification accuracy achieves over 90% for both training and validation set.

Test the trained Neural Network classifier with new data

Figure 4.39: Test trained classifier in ANNHUB with new data-set.

Figure 4.40: ROC curves of the trained classifier in ANNHUB with new data-set.

The new data-set (MentalTaskTestingICAAR.csv) is used to test the train Neural Network classifier. The Figure 4.40 shows that the five mental task classifier responds with new data-set with accuracy rate of around 85%.


In this example, new feature extraction method based on the combination between Auto-Regression and Independent Component Analysis is presented to reveal key features of raw EEG signals that help a Neural Network classifier achieve better classification accurate rate. Although this technique is applied in EEG signals, this principle can be applied in any applications.

This example also provides design tips to deal with classification problem with small data samples. Optimal structure of the classifier could be obtained thanks for evidence framework.