Skip to content

Commit 934603f

Browse files
authored
Add files via upload
1 parent 26d8b7b commit 934603f

File tree

7 files changed

+356
-0
lines changed

7 files changed

+356
-0
lines changed
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
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;
9.32 MB
Binary file not shown.
7.38 MB
Binary file not shown.
336 Bytes
Binary file not shown.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
%% extracting features
2+
function features = myFeatureExtarction(X)
3+
fs=128;
4+
%energy
5+
f1=norm(X,2).^2;
6+
%skewness
7+
f2=skewness(X);
8+
%kurtosis
9+
f3=kurtosis(X);
10+
%shannon entropy
11+
f4=wentropy(X,'shannon');
12+
13+
%AR(4) coefficients
14+
coefficients_4=arburg(X,4);
15+
f5=coefficients_4(2);
16+
f6=coefficients_4(3);
17+
f7=coefficients_4(4);
18+
f8=coefficients_4(5);
19+
%AR(5) coefficients
20+
coefficients_5=arburg(X,5);
21+
f9=coefficients_5(2);
22+
f10=coefficients_5(3);
23+
f11=coefficients_5(4);
24+
f12=coefficients_5(5);
25+
f13=coefficients_5(6);
26+
27+
%PSD peak using Burg
28+
order=12;
29+
[Pxx,F]=pburg(X,order,[],fs);
30+
f14=max(Pxx); %PSD peak
31+
%PSD peak frequency
32+
% f15=find(Pxx==f14)/2;
33+
%first moment of PSD (mean)
34+
f16=mean(Pxx);
35+
%second moment of PSD (variance)
36+
f17=var(Pxx);
37+
38+
%wavelet transform-mean of absolutes
39+
[c,l]=wavedec(X,5,'db9');
40+
[cd2,cd3,cd4]=detcoef(c,l,[2 3 4]); %detail signals
41+
f18=mean(abs(cd2));
42+
f19=mean(abs(cd3));
43+
f20=mean(abs(cd4));
44+
%mean of squares
45+
f21=mean(cd2.^2);
46+
f22=mean(cd3.^2);
47+
f23=mean(cd4.^2);
48+
%standard deviation
49+
f24=std(cd2);
50+
f25=std(cd3);
51+
f26=std(cd4);
52+
%3rd moments of wavelet details (skewness)
53+
f27=skewness(cd2);
54+
f28=skewness(cd3);
55+
f29=skewness(cd4);
56+
%4th moments of wavelet details (kurtosis)
57+
f30=kurtosis(cd2);
58+
f31=kurtosis(cd3);
59+
f32=kurtosis(cd4);
60+
%variance
61+
% f33=var(X);
62+
%mean value
63+
% f34=mean(X);
64+
65+
features=[f1;f2;f3;f4;f5;f6;f7;f8;f9;
66+
f10;f11;f12;f13;f14;f16;f17;f18;f19;
67+
f20;f21;f22;f23;f24;f25;f26;f27;f28;f29;
68+
f30;f31;f32];
69+
end
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
%% Loading data sets
2+
load dataset_BCIcomp1;
3+
load labels_data_set_iii;
4+
%% defining the parameters
5+
fs=128;
6+
n_trials=size(x_train,3); %140
7+
n_channels=size(x_train,2); %3
8+
n=size(x_train,1); %1152
9+
kernel_size=256; %as mentioned in the essay
10+
k=546; % best works with RBF kernel
11+
%% filter design
12+
order=3; type='bandpass';
13+
[b_delta,a_delta]=butter(order,[0.01 4]/(fs/2),type);
14+
[b_theta,a_theta]=butter(order,[4 8]/(fs/2),type);
15+
[b_alpha,a_alpha]=butter(order,[8 12]/(fs/2),type);
16+
[b_beta,a_beta]=butter(order,[12 30]/(fs/2),type);
17+
[b_gamma,a_gamma]=butter(order,[30 63.99]/(fs/2),type);
18+
%% extracting features for trainig
19+
for m=1:n_trials
20+
temp1=[];%zeros(n_channels*n_features,1)
21+
for j=1:n_channels
22+
x=x_train((k:k+kernel_size-1),j,m);
23+
x_delta=filtfilt(b_delta,a_delta,x);
24+
x_theta=filtfilt(b_theta,a_theta,x);
25+
x_alpha=filtfilt(b_alpha,a_alpha,x);
26+
x_beta=filtfilt(b_beta,a_beta,x);
27+
x_gamma=filtfilt(b_gamma,a_gamma,x);
28+
%computing features for selected area
29+
channel_j_features=myFeatureExtarction(x_delta);
30+
temp1=cat( 1 , temp1 , channel_j_features );
31+
channel_j_features=myFeatureExtarction(x_theta);
32+
temp1=cat( 1 , temp1 , channel_j_features );
33+
channel_j_features=myFeatureExtarction(x_alpha);
34+
temp1=cat( 1 , temp1 , channel_j_features );
35+
channel_j_features=myFeatureExtarction(x_beta);
36+
temp1=cat( 1 , temp1 , channel_j_features );
37+
channel_j_features=myFeatureExtarction(x_gamma);
38+
temp1=cat( 1 , temp1 , channel_j_features );
39+
end
40+
temp2(:,m)=temp1;
41+
end
42+
features_train(:,:)=temp2;
43+
44+
for m=1:n_trials
45+
temp1=[];%zeros(n_channels*n_features,1)
46+
for j=1:n_channels
47+
x=x_test((k:k+kernel_size-1),j,m);
48+
x_delta=filtfilt(b_delta,a_delta,x);
49+
x_theta=filtfilt(b_theta,a_theta,x);
50+
x_alpha=filtfilt(b_alpha,a_alpha,x);
51+
x_beta=filtfilt(b_beta,a_beta,x);
52+
x_gamma=filtfilt(b_gamma,a_gamma,x);
53+
% computing features for selected area
54+
channel_j_features=myFeatureExtarction(x_delta);
55+
temp1=cat( 1 , temp1 , channel_j_features );
56+
channel_j_features=myFeatureExtarction(x_theta);
57+
temp1=cat( 1 , temp1 , channel_j_features );
58+
channel_j_features=myFeatureExtarction(x_alpha);
59+
temp1=cat( 1 , temp1 , channel_j_features );
60+
channel_j_features=myFeatureExtarction(x_beta);
61+
temp1=cat( 1 , temp1 , channel_j_features );
62+
channel_j_features=myFeatureExtarction(x_gamma);
63+
temp1=cat( 1 , temp1 , channel_j_features );
64+
end
65+
temp2(:,m)=temp1;
66+
end
67+
features_test(:,:)=temp2;
68+
%% applying PCA
69+
n_PCA_features=34;
70+
[~,newdata] = pca(features_train(:,:)');
71+
newdata=newdata(:,1:n_PCA_features)';
72+
PCA_features_train(:,:)=newdata;
73+
74+
[~,newdata] = pca(features_test(:,:)');
75+
newdata=newdata(:,1:n_PCA_features)';
76+
PCA_features_test(:,:)=newdata;
77+
78+
%% SVM-RBF
79+
%% gaussian
80+
sampleX_train=PCA_features_train;
81+
sampley_train=y_train;
82+
model=fitcsvm(sampleX_train',sampley_train,'Standardize',1,...
83+
'KernelFunction','RBF','KernelScale','auto');
84+
%testing the model
85+
sampleX_test=PCA_features_test;
86+
op=predict(model,sampleX_test');
87+
%computing the accuracy using leave one out method
88+
sampley_test=y_test;
89+
accuracy_normal_validation_with_PCA=sum(op==sampley_test)...
90+
/length(sampley_test) *100
91+
%accuracy is 45% in this method
92+
93+
94+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
%% dividing datas into train and test
2+
3+
function [X_train,X_test,y_train,y_test] = train_test_split(percent,X,y)
4+
5+
num=round(percent*size(X,2));
6+
7+
X_train=X(:,1:num);
8+
X_test=X(:,num+1:end);
9+
y_train=y(1:num);
10+
y_test=y(num+1:end);
11+
end

0 commit comments

Comments
 (0)