feat(matlab): comm lab 5 half

This commit is contained in:
iridiumR 2022-11-23 10:57:47 +08:00
parent ca2410d4ec
commit 5fd8752550

72
Matlab/comm/lab5_0.m Normal file
View file

@ -0,0 +1,72 @@
clc
%================================================
%
%================================================
x = 2047 %/
pcmcode = pcmEnCode(x) %pcmCode()PCM
amp = pcmDeCode(pcmcode) %pcmDecode()PCM
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 %
function pcmcode = pcmEnCode(s)
start_t=[0,2.^(4:11)];
width_t=[1,1,2,4,8,16,32,64];
pcm=0;
if s>0
pcm=pcm+bitshift(1,7);
else
s=abs(s)
end
for i = 0:7
if s < start_t(i+2)
pcm=pcm+bitshift(i,4);
pcm=pcm+floor((s-start_t(i+1))/width_t(i+1));
break
end
end
pcmcode=dec2bin(pcm,8);
end
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;
else
amp=dV;
end
end