该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
均匀量化编码:
function[sqnr,a_quan,code]=upcm(a,n)
amax=max(abs(a));??a、n怎样定义
a_quan=a/amax;
%抽样值归一化
b_quan=a_quan;
d=2/n;
q=d.*[0:n-1]-(n-1)/2*d;
%对归一化的输入信号序列进行量化
for i=1:n
index=find((q(i)-d/2<=a_quan)&(a_quan<=q(i)+d/2));
a_quan(index)=q(i).*ones(1,length(index));
b_quan(find(a_quan==q(i)))=(i-1).*ones(1,length(find(a_quan==q(i))))
end
a_quan=a_quan*amax;
nu=ceil(log2(n));
code=zeros(length(a),nu);
for i=1:length(a)
for j=nu:-1:0
if(fix(b_quan(i)/(2^j))==1)
code(i,nu-j)=1;
b_quan(i)=b_quan(i)-2^j;
end
end
end
sqnr=20*log10(norm(a)./norm(a-a_quan));
PCM编码:
function[out]=pcm_encode(x)
for i=1:n
if x(i)>0
out(i,1)=1;
else
out(i,1)=0
end
if abs(x(i))>0 & abs(x(i))<32
out(i,2)=0;out(i,3)=0;out(i,4)=0;step=2;st=0;??step跟st分别代表什么
elseif
abs(x(i))>=32 & abs(x(i))<64
out(i,2)=0;out(i,3)=0;out(i,4)=1;step=2;st=32;
elseif abs(x(i))>=64
& abs(x(i))<128
out(i,2)=0;out(i,3)=1;out(i,4)=0;step=4;st=64;
elseif
abs(x(i))>=128 & abs(x(i))<256
out(i,2)=0;out(i,3)=1;out(i,4)=1;step=8;st=128;
elseif
abs(x(i))>=256 & abs(x(i))<512
out(i,2)=1;out(i,3)=0;out(i,4)=0;step=16;st=256;
elseif
abs(x(i))>=512 & abs(x(i))<1024
out(i,2)=1;out(i,3)=0;out(i,4)=1;step=32;st=512;
elseif
abs(x(i))>=1024 & abs(x(i))<2048
out(i,2)=1;out(i,3)=1;out(i,4)=0;step=64;st=1024;
elseif
abs(x(i))>=2048 & abs(x(i))<4096
out(i,2)=1;out(i,3)=1;out(i,4)=1;step=128;st=2048;
else
out(i,2)=1,out(i,3)=1;out(i,4)=1;step=128;st=2048;
end
if(abs(x(i))>=4096)
out(i,2:8)=[1 1 1 1 1 1 1];????
else
tmp=floor((abs(x(i))-st)/step);
t=dec2bin(tmp,4)-48; %函数dec2bin输出的是ASCII字符串,48对应0
out(i,5:8)=t(1:4);
end
end
out=reshape(out',1,8*n);
PCM译码:
function[out]=pcm_decode(in,v)
%decode the input pcm code
%in, input the pcm code 8bits sample
%v, quantized level
n=length(in);
in=reshape (in',8,n/8);
slot(1)=0;???
slot(2)=32;
slot(3)=64;
slot(4)=128;
slot(5)=256;
slot(6)=512;
slot(7)=1024;
slot(8)=2048;
step(1)=2;???
step(2)=2;
step(3)=4;
step(4)=8;
step(5)=16;
step(6)=32;
step(7)=64;
step(8)=128;
for i=1:n/8
ss=2*in(1,i)-1;
tmp=in(2,i)*4+in(3,i)*2+in(4,i)+1;
st=slot(tmp);
dt=(in(5,i)*8+in(6,i)*4+in(7,i)*2+in(8,i))*step(tmp)+0.5*step(tmp);
out(i)=ss*(st+dt)/4096*v;
end
主程序中:
figure(1)
for k=1:length(vm)
for m=1:2
x=vm(k)*sin(2*pi*4*t+2*pi*rand(1));
[sqnrM,zxx,zz]=upcm(x,M); %PCM均匀量化编码 ?????直接不了解哇
srm(m)=sqnrM;
v=vm(k);
xx=x/v; %normalize
sxx=floor(xx*4096);
y=pcm_encode(sxx); %PCM编码
yy=pcm_decode(y,v); %PCM解码
nq(m)=sum((x-yy).*(x-yy))/length(x); %噪声平均功率
sq(m)=mean(yy.^2); %信号平均功率
snr(m)=(sq(m)/nq(m)); %信号量化信噪比
usnrq(k)=mean(srm);
snrq(k)=10*log10(mean(snr)); %量化信噪比
end
end