62inline constexpr int kTimingDeltaMax = 2;
66inline constexpr double kFreqStepHz = 0.1;
67inline constexpr int kFreqSteps = 11;
71inline constexpr int kDriftSteps =
72 static_cast<int>(
sizeof(kDriftValues) /
sizeof(kDriftValues[0]));
96 return std::getenv(
"JS8_DISABLE_REDEMOD") ==
nullptr;
106 return (k >= 0 && k < 7) || (k >= 36 && k < 43) || (k >= 72 && k < 79);
111 return j < 29 ? j + 7 : j + 14;
123 std::array<int, kTotalSymbols> &tones) {
124 for (
int block = 0; block < 3; ++block)
125 for (
int column = 0; column < 7; ++column)
126 tones[
static_cast<std::size_t
>(block * 36 + column)] =
127 costas[
static_cast<std::size_t
>(block)]
128 [
static_cast<std::size_t
>(column)];
130 int const b0 = cw[
static_cast<std::size_t
>(3 * j)] != 0 ? 1 : 0;
131 int const b1 = cw[
static_cast<std::size_t
>(3 * j + 1)] != 0 ? 1 : 0;
132 int const b2 = cw[
static_cast<std::size_t
>(3 * j + 2)] != 0 ? 1 : 0;
134 (b0 << 2) | (b1 << 1) | b2;
158 bool haveMin =
false;
160 float const v0 = llr[
static_cast<std::size_t
>(3 * j)];
161 float const v1 = llr[
static_cast<std::size_t
>(3 * j + 1)];
162 float const v2 = llr[
static_cast<std::size_t
>(3 * j + 2)];
164 if (std::isfinite(v0) && std::isfinite(v1) && std::isfinite(v2)) {
165 double const minAbs = std::min(
166 {std::abs(
static_cast<double>(v0)),
167 std::abs(
static_cast<double>(v1)),
168 std::abs(
static_cast<double>(v2))});
169 if (!haveMin || minAbs < out.
minAbsMin) {
176 weight = std::clamp(weight, 0.0, 1.0);
179 out.
weights[
static_cast<std::size_t
>(j)] =
180 static_cast<float>(weight);
197 std::array<float, kTotalSymbols> &weights) {
200 weights[
static_cast<std::size_t
>(k)] = 1.0f;
204 int const j = k < 36 ? k - 7 : k - 14;
205 weights[
static_cast<std::size_t
>(k)] =
207 ? conf.
weights[
static_cast<std::size_t
>(j)]
222 weights[
static_cast<std::size_t
>(k)] =
237 float dtSecondsPerSample) {
238 if (!std::isfinite(xdt2) || !std::isfinite(dtSecondsPerSample))
239 return std::numeric_limits<float>::quiet_NaN();
240 return xdt2 +
static_cast<float>(aidedDt) * dtSecondsPerSample;
245 double residualHz = std::numeric_limits<double>::quiet_NaN();
246 double midpointSeconds = std::numeric_limits<double>::quiet_NaN();
247 double trackerHzAtMid = std::numeric_limits<double>::quiet_NaN();
289 double aidedDf,
double aidedDd,
290 std::array<SymbolBaseline, kTotalSymbols>
const &baselines,
int window,
291 double sampleRateHz, std::array<float, kTotalSymbols>
const &weights) {
293 if (!std::isfinite(aidedDf) || !std::isfinite(aidedDd) || window <= 0 ||
294 !std::isfinite(sampleRateHz) || !(sampleRateHz > 0.0))
296 double tMin = std::numeric_limits<double>::infinity();
297 double tMax = -std::numeric_limits<double>::infinity();
298 double sw = 0.0, swt = 0.0, swtt = 0.0, swr = 0.0, swtr = 0.0;
300 float const w = weights[
static_cast<std::size_t
>(k)];
301 if (!(w > 0.0f) || !std::isfinite(w))
303 double const tracker =
304 static_cast<double>(baselines[
static_cast<std::size_t
>(k)]
307 static_cast<double>(baselines[
static_cast<std::size_t
>(k)]
309 if (!std::isfinite(tracker) || !std::isfinite(start))
311 double const t = (start + window * 0.5) / sampleRateHz;
312 double const r = aidedDf + aidedDd * t - tracker;
313 if (!std::isfinite(t) || !std::isfinite(r))
315 tMin = std::min(tMin, t);
316 tMax = std::max(tMax, t);
317 double const dw =
static_cast<double>(w);
324 if (!(sw > 0.0) || !std::isfinite(sw) || !(tMax >= tMin) ||
325 !std::isfinite(tMin) || !std::isfinite(tMax))
327 double const tMid = 0.5 * (tMin + tMax);
328 double residual = std::numeric_limits<double>::quiet_NaN();
329 double const det = sw * swtt - swt * swt;
330 if (std::isfinite(det) && det > 0.0) {
331 double const a = (swr * swtt - swt * swtr) / det;
332 double const b = (sw * swtr - swt * swr) / det;
333 residual = a + b * tMid;
337 if (!std::isfinite(residual))
339 out.residualHz = residual;
340 out.midpointSeconds = tMid;
341 out.trackerHzAtMid = aidedDf + aidedDd * tMid - residual;
355 int window,
int numSamples,
int maxAbsDelta) {
356 if (window <= 0 || numSamples <= 0 || maxAbsDelta < 0)
358 for (
auto const &base : baselines) {
359 long const first =
static_cast<long>(base.startSamples) - maxAbsDelta;
361 static_cast<long>(base.startSamples) + maxAbsDelta;
362 if (first < 0 || last < 0)
364 if (last + window >
static_cast<long>(numSamples))
384 double trackerHz,
double sampleRateHz) {
385 if (window ==
nullptr || count <= 0 || !std::isfinite(trackerHz) ||
386 !std::isfinite(sampleRateHz) || !(sampleRateHz > 0.0))
388 double const dphi = 2.0 * std::numbers::pi * (trackerHz / sampleRateHz);
389 auto const wstep = std::polar(1.0f,
static_cast<float>(dphi));
390 auto w = std::complex<float>{1.0f, 0.0f};
391 for (
int i = 0; i < count; ++i) {
392 if (!std::isfinite(window[i].real()) ||
393 !std::isfinite(window[i].imag()))
411 double const *otherPowers) {
412 if (otherPowers ==
nullptr || !std::isfinite(expectedPower) ||
414 return std::numeric_limits<double>::quiet_NaN();
416 for (
int t = 0; t <
kTones - 1; ++t) {
417 if (!std::isfinite(otherPowers[t]) || otherPowers[t] < 0.0)
418 return std::numeric_limits<double>::quiet_NaN();
419 sum += otherPowers[t];
421 return expectedPower - sum / (
kTones - 1);
429 double metric = std::numeric_limits<double>::quiet_NaN();
435 double baselineMetric = std::numeric_limits<double>::quiet_NaN();
436 bool baselineFinite =
false;
465 std::array<SymbolBaseline, kTotalSymbols>
const
467 int window,
double sampleRateHz,
468 std::array<int, kTotalSymbols>
const &tones,
469 std::array<float, kTotalSymbols>
const &weights,
470 std::function<
bool()>
const &shouldCancel = {}) {
472 auto const stop = [&] {
473 if (!shouldCancel || !shouldCancel())
475 out.cancelled =
true;
476 out.searched =
false;
481 if (samples ==
nullptr || numSamples <= 0 || window <= 0 || window > 32 ||
482 !std::isfinite(sampleRateHz) || !(sampleRateHz > 0.0))
487 if (tones[
static_cast<std::size_t
>(k)] < 0 ||
488 tones[
static_cast<std::size_t
>(k)] >=
kTones)
490 float const w = weights[
static_cast<std::size_t
>(k)];
491 if (!std::isfinite(w) || w < 0.0f)
497 static_cast<double>(baselines[
static_cast<std::size_t
>(k)]
504 constexpr double twoPi = 2.0 * std::numbers::pi;
507 for (
int tone = 0; tone <
kTones; ++tone) {
508 for (
int n = 0; n < window; ++n) {
510 -twoPi *
static_cast<double>(tone * n) / window;
511 twRe[tone][n] = std::cos(angle);
512 twIm[tone][n] = std::sin(angle);
516#ifndef JS8_BENCHMARK_UNOPTIMIZED_HOTSPOTS
518 struct CachedWindow {
519 std::array<std::complex<float>, 32> samples{};
525 std::vector<CachedWindow> correctedWindows(
527 auto const cached = [&](
int dt,
int k) -> CachedWindow & {
528 return correctedWindows[
static_cast<std::size_t
>(
531 auto const cacheTiming = [&](
int dt) {
533 if (!(weights[
static_cast<std::size_t
>(k)] > 0.0f))
536 baselines[
static_cast<std::size_t
>(k)].startSamples + dt;
537 auto &entry = cached(dt, k);
538 for (
int n = 0; n < window; ++n)
539 entry.samples[
static_cast<std::size_t
>(n)] = samples[start + n];
541 entry.samples.data(), window,
542 baselines[
static_cast<std::size_t
>(k)].trackerHz,
552 auto const scoreHypothesis = [&](
int dt,
double df,
double dd) {
554#ifdef JS8_BENCHMARK_UNOPTIMIZED_HOTSPOTS
555 std::complex<float> replayed[32];
558 float const w = weights[
static_cast<std::size_t
>(k)];
562 baselines[
static_cast<std::size_t
>(k)].startSamples + dt;
563#ifdef JS8_BENCHMARK_UNOPTIMIZED_HOTSPOTS
564 for (
int n = 0; n < window; ++n)
565 replayed[n] = samples[start + n];
568 baselines[
static_cast<std::size_t
>(k)].trackerHz,
571 auto const &entry = cached(dt, k);
575 double binRe[
kTones] = {};
576 double binIm[
kTones] = {};
577 for (
int n = 0; n < window; ++n) {
579 static_cast<double>(start + n) / sampleRateHz;
581 twoPi * df * t + std::numbers::pi * dd * t * t;
582 double const c = std::cos(angle);
583 double const s = std::sin(angle);
584#ifdef JS8_BENCHMARK_UNOPTIMIZED_HOTSPOTS
585 double const yr =
static_cast<double>(replayed[n].real()) * c +
586 static_cast<double>(replayed[n].imag()) * s;
587 double const yi =
static_cast<double>(replayed[n].imag()) * c -
588 static_cast<double>(replayed[n].real()) * s;
591 static_cast<double>(entry.samples[n].real()) * c +
592 static_cast<double>(entry.samples[n].imag()) * s;
594 static_cast<double>(entry.samples[n].imag()) * c -
595 static_cast<double>(entry.samples[n].real()) * s;
597 for (
int tone = 0; tone <
kTones; ++tone) {
598 binRe[tone] += yr * twRe[tone][n] - yi * twIm[tone][n];
599 binIm[tone] += yi * twRe[tone][n] + yr * twIm[tone][n];
602 int const expected = tones[
static_cast<std::size_t
>(k)];
603 double const expPower =
604 binRe[expected] * binRe[expected] +
605 binIm[expected] * binIm[expected];
606 double others[
kTones - 1];
608 for (
int tone = 0; tone <
kTones; ++tone) {
609 if (tone == expected)
612 binRe[tone] * binRe[tone] + binIm[tone] * binIm[tone];
615 if (!std::isfinite(cont))
617 metric +=
static_cast<double>(w) * cont;
622#ifndef JS8_BENCHMARK_UNOPTIMIZED_HOTSPOTS
625 out.baselineMetric = scoreHypothesis(0, 0.0, 0.0);
626 out.baselineFinite = std::isfinite(out.baselineMetric);
627 if (!out.baselineFinite)
630 out.best.metric = out.baselineMetric;
634#ifndef JS8_BENCHMARK_UNOPTIMIZED_HOTSPOTS
640 for (
int dd = 0; dd < kDriftSteps; ++dd) {
641 for (
int f = 0; f < kFreqSteps; ++f) {
648 double const metric =
650 if (std::isfinite(metric) && metric > out.best.metric) {
651 out.best.deltaSamples = d;
652 out.best.deltaHz = df;
654 out.best.metric = metric;
672 for (
int i = 0; i < kFreqSteps; ++i)
677 f == kFreqSteps - 1);
693 if (!(r.
best.metric > r.baselineMetric))
695 if (r.baselineMetric > 0.0)
697 return r.
best.metric > 0.0;
702 if (!(refined > 0.0) || !(baseline > 0.0) ||
703 !std::isfinite(refined) || !std::isfinite(baseline))
705 return 10.0 * std::log10(refined / baseline);
709template <std::
size_t NBlades>
710inline double llrNorm(std::array<float, NBlades>
const &llr) {
712 for (
float v : llr) {
713 if (std::isfinite(v))
714 sum +=
static_cast<double>(v) * v;
716 return std::sqrt(sum);
constexpr double kMinMetricGain
Definition decoder_aided_redemod.h:78
constexpr double kFreqStartHz
Definition decoder_aided_redemod.h:65
constexpr int kDataSymbols
Definition decoder_aided_redemod.h:51
constexpr double kConfidenceFloor
Definition decoder_aided_redemod.h:81
constexpr int kCodeBits
Definition decoder_aided_redemod.h:49
Refinement refineSync(std::complex< float > const *samples, int numSamples, std::array< SymbolBaseline, kTotalSymbols > const &baselines, int window, double sampleRateHz, std::array< int, kTotalSymbols > const &tones, std::array< float, kTotalSymbols > const &weights, std::function< bool()> const &shouldCancel={})
Bounded local sync refinement around the first-pass solution.
Definition decoder_aided_redemod.h:463
void buildSymbolWeights(ConfidenceSummary const &conf, std::array< float, kTotalSymbols > &weights)
Expand 58 data confidences to 79 per-symbol weights.
Definition decoder_aided_redemod.h:196
void buildCostasWeights(std::array< float, kTotalSymbols > &weights)
Expand confidences to Costas-only weights (1.0 on pilots, else 0).
Definition decoder_aided_redemod.h:220
double llrNorm(std::array< float, NBlades > const &llr)
Definition decoder_aided_redemod.h:710
constexpr double kDriftValues[]
Definition decoder_aided_redemod.h:70
bool redemodEnabled()
Kill switch for decoder-aided re-demodulation.
Definition decoder_aided_redemod.h:95
MidpointResidual aidedPhysicalResidualAtMidpoint(double aidedDf, double aidedDd, std::array< SymbolBaseline, kTotalSymbols > const &baselines, int window, double sampleRateHz, std::array< float, kTotalSymbols > const &weights)
Physical carrier residual at the frame midpoint.
Definition decoder_aided_redemod.h:288
float aidedFrameXdt(float xdt2, int aidedDt, float dtSecondsPerSample)
Scalar frame timing seed from an aided timing delta.
Definition decoder_aided_redemod.h:236
ConfidenceSummary tentativeConfidences(std::array< float, kCodeBits > const &llr)
Confidence weight per tentative data symbol from its 3 bit LLRs.
Definition decoder_aided_redemod.h:155
bool symbolBoundsValid(std::array< SymbolBaseline, kTotalSymbols > const &baselines, int window, int numSamples, int maxAbsDelta)
Validate candidate sample bounds for the aided search/extraction.
Definition decoder_aided_redemod.h:354
bool refinementAccepted(Refinement const &r)
Safety gate: accept the refined sync only when it is finite, off the search boundary,...
Definition decoder_aided_redemod.h:687
constexpr int kTotalSymbols
Definition decoder_aided_redemod.h:53
double contrastEight(double expectedPower, double const *otherPowers)
Tone-CONTRAST metric from the actual 8 JS8 FSK bins.
Definition decoder_aided_redemod.h:410
constexpr int kTimingDeltaMin
Definition decoder_aided_redemod.h:61
constexpr double kConfidenceScale
Definition decoder_aided_redemod.h:83
std::array< std::array< int, 7 >, 3 > CostasTones
Definition decoder_aided_redemod.h:86
constexpr int kMaxSyndrome
Definition decoder_aided_redemod.h:58
bool replayTrackerCorrection(std::complex< float > *window, int count, double trackerHz, double sampleRateHz)
Replay one symbol's recorded FrequencyTracker correction.
Definition decoder_aided_redemod.h:383
constexpr int kTones
Definition decoder_aided_redemod.h:55
void codewordToTones(std::array< int8_t, kCodeBits > const &cw, CostasTones const &costas, std::array< int, kTotalSymbols > &tones)
Convert a tentative 174-bit codeword into 79 expected tones.
Definition decoder_aided_redemod.h:121
constexpr int dataSymbolIndex(int j)
Definition decoder_aided_redemod.h:110
constexpr bool isCostasSymbol(int k)
Whether global symbol k is a known Costas pilot.
Definition decoder_aided_redemod.h:105
double metricGainDb(double refined, double baseline)
Definition decoder_aided_redemod.h:701
JS8 namespace for Kalman filter-based trackers.
Definition bp_math.h:12
Definition decoder_aided_redemod.h:139
int used
Symbols with weight > 0.
Definition decoder_aided_redemod.h:141
std::array< float, kDataSymbols > weights
Per-symbol weight in [0,1].
Definition decoder_aided_redemod.h:140
double mean
Mean weight over USED symbols (0 if none).
Definition decoder_aided_redemod.h:142
double minAbsMin
Smallest min|LLR| observed (diagnostic).
Definition decoder_aided_redemod.h:143
Definition decoder_aided_redemod.h:425
double driftHzPerSec
Linear drift correction (Hz/s).
Definition decoder_aided_redemod.h:428
double deltaHz
Residual frequency correction (Hz).
Definition decoder_aided_redemod.h:427
int deltaSamples
Timing offset (downsampled samples).
Definition decoder_aided_redemod.h:426
Definition decoder_aided_redemod.h:244
Definition decoder_aided_redemod.h:433
bool cancelled
Incomplete searches can never be accepted.
Definition decoder_aided_redemod.h:438
Hypothesis best
Best hypothesis (baseline unless beaten strictly).
Definition decoder_aided_redemod.h:434
bool atBoundary
Best sits on the search box boundary.
Definition decoder_aided_redemod.h:439
bool searched
False when inputs were rejected up front.
Definition decoder_aided_redemod.h:437
First-pass per-symbol extraction metadata for the refinement baseline.
Definition decoder_aided_redemod.h:261
float trackerHz
Recorded tracker estimate for the symbol.
Definition decoder_aided_redemod.h:263
int startSamples
First-pass window start (base + shift).
Definition decoder_aided_redemod.h:262