1+ %% loading data
2+ % loading the features we have computed
3+ load features ;
4+ % loading data sets
5+ load dataset_BCIcomp1 ;
6+ %% defining the parameters
7+ fs= 128 ;
8+ n_features= 34 ; % number of extracted features
9+ n_trials= size(x_train ,3 ); % 140
10+ n_channels= size(x_train ,2 ); % 3
11+ n= size(x_train ,1 ); % 1152
12+ kernel_size= 256 ; % as mentioned in the essay
13+ s_point= n - kernel_size + 1 ; % 897, which also describes number of different sets of features we can have
14+ %% applying PCA somehow that 95% of varriance maintains
15+ n_PCA_features= 10 ; % to save 95% of variance
16+ PCA_features= zeros(n_PCA_features ,n_trials ,s_point );
17+ for i= 1 : s_point
18+ [~ ,newdata ] = pca(features(: ,: ,i )' );
19+ newdata= newdata(: ,1 : n_PCA_features )' ;
20+ PCA_features(: ,: ,i )=newdata ;
21+ end
22+ save PCA_features PCA_features
23+ %% showing labels for a specific start point
24+ % features when the start point is j and we choose the i'th feature
25+ j= 431 ;% start point
26+ i= 8 ;% number of the feature
27+ sample1= features(i : n_features : n_channels * n_features ,: ,j );
28+ % finding the datas with 1 lable
29+ leftindex1= find(y_train == 1 );
30+ % finding the datas with 2 lable
31+ rightindex1= find(y_train == 2 );
32+ plot3(sample1(1 ,leftindex1 ),sample1(2 ,leftindex1 ),...
33+ sample1(3 ,leftindex1 ),' .' ,' color' ,' r' );
34+ xlabel(' channel-1' );
35+ ylabel(' channel-2' );
36+ zlabel(' channel-3' );
37+ hold on
38+ plot3(sample1(1 ,rightindex1 ),sample1(2 ,rightindex1 ),...
39+ sample1(3 ,rightindex1 ),' .' ,' color' ,' b' );
40+ legend(' left-hand' ,' right-hand' );
41+ grid on
42+ grid minor
43+ %% KNN
44+ tp_accuracy= zeros(1 ,n_trials );
45+ accuracy= zeros(1 ,s_point );
46+ for k= [1 3 5 7 ]
47+ figure ;
48+ for i= 1 : s_point
49+ % the dataset with the "i"th start point
50+ sample= features(: ,: ,i );
51+ % dividing data into test and split with the function we defined
52+ [sampleX_train ,sampleX_test ,sampley_train ,sampley_test ]=...
53+ train_test_split(0.7 ,sample ,y_train );
54+ % training the model for KNN with k neighbours
55+ model= fitcknn(sampleX_train ' ,sampley_train ,' NumNeighbors' ,k );
56+ % testing the model
57+ op= predict(model ,sampleX_test ' );
58+ % computing the accuracy
59+ accuracy(i )=sum(op == sampley_test )/length(sampley_test ) * 100 ;
60+ end
61+ best_point= find(max(accuracy )==accuracy );
62+ plot(accuracy );
63+ hold on
64+ stem(best_point ,accuracy(best_point ),' *' ,' r' );
65+ title([' KNN-k=' ,num2str(k )]);
66+ xlabel(" start-point" );
67+ ylabel(" accuracy" );
68+ grid on ;
69+ grid minor ;
70+ hold off ;
71+ end
72+ %% SVM
73+ %% linear
74+ accuracy= zeros(1 ,s_point );
75+ for i= 1 : s_point
76+ % the dataset with the "i"th start point
77+ sample= features(: ,: ,i );
78+ % dividing data into test and split with the function we defined
79+ [sampleX_train ,sampleX_test ,sampley_train ,sampley_test ]=...
80+ train_test_split(0.7 ,sample ,y_train );
81+ % training the model with linear kernel
82+ model= fitcsvm(sampleX_train ' ,sampley_train ,' Standardize' ,1 ,...
83+ ' KernelFunction' ,' linear' );
84+ % testing the model
85+ op= predict(model ,sampleX_test ' );
86+ % computing the accuracy
87+ accuracy(i )=sum(op == sampley_test )/length(sampley_test ) * 100 ;
88+ end
89+ best_point= find(max(accuracy )==accuracy );
90+ figure ;
91+ plot(accuracy );
92+ hold on
93+ stem(best_point ,accuracy(best_point ),' *' ,' r' );
94+ title(' SVM-linear-kernel' );
95+ xlabel(" start-point" );
96+ ylabel(" accuracy" );
97+ grid on ;
98+ grid minor ;
99+ hold off ;
100+ %% gaussian
101+ accuracy= zeros(1 ,s_point );
102+ for i= 1 : s_point
103+ % the dataset with the "i"th start point
104+ sample= features(: ,: ,i );
105+ % dividing data into test and split with the function we defined
106+ [sampleX_train ,sampleX_test ,sampley_train ,sampley_test ]=...
107+ train_test_split(0.7 ,sample ,y_train );
108+ % training the model with gaussian kernel
109+ model= fitcsvm(sampleX_train ' ,sampley_train ,' Standardize' ,1 ,...
110+ ' KernelFunction' ,' RBF' ,' KernelScale' ,' auto' );
111+ % testing the model
112+ op= predict(model ,sampleX_test ' );
113+ % computing the accuracy
114+ accuracy(i )=sum(op == sampley_test )/length(sampley_test ) * 100 ;
115+ end
116+ best_point= find(max(accuracy )==accuracy );
117+ figure ;
118+ plot(accuracy );
119+ hold on
120+ stem(best_point ,accuracy(best_point ),' *' ,' r' );
121+ title(' SVM-gaussian-kernel' );
122+ xlabel(" start-point" );
123+ ylabel(" accuracy" );
124+ grid on ;
125+ grid minor ;
126+ hold off ;
127+
128+ %% LDA
129+ accuracy= zeros(1 ,s_point );
130+ for i= 1 : s_point
131+ % the dataset with the "i"th start point
132+ sample= features(: ,: ,i );
133+ % dividing data into test and split with the function we defined
134+ [sampleX_train ,sampleX_test ,sampley_train ,sampley_test ]=...
135+ train_test_split(0.7 ,sample ,y_train );
136+ % training the model using LDA
137+ model= fitcdiscr(sampleX_train ' ,sampley_train );
138+ % testing the model
139+ op= predict(model ,sampleX_test ' );
140+ % computing the accuracy
141+ accuracy(i )=sum(op == sampley_test )/length(sampley_test ) * 100 ;
142+ end
143+ best_point= find(max(accuracy )==accuracy );
144+ figure ;
145+ plot(accuracy );
146+ hold on
147+ stem(best_point ,accuracy(best_point ),' *' ,' r' );
148+ title(' LDA' );
149+ xlabel(" start-point" );
150+ ylabel(" accuracy" );
151+ grid on ;
152+ grid minor ;
153+ hold off ;
154+ %% Naive bayes
155+ accuracy= zeros(1 ,s_point );
156+ for i= 1 : s_point
157+ % the dataset with the "i"th start point
158+ sample= features(: ,: ,i );
159+ % normalizing data
160+ sample= [sample(1 ,: )/max(sample(1 ,: )) ; sample(2 ,: )/max(sample(2 ,: )) ;...
161+ sample(3 ,: )/max(sample(3 ,: ))];
162+ % dividing data intos test and split with the function we defined
163+ [sampleX_train ,sampleX_test ,sampley_train ,sampley_test ]=...
164+ train_test_split(0.7 ,sample ,y_train );
165+ % training the model using naive bayes
166+ model= fitcnb(sampleX_train ' ,sampley_train );
167+ % testing the model
168+ op= predict(model ,sampleX_test ' );
169+ % computing the accuracy
170+ accuracy(i )=sum(op == sampley_test )/length(sampley_test ) * 100 ;
171+ end
172+ best_point= find(max(accuracy )==accuracy );
173+ figure ;
174+ plot(accuracy );
175+ hold on
176+ stem(best_point ,accuracy(best_point ),' *' ,' r' );
177+ title(' naive-bayes' );
178+ xlabel(" start-point" );
179+ ylabel(" accuracy" );
180+ grid on ;
181+ grid minor ;
182+ hold off ;
0 commit comments