一、简介
基于matlab GUI改进的KNN算法之语音情感分类识别
二、源代码
clc;
close all;
defcolor=[0,0,0];
h_fig=figure(1);
set(h_fig,'Menubar','name','语音情感识别系统 v1.0',...
'Numbertitle','off',...
'color',[0.9023 0.9074 0.8055]);
h_text=uicontrol(h_fig,'style','text','unit','normalized',...
'position',[0.0,0.0,1,1]);
h_text1=uicontrol(h_fig,'style','text','unit','normalized',...
'position',[0.0,0.85,0.25,0.05],'horizontal','left',...
'string','请选择待检测wav文件:','fontsize',10,'ForegroundColor',defcolor);
h_edit1=uicontrol(h_fig,'style','edit','unit','normalized',...
'position',[0.28,0.80,0.5,0.1],'horizontal','left',...
'fontsize',10, 'ForegroundColor',defcolor);
h_push3=uicontrol(h_fig,'style','push','unit','normalized',...
'position',[0.78,0.8,0.08,0.1],'horizontal','left',...
'string','...','fontsize',20,'ForegroundColor',defcolor,'callback','getfile');
h_push1=uicontrol(h_fig,'style','push','unit','normalized',...
'position',[0.4,0.75,0.18,0.05],'horizontal','left',...
'string','确定','fontsize',10,'ForegroundColor',defcolor,'callback','process');
h_text3=uicontrol(h_fig,'style','text','unit','normalized',...
'position',[0,0.5,0.28,0.1],'horizontal','left',...
'string','传统KNN算法获得结果为:','fontsize',10,'ForegroundColor',defcolor);
h_edit2=uicontrol(h_fig,'style','edit','unit','normalized',...
'position',[0.28,0.5,0.3,0.1],'horizontal','left',...
'fontsize',10,'ForegroundColor',defcolor);
[y,fs]=wavread(filename);
sound(y,fs)
X3=mean(FunFre(y,fs));
[X1,X2,X4]=TimePara(y);
k=13;
XA=[Aa Ah As X1];XE=[Ea Eh Es X2];XF=[Fa Fh Fs X3];XZ=[Za Zh Zs X4];
PA=mapzo(XA);PE=mapzo(XE);PF=mapzo(XF);PZ=mapzo(XZ);
a=[PA(1:30);PE(1:30);PF(1:30);PZ(1:30)];
h=[PA(31:60);PE(31:60);PF(31:60);PZ(31:60)];
s=[PA(61:90);PE(61:90);PF(61:90);PZ(61:90)];
x=[PA(91);PE(91);PF(91);PZ(91)];
%%%传统KNN算法
disp('使用传统KNN算法识别结果为:')
A=oushi(a,x);
H=oushi(h,x);
S=oushi(s,x);
set(h_edit2,'style','text');
set(h_edit2,'string',judge(A,H,S,k));
%%%%%%%改进算法
disp('使用改进算法识别结果为:')
B=mean([Aa' Ah' As' Ea' Eh' Es' Fa' Fh' Fs' Za' Zh' Zs']);
A=reshape(B,3,4);
O=reshape([Aa Ah As],30,3);
for i=1:3
for j=1:30
OO(j,i)=(abs(O(j,i)-A(i,1))/A(i,1))^2;
end
end
O=sqrt(sum(OO));
P=reshape([Ea Eh Es],30,3);
for i=1:3
for j=1:30
PP(j,i)=(abs(P(j,i)-A(i,2))/A(i,2))^2;
end
end
P=sqrt(sum(PP));
Q=reshape([Fa Fh Fs],30,3);
for i=1:3
for j=1:30
QQ(j,i)=(abs(Q(j,i)-A(i,3))/A(i,3))^2;
end
end
Q=sqrt(sum(QQ));
R=reshape([Za Zh Zs],30,3);
for i=1:3
for j=1:30
RR(j,i)=(abs(R(j,i)-A(i,4))/A(i,4))^2;
end
end
R=sqrt(mean(RR));
X=[O' P' Q' R'];
for i=1:3
for j=1:4
V(i,j)=(sum(X(i,:))-X(i,j))/sum(X(i,:));
end
function [str]=judge(A,H,S,k)
f=[A H S]; %将欧距三个个矩阵合并
g=[A H];
d=numel(f);
c=[1:d]; %用来存放排序后的欧距
c=lowtohigh(f,d);
num1=0; %用来记录被判x类的次数
num2=0; %用来记录被判y类的次数
num3=0;
for i=1:k
for j=1:d
if (c(i)==f(j))
if j<=numel(A) %如果选中的欧距出自x类
num1=num1+1;
elseif j>numel(g) %如果选中的欧距出自y类
num3=num3+1;
else
num2=num2+1;
end
end
j=j+1;
end
i=i+1;
end
if(num1>num2&num1>num3)
w=0;
elseif(num2>num1&num2>num3)
w=1;
elseif(num3>num1&num3>num2)
w=2;
end
end
三、运行结果
四、备注
版本:2014a