www.pudn.com > Polar_code.zip > Main_Homework.asv, change:2013-06-21,size:3053b


clc 
clear 
 
 
n = 3; 
N = 2^n; 
R = 0.8; 
 
 
 
%-----------------------------Channel Parameter---------------------------% 
Channel = 'BEC'; 
%-------------------------------- BEC ------------------------------------% 
p_set = 0:0.1:0.5; 
 
size_p = length(p_set); 
error = zeros(1,length(p_set)); 
Error_ratio = zeros(1,length(p_set)); 
error_ind = 1; 
 
iteration_size = 1000; 
 
for p = p_set 
   % p 
    Total_bit = 0; 
    
   %-------------------Finding positions of information-------------% 
 
    if strcmp(Channel,'BSC') 
        I_w = 1 + p*log2(p) + (1-p)*log2(1-p); 
    elseif strcmp(Channel,'BEC') 
        I_w = 1 - p; 
    end 
    Z_w = 1- I_w ; 
    %Z_w=2*sqrt(p*(1-p)); 
    %Z_w = 1/2; 
 
    temp_Z = Z_w; 
    for i = 1 : n 
        temp_Z = [2*temp_Z - temp_Z.^2; temp_Z.^2]; 
        temp_Z = reshape(temp_Z,[1,2^i]); 
    end 
     
    [Val, ind] = sort(temp_Z,'descend'); 
     
    L_info =  floor(N*R); 
    L_frozen = N - L_info; 
     
    frozen_ind = ind(1:L_frozen); 
    Info_ind = sort(ind(L_frozen+1:N),'ascend'); % find free position 
     
     
    for iteration = 1:iteration_size 
        
     iteration    
        %----------------------------Encoding------------------------------------%  
        u = randsrc(1,length(Info_ind),[1 0]); 
        G = Gen_G(N); 
        u_p = zeros(1,N); 
        u_p(Info_ind) = u; 
        X = mod((u_p*G)',2); 
         
        %-------------------------------Channel(BEC)----------------------------------% 
 
        if strcmp(Channel,'BEC') 
            e = randsrc(N,1,[0 0.5; 1-p p]); 
            while sum(e==0.5) > N*p 
                e = randsrc(N,1,[0 0.5; 1-p p]); 
            end 
            input_y = X + e; 
        %------------------------------Channel(BSC)------------------------------------% 
        elseif strcmp(Channel,'BSC') 
            all_perm_ind = randperm(N); 
            change_bit_ind = all_perm_ind(1:floor(N*p)); 
            X(change_bit_ind) = mod(X(change_bit_ind)+1,2); 
            input_y = X; 
        end 
         
         
        %--------------------------------Decoding---------------------------------% 
      
         
        u_estimate = zeros(1,N); 
         
        for i = 1:length(Info_ind) 
            Current_Loc = Info_ind(i); 
            Decision = W(input_y,u_estimate(1:Current_Loc-1),0,p,Channel)/W(input_y,u_estimate(1:Current_Loc-1),1,p,Channel); 
            if Decision >= 1 
                u_estimate(Current_Loc) = 0; 
            else 
                u_estimate(Current_Loc) = 1; 
            end 
        end 
        u_dec = u_estimate(Info_ind); 
        error(error_ind) = error(error_ind) + sum(u~=u_dec); 
        Total_bit = Total_bit + length(Info_ind); 
        Step_error = error/Total_bit; 
    end 
    Error_ratio(error_ind) = error(error_ind)/Total_bit; 
    error_ind = error_ind + 1; 
end 
Error_ratio(Error_ratio==0)=10^-3; 
 
semilogy(p_set,Error_ratio); 
title('polar decoder performance over BEC channel'); 
xlabel('transition probability');ylabel('BER'); 
grid on;