Skip to content

Commit d937ff0

Browse files
authored
kinda does the job
1 parent 51ccd5f commit d937ff0

14 files changed

+236
-0
lines changed

EEG_SeizureDetector_teamA.m

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
function gmarker = EEG_SeizureDetector_teamA(eeg)
2+
%The following is training of KNN clarification model with the EEG data
3+
%given by Professor online
4+
5+
features = load('features.mat'); %Importing already created feature matrix
6+
features = features.X; %access the matrix from struct
7+
gtrue = load('GT.mat'); %Importing already created GT marix
8+
gtrue = gtrue.Y; %access the matrix from struct
9+
%}
10+
%Finally Train the KNN model with the created GLOBAL feature & GT marixs
11+
mdl = fitcknn(features,gtrue,'NumNeighbors',11,'Standardize',1);
12+
%mdl = load('mdl500.mat');
13+
14+
%mdl = knn(); %This is creating the KNN model from scratch...20min for 1000
15+
%width...suggest not to do it.
16+
17+
band = 5; %0.5-30 Hz
18+
window = 500; %Fast Processing
19+
20+
%Extract the features from the testing signal (Also the remainder after
21+
%windowing
22+
[features,modulus] = eegmeasure(eeg,band,window);
23+
24+
%Run the test signal's features through the KNN clasification model
25+
result = predict(mdl,features);
26+
27+
%Reconstruct a full sized auto GT vector from the windowed version
28+
gmarker = [];
29+
for i = 1:length(result)
30+
%If value is 0, then add 'window' number of 0 to the full sized version
31+
if result(i) == 0
32+
gmarker = [gmarker;zeros(window,1)];
33+
%If value is 1, then add 'window' number of 1 to the full sized version
34+
else
35+
gmarker = [gmarker;ones(window,1)];
36+
end
37+
end
38+
%Add zeros to fill the spaces that were cut when the signal was windowed
39+
gmarker = [gmarker;zeros(modulus,1)]; %The remainder that was removed (Better FP than FN) that is why it is zero
40+
end

GT.mat

6.3 KB
Binary file not shown.

eegfilter.m

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
function [eeg] = eegfilter(signal,band)
2+
%This function is essentially the preproccesing portion
3+
%Select the appropriate Frequency Band
4+
if(band == 1) %Delta
5+
fc1 = 4;
6+
fc2 = 0.5;
7+
elseif(band == 2) %Theta
8+
fc1 = 8;
9+
fc2 = 4;
10+
elseif(band == 3) %alpha
11+
fc1 = 14;
12+
fc2 = 8;
13+
elseif(band == 4) %beta
14+
fc1 = 30;
15+
fc2 = 14;
16+
else %Appropriate band for Seizures
17+
fc1 = 30;
18+
fc2 = 0.5;
19+
end
20+
21+
fs = 256; %Sampling Freq
22+
23+
eegraw = signal;
24+
25+
%Normalize the signal
26+
eeg0 = eegraw - mean(eegraw);
27+
eegnorm = eeg0./max(eeg0);
28+
29+
%Create the Bandpass with the specified Frequencies
30+
[b1,a1] = butter(4,fc1/(fs/2),'low');
31+
[b2,a2] = butter(4,fc1/(fs/2),'high');
32+
33+
%Pass the inputted Signal through the Bandpass filter
34+
eegfilthold = filter(b1,a1,eegnorm);
35+
eegfilt = filter(b2,a2,eegfilthold);
36+
eeg = eegfilt;
37+
end

eegmeasure.m

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
function [features,modulus] = eegmeasure(signal,band,w)
2+
[eeg] = eegfilter(signal,band); %Filter the inputted signal
3+
4+
fs = 256;
5+
6+
modulus = mod(length(eeg),w); %Remainder total size when divided by window size
7+
eeg = eeg(1:(length(eeg) - modulus));
8+
9+
eegwin = zeros(w,length(eeg)/w); %Make matrix: Row=window size col=specific window
10+
11+
for i = 1:(length(eeg)/w) %iterate through each window
12+
eegwin(:,i) = (eeg(1+(i-1)*w:i*w)); %segment signal into the specific segments
13+
end
14+
15+
%Measure each feature
16+
17+
%RMS
18+
eegmean = zeros(length(eegwin(1,:)),1);
19+
for i = 1:length(eegmean)
20+
eegmean(i,1) = rms(eegwin(:,i));
21+
end
22+
23+
%Medium Freq
24+
eegfreq = zeros(length(eegwin(1,:)),1);
25+
for i = 1:length(eegfreq)
26+
eegfreq(i,1) = medfreq(eegwin(:,i),fs);
27+
end
28+
29+
%Entropy
30+
eegent = zeros(length(eegwin(1,:)),1);
31+
for i = 1:length(eegent)
32+
eegent(i,1) = entropy(eegwin(:,i));
33+
end
34+
35+
features = [eegmean,eegfreq,eegent]; %Output all 3 features together
36+
37+
end
38+
39+

entropy.m

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function ent = entropy(signal)
2+
% Generate the histogram
3+
nbins = 25;
4+
[n x] = hist(signal,nbins);
5+
6+
% Normalize the area of the histogram to make it a pdf
7+
n = n / sum(n);
8+
9+
% Calculate the entropy
10+
indices = n ~= 0;
11+
ent = -sum(n(indices).*log2(n(indices)));
12+
end

features.mat

24.7 MB
Binary file not shown.

features1000.mat

12.4 MB
Binary file not shown.

gtrue1000.mat

3.73 KB
Binary file not shown.

knn.m

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function mdl = knn()
2+
%Machine Learning
3+
gtrue = []; %Initilize GT matrix of all samples
4+
features = []; %Initilize features matrix for all samples
5+
for subject = 0:20 %Iterate through each patient
6+
7+
[EEG,seizureGT] = loadfile(subject); %Load the specific patient
8+
9+
for channel = 1:23 %Iterate through each channel
10+
band = 5; %All 0.5- 30Hz range
11+
window = 500; %Fast Processing (Could change later) (took ~20 min)
12+
13+
%Extract 3 features (RMS, MedFreq,Entropy)
14+
[ftemp] = eegmeasure(EEG(channel).ch,band,window);
15+
%Extract the segmented GT of specific subject
16+
[gtemp] = truthsegment(seizureGT,window);
17+
18+
%Add the specific subject features to the GLOBAL feature matrix
19+
features = [features;ftemp];
20+
%Add the specific subject GT to the GLOBAL GT matrix
21+
gtrue = [gtrue;gtemp];
22+
end
23+
end
24+
25+
%Finally Train the KNN model with the created GLOBAL feature & GT marixs
26+
mdl = fitcknn(features,gtrue,'NumNeighbors',5,'Standardize',1);
27+
end

loadfile.m

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function [EEG,seizureGT] = loadfile(subject)
2+
n = num2str(subject); %Convert the number into a string
3+
4+
%If statement corrects the numbering notations for subjects larger than 10
5+
if subject < 10
6+
file = strcat('EEG_subject00',n,'.mat'); %Create the name of the subject file
7+
truthfile = strcat('seizureGT_subject00',n,'.mat'); %Create the name of subject's GT file
8+
else
9+
file = strcat('EEG_subject0',n,'.mat'); %Create the name of the subject file
10+
truthfile = strcat('seizureGT_subject0',n,'.mat'); %Create the name of subject's GT file
11+
end
12+
13+
EEG = load(file); %Input the subject file
14+
EEG = EEG.EEG; %Input the subject file
15+
seizureGT = load(truthfile); %Input the GT file
16+
seizureGT = seizureGT.seizureGT; %Input the GT file
17+
end

mdl.mat

15.5 MB
Binary file not shown.

output.m

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
subject = input('\nEnter Subject #:\n'); %Input the patient #
3+
channel = input('\nEnter Channel #:\n'); %Input the channel to test
4+
5+
[EEG,seizureGT] = loadfile(subject); %Load the EEG and GT files of the subject
6+
7+
seizureMarker_auto = EEG_SeizureDetector_teamA(EEG(channel).ch); %Produce GT_auto
8+
9+
performanceMetrics = validation(seizureMarker_auto, seizureGT); %Output the metrics (tp,fp,fn,tn,accuracy)
10+
11+
performanceMetrics(5)
12+
fs = 256; %Sampling Frequency
13+
t = (0:1:length(EEG(channel).ch)-1)/fs; % create time variable
14+
%Plot binary seizure variable on top of EEG.ch data
15+
%Note scaling of SeizureGT to make visible in plot
16+
figure; plot(t, EEG(channel).ch);
17+
hold on;
18+
plot(t,(max(EEG(channel).ch))*seizureGT, 'r');
19+
plot(t,seizureMarker_auto*(max(EEG(channel).ch)), 'k')
20+
xlabel('Time(s)');
21+
ylabel('Amplitude');
22+
legend('EEG Signal','GT','Auto');

truthsegment.m

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function [seize] = truthsegment(truth,w)
2+
%This function is exclusivly for the GT files
3+
modulus = mod(length(truth),w); %Remainder total size when divided by window size
4+
truth = truth(1:(length(truth) - modulus));
5+
truthwin = zeros(w,length(truth)/w);%Make matrix: Row=window size col=specific window
6+
7+
for i = 1:(length(truth)/w) %iterate through each window
8+
truthwin(:,i) = (truth(1+(i-1)*w:i*w)); %segment signal into the specific segments
9+
end
10+
11+
seize = zeros(length(truthwin(1,:)),1); %Check if there is a seizure in the window
12+
for i = 1:(length(truthwin(1,:)))
13+
for j = 1:w
14+
if (truthwin(j,i) == 1)
15+
seize(i,1) = 1;
16+
break;
17+
end
18+
end
19+
end
20+
21+
22+
end

validation.m

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function [metrics] = validation(auto,gt)
2+
window = 100;
3+
auto = truthsegment(auto,window);
4+
gt = truthsegment(gt,window);
5+
6+
tp = 0; fp = 0; fn = 0; tn = 0;
7+
for i = 1:length(auto)
8+
if (gt(i) == 1) && (auto(i) == 1) %TP
9+
tp = tp+1;
10+
elseif (gt(i) == 0) && (auto(i) == 1) %FP
11+
fp = fp+1;
12+
elseif (gt(i) == 1) && (auto(i) == 0) %FN
13+
fn = fn+1;
14+
else %TN
15+
tn = tn+1;
16+
end
17+
end
18+
accuracy = ((tp+tn)/(tp+tn+fp+fn))*100;
19+
metrics = [tp,fp,fn,tn,accuracy];
20+
end

0 commit comments

Comments
 (0)