|
a |
|
b/ClassificationSVM.m |
|
|
1 |
clear;clc; |
|
|
2 |
%% 载入数据; |
|
|
3 |
fprintf('Loading data...\n'); |
|
|
4 |
tic; |
|
|
5 |
load('N_dat.mat'); |
|
|
6 |
load('L_dat.mat'); |
|
|
7 |
load('R_dat.mat'); |
|
|
8 |
load('V_dat.mat'); |
|
|
9 |
fprintf('Finished!\n'); |
|
|
10 |
toc; |
|
|
11 |
fprintf('=============================================================\n'); |
|
|
12 |
%% 控制使用数据量,每一类5000,并生成标签; |
|
|
13 |
fprintf('Data preprocessing...\n'); |
|
|
14 |
tic; |
|
|
15 |
Nb=Nb(1:5000,:);Label1=ones(1,5000);%Label1=repmat([1;0;0;0],1,5000); |
|
|
16 |
Vb=Vb(1:5000,:);Label2=ones(1,5000)*2;%Label2=repmat([0;1;0;0],1,5000); |
|
|
17 |
Rb=Rb(1:5000,:);Label3=ones(1,5000)*3;%Label3=repmat([0;0;1;0],1,5000); |
|
|
18 |
Lb=Lb(1:5000,:);Label4=ones(1,5000)*4;%Label4=repmat([0;0;0;1],1,5000); |
|
|
19 |
|
|
|
20 |
Data=[Nb;Vb;Rb;Lb]; |
|
|
21 |
Label=[Label1,Label2,Label3,Label4]; |
|
|
22 |
Label=Label'; |
|
|
23 |
|
|
|
24 |
clear Nb;clear Label1; |
|
|
25 |
clear Rb;clear Label2; |
|
|
26 |
clear Lb;clear Label3; |
|
|
27 |
clear Vb;clear Label4; |
|
|
28 |
Data=Data-repmat(mean(Data,2),1,250); %使信号的均值为0,去掉基线的影响; |
|
|
29 |
fprintf('Finished!\n'); |
|
|
30 |
toc; |
|
|
31 |
fprintf('=============================================================\n'); |
|
|
32 |
%% 利用小波变换提取系数特征,并切分训练和测试集; |
|
|
33 |
fprintf('Feature extracting and normalizing...\n'); |
|
|
34 |
tic; |
|
|
35 |
Feature=[]; |
|
|
36 |
for i=1:size(Data,1) |
|
|
37 |
[C,L]=wavedec(Data(i,:),5,'db6'); %% db6小波5级分解; |
|
|
38 |
Feature=[Feature;C(1:25)]; |
|
|
39 |
end |
|
|
40 |
|
|
|
41 |
Nums=randperm(20000); %随机打乱样本顺序,达到随机选择训练测试样本的目的; |
|
|
42 |
train_x=Feature(Nums(1:10000),:); |
|
|
43 |
test_x=Feature(Nums(10001:end),:); |
|
|
44 |
train_y=Label(Nums(1:10000)); |
|
|
45 |
test_y=Label(Nums(10001:end)); |
|
|
46 |
|
|
|
47 |
[train_x,ps]=mapminmax(train_x',0,1); %利用mapminmax内建函数特征归一化到0,1之间; |
|
|
48 |
test_x=mapminmax('apply',test_x',ps); |
|
|
49 |
train_x=train_x';test_x=test_x'; |
|
|
50 |
fprintf('Finished!\n'); |
|
|
51 |
toc; |
|
|
52 |
fprintf('=============================================================\n'); |
|
|
53 |
%% 训练SVM,并测试效果; |
|
|
54 |
fprintf('SVM training and testing...\n'); |
|
|
55 |
tic; |
|
|
56 |
model=libsvmtrain(train_y,train_x,'-c 2 -g 1'); %模型训练; |
|
|
57 |
[ptest,~,~]=libsvmpredict(test_y,test_x,model); %模型预测; |
|
|
58 |
|
|
|
59 |
Correct_Predict=zeros(1,4); %统计各类准确率; |
|
|
60 |
Class_Num=zeros(1,4); |
|
|
61 |
Conf_Mat=zeros(4); |
|
|
62 |
for i=1:10000 |
|
|
63 |
Class_Num(test_y(i))=Class_Num(test_y(i))+1; |
|
|
64 |
Conf_Mat(test_y(i),ptest(i))=Conf_Mat(test_y(i),ptest(i))+1; |
|
|
65 |
if ptest(i)==test_y(i) |
|
|
66 |
Correct_Predict(test_y(i))= Correct_Predict(test_y(i))+1; |
|
|
67 |
end |
|
|
68 |
end |
|
|
69 |
ACCs=Correct_Predict./Class_Num; |
|
|
70 |
fprintf('Accuracy_N = %.2f%%\n',ACCs(1)*100); |
|
|
71 |
fprintf('Accuracy_V = %.2f%%\n',ACCs(2)*100); |
|
|
72 |
fprintf('Accuracy_R = %.2f%%\n',ACCs(3)*100); |
|
|
73 |
fprintf('Accuracy_L = %.2f%%\n',ACCs(4)*100); |
|
|
74 |
toc; |