% Removing all variables, functions, and MEX-files from memory, leaving the
% workspace empty.
clear all
% Deleting all figures whose handles are not hidden.
close all
% Deleting all figures including those with hidden handles.
close all hidden
% Clearing all input and output from the Command Window display giving us a clean screen.
clc
a=input('Enter the number of data which willed be coded: ');
for i=1:a
S(i)=input('Enter The data which willed be coded: ');
end
%for i=1:a
%Range=input('Please enter data Range: ');
%end
Range=max(abs(S));%量化范围
%t=[0:0.1:2*pi];
%u=sin(t);
%subplot(211);
%plot(u);
for i=1:a
[code codeout error codeout2 codeout3]=PCMcoding(Range,S(i));
fprintf('原始输入数据:%8.4f\n',S(i));
str1=['输入数据的编码结果: ' num2str(code)];
disp(str1);
fprintf('量化误差:%d\n',error);
fprintf('输入数据编码后的十进制数:%d\n',codeout);
str2=['输入数据的12位PCM编码结果: ' codeout3];
disp(str2);
%disp(codeout3);
decode=PCMdecoding(Range,code);
fprintf('数据的解码:%8.4f\n',decode);
end
%subplot(212);
%plot(m);
%for i=1:a
%decode=PCMdecoding(Range,code)
%end
clear all
% Removing all variables, functions, and MEX-files from memory, leaving the
% workspace empty.
%PCM Encoding
function [code,codeout,error,codeout2,codeout3]=PCMcoding(Range,S)
code=zeros(1,8);
%% Range=max(S);%量化范围
Normalization=abs(S)/Range;
deta=1/2048;%量化单位
if sign(S)==1%极性划分
code(1)=1;%正数
else
code(1)=0;%负数
end
detas=ceil(Normalization/deta);
%ceil(x) Round x to the nearest
%integer toward positive infinity :ceil(3.1)=4 and ceil(-3.1)=3
%detas是量化后的数值
if detas>128
code(2)=1;
if detas>512
code(3)=1;
if detas>1024
code(4)=1;
end
elseif detas>256
code(4)=1;
end
elseif detas>32
code(3)=1;
if detas>64
code(4)=1;
end
elseif detas>16
code(4)=1;
end
Tribal=bin2dec(num2str(code(2:4)))+1;
% Example
% bin2dec('010111') returns 23
% bin2dec('010 111') also returns 23
% bin2dec(' 010111') also returns 23
if Tribal==1
Interval=1;
else Interval=(2^(Tribal+2))/16;
end
if Tribal==1
code(5)=(2^3)*Interval
code(6)=code(5)*(2^3)*Interval+(2^2)*Interval
code(7)=code(5)*(2^3)*Interval+code(6)*(2^2)*Interval+(2^1)*Interval
code(8)=code(5)*(2^3)*Interval+code(6)*(2^2)*Interval+code(7)*(2^1)*Interval+(2^0)*Interval
else
code(5)=2^(Tribal+2)+(2^3)*Interval
code(6)=2^(Tribal+2)+code(5)*(2^3)*Interval+(2^2)*Interval
code(7)=2^(Tribal+2)+code(5)*(2^3)*Interval+code(6)*(2^2)*Interval+(2^1)*Interval
code(8)=2^(Tribal+2)+code(5)*(2^3)*Interval+code(6)*(2^2)*Interval+code(7)*(2^1)*Interval+(2^0)*Interval
end
if Tribal==1
codeout=code(5)*(2^3)*Interval+code(6)*(2^2)*Interval+code(7)*(2^1)*Interval+code(8)*(2^0)*Interval;
else
codeout=2^(Tribal+2)+code(5)*(2^3)*Interval+code(6)*(2^2)*Interval+code(7)*(2^1)*Interval+code(8)*(2^0)*Interval+(Interval/2);
end
%量化误差为1270-1248=22(量化单位)
error=abs(detas-codeout);
%12位的均匀量化码组
codeout2=dec2bin(codeout);
codeout3=[num2str(code(1)) codeout2];
%实现数据的A/D转换
%PCM Decoding
function S=PCMdecoding(Range,code)
deta=Range/2048;
Tribal=bin2dec(num2str(code(2:4)))+1;
if Tribal==1
Interval=1;
else Interval=(2^(Tribal+2))/16;
end
if Tribal==1
%S=code(5)*(2^3)*Interval+code(6)*(2^2)*Interval+code(7)*(2^1)*Interval+code(8)*(2^0)*Interval+Interval;
S=code(5)*(2^3)*Interval+code(6)*(2^2)*Interval+code(7)*(2^1)*Interval+code(8)*(2^0)*Interval;
else
%S=2^(Tribal+2)+code(5)*(2^3)*Interval+code(6)*(2^2)*Interval+code(7)*(2^1)*Interval+code(8)*(2^0)*Interval+Interva;
S=2^(Tribal+2)+code(5)*(2^3)*Interval+code(6)*(2^2)*Interval+code(7)*(2^1)*Interval+code(8)*(2^0)*Interval+(Interval/2);
end
S=S*deta;
%用量化单位与解码的十进制数据相乘
%得到原始数据的D/A转换
??? Undefined command/function 'PCMdecoding'.
这是我运行出现的错误,能帮我改一下么,非常感谢。