/////////////////////////////////////////////////////////////////////////////////// // Copyright (C) 2026 Jon Beniston, M7RCE // // Some code by AI // // // // This program is free software; you can redistribute it and/or modify // // it under the terms of the GNU General Public License as published by // // the Free Software Foundation as version 3 of the License, or // // (at your option) any later version. // // // // This program is distributed in the hope that it will be useful, // // but WITHOUT ANY WARRANTY; without even the implied warranty of // // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // // GNU General Public License V3 for more details. // // // // You should have received a copy of the GNU General Public License // // along with this program. If not, see . // /////////////////////////////////////////////////////////////////////////////////// #include "drmfacacquisition.h" #include "drmcellmap.h" #include "drmchannelestimator.h" #include "drmofdm.h" #include #include #include namespace modemdrm { namespace { constexpr double WindowPlacementSmoothing = 0.1; constexpr double WindowPlacementSpanFraction = 0.8; bool coherentTransition(Mode mode, const FACData& current, const FACData& next) { if (current.occupancy != next.occupancy || current.modeE != next.modeE || current.mscMode != next.mscMode || current.sdcMode != next.sdcMode || current.legacyModeETail != next.legacyModeETail || current.shortInterleaving != next.shortInterleaving) { return false; } const bool nextIsFirst = next.identity == 0 || next.identity == 3; if (mode != Mode::E) { if (current.identity == 0 || current.identity == 3) { return next.identity == 1; } if (current.identity == 1) { return next.identity == 2; } if (current.identity == 2) { return nextIsFirst; } return false; } if (current.identity == 0 || current.identity == 3) { return !current.toggle && next.identity == 1 && next.toggle; } if (current.identity == 1 && current.toggle) { return next.identity == 1 && !next.toggle; } if (current.identity == 1 && !current.toggle) { return next.identity == 2 && next.toggle; } if (current.identity == 2) { return current.toggle && nextIsFirst && !next.toggle; } return false; } std::vector occupanciesForMode(Mode mode) { std::vector result; for (const SpectrumParameters& spectrum : Parameters::spectra()) { if (spectrum.mode == mode) { result.push_back(spectrum.occupancy); } } return result; } bool decodeTrackedFAC( const std::complex* samples, std::size_t sampleCount, int sampleRate, const AcquisitionCandidate& coarse, int occupancy, int integerCarrierOffset, bool spectralInversion, std::size_t frameStartOffsetSamples, const std::vector& previousBlocks, std::vector& blocks, std::vector& blockValid, int& validatedBlockCount ) { const Mode mode = coarse.mode; const ModeParameters& parameters = Parameters::forMode(mode); const int symbolsPerFrame = parameters.symbolsPerFrame; const int symbolSamples = parameters.symbolSamples(sampleRate); if (symbolSamples <= 0 || !Parameters::forSpectrum(mode, occupancy)) { return false; } const int blockCount = parameters.framesPerSuperframe; blockValid.assign(static_cast(blockCount), false); const int symbolCount = blockCount * symbolsPerFrame; OFDMDemodulator demodulator(mode, sampleRate); // Demodulated straight into frames, which the estimation context shares. std::vector>>> framedSpectra( static_cast(blockCount), std::vector>>( static_cast(symbolsPerFrame) ) ); for (int symbol = 0; symbol < symbolCount; ++symbol) { const std::size_t prefixStart = frameStartOffsetSamples + static_cast(symbol * symbolSamples); if (!demodulator.demodulate( samples, sampleCount, prefixStart, coarse.fractionalCarrierOffsetHz, framedSpectra[static_cast(symbol / symbolsPerFrame)][ static_cast(symbol % symbolsPerFrame)], spectralInversion)) { return false; } } std::vector facSymbols; for (const CellPosition& position : CellMap::facCells(mode)) { if (std::find(facSymbols.begin(), facSymbols.end(), position.symbol) == facSymbols.end()) { facSymbols.push_back(position.symbol); } } blocks.assign( static_cast(blockCount), FACData() ); std::vector haveBlock( static_cast(blockCount), false ); validatedBlockCount = 0; auto matchesSlot = [mode](const FACData& fac, int frame) { if (mode != Mode::E) { return frame == 0 ? fac.identity == 0 || fac.identity == 3 : fac.identity == frame; } if (frame == 0) { return (fac.identity == 0 || fac.identity == 3) && !fac.toggle; } if (frame == 3) { return fac.identity == 2 && fac.toggle; } return fac.identity == 1 && fac.toggle == (frame == 1); }; for (int frame = 0; frame < blockCount; ++frame) { const auto previous = std::find_if( previousBlocks.begin(), previousBlocks.end(), [=](const FACData& fac) { return matchesSlot(fac, frame); } ); if (previous != previousBlocks.end()) { blocks[static_cast(frame)] = *previous; haveBlock[static_cast(frame)] = true; } } // The frames share one window of pilot estimates, prepared on first use. ChannelEstimationContext estimation; int estimationState = 0; const auto estimationReady = [&]() { if (estimationState == 0) { estimationState = estimation.prepare( mode, occupancy, 0, integerCarrierOffset, framedSpectra) ? 1 : -1; } return estimationState > 0; }; for (int frame = 0; frame < blockCount; ++frame) { std::vector equalized( static_cast(symbolsPerFrame) ); if (mode == Mode::E) { // The FAC decoder reads only the FAC symbols. for (int facSymbol : facSymbols) { if (!ChannelEstimator::estimate( mode, occupancy, facSymbol, 1, integerCarrierOffset, framedSpectra[static_cast(frame)][static_cast(facSymbol)], equalized[static_cast(facSymbol)])) { return false; } } } else { bool estimated = estimationReady() && estimation.estimateFrameWiener(frame, equalized); if (!estimated) { for (int facSymbol : facSymbols) { if (!ChannelEstimator::estimate( mode, occupancy, facSymbol, frame % parameters.framesPerSuperframe, integerCarrierOffset, framedSpectra[static_cast(frame)][static_cast(facSymbol)], equalized[static_cast(facSymbol)])) { return false; } } } } FACData fac; if (!FACDecoder::decode(mode, equalized, fac, nullptr, mode == Mode::E)) { // DRM+ selective channels can reject the single-symbol linear // FAC estimate even while the MSC's pilot-context estimate works. // Retry only rejected blocks, using the known superframe phase and // the same bounded temporal/DFT estimator used by the MSC path. if (mode != Mode::E || !estimationReady() || !estimation.estimateFrame(frame, equalized, 3) || !FACDecoder::decode(mode, equalized, fac, nullptr, true)) { continue; } } if (fac.occupancy != occupancy || !matchesSlot(fac, frame)) { continue; } const FACData& reference = blocks.front(); if (frame > 0 && haveBlock.front() && (fac.modeE != reference.modeE || fac.mscMode != reference.mscMode || fac.sdcMode != reference.sdcMode || fac.legacyModeETail != reference.legacyModeETail || fac.shortInterleaving != reference.shortInterleaving)) { continue; } blocks[static_cast(frame)] = std::move(fac); blockValid[static_cast(frame)] = true; haveBlock[static_cast(frame)] = true; ++validatedBlockCount; } // Initial acquisition still requires three consecutive coherent blocks. // Tracking has an established epoch: two fresh coherent observations may // bridge erased slots. The receiver's per-frame loss hysteresis decides // whether lock survives; cached slots never become fresh signalling. return validatedBlockCount >= 2 && std::all_of(haveBlock.begin(), haveBlock.end(), [](bool have) { return have; }); } bool passbandLimited(const FACSearchOptions& options) { return options.passbandLowHz < options.passbandHighHz; } // The orientations to try, the most likely first. std::vector orientations(const FACSearchOptions& options) { if (options.spectralInversion >= 0) { return {options.spectralInversion != 0}; } const bool first = options.haveHint && options.hintSpectralInversion; return {first, !first}; } // The options with one orientation resolved. FACSearchOptions oriented(const FACSearchOptions& options, bool inversion) { FACSearchOptions result = options; result.spectralInversion = inversion ? 1 : 0; return result; } std::vector candidateOccupancies(Mode mode, const FACSearchOptions& options) { std::vector result; for (int occupancy : occupanciesForMode(mode)) { if (options.forcedOccupancy >= 0 && occupancy != options.forcedOccupancy) { continue; } if (passbandLimited(options) && !Parameters::spectrumWithinBand(mode, occupancy, options.passbandLowHz, options.passbandHighHz, options.configuredSearchRangeHz, options.spectralInversion == 1)) { continue; } result.push_back(occupancy); } return result; } // FAC hypothesis search for one robustness mode and orientation: the ranked // candidates, or (exhaustive) the per-symbol search of every hypothesis. With // a preferred total carrier offset, integer hypotheses are tried nearest to // it first. FACAcquisitionResult searchMode( const std::complex* samples, std::size_t sampleCount, int sampleRate, const AcquisitionCandidate& coarse, const std::vector& occupancies, const FACSearchOptions& options, const double* preferredCarrierOffsetHz, bool exhaustive ); bool cancelled(const FACSearchOptions& options) { return options.cancel && options.cancel->load(); } } // namespace FACAcquisitionResult FACAcquisition::search( const std::complex* samples, std::size_t sampleCount, int sampleRate, int configuredSearchRangeHz, bool spectralInversion, int forcedMode, int forcedOccupancy ) { FACSearchOptions options; options.configuredSearchRangeHz = configuredSearchRangeHz; options.spectralInversion = spectralInversion ? 1 : 0; options.forcedMode = forcedMode; options.forcedOccupancy = forcedOccupancy; return search(samples, sampleCount, sampleRate, options); } FACAcquisitionResult FACAcquisition::search( const std::complex* samples, std::size_t sampleCount, int sampleRate, const FACSearchOptions& options ) { FACAcquisitionResult result; const int forcedMode = options.forcedMode; if (forcedMode < -1 || forcedMode > static_cast(Mode::E) || options.forcedOccupancy < -1 || options.forcedOccupancy > 5) { return result; } const std::size_t coarseSampleCount = sampleRate > 0 ? std::min( sampleCount, static_cast(CoarseAcquisitionSeconds) * static_cast(sampleRate) ) : sampleCount; const std::vector tried = orientations(options); // Reacquisition: the lost lock's mode, occupancy and orientation first. if (options.haveHint && options.hintMode >= 0 && options.hintMode <= static_cast(Mode::E) && (forcedMode < 0 || forcedMode == options.hintMode) && (options.spectralInversion < 0 || (options.spectralInversion == 1) == options.hintSpectralInversion)) { const Mode hintMode = static_cast(options.hintMode); const FACSearchOptions hinted = oriented(options, options.hintSpectralInversion); std::vector occupancies = candidateOccupancies(hintMode, hinted); if (std::find(occupancies.begin(), occupancies.end(), options.hintOccupancy) != occupancies.end()) { const AcquisitionCandidate coarse = Acquisition::evaluateMode( samples, coarseSampleCount, sampleRate, hintMode); if (coarse.valid) { // Ranked candidates only: the hint is for speed, and the full // search below covers the hinted occupancy exhaustively too. FACAcquisitionResult found = searchMode(samples, sampleCount, sampleRate, coarse, {options.hintOccupancy}, hinted, &options.hintCarrierOffsetHz, false); if (found.valid) { return found; } } } } if (cancelled(options)) { return result; } // Choose the mode by cyclic-prefix correlation, which mirroring does not // change, among modes the input passband can carry in some orientation: // a narrow capture cannot hold mode E, for example. const auto usable = [&](Mode mode) { return std::any_of(tried.begin(), tried.end(), [&](bool inversion) { return !candidateOccupancies(mode, oriented(options, inversion)).empty(); }); }; for (const ModeParameters& parameters : Parameters::modes()) { if (forcedMode >= 0 && static_cast(parameters.mode) != forcedMode) { continue; } if (!usable(parameters.mode)) { continue; } const AcquisitionCandidate candidate = Acquisition::evaluateMode( samples, coarseSampleCount, sampleRate, parameters.mode); if (forcedMode >= 0 || (candidate.valid && (!result.coarse.valid || candidate.cyclicPrefixCorrelation > result.coarse.cyclicPrefixCorrelation))) { result.coarse = candidate; } } if (!result.coarse.valid) { return result; } // Each orientation's ranked candidates, then the exhaustive search: a // mirrored spectrum decodes no coherent FAC, so trying it costs a search // but cannot lock on the normal one, and a normal signal locks on its // ranked candidates as when only it was tried. for (const bool exhaustive : {false, true}) { for (const bool inversion : tried) { if (cancelled(options)) { return result; } const FACSearchOptions attempt = oriented(options, inversion); FACAcquisitionResult found = searchMode(samples, sampleCount, sampleRate, result.coarse, candidateOccupancies(result.coarse.mode, attempt), attempt, nullptr, exhaustive); if (found.valid) { return found; } } } return result; } namespace { FACAcquisitionResult searchMode( const std::complex* samples, std::size_t sampleCount, int sampleRate, const AcquisitionCandidate& coarse, const std::vector& occupancies, const FACSearchOptions& options, const double* preferredCarrierOffsetHz, bool exhaustive ) { constexpr int RequiredConsecutiveBlocks = FACAcquisition::RequiredConsecutiveBlocks; const bool spectralInversion = options.spectralInversion == 1; FACAcquisitionResult result; result.coarse = coarse; if (occupancies.empty()) { return result; } result.spectralInversion = spectralInversion; if (spectralInversion) { result.coarse.fractionalCarrierOffsetHz = -result.coarse.fractionalCarrierOffsetHz; } const Mode mode = result.coarse.mode; const ModeParameters& parameters = Parameters::forMode(mode); const int symbolsPerFrame = parameters.symbolsPerFrame; const int symbolSamples = parameters.symbolSamples(sampleRate); if (symbolSamples <= 0) { return result; } // Every candidate frame start needs its required blocks; one frame more, // where the input has it, lets the block after them be checked too. const int requiredSymbolCount = (RequiredConsecutiveBlocks + 1) * symbolsPerFrame; OFDMDemodulator demodulator(mode, sampleRate); std::vector>> spectra( static_cast(requiredSymbolCount + symbolsPerFrame) ); int demodulatedSymbolCount = 0; for (int symbol = 0; symbol < requiredSymbolCount + symbolsPerFrame; ++symbol) { const std::size_t prefixStart = static_cast( result.coarse.symbolStartOffsetSamples + symbol * symbolSamples ); if (!demodulator.demodulate( samples, sampleCount, prefixStart, result.coarse.fractionalCarrierOffsetHz, spectra[static_cast(symbol)], spectralInversion)) { if (symbol < requiredSymbolCount) { return result; } break; } demodulatedSymbolCount = symbol + 1; } spectra.resize(static_cast(demodulatedSymbolCount)); std::vector facSymbols; for (const CellPosition& position : CellMap::facCells(mode)) { if (std::find(facSymbols.begin(), facSymbols.end(), position.symbol) == facSymbols.end()) { facSymbols.push_back(position.symbol); } } // Three coherent blocks can still straddle a discontinuity (a dropped // frame, say) that the next block reveals. When the input holds the next // block and it decodes, it must continue the sequence. const auto nextBlockContradicts = [&](int integerOffset, int occupancy, int frameStart, const FACData& last) { const int frame = RequiredConsecutiveBlocks; if (frameStart + (frame + 1) * symbolsPerFrame > demodulatedSymbolCount) { return false; } std::vector equalized(static_cast(symbolsPerFrame)); for (int symbol = 0; symbol < symbolsPerFrame; ++symbol) { if (std::find(facSymbols.begin(), facSymbols.end(), symbol) == facSymbols.end()) { continue; } if (!ChannelEstimator::estimate(mode, occupancy, symbol, mode == Mode::E ? 1 : frame % parameters.framesPerSuperframe, integerOffset, spectra[static_cast(frameStart + frame * symbolsPerFrame + symbol)], equalized[static_cast(symbol)])) { return false; } } FACData next; return FACDecoder::decode(mode, equalized, next, nullptr, mode == Mode::E) && next.occupancy == occupancy && !coherentTransition(mode, last, next); }; std::vector integerHypotheses = Acquisition::integerCarrierHypotheses( mode, result.coarse.fractionalCarrierOffsetHz, options.configuredSearchRangeHz ); if (preferredCarrierOffsetHz) { const double spacing = parameters.carrierSpacingHz(); const double preferred = (*preferredCarrierOffsetHz - result.coarse.fractionalCarrierOffsetHz) / spacing; std::stable_sort(integerHypotheses.begin(), integerHypotheses.end(), [preferred](int left, int right) { return std::abs(left - preferred) < std::abs(right - preferred); }); } // One stage per call: the ranked candidates, or the exhaustive search. if (!exhaustive) { // First rank integer offset, occupancy and frame start by gain-pilot // coherence and decode FAC for the best few only, equalizing with the // time/frequency estimator over the candidate's frames. Each pilot is // correlated with the same carrier one pilot period later: on the // right hypothesis the products share a phase however frequency // selective the channel, and on a wrong one they do not. The // exhaustive per-symbol search below remains the fallback. const int period = mode == Mode::A ? 5 : mode == Mode::B ? 3 : mode == Mode::C ? 2 : mode == Mode::D ? 3 : 4; const int bins = static_cast(spectra.front().size()); const int pairedSymbols = demodulatedSymbolCount - period; // Bin products of each symbol with the one a pilot period later. std::vector>> products(static_cast(pairedSymbols)); for (int j = 0; j < pairedSymbols; ++j) { const auto& first = spectra[static_cast(j)]; const auto& second = spectra[static_cast(j + period)]; auto& product = products[static_cast(j)]; product.resize(static_cast(bins)); for (int bin = 0; bin < bins; ++bin) { product[static_cast(bin)] = first[static_cast(bin)] * std::conj(second[static_cast(bin)]); } } struct Candidate { double coherence; int integerOffset; int occupancy; int frameStart; }; std::vector candidates; const int framePhase = mode == Mode::E ? 1 : 0; const auto unitPilot = [](const PilotCell& cell) { const double phase = 2.0 * 3.14159265358979323846 * cell.phase1024 / 1024.0; return std::complex(static_cast(std::cos(phase)), static_cast(std::sin(phase))); }; for (int occupancy : occupancies) { // For each symbol of the frame: gain pilot carriers present in it // and a pilot period later, with conj(P(s)) P(s + period). std::vector>>> expected( static_cast(symbolsPerFrame)); for (int symbol = 0; symbol < symbolsPerFrame; ++symbol) { const auto& now = CellMap::referenceCells(mode, occupancy, symbol, framePhase); const auto& later = CellMap::referenceCells( mode, occupancy, (symbol + period) % symbolsPerFrame, framePhase); for (const PilotCell& cell : now) { if (cell.kind != ReferenceKind::Gain) { continue; } const auto match = std::find_if(later.begin(), later.end(), [&cell](const PilotCell& other) { return other.carrier == cell.carrier && other.kind == ReferenceKind::Gain; }); if (match == later.end()) { continue; } expected[static_cast(symbol)].push_back( {cell.carrier, std::conj(unitPilot(cell)) * unitPilot(*match)}); } } for (int integerOffset : integerHypotheses) { for (int frameStart = 0; frameStart < symbolsPerFrame; ++frameStart) { std::complex sum(0.0, 0.0); double magnitude = 0.0; for (int j = 0; j < pairedSymbols; ++j) { const int symbol = ((j - frameStart) % symbolsPerFrame + symbolsPerFrame) % symbolsPerFrame; const auto& product = products[static_cast(j)]; for (const auto& pilot : expected[static_cast(symbol)]) { int bin = (pilot.first + integerOffset) % bins; if (bin < 0) { bin += bins; } // Y(s) conj(Y(s+p)) = |H|^2 P(s) conj(P(s+p)) on the right hypothesis. const std::complex value = product[static_cast(bin)]; sum += std::complex(value * pilot.second); magnitude += std::abs(value); } } if (magnitude > 1.0e-20) { candidates.push_back({std::abs(sum) / magnitude, integerOffset, occupancy, frameStart}); } } } } // Stable, so that equal coherences keep the hypothesis order (nearest // the hinted offset first) on every platform. std::stable_sort(candidates.begin(), candidates.end(), [](const Candidate& left, const Candidate& right) { return left.coherence > right.coherence; }); constexpr std::size_t RankedCandidates = 6; // The time/frequency context needs the candidate's frames (A-D only); // candidates sharing a frame start share the copy. std::map>>>> framedByStart; for (std::size_t rank = 0; rank < std::min(RankedCandidates, candidates.size()); ++rank) { if (cancelled(options)) { return result; } const Candidate& candidate = candidates[rank]; auto& framed = framedByStart[candidate.frameStart]; if (mode != Mode::E && framed.empty()) { framed.assign(static_cast(RequiredConsecutiveBlocks), std::vector>>(static_cast(symbolsPerFrame))); for (int frame = 0; frame < RequiredConsecutiveBlocks; ++frame) { for (int symbol = 0; symbol < symbolsPerFrame; ++symbol) { framed[static_cast(frame)][static_cast(symbol)] = spectra[static_cast(candidate.frameStart + frame * symbolsPerFrame + symbol)]; } } } ChannelEstimationContext estimation; const bool robust = mode != Mode::E && estimation.prepare( mode, candidate.occupancy, 0, candidate.integerOffset, framed); std::vector blocks; bool candidateValid = true; for (int frame = 0; frame < RequiredConsecutiveBlocks && candidateValid; ++frame) { std::vector equalized(static_cast(symbolsPerFrame)); const bool estimated = robust && estimation.estimateFrameWiener(frame, equalized); for (std::size_t index = 0; !estimated && index < facSymbols.size(); ++index) { const int symbol = facSymbols[index]; if (!ChannelEstimator::estimate(mode, candidate.occupancy, symbol, mode == Mode::E ? 1 : frame % parameters.framesPerSuperframe, candidate.integerOffset, spectra[static_cast(candidate.frameStart + frame * symbolsPerFrame + symbol)], equalized[static_cast(symbol)])) { candidateValid = false; break; } } FACData fac; const bool decoded = candidateValid && FACDecoder::decode(mode, equalized, fac, nullptr, mode == Mode::E); if (decoded && fac.occupancy == candidate.occupancy && (blocks.empty() || coherentTransition(mode, blocks.back(), fac))) { blocks.push_back(fac); } else { candidateValid = false; } } if (candidateValid && blocks.size() == RequiredConsecutiveBlocks && !nextBlockContradicts(candidate.integerOffset, candidate.occupancy, candidate.frameStart, blocks.back())) { result.valid = true; result.occupancy = candidate.occupancy; result.integerCarrierOffset = candidate.integerOffset; result.frameStartOffsetSamples = static_cast( result.coarse.symbolStartOffsetSamples + candidate.frameStart * symbolSamples ); result.validatedFACBlockCount = RequiredConsecutiveBlocks; result.facBlockValid.assign(RequiredConsecutiveBlocks, true); result.facBlocks = std::move(blocks); return result; } } } if (!exhaustive) { return result; } for (int integerOffset : integerHypotheses) { if (cancelled(options)) { return result; } for (int occupancy : occupancies) { for (int frameStart = 0; frameStart < symbolsPerFrame; ++frameStart) { std::vector blocks; bool candidateValid = true; for (int frame = 0; frame < RequiredConsecutiveBlocks && candidateValid; ++frame) { std::vector equalized( static_cast(symbolsPerFrame) ); if (mode == Mode::E) { for (int frameSymbol : facSymbols) { const int observedSymbol = frameStart + frame * symbolsPerFrame + frameSymbol; if (!ChannelEstimator::estimate( mode, occupancy, frameSymbol, 1, integerOffset, spectra[static_cast(observedSymbol)], equalized[static_cast(frameSymbol)])) { candidateValid = false; break; } } } else { for (int facSymbol : facSymbols) { const int observedSymbol = frameStart + frame * symbolsPerFrame + facSymbol; if (!ChannelEstimator::estimate( mode, occupancy, facSymbol, frame % parameters.framesPerSuperframe, integerOffset, spectra[static_cast(observedSymbol)], equalized[static_cast(facSymbol)])) { candidateValid = false; break; } } } FACData fac; const bool decoded = candidateValid && FACDecoder::decode( mode, equalized, fac, nullptr, mode == Mode::E ); if (decoded && fac.occupancy == occupancy) { if (!blocks.empty() && !coherentTransition(mode, blocks.back(), fac)) { candidateValid = false; } else { blocks.push_back(fac); } } else { candidateValid = false; } } if (candidateValid && blocks.size() == RequiredConsecutiveBlocks && !nextBlockContradicts(integerOffset, occupancy, frameStart, blocks.back())) { result.valid = true; result.occupancy = occupancy; result.integerCarrierOffset = integerOffset; result.frameStartOffsetSamples = static_cast( result.coarse.symbolStartOffsetSamples + frameStart * symbolSamples ); result.validatedFACBlockCount = RequiredConsecutiveBlocks; result.facBlockValid.assign(RequiredConsecutiveBlocks, true); result.facBlocks = std::move(blocks); return result; } } } } return result; } } // namespace FACAcquisitionResult FACAcquisition::searchWindow( const std::complex* samples, std::size_t sampleCount, int sampleRate, int configuredSearchRangeHz, bool spectralInversion, int maximumStartOffsetSeconds, int forcedMode, int forcedOccupancy ) { FACSearchOptions options; options.configuredSearchRangeHz = configuredSearchRangeHz; options.spectralInversion = spectralInversion ? 1 : 0; options.forcedMode = forcedMode; options.forcedOccupancy = forcedOccupancy; return searchWindow(samples, sampleCount, sampleRate, maximumStartOffsetSeconds, options); } FACAcquisitionResult FACAcquisition::searchWindow( const std::complex* samples, std::size_t sampleCount, int sampleRate, int maximumStartOffsetSeconds, const FACSearchOptions& options ) { FACAcquisitionResult result; if (!samples || sampleRate <= 0 || maximumStartOffsetSeconds < 0) { return result; } for (int seconds = 0; seconds <= maximumStartOffsetSeconds; ++seconds) { const std::size_t offset = static_cast(seconds) * static_cast(sampleRate); if (offset >= sampleCount || cancelled(options)) { break; } result = search(samples + offset, sampleCount - offset, sampleRate, options); if (result.valid) { result.coarse.symbolStartOffsetSamples += static_cast(offset); result.frameStartOffsetSamples += offset; return result; } } return result; } FACAcquisitionResult FACAcquisition::track( const std::complex* samples, std::size_t sampleCount, int sampleRate, const FACAcquisitionResult& previous, std::size_t expectedFrameStartOffsetSamples ) { FACAcquisitionResult result; if (!samples || sampleRate <= 0 || !previous.valid || previous.facBlocks.empty()) { return result; } const Mode mode = previous.coarse.mode; const ModeParameters& parameters = Parameters::forMode(mode); const int symbolSamples = parameters.symbolSamples(sampleRate); if (symbolSamples <= 0 || expectedFrameStartOffsetSamples >= sampleCount) { return result; } // Measure timing over up to a second from a guard before the expected // frame, not from older history the caller keeps before it (A-D captures // hold about a second there; a steady mode E capture about 0.4 s). const int guardSamples = parameters.guardSamples(sampleRate); const std::size_t windowStart = expectedFrameStartOffsetSamples > static_cast(guardSamples) ? expectedFrameStartOffsetSamples - static_cast(guardSamples) : 0U; const std::size_t trackingSampleCount = std::min( sampleCount - windowStart, static_cast(sampleRate) ); // Track on the guard window of greatest correlation, not the first path // (acquisition aligns to that): per-superframe first-path estimates wander // with the noise on the profile's leading edge, and timing on the first // path cost superframes on the mode E sweeps and Kuwait 10:02 for a small // gain on simulated HF channels. result.coarse = Acquisition::evaluateMode( samples + windowStart, trackingSampleCount, sampleRate, mode, false ); if (!result.coarse.valid) { return result; } result.spectralInversion = previous.spectralInversion; if (result.spectralInversion) { result.coarse.fractionalCarrierOffsetHz = -result.coarse.fractionalCarrierOffsetHz; } const int expectedPhase = static_cast( (expectedFrameStartOffsetSamples - windowStart) % static_cast(symbolSamples) ); // With echoes that narrow the span of FFT-window starts holding every // significant path, place the window a margin before that span's end // (half the span when it is short) instead of at the guard-window // maximum, which can leave the window a few samples from the first path's // next symbol (BBC 3955 kHz with a 2.5 ms echo: 1 to 2 dB of MSC MER). // Move a tenth of the way to each superframe's measurement: jumping to it // cost Kuwait 10:02 and RFmondial frames. A span over 80% of the guard is // a single path, where the guard-window maximum is kept (moving earlier // there cost Kuwait 10:02 3%). Not mode E: no gain on its sweeps. double placement = previous.windowPlacementSamples; if (mode != Mode::E && result.coarse.windowSpanSamples > 0) { const int guard = parameters.guardSamples(sampleRate); const int margin = std::min(guard / 8, result.coarse.windowSpanSamples / 2); int target = 0; if (result.coarse.windowSpanSamples < WindowPlacementSpanFraction * guard) { target = result.coarse.windowEndOffsetSamples - margin - (guard - OFDMDemodulator::backoffSamplesFor(guard)) - result.coarse.symbolStartOffsetSamples; target = ((target % symbolSamples) + symbolSamples) % symbolSamples; if (target > symbolSamples / 2) { target -= symbolSamples; } } placement += WindowPlacementSmoothing * (target - placement); placement = std::clamp(placement, -0.5 * guard, 0.25 * guard); } result.windowPlacementSamples = placement; int timingCorrection = result.coarse.symbolStartOffsetSamples + static_cast(std::lround(placement)) - expectedPhase; if (timingCorrection > symbolSamples / 2) { timingCorrection -= symbolSamples; } if (timingCorrection < -symbolSamples / 2) { timingCorrection += symbolSamples; } const std::int64_t correctedStart = static_cast( expectedFrameStartOffsetSamples ) + timingCorrection; // Callers retain a guard interval of history before the frame (A-D also // keep the preceding frame), so a correction earlier than that leaves a // deficit. One within the FFT-window backoff is absorbed by clamping (the // window still starts inside the guard); a larger one would misplace every // FFT window, so reject it. if (correctedStart < 0) { const std::int64_t tolerance = OFDMDemodulator::backoffSamplesFor(parameters.guardSamples(sampleRate)); if (-correctedStart > tolerance) { result.coarse.valid = false; return result; } } result.frameStartOffsetSamples = correctedStart > 0 ? static_cast(correctedStart) : 0U; result.coarse.symbolStartOffsetSamples = static_cast( result.frameStartOffsetSamples ); const double carrierSpacing = parameters.carrierSpacingHz(); const double previousCarrierOffset = previous.coarse.fractionalCarrierOffsetHz + previous.integerCarrierOffset * carrierSpacing; result.integerCarrierOffset = static_cast(std::llround( (previousCarrierOffset - result.coarse.fractionalCarrierOffsetHz) / carrierSpacing )); result.occupancy = previous.occupancy; int validatedBlockCount = 0; if (!decodeTrackedFAC( samples, sampleCount, sampleRate, result.coarse, result.occupancy, result.integerCarrierOffset, result.spectralInversion, result.frameStartOffsetSamples, previous.facBlocks, result.facBlocks, result.facBlockValid, validatedBlockCount)) { result.validatedFACBlockCount = validatedBlockCount; result.facBlocks.clear(); return result; } const FACData& first = result.facBlocks.front(); if ((first.identity != 0 && first.identity != 3) || (mode == Mode::E && first.toggle)) { result.facBlocks.clear(); return result; } result.validatedFACBlockCount = validatedBlockCount; result.valid = true; return result; } } // namespace modemdrm