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

57 lines
1.3 KiB
Mathematica
Raw Normal View History

2022-11-23 02:57:47 +00:00
clc
2022-11-26 09:24:08 +00:00
clear
2022-11-23 02:57:47 +00:00
%================================================
%
%================================================
2022-11-26 09:24:08 +00:00
x = 500
pcmcode = pcmEnCode(x)
amp = pcmDeCode(pcmcode)
qerror = x-amp
2022-11-23 02:57:47 +00:00
%================================================
%
%================================================
2022-11-26 09:24:08 +00:00
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
2022-11-23 02:57:47 +00:00
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
2022-11-26 09:24:08 +00:00
s=abs(s);
2022-11-23 02:57:47 +00:00
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);
2022-11-26 09:24:08 +00:00
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;
2022-11-23 02:57:47 +00:00
else
2022-11-26 09:24:08 +00:00
amp=amp;
2022-11-23 02:57:47 +00:00
end
end
2022-11-26 09:24:08 +00:00