%% 娓呯┖鐜鍙橀噺
clear;
clc;
close all;
%% 娣诲姞褰撳墠璺緞鑷冲伐浣滅幆澧?
currentFolder = pwd;
addpath(genpath(currentFolder));
%% 鎻愬彇鍑轰竴缁勬牱鏈殑16涓俊閬撶殑鍘熷EEG淇″彿锛岃繘琛屾樉绀?
%% 瀵煎叆涓ょ被鏍锋湰鏁版嵁浠ュ強瀵瑰簲鏍囩锛堜粠鍘熷鏁版嵁闆嗕腑鎻愬嚭锛?
% 瀵瑰簲鏍囩璇存槑锛?浠h〃awake鐘舵? 1浠h〃sleep鐘舵?
load('C:\Users\Administrator\Desktop\calm.mat') %鏁版嵁鐨勮矾寰勫彲淇敼
load('C:\Users\Administrator\Desktop\calm_label.mat') %鏁版嵁鐨勮矾寰勫彲淇敼
load('C:\Users\Administrator\Desktop\angry.mat') %鏁版嵁鐨勮矾寰勫彲淇敼
load('C:\Users\Administrator\Desktop\angry_label.mat') %鏁版嵁鐨勮矾寰勫彲淇敼
load('C:\Users\Administrator\Desktop\sad.mat')
load('C:\Users\Administrator\Desktop\sad_label.mat')
%% 灏忔尝鍙樻崲
calm_1 = zeros(900,31); %鍒嗛厤鍐呭瓨绌洪棿
angry_1 = zeros(800,31);
sad_1 = zeros(1000,31);
% 鍒嗗埆瀵逛袱绫绘暟鎹繘琛屽皬娉㈠彉鎹?
for i=1:1:900
t = calm(i,:);
[c,l] = wavedec(t,3,'db2');
calm_1(i,:) = c;
end
for j=1:1:800
q = angry(j,:);
[c,l] = wavedec(q,3,'db2');
angry_1(j,:) = c;
end
for k=1:1:1000
p = sad(k,:);
[c,l] = wavedec(p,3,'db2');
sad_1(k,:) = c;
end
% 灏嗚繘杩囧皬娉㈠彉鎹㈢殑涓ょ被鏁版嵁鍒嗗埆閫佸洖鍘熷彉閲?
calm =calm_1;
angry = angry_1;
sad = sad_1;
%% 灏嗘墍鏈夋暟鎹繘琛屽綊涓?寲澶勭悊
input_train = [calm;angry;sad];
[inputn,inputps] = mapminmax(input_train);
% 褰掍竴鍖栧悗鐨勬暟鎹?鍥炲師鍙橀噺
calm = inputn((1:900),:);
angry = inputn((901:1700),:);
sad = inputn((1701:2700),:);
%% 涓ょ被淇″彿鐨?0%鐨勬牱鏈敤浜庤缁僑VM锛?0%鐨勬牱鏈敤浜庢祴璇曟ā鍨嬪垎绫昏兘鍔?
%浜х敓闅忔満鏁帮紝纭繚瀹為獙鐨勬棤搴忔?
number_1 = randperm(size(calm,1));
number_2 = randperm(size(angry,1));
number_3 = randperm(size(sad,1));
% 鍖哄垎awake鐘舵?涓嬬殑鏍锋湰鏁版嵁
% 230*0.7=161涓猘wake鐘舵?鏍锋湰鐢ㄤ簬璁粌
% 230*0.3=69涓猘wake鐘舵?鏍锋湰鐢ㄤ簬娴嬭瘯
calm_train_label = zeros(size(calm_label,1)*0.7,size(calm_label,2));
calm_test_label = zeros(100,size(calm_label,2));
calm_train = zeros(size(calm,1)*0.7,size(calm,2));
calm_test = zeros(100,size(calm,2));
% 鍒嗛厤70%鐨刟wake鏁版嵁鑷宠缁冮泦
for i=1:1:(size(calm,1)*0.7)
calm_train(i,:) = calm(number_1(i),:);
calm_train_label(i,1) = calm_label(number_1(i),:);
end
% 鍒嗛厤30%鐨刟wake鏁版嵁(鍓╀綑鐨?鑷虫祴璇曢泦
for i=(size(calm,1)*0.7+1):1:size(calm,1)*0.7+100
calm_test(i-(size(calm,1)*0.7),:) = calm(number_1(i),:);
calm_test_label(i-(size(calm,1)*0.7),1) = calm_label(number_1(i),:);
end
% 鍖哄垎鐫$湢鐘舵?涓嬬殑鏍锋湰鏁版嵁
% 150*0.7=105涓猻leep鐘舵?鏍锋湰鐢ㄤ簬璁粌
% 150*0.3=45涓猻leep鐘舵?鏍锋湰鐢ㄤ簬娴嬭瘯
angry_train_label = zeros(size(angry_label,1)*0.7,size(angry_label,2));
angry_test_label = zeros(100,size(angry_label,2));
angry_train = zeros(size(angry,1)*0.7,size(angry,2));
angry_test = zeros(100,size(angry,2));
% 鍒嗛厤70%鐨剆leep鏁版嵁鑷宠缁冮泦
for j=1:1:size(angry,1)*0.7
angry_train(j,:) = angry(number_2(j),:);
angry_train_label(j,1) = angry_label(number_2(j),:);
end
% 鍒嗛厤30%鐨剆leep鏁版嵁(鍓╀綑鐨?鑷虫祴璇曢泦
for j=(size(angry,1)*0.7+1):1:size(angry,1)*0.7+100
angry_test(j-(size(angry,1)*0.7),:)= angry(number_2(j),:);
angry_test_label(j-(size(angry,1)*0.7),1) = angry_label(number_2(j),:);
end
sad_train_label = zeros(size(sad_label,1)*0.7,size(sad_label,2));
sad_test_label = zeros(100,size(sad_label,2));
sad_train = zeros(size(sad,1)*0.7,size(sad,2));
sad_test = zeros(100,size(sad,2));
% 鍒嗛厤70%鐨剆ad鏁版嵁鑷宠缁冮泦
for k=1:1:size(sad,1)*0.7
sad_train(k,:) = sad(number_3(k),:);
sad_train_label(k,1) = sad_label(number_3(k),:);
end
% 鍒嗛厤30%鐨剆leep鏁版嵁(鍓╀綑鐨?鑷虫祴璇曢泦
for k=(size(sad,1)*0.7+1):1:size(sad,1)*0.7+100
sad_test(k-(size(sad,1)*0.7),:)= sad(number_3(k),:);
sad_test_label(k-(size(sad,1)*0.7),1) = sad_label(number_3(k),:);
end
%% 姹囨?鏁版嵁
% 璇存槑锛歺Tr銆亂Tr瀵瑰簲涓哄叏浣撹缁冩牱鏈殑鏁版嵁銆佹爣绛?
% 璇存槑锛歺Te銆亂Te瀵瑰簲涓哄叏浣撴祴璇曟牱鏈殑鏁版嵁銆佹爣绛?
xTr = [angry_train;calm_train];
yTr = [angry_train_label;calm_train_label];
xTr1 = [sad_train;calm_train];
yTr1 = [sad_train_label;calm_train_label];
model = fitcsvm(xTr,yTr); % 璁粌xTr鏁版嵁锛屽嵆鐫$潃浜嗗拰閱掔潃鐨勬暟鎹?
model1 = fitcsvm(xTr1,yTr1); %%璁粌xTr1鏁版嵁锛屽嵆鎮蹭激鍜岄啋鐫?殑鏁版嵁
test = calm_test;
yPre1 = predict(model,test);
yPre3 = predict(model1,test);
a = sum(calm_test_label == yPre1)/length(yPre1);
b = sum(angry_test_label == yPre1)/length(yPre1);
c = sum(sad_test_label == yPre3)/length(yPre3);
disp(a);
disp(b);
disp(c);
if(a<b)
if(b<c)
disp('the emotion is sad');
motion=c;
else
disp('the emotion is angry');
motion=b;
end
else
if(a<c)
disp('the emotion is sad');
motion=c;
else
disp('the emotion is calm');
motion=a;
end
end
xTe = [angry_test;calm_test];
yTe = [angry_test_label;calm_test_label];
yPre = predict(model,xTe);% 灏嗘祴璇曟暟鎹?鍏ユā鍨嬶紝寰楀埌棰勬祴鏍囩yPre
switch(motion)
case a
[y,Fs]=audioread('C:\Users\Administrator\Desktop\calm.mp3');
sound(y,Fs);
case b
[y,Fs]=audioread('C:\Users\Administrator\Desktop\angry.mp3');
sound(y,Fs);
case c
[y,Fs]=audioread('C:\Users\Administrator\Desktop\sad.mp3');
sound(y,Fs);
end
% %% 璁粌銆佸垎绫?
% %% 瀹為獙缁撴灉鍒嗘瀽
% Acc = sum(yTe == yPre)/length(yTe);
% C = confusionmat(yTe, yPre); % C涓烘贩娣嗙煩闃?
% recall = 0;
% accuracy = 0;
% n = length(C);
% for i=1:n
% recall = recall + C(i,i)/sum(C(i,:));
% accuracy = accuracy + C(i,i)/sum(C(:,i));
% end
% recall = recall/n;
% accuracy = accuracy/n;
% f1 = 2*(recall*accuracy)/(recall+accuracy);
% disp(['Acc =' num2str(Acc)]);
% disp(['f1 =' num2str(f1)]);
% disp(['recall =' num2str(recall)]);
% disp(['accuracy =' num2str(accuracy)]);
% plotconfusion(yPre',yTe')
% disp('222')
评论2