MATLAB——PCM encoding and decoding experiment

Table of contents

MATLAB——PCM encoding and decoding

1. Experimental principle

1. Master the principles of PCM encoding and decoding

2. Practice using Matlab programming to implement PCM encoding and decoding

3. Understand the concept of distortion and be able to perform distortion analysis on decoding results.

2. Experimental principles

Pulse code modulation is to convert an analog signal that is continuous in time and continuous in value into a digital signal that is discrete in time and discrete in value and then transmitted in the channel. Pulse code modulation is a process in which the analog signal is first sampled, and then the sample amplitude is quantized and encoded.

Sampling is to periodically scan the analog signal and turn the time-continuous signal into a time-discrete signal. Sampling must follow the Nyquist sampling theorem. After sampling, the analog signal should also contain all the information in the original signal, which means that the original analog signal can be restored without distortion. The lower limit of its sampling rate is determined by the sampling theorem.
Insert image description here

Quantization means discretizing the amplitude of the instantaneous value obtained by sampling, that is, using a set of specified levels, and using the closest level value to the instantaneous sampled value. Represented, usually in binary.

Quantization error, the difference between the quantized signal and the sampled signal. Quantization error appears as noise at the receiving end, which is called quantization noise. The more quantization levels, the smaller the error, the more corresponding binary code bits, the higher the transmission rate and the wider the frequency band required. In order to make the quantization noise as small as possible without requiring too many code bits, non-uniform quantization is usually used for quantization. Non-uniform quantization determines the quantization interval according to different intervals of amplitude. The quantization interval is obtained for intervals with small amplitudes, and the quantization interval is obtained for intervals with large amplitudes. After an analog signal is sampled and quantized, a quantized pulse amplitude modulation signal is obtained, which only has a limited number of values.
Insert image description here

Insert image description here

Encoding is to use a set of binary codes to represent each quantized value with a fixed level. However, in fact, quantization is completed simultaneously during the encoding process, so the encoding process is also called analog/digital conversion, which can be recorded as A/D.
In order to solve the problem of large small signal quantization error and poor sound quality during uniform quantization, a nonlinear quantization method with uneven selection of quantization intervals is used in practice, that is, the quantization characteristics are hierarchically dense in small signals. , the quantization interval is small, but when the signal is large, the layering is sparse and the quantization interval is large. In practice, two logarithmic compression characteristics are used: A-law and μ-law.

3. Experimental requirements

1. Use Matlab to generate an analog signal, such as: Either write a signal yourself, or find a certain voice signal...

2. Under the condition that the requirements of the sampling theorem are met, the signal is sampled, implemented using Matlab programming, the sampled signal is drawn, and compared with the original signal.

3. Perform PCM coding on the sampled signal, implement it with Matlab programming, draw the coding waveform (rectangular pulse, unipolar non-return to zero), and mark the 8-bit coding result of a certain sampling value.

4. Perform PCM decoding on the PCM code obtained through channel transmission, implement it with Matlab programming, and draw the decoding waveform.

5. Perform distortion analysis on the decoding results.

6. (Extended exercise) Use PCM coding, 2PSK combined with simulink to realize the transmission simulation of voice signals (you can use the voice signals that come with Matlab).

4. Experimental content

4.1 Use Matlab to generate an analog signal

t=0:1/2000:0.1;
f=cos(100*pi*t)+2*sin(200*pi*t)+3*cos(300*pi*t);

plot(t,f);
xlabel('t');
title('f(t)的时域波形');

Insert image description here

4.2 Draw the sampled signal and compare it with the original signal

clear;
clc;
T=0.0005;
t=-0.05:T:0.05;
fs=2000;
sdt=1/fs;
t1=-0.05:sdt:0.05;

% 原始信号
f=cos(100*pi*t)+2*sin(200*pi*t)+3*cos(300*pi*t);
%抽样信号
fs=cos(100*pi*t1)+2*sin(200*pi*t1)+3*cos(300*pi*t1);

