ADPCM语音编码标准MATLAB实现(2012-03-0814:54:12)转载▼标签:g.721adpcm语音编码标准matlab实现杂谈分类:信号处理该方法的理论不再赘述,参考《数字语音处理及MATLAB实现》这本书的源程序,对一段英文歌曲进行编码解码:%主程序clc;clear;coe=[1,0,1,0,0,0,0,0,0,0,0];coe1=[0,0,0];coe2=[0,0,0,0,0,0,0,0,0,0];coe3=[0];Dqk=zeros(1,7);%自适应你量化器输出fid=fopen('speech1.txt');a=fscanf(fid,'%e\n');fclose(fid);fid=fopen('spp.txt','wt');%存放解码输出波形fori=1:size(a,1);Slk=a(i);[coe,coe1,coe2,coe3,Dqk]=adpcm(Slk,coe,coe1,coe2,coe3,Dqk);fprintf(fid,'%f\n',coe2(5));endfclose(fid);fid=fopen('spp.txt','rt');b=fscanf(fid,'%e\n');subplot(211);plot(a);subplot(212);plot(b);%语音编解码子函数程序adpcm.mfunction[coe,coe1,coe2,coe3,Dqk]=adpcm(Slk,coe,coe1,coe2,coe3,Dqk)%语音编解码函数Yk_pre=coe2(1);%初值传递Sek_pre=coe2(2);Ik_pre=coe2(3);Ylk_pre_pre=coe2(4);Srk_pre=coe2(5);Srk_pre_pre=coe2(6);a2=coe2(7);Tdk_pre=coe2(8);Trk_pre=coe2(9);Num=coe2(10);coe2(10)=coe2(10)+1;[Sek,coe]=Sek_com(Srk_pre,Srk_pre_pre,Dqk,coe);%自适应预测Dk=Dk_com(Slk,Sek);%采样值与其估值差值计算Yuk_pre=yu_result(Yk_pre,wi_result(abs(Ik_pre)));%快速非锁定标度因子计算ifYuk_pre1.06Yuk_pre=1.06;elseifYuk_pre10.00Yuk_pre=10.00;endendYlk_pre=yl_result(Ylk_pre_pre,Yuk_pre);%锁定标度因子计算Trk_pre=Trk_com(a2,Dqk(6),Ylk_pre);%窄带信号瞬变判定Tdk_pre=Tdk_com(a2);%单频信号判定[Alk,coe1]=Alk_com(Ik_pre,Yk_pre,coe1,Tdk_pre,Trk_pre);%自适应速度控制与自适应预测ifAlk0.0Alk=0.0;elseifAlk1.0Alk=1.0;endend[Yk,coe3]=Yk_com(Ik_pre,Alk,Yk_pre,coe3);%量化阶距自适应因子计算Ik=Ik_com(Dk,Yk);%自适应量化并编码输出Yk_pre=Yk;Srk_pre_pre=Srk_pre;Sek_pre=Sek;Ylk_pre_pre=Ylk_pre;Ik_pre=Ik;coe2(1)=Yk;coe2(6)=Srk_pre;coe2(2)=Sek;coe2(4)=Ylk_pre;coe2(3)=Ik;Dqk(1)=Dqk(2);Dqk(2)=Dqk(3);Dqk(3)=Dqk(4);Dqk(4)=Dqk(5);Dqk(5)=Dqk(6);Dqk(6)=Dqk(7);Dqk(7)=Dqk_com(Ik_pre,Yk_pre);%自适应逆量化器输出Srk_pre=Srk_com(Dqk(7),Sek_pre);%重建信号输出coe2(5)=Srk_pre;%自适应速度控制与自适应预测子函数function[h,coe1]=Alk_com(Ik_pre,Yk_pre,coe1,Tdk_pre,Trk_pre)Dmsk_p2=coe1(1);Dmlk_p2=coe1(2);Apk_pre2=coe1(3);Dmsk_p1=(1-2^(-5))*Dmsk_p2+2^(-5)*fi_result(abs(Ik_pre));Dmlk_p1=(1-2^(-7))*Dmlk_p2+2^(-7)*fi_result(abs(Ik_pre));coe1(1)=Dmsk_p1;coe1(2)=Dmlk_p1;if((abs(Dmsk_p1-Dmlk_p1)=2^(-3)*Dmlk_p1)|(Yk_pre3)|(Tdk_pre==1))Apk_pre1=(1-2^(-4))*Apk_pre2+2^(-3);elseif(Trk_pre==1)Apk_pre1=1;elseApk_pre1=(1-2^(-4))*Apk_pre2;endcoe1(3)=Apk_pre1;endifApk_pre1=1Alk=1;elseAlk=Apk_pre1;endh=Alk;%采样值与其估计值差值计算奇函数functiond=Dk_com(Slk,Sek);Dk=Slk-Sek;d=Dk;%自适应逆量化器输出子函数functionf=Dqk_com(Ik,Yk)ifIk=0Dqsk=0;i=Ik;elseDqsk=1;i=-Ik;endswitchicase7Dqlnk=3.32;case6Dqlnk=2.91;case5Dqlnk=2.52;case4Dqlnk=2.13;case3Dqlnk=1.66;case2Dqlnk=1.05;case1Dqlnk=0.031;case0Dqlnk=-1000;end%归一化输出Dqlk=Dqlnk+Yk;Dqk=2^Dqlk;ifDqsk==1Dqk=-Dqk;endf=Dqk;%自适应预测中f函数值计算子函数functionb=f_com(a)ifabs(a)=0.5b=4*a;elseb=2*sgn_com(a);end%速度控制中F[I(k)]计算子函数functionw=fi_result(in)switchincase0fi=0;case1fi=0;case2fi=0;case3fi=1;case4fi=1;case5fi=1;case6fi=3;case7fi=7;endw=fi;%自适应量化并编码输出子函数functionf=Ik_com(Dk,Yk)ifDk0Dsk=0;elseDsk=1;endifDk==0Dk=Dk+0.0001;endDlk=log(abs(Dk))/log(2);Dlnk=Dlk-Yk;x=Dlnk;a=10;ifDlnk-0.98Ik=0;endif-0.98=Dlnk&Dlnk0.62Ik=1;endif0.62=Dlnk&Dlnk1.38Ik=2;endif1.38=Dlnk&Dlnk1.91Ik=3;endif1.91=Dlnk&Dlnk2.34Ik=4;endif2.34=Dlnk&Dlnk2.72Ik=5;endif2.72=Dlnk&Dlnk3.12Ik=6;endifDlnk=3.12Ik=7;endifDsk==1Ik=-Ik;endf=Ik;%自适应预测子函数function[g,f]=Sek_com(Srk_pre,Srk_pre_pre,Dqk,coe)a1_pre=coe(1);a2_pre=coe(2);b1_pre=coe(3);b2_pre=coe(4);b3_pre=coe(5);b4_pre=coe(6);b5_pre=coe(7);b6_pre=coe(8);Sezk_pre=coe(9);p_pre2=coe(10);p_pre3=coe(11);%六阶零点预测器系数b1=(1-2^(-8))*b1_pre+2^(-7)*sgn_com(Dqk(7))*sgn_com(Dqk(6));b2=(1-2^(-8))*b2_pre+2^(-7)*sgn_com(Dqk(7))*sgn_com(Dqk(5));b3=(1-2^(-8))*b3_pre+2^(-7)*sgn_com(Dqk(7))*sgn_com(Dqk(4));b4=(1-2^(-8))*b4_pre+2^(-7)*sgn_com(Dqk(7))*sgn_com(Dqk(3));b5=(1-2^(-8))*b5_pre+2^(-7)*sgn_com(Dqk(7))*sgn_com(Dqk(2));b6=(1-2^(-8))*b6_pre+2^(-7)*sgn_com(Dqk(7))*sgn_com(Dqk(1));%二阶极点预测器系数Sezk=b1*Dqk(7)+b2*Dqk(6)+b3*Dqk(5)+b4*Dqk(4)+b5*Dqk(3)+b6*Dqk(2);p_pre1=Dqk(7)+Sezk_pre;ifabs(p_pre1)=0.000001;a1=(1-2^(-8))*a1_pre;a2=(1-2^(-7))*a2_pre;elsea1=(1-2^(-8))*a1_pre+(3*2^(-8))*sgn_com(p_pre1)*sgn_com(p_pre2);a2=(1-2^(-7))*a2_pre+2^(-7)*(sgn_com(p_pre1)*sgn_com(p_pre3)-f_com(a1_pre)*sgn_com(p_pre1)*sgn_com(p_pre2));end%自适应预测和重建信号计算器coe(1)=a1;coe(2)=a2;coe(3)=b1;coe(4)=b2;coe(5)=b3;coe(6)=b4;coe(7)=b5;coe(8)=b6;coe(9)=Sezk;coe(10)=p_pre1;coe(11)=p_pre2;g=(a1*Srk_pre+a2*Srk_pre_pre+Sezk);f=coe;%sgn_com函数functionb=sgn_com(a)ifa=0.000001b=1;elseb=-1;end%重建信号输出子函数functionSrk=Srk_com(Dqk,Sek)Srk=Dqk+Sek;%单频信号判定子函数functionTdk=Tdk_com(A2k)if(A2k-0.71785)Tdk=1;elseTdk=0;endTdk=Tdk;%窄带信号顺便判定子函数functionTrk=Trk_com(A2k,Dqk,Ylk)if(A2k-0.71785)&(fabs(Dqk)pow(24.2,Ylk))Trk=1;elseTrk=0;endTrk=Trk;%量化器标度因子自适应wi的选取子函数functionJ=wi_result(in)switchincase0wi=-0.75;case1wi=1.13;case2wi=2.56;case3wi=4.00;case4wi=7.00;case5wi=12.38;case6wi=22.19;case7wi=70.13;endJ=wi;%量化阶矩自适应因子计算子函数function[Yk,coe3]=Yk_com(Ik_pre,Alk,Yk_pre,coe3);Yl_pre_pre=coe3;Yu_pre=(1-2^(-5))*Yk_pre+2^(-5)*wi_result(abs(Ik_pre));Yl_pre=yl_result(Yl_pre_pre,Yu_pre);coe3=Yl_pre;Yk=Alk*Yu_pre+(1-Alk)*Yl_pre;%锁定标度因子计算子函数functionyl=yl_result(yl_pre,yu_now)yl=(1-2^(-6))*yl_pre+2^(-6)*yu_now;yl=yl;%快速非锁定标度因子计算子函数functionyu=yu_re