matlab--PCM编码.docx
PCM编码:clearall:closeall;先建立原信号dt=O.OO2;%取时间间隔为0.01t=0:dt:10;%时域间隔dt为间隔从。到IQ画图fc=l%xt里最大频率xt=sin(2*pi*fc*t)+cos(2*pi*fc*t);%Xt方程%采样:时间连续信号变为时间离散模拟信号fs=10;%抽样fs>=2fc,每秒钟内的抽样点数目将等于或大于2fcs<It=lfs;%领域采样间隔01tl=0:sdt:10:%以Sdt为间隔从。到10画图%coswt=cos2pift,2pif=wst=sin(2*pi*t,c*tI)+cos(2*pi*fc*t1);figured);subplot(3II);plot(t.xt)"itleC原始信号,);gridonSUbPlOt(312);stem(tl,st,'.);Utl”抽样信号Xgridon%条状图,连续图%Bi背景%杆状图,离散图先画背景%量化编码一步完成n=length(st);%取St的长度为n,本题函数n为101M=max(st);=(stM)2048;%a1(极性码)a2a3a4(段落码)a5a6a7a8(段内电平码)code=xers(i,8);%产生产8的零矩阵%极性码al%i116环语句fori=l:nifA(i)>=Ocode(i,l)=l;%便正值elsecode(i.l)=0;%代表负值end%段内码a2a3a4ifabs(i)>=0abs(i)><16COde(i,2)=0;COde(i,3)=0;COdC(i,4)=0;SteP=Iart=0;elseif16<=abs(A(i)&&abs(A(i)<32code(i,2)=0;COde(i,3=0;COde(i,4)=l;SteP=1;Start=16;elseif32<=abs(A(i)&&abs(A(i)<64COde(i,2)=0;Code(i,3)=l;COde(i.4)=0;SteP=2;start=32;elseif64<=ahs(A(i)&&abs(A(i)<12«Code(i,2)=0;Code(i,3)=l;Code(i,4)=l:SteP=4:Start=64:elseif128<=abs(A(i)&&al,s(A(i)<256Code(i,2)=l:Code(i,3)=0:COde(i,4)=0:SteP=8:Start=I28;elseif256<=abs(.(i)abs(.(i)<512coded,2)=1;code(i,3>=0;code(i,4)=1;step=16;start=256;elseif512<=abs(.-(i)&&abs(?(i)<1024coded,2)=1;code(i,3)=1;COde(i,4>=0;SteP=32;start=512;elseif1024<=abs(A(i)&&abs(A(i)<2048codc(i,2)=1;codc(i,3)=i;codc(i,4)=1;Step=64;Start=1024;endB=11oor(abs(A(i)>start)step);%段内码储码floor取整4she5rut=dec2bin(B,4)-48;%dec2bin定义将B变为血2进制码,-48变更格式code(i,5:8)=t(l:4);%输出段内码endco<lc=resha>c(code1,8*n);%rcshapi代表从新星形codeSUbPlot(313):stcm(co<Ie,'.')axis(16001);title。编码侑号*gridona,r<.已44候嘉医小火仲,腐K俄*依信号,疗程编修。一分,x,fs,bits=wavread('notify.wav*);subplot(211);plot(x);title('#«4;n=length(×);M=max(x);A=(xM)*2048;code=zeros(i,8);fori=l: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)=O;step=l;Start=0;elseif16<=abs(A(i)&&abs(A(i)<32code(i,2)=O;code(i,3)=0;code(i,4)=l;step=l;start=16;elsei£32<=abs(A(i)&&abs(A(i)<64code(i,2)=0;code(i,3)=l;code(i,4)=0;step=2;start=32;elseif64<=abs(A(i)&&abs(A(i)<128code(i,2)=0;code(i,3)=l;code(i,4)=l;step=4;start=64;elseif128<=abs(A(i)&&abs(A(i)<256code(i,2)=l;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)=l;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)=l;step=64;start=1024;endB=floor(abs(A(i)-start)/step);t=dec2bin(B,4)-48;code(i,5:8)=t(l:4);endcode=reshape(code',1,8*n);codesubplot(212);stem(code,.,);axis(110001);title(<4);gridon