Skip to content

Commit

Permalink
added polar code reliability sequence constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmet Inan committed Jul 7, 2023
1 parent b9d074a commit cb7271c
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 26 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ Bit freezers for the construction of [polar codes](https://en.wikipedia.org/wiki
* PolarFreezer: Constructs code for a given erasure probability without the need for storing nor sorting of erasure probabilities for the virtual channels.
* PolarCodeConst0: Constructs code by choosing the K best virtual channels computed from a given erasure probability.

### [polar_sequence.hh](polar_sequence.hh)

Construction of reliability sequences for [polar codes](https://en.wikipedia.org/wiki/Polar_code_(coding_theory)).

* PolarSeqConst0: Constructs a sequence by sorting virtual channel reliabilities computed from a given erasure probability.

### [polar_encoder.hh](polar_encoder.hh)

Encoders for [non-systematic and systematic](https://en.wikipedia.org/wiki/Systematic_code) [polar codes](https://en.wikipedia.org/wiki/Polar_code_(coding_theory)).
Expand Down
40 changes: 40 additions & 0 deletions polar_sequence.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Construct reliability sequences for polar codes
Copyright 2023 Ahmet Inan <[email protected]>
*/

#pragma once

#include <algorithm>

namespace CODE {

template <int MAX_M>
class PolarSeqConst0
{
void compute(double pe, int i, int h)
{
if (h) {
compute(pe * (2-pe), i, h/2);
compute(pe * pe, i+h, h/2);
} else {
prob[i] = pe;
}
}
double prob[1<<MAX_M];
int index[1<<MAX_M];
public:
void operator()(int *sequence, int level, double erasure_probability = std::exp(-1.))
{
assert(level <= MAX_M);
int length = 1 << level;
compute(erasure_probability, 0, length / 2);
for (int i = 0; i < length; ++i)
sequence[i] = i;
std::sort(sequence, sequence+length, [this](int a, int b){ return prob[a] > prob[b]; });
}
};

}

30 changes: 14 additions & 16 deletions tests/polar_list_regression_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Copyright 2020 Ahmet Inan <[email protected]>
#include "polar_helper.hh"
#include "polar_list_decoder.hh"
#include "polar_encoder.hh"
#include "polar_freezer.hh"
#include "polar_sequence.hh"
#include "crc.hh"
#include "sequence.h"

Expand Down Expand Up @@ -52,35 +52,33 @@ int main()
typedef std::default_random_engine generator;
typedef std::uniform_int_distribution<int> distribution;
auto data = std::bind(distribution(0, 1), generator(rd()));
auto frozen = new uint32_t[(N+31)/32];
auto frozen = new uint32_t[N/32];
auto codeword = new code_type[N];
auto temp = new simd_type[N];

long double erasure_probability = 0.5;
const int *reliability_sequence;
double erasure_probability = 0.5;
int K = (1 - erasure_probability) * N;
double design_SNR = 10 * std::log10(-std::log(erasure_probability));
std::cerr << "design SNR: " << design_SNR << std::endl;
#if 0
if (0) {
CODE::PolarFreezer freeze;
long double freezing_threshold = 0 ? 0.5 : std::numeric_limits<float>::epsilon();
K = freeze(frozen, M, erasure_probability, freezing_threshold);
} else {
auto freeze = new CODE::PolarCodeConst0<M>;
std::cerr << "sizeof(PolarCodeConst0<M>) = " << sizeof(CODE::PolarCodeConst0<M>) << std::endl;
auto construct = new CODE::PolarSeqConst0<M>;
std::cerr << "sizeof(PolarSeqConst0<M>) = " << sizeof(CODE::PolarSeqConst0<M>) << std::endl;
double better_SNR = design_SNR + 1.59175;
std::cerr << "better SNR: " << better_SNR << std::endl;
long double probability = std::exp(-pow(10.0, better_SNR / 10));
double probability = std::exp(-pow(10.0, better_SNR / 10));
std::cerr << "prob: " << probability << std::endl;
(*freeze)(frozen, M, K, probability);
delete freeze;
auto rel_seq = new int[N];
(*construct)(rel_seq, M, probability);
delete construct;
reliability_sequence = rel_seq;
} else {
reliability_sequence = sequence;
}
#else
for (int i = 0; i < N / 32; ++i)
frozen[i] = 0;
for (int i = 0; i < N - K; ++i)
frozen[sequence[i]/32] |= 1 << (sequence[i]%32);
#endif
frozen[reliability_sequence[i]/32] |= 1 << (reliability_sequence[i]%32);
std::cerr << "Polar(" << N << ", " << K << ")" << std::endl;
auto message = new code_type[K];
auto decoded = new simd_type[K];
Expand Down
28 changes: 18 additions & 10 deletions tests/polar_regression_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Copyright 2020 Ahmet Inan <[email protected]>
#include "polar_decoder.hh"
#include "polar_encoder.hh"
#include "polar_freezer.hh"
#include "polar_sequence.hh"

bool get_bit(const uint32_t *bits, int idx)
{
Expand All @@ -39,26 +40,33 @@ int main()
typedef std::default_random_engine generator;
typedef std::uniform_int_distribution<int> distribution;
auto data = std::bind(distribution(0, 1), generator(rd()));
auto frozen = new uint32_t[(N+31)/32];
auto frozen = new uint32_t[N/32];
auto codeword = new code_type[N];
auto temp = new code_type[N];

long double erasure_probability = 1. / 3.;
double erasure_probability = 1. / 3.;
int K = (1 - erasure_probability) * N;
double design_SNR = 10 * std::log10(-std::log(erasure_probability));
std::cerr << "design SNR: " << design_SNR << std::endl;
if (0) {
CODE::PolarFreezer freeze;
long double freezing_threshold = 0 ? 0.5 : std::numeric_limits<float>::epsilon();
K = freeze(frozen, M, erasure_probability, freezing_threshold);
} else {
double better_SNR = design_SNR + 0.5;//1.59175;
std::cerr << "better SNR: " << better_SNR << std::endl;
double probability = std::exp(-pow(10.0, better_SNR / 10));
if (1) {
auto freeze = new CODE::PolarCodeConst0<M>;
std::cerr << "sizeof(PolarCodeConst0<M>) = " << sizeof(CODE::PolarCodeConst0<M>) << std::endl;
double better_SNR = design_SNR + 0.5;//1.59175;
std::cerr << "better SNR: " << better_SNR << std::endl;
long double probability = std::exp(-pow(10.0, better_SNR / 10));
(*freeze)(frozen, M, K, probability);
delete freeze;
} else {
auto sequence = new int[N];
auto construct = new CODE::PolarSeqConst0<M>;
std::cerr << "sizeof(PolarSeqConst0<M>) = " << sizeof(CODE::PolarSeqConst0<M>) << std::endl;
(*construct)(sequence, M, probability);
delete construct;
for (int i = 0; i < N / 32; ++i)
frozen[i] = 0;
for (int i = 0; i < N - K; ++i)
frozen[sequence[i]/32] |= 1 << (sequence[i]%32);
delete[] sequence;
}
std::cerr << "Polar(" << N << ", " << K << ")" << std::endl;
auto message = new code_type[K];
Expand Down

0 comments on commit cb7271c

Please sign in to comment.