Introduction

In previous mental task classification, only two mental tasks are classified, and time domain technique (auto-regression) is applied to find features to be used for data-set preparation.


In this example, three mental tasks’ classifier will be designed to classify multiplication task, figure rotation task, and counting task. The objective of this example is to demonstrate another feature extraction method that uses frequency domain technique to formulate data-set using frequency bands' information.


Data preparation and feature extraction

The Figure 4.27 shows the block diagram of the feature extraction method using frequency domain technique. First the raw EEG data is segmented into 1 second time domain segments. These segments are then converted into frequency domain using Fast Fourier Transformation (FFT). The Power Spectral Densities of these data segments are then calculated before being broken into  frequency bands. There are five frequency bands:  delta band under 4Hz, theta band  from 5 to 8 Hz, alpha band from 9-14Hz, beta band from 13  to 30Hz, and gamma band from above 31Hz. Centroid frequencies of bands can be calculated, but in this example the means of these bands appears to be better features that provide better classification accuracy.  





Figure 4.27: Feature extraction for three mental task classifier using frequency bands.



The Matlab code for feature extraction procedure is shown as follows,


1. Main Matlab program for feature extraction


%% 1. Get EEG mental task data

clear all;

load eegdata;

Fs =250;            % Sampling rate = 250Hz

baseline_task =[];

multiplication_task=[];

letter_composing_task=[];

rotation_task =[];

counting_task =[];


%% 2. Extract mental tasks' data.

for i=1:length(data)

  subjectID = data{i}{1};

 if(subjectID=='subject 1')    

     taskname = data{i}{2};

     switch taskname

         case 'baseline',

             baseline_task=[baseline_task,data{i}{4}];

         case 'multiplication',

             multiplication_task=[multiplication_task,data{i}{4}];

          case 'letter-composing',

             letter_composing_task=[letter_composing_task,data{i}{4}];  

          case 'rotation',

             rotation_task=[rotation_task,data{i}{4}];    

          case 'counting',

             counting_task=[counting_task,data{i}{4}];

         otherwise,

     end 

 end 

end


%% 3. Calculate Power Spectral Density with 1 second data segmentation (no overlappinp)

[N,M] = size(multiplication_task);

L = round(M/Fs);

for i=1:N

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

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

     

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

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

     

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

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

     

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

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

     

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

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

end

 

%% 4. Convert mental tasks' PSD into frequency bands (as inputs)

baselineBands           = ConvertToFreqBands(baselineTask);

multiplicationBands     = ConvertToFreqBands(multiplicationTask);

lettercomposingBands    = ConvertToFreqBands(lettercomposingTask);

rotationBands           = ConvertToFreqBands(rotationTask);

countingBands           = ConvertToFreqBands(countingTask);

 

 

%% 5. Adding targets into the Bands to form data-set.

% Map targets to 2 mental tasks

multiplicationTargets   = [0 0 1];

rotationTargets         = [0 1 0];

countingTargets         = [1 0 0];

 

% Adding targets

multiplicationData      = AddTargets(multiplicationBands,multiplicationTargets);

rotationData            = AddTargets(rotationBands,rotationTargets);

countingData            = AddTargets(countingBands,countingTargets);

% Forming data-set

MentalTaskData          =[multiplicationData;rotationData;countingData];

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

[~,inputLength] =size(rotationBands);

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('MentalTaskTrainingFreq.csv','w');

fprintf(fid,'%s\n',textHeader);

fclose(fid);

dlmwrite('MentalTaskTrainingFreq.csv',edata,'-append');

 

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('MentalTaskTestingFreq.csv','w');

fprintf(fid,'%s\n',textHeader);

fclose(fid);

dlmwrite('MentalTaskTestingFreq.csv',edata,'-append');


2. Utility functions used in main script

function PSD = PowerSpectralDensities(taskData,Fs)

    psdResult =[];

    [N,M] = size(taskData);

    for i =1:N

       [freqTemp,temp] = PowerSpectralDensity(taskData(i,:),Fs);

        psdResult =[psdResult;temp];

    end

    PSD = psdResult;

end


function [freq,PSD] = PowerSpectralDensity(x,Fs)

    N = length(x);

    xdft = fft(x);

    xdft = xdft(1:N/2+1);

    psdx = (1/(Fs*N)) * abs(xdft).^2;

    psdx(2:end-1) = 2*psdx(2:end-1);

    freq = 0:Fs/length(x):Fs/2;

    PSD = (psdx);

end


function freqbandTask = ConvertToFreqBands(mentalTask)

    Result =[];

    [~,N] = size(mentalTask);

    for i =1:N-1

        [M,~] =size(mentalTask{i});

        temp =[];

        for j=1:M

            temp = [temp;FrequencyBand(mentalTask{i}(j,:))];

        end 

         Result = [Result,temp];

    end 

    freqbandTask = Result;

end 



function freqBand = FrequencyBand(x)

%    Calculate centroid frequency bands

%    deltaBand = sum(x(1:4).*[1:1:4])/sum(x(1:4));

%    thetaBand = sum(x(5:8).*[5:1:8])/sum(x(5:8));

%    alphaBand = sum(x(9:14).*[9:1:14])/sum(x(9:14));

%    betaBand  = sum(x(15:30).*[15:1:30])/sum(x(15:30));

%    gamaBand  = sum(x(31:40).*[31:1:40])/sum(x(31:40));

 

%    Calculate mean of frequency bands

   deltaBand = mean(x(1:4));

   thetaBand = mean(x(5:8));

   alphaBand = mean(x(9:14));

   betaBand  = mean(x(15:30));

   gamaBand  = mean(x(31:40));

   freqBand  =[deltaBand,thetaBand,alphaBand,betaBand,gamaBand];

end



function taskdata-set = AddTargets(freqBands,Targets)

    [N,~] =size(freqBands);

    for i = 1:N

        taskdata-set(i,:) = [freqBands(i,:),Targets];

    end 

end



Load data

 

Similar to previous example, three mental task training set can be loaded into ANNHUB in the Step 1 as shown in Figure 4.28.

Figure 4.28: Loading three mental task training set into ANNHUB

Configure Neural Network classifier


Figure 4.29: Configure Neural Network structure for three mental task recognition


In this example, a pattern recognition Neural Network that uses Scaled Conjugate Gradient training algorithm is configured as shown in Figure 4.29.


Train Neural Network classifier

\

Figure 4.30: Train Neural Network classifier using Scaled Conjugate Gradient with early stopping technique.


The stopping criteria is selected as in Figure 4.30 and early stopping technique is applied to avoid over-fitting issue.

Evaluate the trained Neural Network classifier

Figure 4.31: Evaluate the trained Neural Network three mental task classifier using confusion matrix


The confusion matrix shown in Figure 4.31, overall accuracy might achieve over 83%.

Test the trained Neural Network classifier with new data



Figure 4.32: Test the trained Neural Network classifier using new data-set for three mental tasks.


The new test set is used to verify the generalization of the trained Neural Network. The trained Neural Network is able to recognise three mental tasks with success rate of around 76%.  

Conclusion

In this example, frequency domain technique has been used to extract feature from raw EEG data-set to form training set for the three mental task Neural Network classifier. Since the EEG signals are non-stationary, the obtained features might not be significant to review the distinguish characteristics of three mental tasks. As a result, the overall accuracy for new data-set only reaches around 76%.