📄 conv_viterbi_simu.m
字号:
% simulate the process of convolutional encoding and viterbi decoding
% using matlab functions
clear all;
clc;
% coding parameters
msg_len = 1494;
L=7; % constraint length
G=[133 165 171]; % generator polynomial for matlab
tblen=100; % trace back length
soft_bit_len=4; % bit length of soft input
tail_bit_len=L-1; % length of tail bits
global g_soft_bit_len; % soft decision input word length
g_soft_bit_len=2;
tail_bit=zeros(tail_bit_len,1);
len=msg_len+tail_bit_len;
t = poly2trellis(L,G); % Define trellis.
%random bit stream generation
msg = randint(msg_len,1,2,94384); % Random data
msg=[msg;tail_bit]; % appending by zeros tail bits
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%matlab%%%%%%%%%%%%%%%%
% convolutional encoding
code = convenc(msg,t); % Length is 2*len.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%third party%%%%%%%%%%%%%%%%
%对原信息编码
k0=1;
G_3=[1 0 1 1 0 1 1;1 1 1 0 1 0 1;1 1 1 1 0 0 1];
code_3=cnv_encd(G_3,k0,msg');
code_3=code_3(1:end-18);
% logcial transfering
tcode = -2*code+1; % Transmit -1s and 1s.
nicode=tcode;
% % puncturing
% punctcode = tcode;
% punctcode(3:3:end)=[]; % Length is (2*len)*2/3.
% ncode=punctcode;
%
% % depuncuring
% nicode = zeros(2*len,1); % Zeros represent inserted data.
% nicode(1:3:end) = ncode(1:2:end); % Write actual data.
% nicode(2:3:end) = ncode(2:2:end); % Write actual data.
% viterbi decoding for -1/1 logical input,hard decision
decoded = vitdec(nicode,t,tblen,'trunc','unquant'); % Decode.
% BER calculation
[number,ratio]=biterr(decoded,msg); % Bit error rate
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% matlab
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%viterbi decoding for soft decision
% transfer input into soft input bits, range [0,2~soft_bit_len-1]
%nicode=nicode*(-1)+2;
% for cn=1:length(nicode)
% if(nicode(cn)==1)
% nicode(cn)=0;
% end;
% end;
tcode=code*(2^g_soft_bit_len-1);
nicode=tcode;
decoded = vitdec(nicode,t,tblen,'term','soft',2); % Decode.
%decoded=decoded';
[number,ratio]=biterr(decoded,msg); % Bit error rate
% puncture
%data_temp=nicode;
for cn=1:(length(nicode)/15)
data_temp((cn-1)*8+1:(cn-1)*8+2)=nicode((cn-1)*15+1:(cn-1)*15+2);
data_temp((cn-1)*8+3:(cn-1)*8+5)=nicode((cn-1)*15+6:(cn-1)*15+8);
data_temp((cn-1)*8+6:(cn-1)*8+8)=nicode((cn-1)*15+12:(cn-1)*15+14);
end;
nicode=data_temp';
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% depuncture
% puncture and depuncture
global g_phy_para; % system parameters for PHY
% PHY system parameters initialization mannually
g_phy_para=[1 % TFC
1 % band group
0 % preamble mode; 0,standard; 1, burst
0 % 53.3 80 106.7 200
% 320 400 480; [0,1,2,~6]
0 % modulation mode; QPSK, 0; DCM,1;
5/8 % coding rate,index 6
0 % FDS mode; 0, not using FDS;1, using FDS
0 % TDS mode; 0,not using TDS; 1, using TDS
00100 % rate bits
40 % length of PSDU by bytes
01 % scrambler;
0 % preambler type bit, denotes the type used in next packet; 0, standard;1, burst;
1 % burst mode bit
128 % FFT size, index 14
100 % number of data subcarriers
12 % number of pilot subcarriers
10 % number of guard subcarriers
];
% depuncture and insert dummy bits
dumy_bit=floor(2^(g_soft_bit_len-1));
depunct_output=DEPUNCT(nicode,dumy_bit);
decoded = vitdec(depunct_output,t,tblen,'term','soft',2); % Decode.
decoded=decoded';
[number,ratio]=biterr(decoded,msg); % Bit error rate
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% third party
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[decoder_output,survivor_state,cumulated_metric]=viterbi(G_3,k0,nicode');
[number,ratio]=biterr(decoded,msg); % Bit error rate
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -