Matlab PCM编码解码

        好久没发博文了,这两天正好在做通原的实验,正好学学怎么用Live Writer。

        通常我们会把模拟信号抽样、量化,直到最后变换成二进制符号的集成过程称为脉冲编码调制(Pulse Code Modulation),实现的主要包括三个步骤:抽样(sampling)、量化(quantization)和编码(coding)。

         1、Sampling

          Matlab仿真过程中选定模拟信号sin(4*t),按抽样定理,抽样频率只需高于模拟信号最高频率的两倍即可,但是实际情况下会大于两倍的模拟最高频率。

        

         2、Quantization

         量化方法包括:均匀量化和非均匀量化。这里主要讨论非均匀量化,并模拟A率压缩,通过13折现近似实现。

代码如下:

%13折线量化
function y=zhe13(x) 
x=x/max(x); 
z=sign(x);
x=abs(x); 
for i=1:length(x) 
    if ((x(i)>=0)&x(i)<1/64)      
        y(i)=16*x(i);      
    else if ((x(i)>=1/64)&x(i)<1/32)
            y(i)=8*x(i)+1/8;       
        else if ((x(i)>=1/32)&x(i)<1/16)  
                y(i)=4*x(i)+2/8;       
            else if ((x(i)>=1/16)&x(i)<1/8)    
                    y(i)=2*x(i)+3/8;       
                else if ((x(i)>=1/8)&x(i)<1/4) y(i)=x(i)+4/8;          
                    else if ((x(i)>=1/4)&x(i)<1/2)   
                            y(i)=(1/2)*x(i)+5/8;       
                        else y(i)=(1/4)*x(i)+6/8;    
                        end
                    end
                end;
            end;
        end;
    end;
end; 
y=z.*y;

     

       3、Coding

       编码包括A率13折线编码和解码两个过程,具体原理可参考樊昌信所著《通信原理》第六版中‘模拟信号的数字传输’一章。

代码如下,包括A率13折线编码和A率13折线解码:

% A率13折线编码 
function CodeofPCM = C_CodeofPCM(A_Crent,Amax) 
%% 极性
Jixing = 1; 
if A_Crent<0     
    Jixing=0; 
end
Jixing = dec2bin(Jixing,1);
%% 段号  
DuanHao = 0; 
A_Crentmaxto1 = floor(abs(A_Crent)/Amax*2048); 
LinghuaJG = 0; %分段操作 
if A_Crentmaxto1 < 16    
    DuanHao = 0;   
    DuanluoQS = 0;    
    LinghuaJG = 1; 
elseif A_Crentmaxto1 < 32   
    DuanHao = 1;    
    DuanluoQS = 16;   
    LinghuaJG = 1;
elseif A_Crentmaxto1 < 64  
    DuanHao = 2;   
    DuanluoQS = 32;
    LinghuaJG = 2; 
elseif A_Crentmaxto1 < 128    
    DuanHao = 3;    
    DuanluoQS = 64;  
    LinghuaJG = 4;
elseif A_Crentmaxto1 < 256  
    DuanHao = 4;   
    DuanluoQS = 128;
    LinghuaJG = 8; 
elseif A_Crentmaxto1 < 512    
    DuanHao = 5;     
    DuanluoQS = 256; 
    LinghuaJG = 16;
elseif A_Crentmaxto1 < 1024 
    DuanHao = 6;    
    DuanluoQS = 512;   
    LinghuaJG = 32;
else
    DuanHao = 7;     
    DuanluoQS = 1024;  
    LinghuaJG = 64; 
end
DuanHao = dec2bin(DuanHao,3); 
%% 段内码 
if A_Crentmaxto1 == 2048    
    A_Crentmaxto1 = A_Crentmaxto1-1; 
end
DuanNeima1 = floor((A_Crentmaxto1-DuanluoQS)/LinghuaJG);
DuanNeima  = dec2bin(DuanNeima1,4); 
%% PCM码组 CodeofPCM 
PCM = strcat(Jixing,DuanHao, DuanNeima); 
CodeofPCM = str2num(PCM);
% A率13折线解码 
function A_Crent=D_CodeofPCM(CodeofPCM) 
CodeofPCM_int = CodeofPCM;
%% 极性判断  
Jixing = 1; 
if floor(CodeofPCM_int/10^7) == 0   
    Jixing = -1; 
end
%% 段号 
DuanHao = floor(mod(abs(CodeofPCM_int),10^7)/10^4); 
DuanHao = num2str(DuanHao); 
DuanHao = bin2dec(DuanHao); 
switch DuanHao  
    case 0    
        DuanluoQS=0;        
        LinghuaJG=1;   
    case 1     
        DuanluoQS=16;   
        LinghuaJG=1;   
    case 2       
        DuanluoQS=32;   
        LinghuaJG=2;    
    case 3        
        DuanluoQS=64;      
        LinghuaJG=4;   
    case 4   
        DuanluoQS=128;    
        LinghuaJG=8;   
    case 5     
        DuanluoQS=256;    
        LinghuaJG=16;  
    case 6       
        DuanluoQS=512;    
        LinghuaJG=32;   
    case 7        
        DuanluoQS=1024;   
        LinghuaJG=64; 
end
%%段内码提取
DuanNeima = mod(abs(CodeofPCM_int),10^4); 
DuanNeima = num2str(DuanNeima); 
DuanNeima = bin2dec(DuanNeima); 
absofA_Crent = DuanluoQS + LinghuaJG*DuanNeima + 0.5*LinghuaJG; 
A_Crent=absofA_Crent*Jixing/2048;

 

 

    PCM具体实现:

    1、选取模拟信号x = sin(4*t),抽样频率为10Hz,得到:

Matlab PCM编码解码_第1张图片

   2、量化,13折线量化:

Matlab PCM编码解码_第2张图片

 

   3、编码:

   编码得到:

      Columns 1 through 8

       11111110    11111011    11101110     1111001     1111101     1111111     1111111     1111111

     Columns 9 through 16

       1111101     1111010    10000000    11111010    11111101    11111111    11111111    11111111

     Columns 17 through 21

        11111101    11111001     1101110     1111011     1111110

 

    解码后与编码比较:

Matlab PCM编码解码_第3张图片

 

 

 

 

转载于:https://www.cnblogs.com/ksheep/archive/2013/05/10/3071385.html

你可能感兴趣的:(matlab)