📄 alupcm.m
字号:
clear all
close all
t=0:0.01:10;
vm1=-70:1:0; % 输入的正弦信号幅度不同
vm=10.^(vm1/20);
figure(1)
for k=1:length(vm)
for m=1:2
x=vm(k)*sin(2*pi*t+2*pi*rand(1));
v=1;
xx=x/v; % normalize
sxx=floor(xx*4096);
y=pcm_encode(sxx);
yy=pcm_decode(y,v);
nq(m)=sum((x-yy).*(x-yy))/length(x);
sq(m)=mean(yy.^2);
snr(m)=(sq(m)/nq(m));
drawnow
subplot(211)
plot(t,x);
title('sample sequence')
subplot(212)
plot(t,yy)
title('pcm decode sequence')
end
snrq(k)=10*log10(mean(snr));
end figure(2)
plot(vm1,snrq)
xlabel('输入正弦信号幅度的dB值')
ylabel('量化信噪比')
axis([-60 0 0 60])
grid
****************************************
其中编码函数为pcm_encode.m。
***********************************
function [out]=pcm_encode(x)
n=length(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;
elseif 32<=abs(x(i)) & abs(x(i))<64
out(i,2)=0; out(i,3)=0; out(i,4)=1; step=2; st=32;
elseif 64<=abs(x(i)) & abs(x(i))<128
out(i,2)=0; out(i,3)=1; out(i,4)=0; step=4; st=64;
elseif 128<=abs(x(i)) & abs(x(i))<256
out(i,2)=0; out(i,3)=1; out(i,4)=1; step=8; st=128;
elseif 256<=abs(x(i)) & abs(x(i))<512
out(i,2)=1; out(i,3)=0; out(i,4)=0; step=16; st=256;
elseif 512<=abs(x(i)) & abs(x(i))<1024
out(i,2)=1; out(i,3)=0; out(i,4)=1; step=32; st=512;
elseif 1024<=abs(x(i)) & abs(x(i))<2048
out(i,2)=1; out(i,3)=1; out(i,4)=0; step=64; st=1024;
elseif 2048<=abs(x(i)) & 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_decode.m。
*******************************
function [out]=pcm_decode(in,v)
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(i,1)-1;
tmp=in(i,2)*4+in(i,3)*2+in(i,4)+1;
st=slot(tmp);
dt=(in(i,5)*8+in(i,6)*4+in(i,7)*2+in(i,8))*step(tmp)+0.5*step(tmp);
out(i)=ss*(st+dt)/4096*v;
end
*******************************
序列Lloyd算法,对一个正弦信号数据进行标量量化
clear all
clear close
N=2^3;
t=[0:100]*pi/20; % 以3比特传输信道
u=cos(t);
[p,c]=lloyds(u,N); % 生成分界点矢量和编码手册
[index,quant,distor]=quantiz(u,p,c); % 量化信号
plot(t,u,t,quant,'*')
**********************************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -