当前位置:首页 > 商业/管理/HR > 经营企划 > PCM的编码及解码-matlab源程序
x=randn(1,1000);%²úÉúÒ»Õý̬·Ö²¼ÐòÁÐxf=fft(x,256);%1024ΪÊý¾ÝµãÊý£¬¼´¶ÔÐźŲÉÑùÊý¾ÝΪ1024µãµÄ´¦Àíd=x;subplot(2,1,1);%»æÖÆÕý̬·Ö²¼ÐòÁÐʱÓòÓëƵÓòͼplot(x);title('³¤¶ÈΪ1000µÄ±ê×¼Õý̬·Ö²¼µÄËæ»úÐźÅʱÓò²¨ÐÎͼ');subplot(2,1,2);plot(abs(xf));xlabel('ƵÂÊ/Hz');ylabel('Õñ·ù');title('Õý̬·Ö²¼µÄƵÓòͼ');figure;%´ò¿ªÐµĴ°¿Ú£¬»æÖƱàÂëÓë½âÂëÐźÅʱÓòÓëƵÓòͼu=255;xx=0:0.01:1;xy=log(1+u*xx)/log(1+u);plot(xx,xy);title('¦ÌÂÉPCM±àÂëÊäÈëÊä³ö¹ØϵÇúÏß');%±àÂë¹ý³Ìc=xfori=1:100x(i)=x(i)/4x(i)=fix(x(i)*4079);s=sign(x(i));ifs0pcm((i-1)*8+1)=0;elsepcm((i-1)*8+1)=1;endx(i)=abs(x(i));p(i)=x(i);ifx(i)=15.5pcm((i-1)*8+2:(i-1)*8+4)=[0,0,0]elseifx(i)=47.5pcm((i-1)*8+2:(i-1)*8+4)=[0,0,1]p(i)=floor((x(i)-15.5)/2);elseifx(i)=111.5pcm((i-1)*8+2:(i-1)*8+4)=[0,1,0];p(i)=floor((x(i)-47.5)/4);elseifx(i)=239.5pcm((i-1)*8+2:(i-1)*8+4)=[0,1,1];p(i)=floor((x(i)-111.5)/8);elseifx(i)=495.5pcm((i-1)*8+2:(i-1)*8+4)=[1,0,0];p(i)=floor((x(i)-239.5)/16);elseifx(i)=1007.5pcm((i-1)*8+2:(i-1)*8+4)=[1,0,1];p(i)=floor((x(i)-495.5)/32);elseifx(i)=2031.5pcm((i-1)*8+2:(i-1)*8+4)=[1,1,0];p(i)=floor((x(i)-1007.5)/64);elseifx(i)=4079.5pcm((i-1)*8+2:(i-1)*8+4)=[1,1,1];p(i)=floor((x(i)-2031.5)/128);end;end;end;end;end;end;end;end;y=str2double(dec2bin(p(i)));pcm((i-1)*8+5)=floor(y/1000);pcm((i-1)*8+6)=floor(mod(y,1000)/100);pcm((i-1)*8+7)=floor(mod(y,100)/10);pcm((i-1)*8+8)=floor(mod(y,10));end%PCM½âÂëfori=1:100k(i)=pcm((i-1)*8+2)*4+pcm((i-1)*8+3)*2+pcm((i-1)*8+4);h(i)=pcm((i-1)*8+5)*8+pcm((i-1)*8+6)*4+pcm((i-1)*8+7)*2+pcm((i-1)*8+8);switchk(i)case0dpcm(i)=h(i);case1dpcm(i)=16.5+h(i)*2+1;case2dpcm(i)=49.5+h(i)*4+2;case3dpcm(i)=115.5+h(i)*8+4;case4dpcm(i)=247.5+h(i)*16+8;case5dpcm(i)=511.5+h(i)*32+16;case6dpcm(i)=1039.5+h(i)*64+32;case7dpcm(i)=2095.5+h(i)*128+64;otherwisedisp('error');endifpcm((i-1)*8+1)==0dpcm(i)=0-dpcm(i);end;y(i)=dpcm(i);end;figure;subplot(3,1,1)stem(c)title('´ýÁ¿»¯±àÂëÐòÁÐ');xlim([0,6])subplot(3,1,2)stem(pcm)title('²ÉÓæÌÂÉÁ¿»¯±àÂëºóµÄÐòÁÐ');xlim([0,48])subplot(3,1,3)stem(dpcm)title('²ÉÓæÌÂɽâÂëºóµÄÐòÁÐ');xlim([0,6])figure;y=y/4079;y0=fft(y,256);subplot(2,1,1);plot(y);title('½âÂëºóʱÓòÐźÅ');subplot(2,1,2);plot(abs(y0));title('½âÂëºóƵÓòÐźÅ');%¼ÆËãÁ¿»¯ÐÅÔë±ÈSQNR=0;fori=1:100g(i)=y(i)-d(i);SNR(i)=(1/2*d(i)^2)/(1/2*g(i)^2);SQNR=SQNR+SNR(i);end;SQNR=10*log(SQNR/100);%ÉèÖÃÐŵÀÎóÂëÂÊr=rand(1,1000)%[-1,1]ÉϾùÔÈ·Ö²¼fori=1:1000ifr(i)=0.5r(i)=1;elseifr(i)0.5r(i)=0;end;end;%0Óë1¾ùÔȳöÏÖµÄÐźÅt=r+d;e=0;fori=1:1000ift(i)=0.5;t(i)=1;elseift(i)0.5;t(i)=0;%Åоöend;ift(i)~=r(i);e=e+1;%ÎóÂë´ÎÊýend;end;P=e./1000;%ÎóÂëÂÊ%½øÐоùÔÈÁ¿»¯%³õʼ»¯£¬nΪÁ¿»¯¼¶Êýn=input('Pleaseenterthequalifyleveln:');ifisempty(n)n=8;end;k=100;%K:Ðźų¤¶Èfori=1:100;s(i)=d(i);%S:ÊäÈëÐźŵçƽjypcm=zeros(k,n);A=max(s(i));%È¡³ö¸Ãº¯ÊýµÄ×î´óÖµderta=A/2^(n-1);z(i)=fix(s(i)/derta);fori=1:kifz(i)0;jypcm(i,1)=0z(i)=0-z(i);elsejypcm(i,1)=1;end%yy(i)=dec2bin(y0(i));sss(i)=str2double(dec2bin(z(i)));forj=1:(n-1)jypcm(i,n-j+1)=mod(floor(sss(i)/10^(j-1)),10^j);end;end;end;figure;stem(jypcm)gridontitle('Á¿»¯¼¶ÊýΪnʱÁ¿»¯Êä³ö²¨ÐÎ')£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿1.PCM±àÂëʵÏÖ£¨Mº¯Êý£©functioncode=PCMcoding(Range,S)code=zeros(1,8);Normalization=abs(S)/Range;deta=1/2048;ifsign(S)==1code(1)=1;elsecode(1)=0;enddetas=ceil(Normalization/deta);ifdetas128code(2)=1;ifdetas512code(3)=1;ifdetas1024code(4)=1;endelseifdetas256code(4)=1;endelseifdetas32code(3)=1;ifdetas64code(4)=1;endelseifdetas16code(4)=1;endTribal=bin2dec(num2str(code(2:4)))+1;ifTribal==1Interval=1;elseInterval=(2^(Tribal+2))/16;endifTribal==1code(5)=(2^3)*Intervaldetas;code(6)=code(5)*(2^3)*Interval+(2^2)*Intervaldetas;code(7)=code(5)*(2^3)*Interval+code(6)*(2^2)*Interval+(2^1)*Intervaldetas;code(8)=code(5)*(2^3)*Interval+code(6)*(2^2)*Interval+code(7)*(2^1)*Interval+(2^0)*Intervaldetas;elsecode(5)=2^(Tribal+2)+(2^3)*Intervaldetas;code(6)=2^(Tribal+2)+code(5)*(2^3)*Interval+(2^2)*Intervaldetas;code(7)=2^(Tribal+2)+code(5)*(2^3)*Interval+code(6)*(2^2)*Interval+(2^1)*Intervaldetas;code(8)=2^(Tribal+2)+code(5)*(2^3)*Interval+code(6)*(2^2)*Interval+code(7)*(2^1)*Interval+(2^0)*Intervaldetas;end2¡£PCM½âÂëʵÏÖ£¨Mº¯Êý£©%PCMDecodingfunctionS=PCMdecoding(Range,code)deta=Range/2048;Tribal=bin2dec(num2str(code(2:4)))+1;ifTribal==1Interval=1;elseInterval=(2^(Tribal+2))/16;endifTribal==1S=code(5)*(2^3)*Interval+code(6)*(2^2)*Interval+code(7)*(2^1)*Interval+code(8)*(2^0)*Interval+Interval;elseS=2^(Tribal+2)+code(5)*(2^3)*Interval+code(6)*(2^2)*Interval+code(7)*(2^1)*Interval+code(8)*(2^0)*Interval+Interval;endS=S*deta;
本文标题:PCM的编码及解码-matlab源程序
链接地址:https://www.777doc.com/doc-1655866 .html