feat(matlab): comm lab 5
This commit is contained in:
parent
19d2491d51
commit
05c2027520
1 changed files with 22 additions and 38 deletions
|
@ -1,23 +1,23 @@
|
||||||
clc
|
clc
|
||||||
|
clear
|
||||||
%================================================
|
%================================================
|
||||||
%已知抽样值以△为单位
|
%已知抽样值以△为单位
|
||||||
%================================================
|
%================================================
|
||||||
x = 2047 %抽样值/量化单位△
|
x = 500
|
||||||
pcmcode = pcmEnCode(x) %调用pcmCode()函数实现PCM编码
|
pcmcode = pcmEnCode(x)
|
||||||
amp = pcmDeCode(pcmcode) %调用pcmDecode()函数实现PCM译码
|
amp = pcmDeCode(pcmcode)
|
||||||
amp = pcmDecode(pcmcode)
|
qerror = x-amp
|
||||||
%qerror = x-amp %求量化误差
|
|
||||||
%================================================
|
%================================================
|
||||||
%已知抽样值为实际电压值,则需先转换为以△为单位
|
%已知抽样值为实际电压值,则需先转换为以△为单位
|
||||||
%================================================
|
%================================================
|
||||||
% M = 5; %设置量化范围 -5~+5
|
M = 5
|
||||||
% delta = M/2^11; %求量化单位△
|
d = M/2^11;
|
||||||
% y = +2.5 %设置抽样值/V
|
x = 2.5
|
||||||
% y0 = round(y/delta) %将抽样值转换为以△为单位
|
x0 = round(x/d)
|
||||||
% pcmcode = pcmCode(y0) %PCM编码
|
pcmcode = pcmEnCode(x0);
|
||||||
% amp=pcmDecode(pcmcode); %PCM译码
|
amp0=pcmDeCode(pcmcode);
|
||||||
% amp = amp*delta %转换为实际电压值
|
amp0 = amp0*d
|
||||||
% qerror=y-amp %求量化误差
|
qerror=x-amp0
|
||||||
|
|
||||||
|
|
||||||
function pcmcode = pcmEnCode(s)
|
function pcmcode = pcmEnCode(s)
|
||||||
|
@ -27,7 +27,7 @@ function pcmcode = pcmEnCode(s)
|
||||||
if s>0
|
if s>0
|
||||||
pcm=pcm+bitshift(1,7);
|
pcm=pcm+bitshift(1,7);
|
||||||
else
|
else
|
||||||
s=abs(s)
|
s=abs(s);
|
||||||
end
|
end
|
||||||
for i = 0:7
|
for i = 0:7
|
||||||
if s < start_t(i+2)
|
if s < start_t(i+2)
|
||||||
|
@ -43,30 +43,14 @@ function amp = pcmDeCode(pcmcode)
|
||||||
start_t=[0,2.^(4:11)];
|
start_t=[0,2.^(4:11)];
|
||||||
width_t=[1,1,2,4,8,16,32,64];
|
width_t=[1,1,2,4,8,16,32,64];
|
||||||
code=bin2dec(pcmcode);
|
code=bin2dec(pcmcode);
|
||||||
isNeg=0;
|
ucode=bitand(code,0b01111111);
|
||||||
if code < 128
|
k=bitshift(ucode,-4);
|
||||||
isNeg=1;
|
m=bitand(ucode,0b00001111);
|
||||||
k=bitshift(bitand(code,0b01111111),-4);
|
amp=start_t(k+1)+double(m)*width_t(k+1)+width_t(k+1)/2;
|
||||||
amp=start_t(k+1) ...
|
if code<=128
|
||||||
+ double(mod(bitand(code,0b01111111),16)) ...
|
amp=-amp;
|
||||||
* width_t(k+1);
|
|
||||||
if isNeg=1 amp=-amp;end
|
|
||||||
end
|
|
||||||
|
|
||||||
%PCM译码函数
|
|
||||||
function amp = pcmDecode(pcmcode)
|
|
||||||
code_table=[0,2.^(4:11)]; %段落起始电平表
|
|
||||||
delta=[1,1,2,4,8,16,32,64]; %段内量化间隔表
|
|
||||||
codeDec=bin2dec(pcmcode); %二进制编码字符串转换为十进制数值
|
|
||||||
ucode=bitand(codeDec,0b01111111); %屏蔽极性位
|
|
||||||
k=bitshift(ucode,-4); %右移4位,提取段落码
|
|
||||||
x0=code_table(k+1); %查表求段落起始电平
|
|
||||||
m=bitand(ucode,0b00001111); %提取段内码
|
|
||||||
x1=double(m)*delta(k+1); %求段内量化电平
|
|
||||||
dV=x0+x1+delta(k+1)/2; %译码输出电平
|
|
||||||
if codeDec<=128 %处理极性
|
|
||||||
amp=-dV;
|
|
||||||
else
|
else
|
||||||
amp=dV;
|
amp=amp;
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Reference in a new issue