BP网络源代码:clcclear%%训练数据预测数据提取及归一化%下载四类语音信号loaddata1c1loaddata2c2loaddata3c3loaddata4c4%四个特征信号矩阵合成一个矩阵data(1:500,:)=c1(1:500,:);data(501:1000,:)=c2(1:500,:);data(1001:1500,:)=c3(1:500,:);data(1501:2000,:)=c4(1:500,:);%从1到2000间随机排序k=rand(1,2000);[m,n]=sort(k);%输入输出数据input=data(:,2:25);output1=data(:,1);%把输出从1维变成4维fori=1:2000switchoutput1(i)case1output(i,:)=[1000];case2output(i,:)=[0100];case3output(i,:)=[0010];case4output(i,:)=[0001];endend%随机提取1500个样本为训练样本,500个样本为预测样本input_train=input(n(1:1500),:)';output_train=output(n(1:1500),:)';input_test=input(n(1501:2000),:)';output_test=output(n(1501:2000),:)';%输入数据归一化[inputn,inputps]=mapminmax(input_train);%%网络结构初始化innum=24;midnum=25;outnum=4;%权值初始化w1=rands(midnum,innum);b1=rands(midnum,1);w2=rands(midnum,outnum);b2=rands(outnum,1);w2_1=w2;w2_2=w2_1;w1_1=w1;w1_2=w1_1;b1_1=b1;b1_2=b1_1;b2_1=b2;b2_2=b2_1;%学习率xite=0.1alfa=0.01;%%网络训练forii=1:10E(ii)=0;fori=1:1:1500%%网络预测输出x=inputn(:,i);%隐含层输出forj=1:1:midnumI(j)=inputn(:,i)'*w1(j,:)'+b1(j);Iout(j)=1/(1+exp(-I(j)));end%输出层输出yn=w2'*Iout'+b2;%%权值阀值修正%计算误差e=output_train(:,i)-yn;E(ii)=E(ii)+sum(abs(e));%计算权值变化率dw2=e*Iout;db2=e';forj=1:1:midnumS=1/(1+exp(-I(j)));FI(j)=S*(1-S);endfork=1:1:innumforj=1:1:midnumdw1(k,j)=FI(j)*x(k)*(e(1)*w2(j,1)+e(2)*w2(j,2)+e(3)*w2(j,3)+e(4)*w2(j,4));db1(j)=FI(j)*(e(1)*w2(j,1)+e(2)*w2(j,2)+e(3)*w2(j,3)+e(4)*w2(j,4));endendw1=w1_1+xite*dw1';b1=b1_1+xite*db1';w2=w2_1+xite*dw2';b2=b2_1+xite*db2';w1_2=w1_1;w1_1=w1;w2_2=w2_1;w2_1=w2;b1_2=b1_1;b1_1=b1;b2_2=b2_1;b2_1=b2;endend%%语音特征信号分类inputn_test=mapminmax('apply',input_test,inputps);forii=1:1fori=1:500%1500%隐含层输出forj=1:1:midnumI(j)=inputn_test(:,i)'*w1(j,:)'+b1(j);Iout(j)=1/(1+exp(-I(j)));endfore(:,i)=w2'*Iout'+b2;endend%%结果分析%根据网络输出找出数据属于哪类fori=1:500output_fore(i)=find(fore(:,i)==max(fore(:,i)));end%BP网络预测误差error=output_fore-output1(n(1501:2000))';%画出预测语音种类和实际语音种类的分类图figure(1)plot(output_fore,'r')holdonplot(output1(n(1501:2000))','b')legend('预测语音类别','实际语音类别')%画出误差图figure(2)plot(error)title('BP网络分类误差','fontsize',12)xlabel('语音信号','fontsize',12)ylabel('分类误差','fontsize',12)%print-dtiff-r6001-4k=zeros(1,4);%找出判断错误的分类属于哪一类fori=1:500iferror(i)~=0[b,c]=max(output_test(:,i));switchccase1k(1)=k(1)+1;case2k(2)=k(2)+1;case3k(3)=k(3)+1;case4k(4)=k(4)+1;endendend%找出每类的个体和kk=zeros(1,4);fori=1:500[b,c]=max(output_test(:,i));switchccase1kk(1)=kk(1)+1;case2kk(2)=kk(2)+1;case3kk(3)=kk(3)+1;case4kk(4)=kk(4)+1;endend%正确率rightridio=(kk-k)./kk