-
Notifications
You must be signed in to change notification settings - Fork 0
/
classify_knn.m
55 lines (43 loc) · 1.84 KB
/
classify_knn.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
function [ accuraccy, TP, FP, TN, FN ] = classify_knn( PATH_TRAIN, PATH_TEST, stWin, stStep, mtWin, mtStep, Statistics, K )
MODEL_NAME = ['knn_' datestr(now,'ddmmyyyy_HHMMSSFFF') '.mat'];
MODEL_PATH = ['models\' MODEL_NAME];
kNN_model_add_class(MODEL_PATH, 'good', [PATH_TRAIN 'good'], Statistics, stWin, stStep, mtWin, mtStep);
kNN_model_add_class(MODEL_PATH, 'bad', [PATH_TRAIN 'bad'], Statistics, stWin, stStep, mtWin, mtStep);
correct_predictions = 0;
wrong_predictions = 0;
TP = 0;
FP = 0;
TN = 0;
FN = 0;
files_to_test = dir(PATH_TEST);
files_to_test(1) = []; files_to_test(1) = [];
[Features, ClassNames, MEAN, STD, Statistics, ...
stWin, stStep, mtWin, mtStep] = kNN_model_load(MODEL_PATH);
for l = 1:length(files_to_test)
[y, Fs] = wavread([PATH_TEST files_to_test(l).name]);
stF = stFeatureExtraction(y, Fs, stWin, stStep);
mtWinRatio = mtWin/stWin;
mtStepRatio = mtStep/stStep;
[mtFeatures] = mtFeatureExtraction(stF, mtWinRatio, mtStepRatio, Statistics);
mtFeatures = mean(mtFeatures, 2);
[P, label] = classifyKNN_D_Multi(Features, (mtFeatures - MEAN') ./ STD', K, 1);
if(strfind(files_to_test(l).name, 'b'))
if(label == 1)
correct_predictions = correct_predictions + 1;
TP = TP + 1;
else
wrong_predictions = wrong_predictions + 1;
FP = FP + 1;
end;
elseif(strfind(files_to_test(l).name, 'r'))
if(label == 2)
correct_predictions = correct_predictions + 1;
TN = TN + 1;
else
wrong_predictions = wrong_predictions + 1;
FN = FN + 1;
end;
end;
end;
accuraccy = (TP+TN)/(TP+TN+FP+FN);
end