www.pudn.com > LBG_GA.rar > main_galbg.m, change:2012-01-07,size:3425b


tic 
clear all; 
clc 
close all; 
global gen  
global k 
fs=8000;    
n=5;k=32;p=16; 
train_data(1,:)=wavread('myn8s.wav',64000); 
train_data(2,:)=wavread('ll8s.wav',64000); 
train_data(3,:)=wavread('zzx8s.wav',64000); 
train_data(4,:)=wavread('ly8s.wav',64000); 
train_data(5,:)=wavread('kr8s.wav',64000); 
 
 
feature_1=cell(n,1); 
for i = 1:n                     %对每一个说话人得到一个训练结果 
    s=train_data(i,:); 
    v = mfcc(s, fs,p); %提出特征系数12*318     
    %     codebook{i} = vqlbg(v, k); 
    feature_1(i)=mat2cell(v);%5*12*318  mat2cell(v) 将矩阵转化为元胞 
end 
clear i; 
 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
%编码0~255之间的正整数串 
x=zeros(size(v,1),size(v,2),5); 
% feature_dim=zeros(1,size(v,2)); 
for i=1:n 
 feature_x=cell2mat(feature_1(i));  %将元胞数组元素转化成矩阵 
 feature=double(feature_x); 
 x(:,:,i)=normlization(feature);  %归一化 
end 
 
clear i; 
 
 
 
 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
N=50; 
best_codebook=zeros(1,k*p); 
objj=ones(200,5);    %存放每代最优个体适应度值 
%用LBG算法生成初始码本,码本尺寸为32,初始种群为50 
for i=1:n 
    a= x(:,:,i);%12*318 
     pop=vqlbgv(a,k); 
 %   initialp=initial_pop(a',N,k);  %随机选取特征矢量作初始种群 
    initialp=initialpop(pop',N);  %在LBG码本中选取生成初始码本 
     
    pop=initialp; 
    
     
   [adapt,best_pos]=adapting(a,pop,k,p); 
    best_indiv=pop(best_pos,:); 
    adapt_min=max(adapt); 
     
    
     
    %生成初始种群的大小为50*384的整数串 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
%进行遗传操作选择最优码本 
   G=200;cont=0; 
   % while(gen<G)      %对初始码本进行遗传操作 
   pc=0.2;pm=0.001; 
for gen=1:G    
      newpop=selection(pop,adapt,best_indiv); 
      cro_pop=crossover(newpop,adapt,pc); 
      pop=mutation(cro_pop,adapt,pm);       
      [adapt_new,best_pos_new]=adapting(a,pop,k,p); 
      best_indiv=keepbest(pop,best_pos_new); 
      adapt_new_min=max(adapt_new); 
      objj(gen,i)=adapt_new_min; 
      %迭代停止条件 
       if gen==1 
        error(gen)=1; 
       else 
      error(gen)=abs(objj(gen,i)-objj(gen-1,i)); 
       end 
      if error(gen)<=0.00001 
      cont=cont+1; 
      else 
      cont=0; 
      end 
   
      if cont==30 
        i 
       gen 
        break;  
      end     
      gen=gen+1; 
end 
best_codebook=best_indiv; 
codebook(:,:,i)=transfer_mat(best_codebook,p,k);%32*12 
end 
 objj(:,1) 
plot(1:1:200,objj(:,1),'r:',1:1:200,objj(:,2),'b*',1:1:200,objj(:,3),'y+',1:1:200,objj(:,4),'b-',1:1:200,objj(:,5),'gx') 
xlabel('迭代次数'); 
ylabel('最优适应度值'); 
 
save main_galbg codebook 
 
%程序了五个32*12的码本库 
 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
%%%%%%测试程序%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
count(1,5)=0;per(1,5)=0;temp=0; 
train_test(1,:)=wavread('ly0.wav'); 
w = mfcc(train_test, fs,p); 
test_result=zeros(1,n); 
result=0; 
person=0; 
xx=zeros(32,12); 
% test_cb=w';%318*12 
testcb=normlization(w)'; 
% testcb=round((test_cb-min(min(test_cb)))/(max(max(test_cb))-min(min(test_cb)))*255);   %归一化 
% test_cb=x(:,:,6)';%318*12 
 
for j=1:100 
    for i=1:5 
        xx=codebook(:,:,i)'; 
    %     xx=round(codebook(:,:,i)); 
        test_result(i)=disdis_1(testcb,xx);  %比较两个矩阵的距离 
    end 
        [result,person]=min(test_result); 
         count(person)=count(person)+1; 
end 
 
count 
per=count/100.00 
 
toc