feat(matlab): comm lab 5

This commit is contained in:
iridiumR 2022-11-26 17:24:08 +08:00
parent 19d2491d51
commit 05c2027520

View file

@ -1,23 +1,23 @@
clc
clear
%================================================
%
%================================================
x = 2047 %/
pcmcode = pcmEnCode(x) %pcmCode()PCM
amp = pcmDeCode(pcmcode) %pcmDecode()PCM
amp = pcmDecode(pcmcode)
%qerror = x-amp %
x = 500
pcmcode = pcmEnCode(x)
amp = pcmDeCode(pcmcode)
qerror = x-amp
%================================================
%
%================================================
% M = 5; % -5~+5
% delta = M/2^11; %
% y = +2.5 %/V
% y0 = round(y/delta) %
% pcmcode = pcmCode(y0) %PCM
% amp=pcmDecode(pcmcode); %PCM
% amp = amp*delta %
% qerror=y-amp %
M = 5
d = M/2^11;
x = 2.5
x0 = round(x/d)
pcmcode = pcmEnCode(x0);
amp0=pcmDeCode(pcmcode);
amp0 = amp0*d
qerror=x-amp0
function pcmcode = pcmEnCode(s)
@ -27,7 +27,7 @@ function pcmcode = pcmEnCode(s)
if s>0
pcm=pcm+bitshift(1,7);
else
s=abs(s)
s=abs(s);
end
for i = 0:7
if s < start_t(i+2)
@ -43,30 +43,14 @@ function amp = pcmDeCode(pcmcode)
start_t=[0,2.^(4:11)];
width_t=[1,1,2,4,8,16,32,64];
code=bin2dec(pcmcode);
isNeg=0;
if code < 128
isNeg=1;
k=bitshift(bitand(code,0b01111111),-4);
amp=start_t(k+1) ...
+ double(mod(bitand(code,0b01111111),16)) ...
* 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;
ucode=bitand(code,0b01111111);
k=bitshift(ucode,-4);
m=bitand(ucode,0b00001111);
amp=start_t(k+1)+double(m)*width_t(k+1)+width_t(k+1)/2;
if code<=128
amp=-amp;
else
amp=dV;
amp=amp;
end
end