This repository has been archived on 2024-01-06. You can view files and clone it, but cannot push or open issues or pull requests.
justhomework/Matlab/comm/lab5_0.m
2022-11-26 17:24:08 +08:00

56 lines
1.3 KiB
Matlab

clc
clear
%================================================
%已知抽样值以△为单位
%================================================
x = 500
pcmcode = pcmEnCode(x)
amp = pcmDeCode(pcmcode)
qerror = x-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)
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);
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=amp;
end
end