29 std::array<float, 3 * ND> llr0;
30 std::array<float, 3 * ND> llr1;
31 bool whiteningApplied;
38 static Result process(std::array<std::array<float, ND>, NROWS>
const &s1,
39 std::array<int, ND>
const &symbolWinners,
40 float erasureThreshold,
bool debug) {
42 [](std::vector<float> &values) -> std::optional<float> {
46 auto const mid = values.size() / 2;
48 std::nth_element(values.begin(), values.begin() + mid,
50 float med = values[mid];
52 if ((values.size() % 2) == 0 && mid > 0) {
53 std::nth_element(values.begin(), values.begin() + (mid - 1),
55 med = 0.5f * (med + values[mid - 1]);
63 auto const toneNoise =
64 [&]() -> std::optional<std::array<float, NROWS>> {
65 std::array<std::vector<float>, NROWS> toneSamples;
66 std::array<float, NROWS> noise = {};
69 for (
int j = 0; j < ND; ++j) {
70 int const winner = symbolWinners[j];
72 for (
int i = 0; i < NROWS; ++i) {
74 toneSamples[i].push_back(s1[i][j]);
80 for (
int i = 0; i < NROWS; ++i) {
81 if (
auto m = median(toneSamples[i]); m) {
95 if (toneNoise && debug) {
96 std::ostringstream oss;
99 for (
auto const value : *toneNoise)
102 qCDebug(decoder_js8).noquote() << oss.str().c_str();
107 auto const symbolNoise = [&]() -> std::optional<std::vector<float>> {
108 std::vector<float> noise;
111 for (
int j = 0; j < ND; ++j) {
112 std::vector<float> bins;
113 bins.reserve(NROWS - 1);
115 int const winner = symbolWinners[j];
117 for (
int i = 0; i < NROWS; ++i) {
119 bins.push_back(s1[i][j]);
122 if (
auto m = median(bins); m) {
132 if (symbolNoise && !symbolNoise->empty() && debug) {
133 auto const [minIt, maxIt] =
134 std::minmax_element(symbolNoise->begin(), symbolNoise->end());
135 float const avg = std::accumulate(symbolNoise->begin(),
136 symbolNoise->end(), 0.0f) /
137 static_cast<float>(symbolNoise->size());
140 <<
"symbolNoise avg/min/max" << avg << *minIt << *maxIt;
145 bool const disableWhitening =
146 std::getenv(
"JS8_DISABLE_WHITENING") !=
nullptr;
147 bool const whiteningAvailable = toneNoise && symbolNoise &&
148 !symbolNoise->empty() &&
150 bool const applyErasureInWhitening =
151 whiteningAvailable && erasureThreshold > 0.0f;
152 double sumAbsPre = 0.0;
153 double sumAbsPost = 0.0;
154 std::size_t erasures = 0;
156 for (
int j = 0; j < ND; ++j) {
157 int const i1 = 3 * j;
158 int const i2 = 3 * j + 1;
159 int const i4 = 3 * j + 2;
161 std::array<float, NROWS> ps;
163 for (
int i = 0; i < NROWS; ++i)
167 result.llr0[i1] = std::max({ps[4], ps[5], ps[6], ps[7]}) -
168 std::max({ps[0], ps[1], ps[2], ps[3]});
169 result.llr0[i2] = std::max({ps[2], ps[3], ps[6], ps[7]}) -
170 std::max({ps[0], ps[1], ps[4], ps[5]});
171 result.llr0[i4] = std::max({ps[1], ps[3], ps[5], ps[7]}) -
172 std::max({ps[0], ps[2], ps[4], ps[6]});
175 x = std::log(x + 1e-32f);
178 result.llr1[i1] = std::max({ps[4], ps[5], ps[6], ps[7]}) -
179 std::max({ps[0], ps[1], ps[2], ps[3]});
180 result.llr1[i2] = std::max({ps[2], ps[3], ps[6], ps[7]}) -
181 std::max({ps[0], ps[1], ps[4], ps[5]});
182 result.llr1[i4] = std::max({ps[1], ps[3], ps[5], ps[7]}) -
183 std::max({ps[0], ps[2], ps[4], ps[6]});
185 if (whiteningAvailable) {
186 int const winner = symbolWinners[j];
187 float const tn = std::max(0.0f, (*toneNoise)[winner]);
188 float const sn = std::max(0.0f, (*symbolNoise)[j]);
189 float const localNoise = std::sqrt(tn * sn + 1e-12f);
191 auto const applyWhitening = [&](
float &value) {
192 float const pre = std::abs(value);
195 if (localNoise > 0.0f && std::isfinite(localNoise)) {
199 if (applyErasureInWhitening &&
200 std::abs(value) < erasureThreshold) {
205 sumAbsPost += std::abs(value);
208 applyWhitening(result.llr0[i1]);
209 applyWhitening(result.llr0[i2]);
210 applyWhitening(result.llr0[i4]);
211 applyWhitening(result.llr1[i1]);
212 applyWhitening(result.llr1[i2]);
213 applyWhitening(result.llr1[i4]);
217 auto const normalizeLLR = [](
auto &llr) {
219 float sum_of_squares = 0.0f;
221 for (
auto const value : llr) {
223 sum_of_squares += value * value;
226 float const llrav = sum / llr.size();
227 float const llr2av = sum_of_squares / llr.size();
228 float const variance = llr2av - llrav * llrav;
229 float const llrsig = std::sqrt(variance > 0.0f ? variance : llr2av);
231 for (
float &val : llr)
232 val = (val / llrsig) * 2.83f;
237 normalizeLLR(result.llr0);
238 normalizeLLR(result.llr1);
240 if (whiteningAvailable && debug) {
242 static_cast<double>(result.llr0.size() + result.llr1.size());
243 double const avgPre = total > 0.0 ? sumAbsPre / total : 0.0;
244 double const avgPost = total > 0.0 ? sumAbsPost / total : 0.0;
246 qCDebug(decoder_js8) <<
"LLR whitening applied"
247 <<
"avg|LLR| pre/post:" << avgPre << avgPost
248 <<
"erasures:" << erasures;
251 result.whiteningApplied = whiteningAvailable;
252 result.erasureApplied = applyErasureInWhitening;
253 result.erasures = erasures;
254 result.avgAbsPre = sumAbsPre;
255 result.avgAbsPost = sumAbsPost;