您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 电子设计/PCB > matlab--PCM编码
PCM编码:clearall;closeall;%建立原信号dt=0.002;%取时间间隔为0.01t=0:dt:10;%时域间隔dt为间隔从0到10画图fc=1%xt里最大频率xt=sin(2*pi*fc*t)+cos(2*pi*fc*t);%xt方程%采样:时间连续信号变为时间离散模拟信号fs=10;%抽样fs=2fc,每秒钟内的抽样点数目将等于或大于2fc个sdt=1/fs;%频域采样间隔0.1t1=0:sdt:10;%以sdt为间隔从0到10画图st=sin(2*pi*fc*t1)+cos(2*pi*fc*t1);%coswt=cos2pift,2pif=wfigure(1);subplot(311);plot(t,xt);title('原始信号');%条状图,连续图gridon%画背景subplot(312);stem(t1,st,'.');%杆状图,离散图title('抽样信号');gridon%画背景%量化编码一步完成n=length(st);%取st的长度为n,本题函数n为101M=max(st);A=(st/M)*2048;%a1(极性码)a2a3a4(段落码)a5a6a7a8(段内电平码)code=zeros(i,8);%产生i*8的零矩阵%极性码a1fori=1:n%if循环语句ifA(i)=0code(i,1)=1;%代表正值elsecode(i,1)=0;%代表负值end%段内码a2a3a4ifabs(A(i))=0&&abs(A(i))16code(i,2)=0;code(i,3)=0;code(i,4)=0;step=1;start=0;elseif16=abs(A(i))&&abs(A(i))32code(i,2)=0;code(i,3)=0;code(i,4)=1;step=1;start=16;elseif32=abs(A(i))&&abs(A(i))64code(i,2)=0;code(i,3)=1;code(i,4)=0;step=2;start=32;elseif64=abs(A(i))&&abs(A(i))128code(i,2)=0;code(i,3)=1;code(i,4)=1;step=4;start=64;elseif128=abs(A(i))&&abs(A(i))256code(i,2)=1;code(i,3)=0;code(i,4)=0;step=8;start=128;elseif256=abs(A(i))&&abs(A(i))512code(i,2)=1;code(i,3)=0;code(i,4)=1;step=16;start=256;elseif512=abs(A(i))&&abs(A(i))1024code(i,2)=1;code(i,3)=1;code(i,4)=0;step=32;start=512;elseif1024=abs(A(i))&&abs(A(i))2048code(i,2)=1;code(i,3)=1;code(i,4)=1;step=64;start=1024;endB=floor((abs(A(i))-start)/step);%段内码编码floor取整4she5rut=dec2bin(B,4)-48;%dec2bin定义将B变为4位2进制码,-48改变格式code(i,5:8)=t(1:4);%输出段内码endcode=reshape(code',1,8*n);%reshape代表从新塑形codesubplot(313);stem(code,'.');axis([16001]);title('编码信号');gridon五、扩展:将自己的语音保存成一个文件,用matlab读出该信号,进行PCM编码。程序:[x,fs,bits]=wavread('notify.wav');subplot(211);plot(x);title('抽样信号');n=length(x);M=max(x);A=(x/M)*2048;code=zeros(i,8);fori=1:nifA(i)=0code(i,1)=1;elsecode(i,1)=0;endifabs(A(i))=0&&abs(A(i))16code(i,2)=0;code(i,3)=0;code(i,4)=0;step=1;start=0;elseif16=abs(A(i))&&abs(A(i))32code(i,2)=0;code(i,3)=0;code(i,4)=1;step=1;start=16;elseif32=abs(A(i))&&abs(A(i))64code(i,2)=0;code(i,3)=1;code(i,4)=0;step=2;start=32;elseif64=abs(A(i))&&abs(A(i))128code(i,2)=0;code(i,3)=1;code(i,4)=1;step=4;start=64;elseif128=abs(A(i))&&abs(A(i))256code(i,2)=1;code(i,3)=0;code(i,4)=0;step=8;start=128;elseif256=abs(A(i))&&abs(A(i))512code(i,2)=1;code(i,3)=0;code(i,4)=1;step=16;start=256;elseif512=abs(A(i))&&abs(A(i))1024code(i,2)=1;code(i,3)=1;code(i,4)=0;step=32;start=512;elseif1024=abs(A(i))&&abs(A(i))2048code(i,2)=1;code(i,3)=1;code(i,4)=1;step=64;start=1024;endB=floor((abs(A(i))-start)/step);t=dec2bin(B,4)-48;code(i,5:8)=t(1:4);endcode=reshape(code',1,8*n);codesubplot(212);stem(code,'.');axis([110001]);title('编码信号');gridon
本文标题:matlab--PCM编码
链接地址:https://www.777doc.com/doc-6058545 .html