figure;
subplot(2,1,1);plot(t,f);title('原始信号');grid on;
subplot(2,1,2);stem(t1,fs,'.');title('抽样信号');grid on;

Insert image description here

4.3 PCM encoding of sampled signals

% 原始信号
f=cos(100*pi*t)+2*sin(200*pi*t)+3*cos(300*pi*t);
%抽样信号
fs=cos(100*pi*t1)+2*sin(200*pi*t1)+3*cos(300*pi*t1);
max = max(abs(fs));
% 原始信号

figure;
subplot(2,1,1);plot(t,f);title('原始信号');grid on;
subplot(2,1,2);stem(t1,fs,'.');title('抽样信号');grid on;



pcm_encode = PCMcoding(fs);
figure;
stairs(pcm_encode);%绘制信号的阶梯图
axis([0 328 -0.1 1.1]);
title('PCM 编码');axis([0 168 -0.1 1.1]);
set(gca,'xtick',[ 1 9  17 25 33 41 49 57 168]);
xticklabels({
    
    '1','9','17','25','33','41','49','57','168'})
grid on;

function code=PCMcoding(S)
    z=sign(S);                                %判断S的正负
    MaxS=max(abs(S));                         %求S的最大值 
    S=abs(S/MaxS);                            %归一化
    Q=2048*S;                                 %量化
    code=zeros(length(S),8);                  %PCM编码存储矩阵
    %%
    % 段落码判断程序
    for i=1:length(S)
        if (Q(i)>128)&&(Q(i)<=2048)
        code(i,2)=1;            %在第五段与第八段之间,段位码第一位都为"1"
        end
        if (Q(i)>32)&&(Q(i)<=128)||(Q(i)>512)&&(Q(i)<=2048)
        code(i,3)=1;            %在第三四七八段内,段位码第二位为"1"
        end
        if (Q(i)>16)&&(Q(i)<=32)||(Q(i)>64)&&(Q(i)<=128)||(Q(i)>256)&&(Q(i)<=512)||(Q(i)>1024)&&(Q(i)<=2048)
        code(i,4)=1;            %在二四六八段内,段位码第三位为"1"
        end
    end

    %%
    % 段内码判断程序
    N=zeros(length(S));                              
    for i=1:length(S)
        N(i)=bin2dec(num2str(code(i,2:4)))+1;        %找到code位于第几段,bin2dec将二进制整数的文本表示转换为双精度值,
    end

    a=[0,16,32,64,128,256,512,1024];                 %13折线各段起始对应的量化单位数
    b=[1,1,2,4,8,16,32,64];                          %除以16,得到每段的最小量化间隔
    for i=1:length(S)  
        q=ceil((Q(i)-a(N(i)))/b(N(i)));              %求出在段内的位置,ceil将 X 的每个元素四舍五入到大于或等于该元素的最接近整数
        if q==0
            code(i,(5:8))=[0,0,0,0];                 %如果输入为零则输出"0"
        else k=dec2bin(q-1,4);                       %将字符数组或字符串转换为数值数组,编码段内码为二进制,dec2bin将十进制整数转换为其二进制表示字符向量
            code(i,5)=str2num(k(1));
            code(i,6)=str2num(k(2));
            code(i,7)=str2num(k(3));
            code(i,8)=str2num(k(4));
        end
        
        %符号位的判断
        if z(i)>0
            code(i,1)=1;
        elseif z(i)<0
            code(i,1)=0;
        end                                        
    end

    code = reshape(code', 1, []);
end

Insert image description here
Insert image description here
Insert image description here
The point we choose to observe is the point with the largest negative value in the analog signal, and it is also the fifth point of the sampled signal. Its PCM encoding result should be 0111 1111. We open pcm_encode in Workbench and observe bits 33~40. It is found that the encoding result is indeed 0111 1111, which conforms to the encoding logic and proves that the code is written correctly.

4.4 PCM decoding

function s=PCMdecoding(encode, max)
    encode=(reshape(encode',8,length(encode)/8))';
    l=size(encode,1);
    a=[0,16,32,64,128,256,512,1024];
    b=[1 1 2 4 8 16 32 64];
    c=[0 1.5:15.5];
    for i=1:l
        x=encode(i,1);
        T=bin2dec(num2str(encode(i,(2:4))))+1;
        Y=bin2dec(num2str(encode(i,(5:8))));
        if Y==0
            k(i)=a(T)/2048;
        else
            k(i)=(a(T)+b(T)*c(Y))/2048;
        end
        if x==0
            s(i)=-k(i);
        else
            s(i)=k(i);
        end
    end
    s = s*max;
end

Insert image description here
By comparing the original signal with the PCM encoding and decoding, we can see that the original signal can be reconstructed relatively well. The comparison can be better seen through the picture below.
Insert image description here

4.5 Distortion analysis

Insert image description here

figure;
subplot(2,1,1);plot(t,f);title('原始信号');grid on;
subplot(2,1,2);stem(t1,fs,'.');title('抽样信号');grid on;

pcm_encode = PCMcoding(fs);

pcm_decode = PCMdecoding(pcm_encode, max);
figure;
plot(t, pcm_decode);hold on
title('PCM 译码');grid on;
plot(t,f);
title('原始信号');grid on;


% 计算失真度
da=0; 
for i=1:length(t)
    dc=(f(i)-pcm_decode(i))^2/length(t);
    da=da+dc;
end
fprintf('失真度是:%.6f\n',da);

We think the distortion degree is the mean square error between the front and rear signals. After calculation, it is:
The distortion degree is: 0.010716

4.6 Simulink implements voice signal transmission simulation

First, I designed a 2psk encoding module that can convert unipolar baseband binary codes into 2PSK signals. From the Scope module, we can get the 2PSK waveform generated by the random binary baseband signal generated by the Bernoulli Binary module to verify the correctness of the module.

Insert image description here
Insert image description here
Next, I combined the previously written PCM encoding and decoding module with the 2PSK module to simulate the speech signal encoding, transmission and decoding process. I used the voice package crisp that comes with MATLAB to make the sound of birdsong.
Next, I combined the previously written PCM encoding and decoding module with the 2PSK module to simulate the speech signal encoding, transmission and decoding process. To select the voice signal, I used the voice package crisp that comes with MATLAB, which can make the sound of birdsong.

load chirp % matlab自带语音信号
y1=[y,y];
x=y1(1:20000); %取前20000个采样点
sound(x,Fs);

The next step is the Simulink simulation of transmitting the voice signal. The simout is the transmitted voice signal, but note that this is in the form of pcm octal encoding, which must be decoded and compared with the original signal to verify the correctness of the transmission.

Insert image description here

%% 译码
demodata=simout(2:160001);
zz=pcm_decode(demodata,0.8);
figure;
subplot(1,1,1);
plot(zz);
title('译码的语音信号');
sound(zz,Fs);
%x是初始的语音信号矩阵
figure;
plot(x,'b');
hold on
%zz是译码得到的语音信号
plot(zz,'r');
legend('原语音信号','恢复的语音信号');
title('语音信号对比');

Insert image description here
Insert image description here
Insert image description here

5. Quote

[1]Xing Juwei. Digital transmission of analog signals-PCM coding[J]. Heilongjiang Science and Technology Information, 2014(33):116.
[2]Chen Kui. Voice PCM SIMULINK simulation of coded communication system [J]. Fujian Computer, 2012, 28(10):115-117.
[3] Zhao Shoubin, Li Honggang. Using Matlab to realize the A-law 13 polyline of PCM coding Method Quantification[J]. Science and Technology Information, 2009(36):547.

Guess you like

Origin blog.csdn.net/m0_51143578/article/details/128610247