pcm编码解码matlab代码的解释要注释急~求大神帮忙 - 爱问答

(爱问答)

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=xfor i=1:100x(i)=x(i)/4x(i)=fix(x(i)*4079);s=sign(x(i));if s<0pcm((i-1)*8+1)=0;elsepcm((i-1)*8+1)=1;endx(i)=abs(x(i));p(i)=x(i);if x(i)<=15.5pcm((i-1)*8+2:(i-1)*8+4)=[0,0,0]else if x(i)<=47.5pcm((i-1)*8+2:(i-1)*8+4)=[0,0,1]p(i)=floor((x(i)-15.5)/2);else if x(i)<=111.5pcm((i-1)*8+2:(i-1)*8+4)=[0,1,0];p(i)=floor((x(i)-47.5)/4);else if x(i)<=239.5pcm((i-1)*8+2:(i-1)*8+4)=[0,1,1];p(i)=floor((x(i)-111.5)/8);else if x(i)<=495.5pcm((i-1)*8+2:(i-1)*8+4)=[1,0,0];p(i)=floor((x(i)-239.5)/16);else if x(i)<=1007.5pcm((i-1)*8+2:(i-1)*8+4)=[1,0,1];p(i)=floor((x(i)-495.5)/32);else if x(i)<=2031.5pcm((i-1)*8+2:(i-1)*8+4)=[1,1,0];p(i)=floor((x(i)-1007.5)/64);else if x(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解码for i=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);switch k(i)case 0dpcm(i)=h(i);case 1dpcm(i)=16.5+h(i)*2+1;case 2dpcm(i)=49.5+h(i)*4+2;case 3dpcm(i)=115.5+h(i)*8+4;case 4dpcm(i)=247.5+h(i)*16+8;case 5dpcm(i)=511.5+h(i)*32+16;case 6dpcm(i)=1039.5+h(i)*64+32;case 7dpcm(i)=2095.5+h(i)*128+64;otherwisedisp('error');endif pcm((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;for i=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]上均匀分布for i=1:1000if r(i)>=0.5r(i)=1;elseif r(i)<0.5r(i)=0;end;end;%0与1均匀出现的信号t=r+d;e=0;for i=1:1000if t(i)>=0.5;t(i)=1;elseif t(i)<0.5;t(i)=0;%判end;if t(i)~=r(i);e=e+1;%误码次数end;end;P=e./1000;%误码率%进行均匀量化%初始化,n为量化级数n=input('Please enter the qualify level n:');if isempty(n)n=8;end;k=100; % K:信号长度for i=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);for i=1:kif z(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)));for j=1:(n-1)jypcm(i,n-j+1)=mod(floor(sss(i)/10^(j-1)),10^j);end;end;end;figure;stem(jypcm)grid ontitle('量化级数为n时量化输出波形')



function code=PCMcoding(Range,S)
code=zeros(1,8);
Normalization=abs(S)/Range;
deta=1/2048;
if sign(S)==1
      code(1)=1;
else
      code(1)=0;
end
detas=ceil(Normalization/deta);
if detas>128
      code(2)=1;
      if detas>512
          code(3)=1;
          if detas>1024
              code(4)=1;
          end
      elseif detas>256
          code(4)=1;
      end
elseif detas>32
      code(3)=1;
      if detas>64
          code(4)=1;
      end
elseif detas>16
      code(4)=1;
end
Tribal=bin2dec(num2str(code(2:4)))+1;
if Tribal==1
      Interval=1;
else Interval=(2^(Tribal+2))/16;
end
if Tribal==1
      code(5)=(2^3)*Interval<detas;
      code(6)=code(5)*(2^3)*Interval+(2^2)*Interval<detas;
      code(7)=code(5)*(2^3)*Interval+code(6)*(2^2)*Interval+(2^1)*Interval<detas;
      code(8)=code(5)*(2^3)*Interval+code(6)*(2^2)*Interval+code(7)*(2^1)*Interval+(2^0)*Interval<detas;
else
      code(5)=2^(Tribal+2)+(2^3)*Interval<detas;
      code(6)=2^(Tribal+2)+code(5)*(2^3)*Interval+(2^2)*Interval<detas;
      code(7)=2^(Tribal+2)+code(5)*(2^3)*Interval+code(6)*(2^2)*Interval+(2^1)*Interval<detas;
      code(8)=2^(Tribal+2)+code(5)*(2^3)*Interval+code(6)*(2^2)*Interval+code(7)*(2^1)*Interval+(2^0)*Interval<detas;
end
2。PCM 解码实现(M函数)
%PCM Decoding
function S=PCMdecoding(Range,code)
deta=Range/2048;
Tribal=bin2dec(num2str(code(2:4)))+1;
if Tribal==1
      Interval=1;
else Interval=(2^(Tribal+2))/16;
end
if Tribal==1
      S=code(5)*(2^3)*Interval+code(6)*(2^2)*Interval+code(7)*(2^1)*Interval+code(8)*(2^0)*Interval+Interval;
else
      S=2^(Tribal+2)+code(5)*(2^3)*Interval+code(6)*(2^2)*Interval+code(7)*(2^1)*Interval+code(8)*(2^0)*Interval+Interval;
end
S=S*deta;

相关标签:大神matlab

下一篇:网易邮箱安全验证怎么弄,如图

上一篇:运行以下程序段,给出程序运行的结果,并分析原因.

热门标签:
excel 网盘 破解 word dll
最新更新:
微软重新评估新的Outlook的使用时机 联想推出搭载联发科Helio G80芯片组的Tab M9平板 英特尔创新大赛时间确定! 微软Edge浏览器在稳定渠道中推出Workspaces功能 英伟达RTX4060TiGPU推出MaxSun动漫主题! 谷歌地图为用户提供了街景服务! GameSir 在T4 Kaleid中推出了一款出色的控制器! 微软开始在Windows 11 中测试其画图应用程序的新深色模式! LG电子推出全球首款无线OLED电视 英伟达人工智能芯片崭露头角! Steam Deck可以玩什么游戏-Steam Deck价格限时优惠 雷蛇推出CobraPro鼠标 Kindle电子阅读器可以访问谷歌商店吗 Windows10如何加入组策略 window10图片查看器怎么没有了?