A律13折线PCM编码及其Matlab实现
(2014-05-04 18:31:24)
转载▼ 标签: pcm编译码
分类: 2014年5月
“抽样,量化和编码”是模拟信号数字化的基本过程。脉冲编码调制(PCM)是信号数字化的常用方法。这种编码技术于20 世纪40年代已经在通信技术中应用了。其中编码环节多采用A律13折线编码。 1.主程序 clc;clear all; %%
%coding %for j=1:20 fm=1000;
fs=8000;%signal frequence:1Hz,sample rate: 8000Hz �(j)=1/j;
Ac=1;%the signal amplitude is 1 t=0:10^-6:3*1/fm;
xt=Ac*sin(2*pi*fm*t); figure;
subplot(2,1,1); plot(t,xt);grid on;
st=xt(1:10^6/fs:length(xt));hold on; stem(0:1/fs:3*1/fm,st,'.r');
xlabel('t'),ylabel('源信号和采样信号')
y_pcm=zeros(1,length(st)*8);% the output of PCM coding
%construct a function do PCM coding ,return 8 bits,C1C2C3...C8 for i=1:length(st)
y_pcm(8*(i-1)+1:8*(i-1)+8)=PCMcoding(st(i));% coding end
subplot(2,1,2);
stem((1:length(y_pcm))/fs/8,y_pcm,'.black'); axis([0 length(y_pcm)/fs/8 -0.1 1.2]); xlabel('t'),ylabel('PCM输出信号') %%
% decoding
RCVst=zeros(1,length(st)); for i=1:length(st)
M=y_pcm((i-1)*8+1:(i-1)*8+8);
RCVst(i)=decoding(M); end
figure;
plot(t,xt);grid on;hold on;
stem(0:1/fs:3*1/fm,RCVst,'.g'); xlabel('t'),ylabel('译码信号')
quat_error=sum((st-RCVst).^2)/( sum(st.^2)) %end
%plot(Ac,10*log10(quat_error),'r','LineWidth',2),axis([0 1 -400 0])
%xlabel('信号幅度Ac'),ylabel('量化误差dB'),title('不同信号幅度下量化误差')
2.PCMcoding()编码函数 function C = PCMcoding( Is) %PCMcoding: do PCM coding % C=PCMcoding(Is)
% input:
% Is= amplitude to be coded % output:
% C=the array after coding(C=[C1C2...C8])
if Is>1||Is<-1,error('input must within [-1,1]'),end Is=round(Is*2048); C=zeros(1,8); if Is>0
C(1)=1 ; else
C(1)=0;
end % the polarity determins C(1) Is=abs(Is);
if 0<=Is&&Is<=16 C(2:4)=[0 0 0]; q=1;
a=0;
C(5:8)=e_coding(Is,q,a); end
if 16<=32
C(2:4)=[0 0 1]; q=1; a=16;
C(5:8)=e_coding(Is,q,a); end
if 32<=64
C(2:4)=[0 1 0]; q=2;
a=32;
C(5:8)=e_coding(Is,q,a); end
if 64<=128
C(2:4)=[0 1 1]; q=4; a=64;
C(5:8)=e_coding(Is,q,a); end
if 128<=256
C(2:4)=[1 0 0]; q=8;
a=128;
C(5:8)=e_coding(Is,q,a); end
if 256<=512
C(2:4)=[1 0 1]; q=16; a=256;
C(5:8)=e_coding(Is,q,a); end
if 512<=1024
C(2:4)=[1 1 0]; q=32; a=512;
C(5:8)=e_coding(Is,q,a); end
if 1024<=2048 C(2:4)=[1 1 1]; q=64;
a=1024;
C(5:8)=e_coding(Is,q,a); end end
3.e_coding()均匀量化编码
function C = e_coding( Is,q,a )
%retun the last 4 bits of PCM coding %Example: C=e_coding(513,32,512) %Then you will get C=0 0 0 0
% input:
% Is=quantified signal to PCM coding % a=the biginning of the segment
% q=the unit quantization % output:
% C=the coded binary array C=zeros(1,4); if a<=a+q C=[0 0 0 0]; end
if a+q<=a+2*q C=[0 0 0 1]; end
if a+2*q<=a+3*q C=[0 0 1 0]; end
if a+3*q<=a+4*q C=[0 0 1 1]; end
if a+4*q<=a+5*q C=[0 1 0 0]; end
if a+5*q<=a+6*q C=[0 1 0 1]; end
if a+6*q<=a+7*q C=[0 1 1 0]; end
if a+7*q<=a+8*q C=[0 1 1 1]; end
if a+8*q<=a+9*q C=[1 0 0 0]; end
if a+9*q<=a+10*q C=[1 0 0 1]; end
if a+10*q<=a+11*q C=[1 0 1 0]; end
if a+11*q<=a+12*q C=[1 0 1 1]; end
if a+12*q<=a+13*q C=[1 1 0 0]; end
if a+13*q<=a+14*q
C=[1 1 0 1]; end
if a+14*q<=a+15*q C=[1 1 1 0]; end
if a+15*q<=a+16*q C=[1 1 1 1]; end end
4.decoding()译码函数
function R = decoding( M )
�CODING Summary of this function goes here % Detailed explanation goes here temp=M(2)*2^2+M(3)*2+M(4); if temp==0 q=1; a=0; end if temp==1 q=1; a=16; end
if temp==2 q=2; a=32; end
if temp==3 q=4; a=64; end if temp==4 q=8; a=128; end
if temp==5 q=16; a=256; end if temp==6 q=32; a=512; end
if temp==7 q=64; a=1024; end
R=a+( M(5)*2^3+M(6)*2^2+M(7)*2+M(8) )*q+q/2; if M(1)==0 R=-R; end
R=R/2048; end
将以上4个函数分别作为4个m文件(文件名与函数名一致),运行主程序得到以下结果: 编码图形:
译码图形:
不同幅度下信噪比:
因篇幅问题不能全部显示,请点此查看更多更全内容