awgn_conv_r12_k3_hard.m

来自「详细讲述纠错码的书籍」· M 代码 · 共 73 行

M
73
字号
% Name: AWGN_conv_R12_K3_hard.m
% Description:
% Simulation of rate-1/2 convolutional coding over an AWGN channel.
%
% Copyright (c) 2006. Robert Morelos-Zaragoza. All rights reserved.

clear all

trellis = poly2trellis(3, [7 5]);               % Trellis structure

nsim = 1000;                                    % Number of simulations

SNRinit = 0;
SNRinc = 1;
SNRfinal = 8;
SNR = [];

n = 10000;                                      % Block length
k =  5000;                                      % Number of message bits
rate = k/n;                                     % Code rate
tblength = 16;                                  % Traceback length

P2 = [];

seed = 12345678911;
rand('state',seed);
randn('state',seed);

for SNRdB = SNRinit:SNRinc:SNRfinal 
    
	TotalNumError = 0;
	TotalN = 0;

    SNRdBs = SNRdB + 10*log(rate)/log(10);
    No = (10.^(-SNRdBs/10));
    Var = No/2;                                     %Noise in dBW, Eb=1
    sigma = sqrt(Var);

    for Nsimulation = 1:nsim
        % Print progress ...
        fprintf('.');
        if mod(Nsimulation,50) == 0
            fprintf('\n');
        end
        
        %*************************** Encoding ****************************
        x = (sign(randn(1,k))+1)/2; % random bits
        y = convenc(x,trellis); % Encode the message.
        z = -2*y+1; % BPSK modulation
        
        % AWGN
        z=z+sqrt(Var)*randn(1,n);
        
        % Hard decisions
        z=(1-sign(z))/2;
        
        %*************************** Decoding ****************************
        xhat = vitdec(z,trellis,tblength,'cont','hard');     
        [errors,ratio]=biterr(xhat(tblength+1:end),x(1:end-tblength));

        TotalNumError = TotalNumError + errors;     
        TotalN = TotalN + k;                        
    end
    
    P1 = TotalNumError/TotalN;                    % Find the bit error rate
    P2 = [P2 P1];                                 % Concatenate all P1 data
    SNR = [SNR SNRdB];
    fprintf('%5.2f\t%e\t%6.0f\t%6.0f\n', SNRdB, P1, TotalNumError, TotalN);
end

semilogy (SNR,P2,'-bs');                          %Plot bit error rate
hold on

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?