├── LICENSE ├── README.md ├── bit_revert.m ├── f.m ├── g.m ├── polar_decode.m ├── polar_encode.m ├── polar_encode_prime.m ├── polar_encode_recursive.m └── test_polar.m /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Xiao, Shaoning 萧少宁 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Polar Codes encoding and decoding 2 | ============================= 3 | 4 | The simulation codes of polar codes encoding and successive cancellation decoding are included. 5 | 6 | To run the code for the block length N, suppose n = log2(N), 7 | please enter the following command 8 | * test_polar(n) 9 | 10 | The frozen bits can be specified in the test bench. 11 | -------------------------------------------------------------------------------- /bit_revert.m: -------------------------------------------------------------------------------- 1 | function result = bit_revert(x, n) 2 | 3 | result = zeros(1, length(x)); 4 | for index = 1:length(x) 5 | result(index) = bitget(x(index), 1:n) * 2.^((n-1):-1:0).'; 6 | end 7 | 8 | end -------------------------------------------------------------------------------- /f.m: -------------------------------------------------------------------------------- 1 | % function result = f(p, q) 2 | % result = (p * q + 1)/(p + q); 3 | % end 4 | 5 | function result = f(x, y) 6 | % result = log((exp(x+y) + 1)/(exp(x) + exp(y))); 7 | % result = 2*atanh(tanh(x/2) * tanh(y/2)); 8 | result = sign(x) * sign(y) * min(abs(x), abs(y)); 9 | end 10 | -------------------------------------------------------------------------------- /g.m: -------------------------------------------------------------------------------- 1 | % function result = g(p, q, u) 2 | % result = (p^(1-2*u)) * q; 3 | % end 4 | 5 | function result = g(x, y, u) 6 | if u == 0 7 | result = x + y; 8 | elseif u == 1 9 | result = y - x; 10 | else 11 | error('wrong u value.'); 12 | end 13 | end -------------------------------------------------------------------------------- /polar_decode.m: -------------------------------------------------------------------------------- 1 | function [u, v] = polar_decode(y, frozen_bits_indicator) 2 | 3 | N = length(y); 4 | 5 | if N == 2 6 | 7 | L_u_1 = f(y(1), y(2)); 8 | 9 | u = zeros(1, 2); 10 | if frozen_bits_indicator(1) == 1 11 | u(1) = 0; 12 | elseif L_u_1 >= 0 13 | u(1) = 0; 14 | else 15 | u(1) = 1; 16 | end 17 | 18 | L_u_2 = g(y(1), y(2), u(1)); 19 | 20 | if frozen_bits_indicator(2) == 1 21 | u(2) = 0; 22 | elseif L_u_2 >= 0 23 | u(2) = 0; 24 | else 25 | u(2) = 1; 26 | end 27 | 28 | v = zeros(1, 2); 29 | v(1) = bitxor(u(1), u(2)); 30 | v(2) = u(2); 31 | 32 | else 33 | 34 | L_w_odd = zeros(1, N/2); 35 | for index = 1:(N/2) 36 | L_w_odd(index) = f(y(2*index-1), y(2*index)); 37 | end 38 | 39 | frozen_bits_indicator_1 = frozen_bits_indicator(1:(N/2)); 40 | 41 | [u_1, v_1] = polar_decode(L_w_odd, frozen_bits_indicator_1); 42 | 43 | L_w_even = zeros(1, N/2); 44 | for index = 1:(N/2) 45 | L_w_even(index) = g(y(2*index-1), y(2*index), v_1(index)); 46 | end 47 | 48 | frozen_bits_indicator_2 = frozen_bits_indicator((N/2 +1):N); 49 | 50 | [u_2, v_2] = polar_decode(L_w_even, frozen_bits_indicator_2); 51 | 52 | u = [u_1, u_2]; 53 | 54 | v = zeros(1, N); 55 | for index = 1:(N/2) 56 | v(2*index-1) = bitxor(v_1(index), v_2(index)); 57 | v(2*index) = v_2(index); 58 | end 59 | 60 | end 61 | 62 | end -------------------------------------------------------------------------------- /polar_encode.m: -------------------------------------------------------------------------------- 1 | function x = polar_encode(u, frozen_bits_indicator) 2 | 3 | N = length(u); 4 | 5 | n = log2(N); 6 | 7 | if N ~= 2^n 8 | error('wrong length of input'); 9 | end 10 | 11 | u(frozen_bits_indicator == 1) = 0; 12 | 13 | x = polar_encode_recursive(u); 14 | 15 | end -------------------------------------------------------------------------------- /polar_encode_prime.m: -------------------------------------------------------------------------------- 1 | function x = polar_encode_prime(u, frozen_bits_indicator) 2 | 3 | N = length(u); 4 | 5 | n = log2(N); 6 | 7 | if N ~= 2^n 8 | error('wrong length of input'); 9 | end 10 | 11 | u(frozen_bits_indicator == 1) = 0; 12 | 13 | G_2 = [1, 0; 1, 1]; 14 | 15 | G = G_2; 16 | 17 | for index = 1:(n-1) 18 | G = kron(G_2, G); 19 | end 20 | 21 | bit_reverted_list = bit_revert(0:(N-1), n) + 1; 22 | 23 | x = mod(u(bit_reverted_list) * G, 2); 24 | 25 | end -------------------------------------------------------------------------------- /polar_encode_recursive.m: -------------------------------------------------------------------------------- 1 | function y = polar_encode_recursive(u) 2 | 3 | N = length(u); 4 | 5 | n = log2(N); 6 | 7 | if N ~= 2^n 8 | error('wrong length of input'); 9 | end 10 | 11 | if N == 2 12 | y = mod(u * [1, 0; 1, 1], 2); 13 | return 14 | end 15 | 16 | u_1 = zeros(1, N); 17 | 18 | u_1(1:2:end) = bitxor(u(1:2:end), u(2:2:end)); 19 | u_1(2:2:end) = u(2:2:end); 20 | 21 | u_2 = [u_1(1:2:end), u_1(2:2:end)]; 22 | 23 | y_1 = polar_encode_recursive(u_2(1:N/2)); 24 | y_2 = polar_encode_recursive(u_2((N/2+1):end)); 25 | 26 | y = [y_1, y_2]; 27 | 28 | end -------------------------------------------------------------------------------- /test_polar.m: -------------------------------------------------------------------------------- 1 | function test_polar(n) 2 | 3 | if nargin == 0 4 | n = 3; 5 | elseif nargin > 1 6 | disp('usage: test_polar(n)'); 7 | end 8 | 9 | N = 2^n; 10 | 11 | u = randi([0, 1], 1, N); 12 | 13 | frozen_bits_indicator = randi([0, 1], 1, N); 14 | 15 | x = polar_encode(u, frozen_bits_indicator); 16 | x_prime = polar_encode_prime(u, frozen_bits_indicator); 17 | 18 | if isequal(x, x_prime) 19 | disp('polar encoding test passed.'); 20 | end 21 | 22 | symbols = 1 - 2*x; 23 | 24 | snr = 0.2; 25 | 26 | waveform = symbols + snr * randn(1, N); 27 | 28 | demodulated_symbols = waveform; 29 | 30 | decoded_bits = polar_decode(demodulated_symbols, frozen_bits_indicator); 31 | 32 | u(frozen_bits_indicator == 1) = 0; 33 | 34 | if isequal(decoded_bits, u) 35 | disp('polar decoding test passed.'); 36 | else 37 | disp('polar decoding test failed.'); 38 | end 39 | 40 | end --------------------------------------------------------------------------------