├── README.md ├── polar_code_SC_decoder.m ├── polar_code_channel.m ├── polar_code_encoder.m ├── polar_code_initHB.m ├── polar_code_initLLR.m ├── polar_code_main.m ├── polar_code_updateHB_L.m ├── polar_code_updateHB_R.m ├── polar_code_updateLLR_L.m └── polar_code_updateLLR_R.m /README.md: -------------------------------------------------------------------------------- 1 | # polar-codes-on-MATLAB 2 | 3 | This is a simple implementation of polar codes, including encoding, AWGN channel, SC decoding. 4 | 5 | I will add more code in future. 6 | 7 | # References 8 | 9 | [1] Arikan, Erdal. "Channel polarization: A method for constructing capacity-achieving codes for symmetric binary-input memoryless channels." IEEE Transactions on Information Theory 55.7 (2009): 3051-3073. 10 | 11 | [2] Sarkis, Gabi, et al. "Fast polar decoders: Algorithm and implementation." IEEE Journal on Selected Areas in Communications 32.5 (2014): 946-957. 12 | -------------------------------------------------------------------------------- /polar_code_SC_decoder.m: -------------------------------------------------------------------------------- 1 | function [u_e] = polar_code_SC_decoder(n,N,y,AC) 2 | % Description: 3 | % polar code successive cancellation encoder 4 | % input: 5 | % n: number of level 6 | % N: length of codeword 7 | % y: undecoded codeword 8 | % AC: frozen bit 9 | % output: 10 | % u_e: estimate of codeword 11 | 12 | LLR = polar_code_initLLR(n,N,y); 13 | HardBits = polar_code_initHB(n,N,LLR); 14 | for stage=1:n 15 | LLR = polar_code_updateLLR_L(n,N,stage,LLR); % Calculate alpha_left 16 | HardBits = polar_code_updateHB_L(n,N,stage,LLR,HardBits,AC); %Calculate beta_left 17 | LLR = polar_code_updateLLR_R(n,N,stage,LLR,HardBits); % Calculate alpha_right 18 | HardBits = polar_code_updateHB_R(n,N,stage,LLR,HardBits,AC); %Calculate beta_right 19 | end 20 | u_e = HardBits(:,n+1)'; 21 | end 22 | 23 | -------------------------------------------------------------------------------- /polar_code_channel.m: -------------------------------------------------------------------------------- 1 | function [ y ] = polar_code_channel(N,x,snr) 2 | % Description: 3 | % channel simulation 4 | % input: 5 | % N: length of codeword 6 | % x: input codeword 7 | % snr: Signal Noise Ratio of AWGN 8 | % output: 9 | % y: output codeword 10 | 11 | y=zeros(1,N); 12 | % BPSK mapping 13 | for i=1:N 14 | if(x(i)==0) 15 | y(i) = 1; % 0-->1 16 | else 17 | y(i) = -1; % 1-->-1 18 | end 19 | end 20 | % AWGN 21 | y=awgn(y,snr); 22 | end 23 | 24 | -------------------------------------------------------------------------------- /polar_code_encoder.m: -------------------------------------------------------------------------------- 1 | function [u,x] = polar_code_encoder(n,A,u_A,AC,u_AC) 2 | % Description: 3 | % polar code encoder 4 | % input: 5 | % n: number of level 6 | % A: information bit 7 | % u_A: information vector 8 | % AC: frozen bit 9 | % u_AC: frozen vector 10 | % output: 11 | % u: unencoded codeword 12 | % x: encoded codeword 13 | 14 | F=[1 0;1 1]; 15 | F_n=F; 16 | for i=1:(n-1) 17 | F_n=kron(F_n,F); 18 | end 19 | I=eye(2^n); 20 | G_n=F_n; 21 | % G_n=bitrevorder(F_n); % not necessary 22 | u=u_A*I(A,:)+u_AC*I(AC,:); 23 | x=mod(u_A*G_n(A,:)+u_AC*G_n(AC,:),2); 24 | end 25 | -------------------------------------------------------------------------------- /polar_code_initHB.m: -------------------------------------------------------------------------------- 1 | function [HB] = polar_code_initHB(n,N,LLR) 2 | % Description: 3 | % init estimated hard bits 4 | % input: 5 | % n: number of level 6 | % N: length of codeword 7 | % LLR: log-likelihood ratio 8 | % output: 9 | % HB: estimated hard bits 10 | 11 | HB = zeros(N,n+1); 12 | for i=1:N 13 | if(LLR(i,1)>=0) 14 | HB(i,1) = 0; 15 | else 16 | HB(i,1) = 1; 17 | end 18 | end 19 | end 20 | 21 | -------------------------------------------------------------------------------- /polar_code_initLLR.m: -------------------------------------------------------------------------------- 1 | function LLR = polar_code_initLLR(n,N,y) 2 | % Description: 3 | % init log-likelihood ratio 4 | % input: 5 | % n: number of level 6 | % N: length of codeword 7 | % y: undecoded codeword 8 | % output: 9 | % LLR: log-likelihood ratio 10 | 11 | LLR = zeros(N,n+1); 12 | LLR(:,1) = y; 13 | end 14 | 15 | -------------------------------------------------------------------------------- /polar_code_main.m: -------------------------------------------------------------------------------- 1 | clc 2 | clear 3 | close all 4 | 5 | N=8; % Channels 6 | n=log2(N); 7 | A=[4 6 7 8]; % information bit 8 | u_A=[1 1 0 1]; % information vetor 9 | AC=[1 2 3 5]; % frozen bit 10 | u_AC=[0 0 0 0]; % frozen vector 11 | snr=6; 12 | 13 | [u,x] = polar_code_encoder(n,A,u_A,AC,u_AC); 14 | y = polar_code_channel(N,x,snr); 15 | u_e = polar_code_SC_decoder(n,N,y,AC); 16 | 17 | if(u==u_e) 18 | disp('correct'); 19 | else 20 | disp('error'); 21 | end -------------------------------------------------------------------------------- /polar_code_updateHB_L.m: -------------------------------------------------------------------------------- 1 | function [HB] = polar_code_updateHB_L(n,N,stage,LLR,HB,AC) 2 | % Description: 3 | % Calculate beta_left 4 | % input: 5 | % n: number of level 6 | % N: length of codeword 7 | % stage: current processing stage 8 | % LLR: log-likelihood ratio 9 | % HB: estimated hard bits 10 | % AC: frozen bit 11 | % output: 12 | % HB: estimated hard bits 13 | 14 | Ns = 2^(n-stage+1); 15 | for j=1:Ns:N 16 | for i=j:j+Ns/2-1 17 | if(stage==n&&ismember(i,AC)) 18 | HB(i,stage+1) = 0; 19 | elseif(LLR(i,stage+1)>=0) 20 | HB(i,stage+1) = 0; 21 | else 22 | HB(i,stage+1) = 1; 23 | end 24 | end 25 | end 26 | end 27 | 28 | -------------------------------------------------------------------------------- /polar_code_updateHB_R.m: -------------------------------------------------------------------------------- 1 | function [HB] = polar_code_updateHB_R(n,N,stage,LLR,HB,AC) 2 | % Description: 3 | % Calculate beta_right 4 | % input: 5 | % n: number of level 6 | % N: length of codeword 7 | % stage: current processing stage 8 | % LLR: log-likelihood ratio 9 | % HB: estimated hard bits 10 | % AC: frozen bit 11 | % output: 12 | % HB: estimated hard bits 13 | 14 | Ns = 2^(n-stage+1); 15 | for j=1:Ns:N 16 | for i=j+Ns/2:j+Ns-1 17 | if(stage==n&&ismember(i,AC)) 18 | HB(i,stage+1) = 0; 19 | elseif(LLR(i,stage+1)>=0) 20 | HB(i,stage+1) = 0; 21 | else 22 | HB(i,stage+1) = 1; 23 | end 24 | end 25 | end 26 | end 27 | 28 | -------------------------------------------------------------------------------- /polar_code_updateLLR_L.m: -------------------------------------------------------------------------------- 1 | function [LLR] = polar_code_updateLLR_L(n,N,stage,LLR) 2 | % Description: 3 | % Calculate alpha_left 4 | % input: 5 | % n: number of level 6 | % N: length of codeword 7 | % stage: current processing stage 8 | % LLR: log-likelihood ratio 9 | % output: 10 | % LLR: log-likelihood ratio 11 | 12 | Ns = 2^(n-stage+1); 13 | for j=1:Ns:N 14 | for i=j:j+Ns/2-1 15 | p = LLR(i,stage); %alpha_i 16 | q = LLR(i+Ns/2,stage); %alpha_(i+Ns/2) 17 | LLR(i,stage+1) = sign(p)*sign(q)*min([abs(p),abs(q)]); 18 | end 19 | end 20 | end 21 | 22 | -------------------------------------------------------------------------------- /polar_code_updateLLR_R.m: -------------------------------------------------------------------------------- 1 | function [LLR] = polar_code_updateLLR_R(n,N,stage,LLR,HB) 2 | % Description: 3 | % Calculate alpha_right 4 | % input: 5 | % n: number of level 6 | % N: length of codeword 7 | % stage: current processing stage 8 | % LLR: log-likelihood ratio 9 | % HB: estimated hard bits 10 | % output: 11 | % LLR: log-likelihood ratio 12 | 13 | Ns = 2^(n-stage+1); 14 | for j=1:Ns:N 15 | for i=j:j+Ns/2-1 16 | p = LLR(i,stage); %alpha_i 17 | q = LLR(i+Ns/2,stage); %alpha_(i+Ns/2) 18 | LLR(i+Ns/2,stage+1) = (1-2*HB(i,stage+1))*p+q; 19 | end 20 | end 21 | end 22 | 23 | --------------------------------------------------------------------------------