/////////////////////////////////////////////////////////////////////////////////// // 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 "drmchannelestimator.h" #include "drmcellmap.h" #include "sdrbase/dsp/kissfft.h" #include #include #include #include #include #include #include #include #include #include namespace modemdrm { namespace { constexpr double Pi = 3.14159265358979323846; std::complex binForCarrier(const std::vector>& bins, int carrier) { const int size = static_cast(bins.size()); int index = carrier % size; if (index < 0) { index += size; } return bins[static_cast(index)]; } std::complex pilotValue(const PilotCell& pilot) { const double phase = 2.0 * Pi * pilot.phase1024 / 1024.0; return pilot.amplitude * std::complex( static_cast(std::cos(phase)), static_cast(std::sin(phase)) ); } // The transmitted value of each reference cell of a symbol, in // CellMap::referenceCells order, computed once per symbol position. const std::vector>& referenceValues( Mode mode, int occupancy, int symbol, int frameInSuperframe) { static std::mutex mutex; static std::map>> cache; const int key = ((static_cast(mode) * 8 + occupancy) * 64 + symbol) * 8 + frameInSuperframe; std::lock_guard lock(mutex); auto found = cache.find(key); if (found == cache.end()) { std::vector> values; for (const PilotCell& pilot : CellMap::referenceCells(mode, occupancy, symbol, frameInSuperframe)) { values.push_back(pilotValue(pilot)); } found = cache.emplace(key, std::move(values)).first; } return found->second; } using TimedPilot = ChannelPilotObservation; // Regression weights by symbol distance: temporalCorrelation^|time| for the // twelve symbols either side used below. using TemporalWeights = std::array; TemporalWeights temporalWeights(float temporalCorrelation) { TemporalWeights weights {}; for (int time = 0; time < static_cast(weights.size()); ++time) { weights[static_cast(time)] = std::pow( static_cast(temporalCorrelation), time ); } return weights; } // Removes the common phase advance between symbols (residual carrier offset) // relative to a target symbol. The rate comes from successive observations of // the same pilot carriers: per-symbol estimates interpolated across the band // alias on a frequency-selective channel, and correlating those gives // arbitrary phases. class PhaseCorrections { public: PhaseCorrections(double phaseRate, int targetSequenceSymbol) : m_phaseRate(phaseRate), m_target(targetSequenceSymbol) { } std::complex operator()(int sequenceSymbol) const { return std::polar(1.0f, static_cast(-m_phaseRate * (sequenceSymbol - m_target))); } private: double m_phaseRate; int m_target; }; bool invertMatrix(std::vector>& matrix, std::size_t size); // Wiener time interpolation: a Gaussian time correlation (fixed by its value // one pilot period apart) and a noise to channel power ratio. struct TimeWienerModel { double periodCorrelation = 1.0; int period = 1; double noiseRatio = 0.01; double at(int lag) const { const double x = static_cast(lag) / period; return std::pow(std::max(1.0e-6, periodCorrelation), x * x); } }; // Wiener time weights by the observations' times relative to the target. For // one model they depend on nothing else, so every pilot carrier and symbol // with the same pattern of observations shares them (an empty vector records // a singular system). using TimeWienerWeights = std::map, std::vector>>; std::complex estimateTimedPilot( const std::vector& observations, int targetSequenceSymbol, const PhaseCorrections* phaseCorrections = nullptr, const TemporalWeights* weights = nullptr, int window = 12, const TimeWienerModel* timeModel = nullptr, TimeWienerWeights* timeWeights = nullptr ) { const auto upper = std::lower_bound( observations.begin(), observations.end(), targetSequenceSymbol, [](const TimedPilot& observation, int symbol) { return observation.sequenceSymbol < symbol; } ); const auto corrected = [phaseCorrections](const TimedPilot& observation) { return observation.channel * (phaseCorrections ? (*phaseCorrections)(observation.sequenceSymbol) : std::complex(1.0f, 0.0f)); }; if (phaseCorrections && timeModel) { // Observations are chronological: only those within the window count. const auto first = std::lower_bound( observations.begin(), observations.end(), targetSequenceSymbol - window, [](const TimedPilot& observation, int symbol) { return observation.sequenceSymbol < symbol; } ); std::vector times; std::vector> values; for (auto observationIt = first; observationIt != observations.end(); ++observationIt) { const int time = observationIt->sequenceSymbol - targetSequenceSymbol; if (time > window) { break; } times.push_back(time); values.push_back(std::complex(corrected(*observationIt))); } const std::size_t size = times.size(); if (size >= 1U) { const auto solve = [&]() { std::vector> matrix(size * size); for (std::size_t i = 0; i < size; ++i) { for (std::size_t j = 0; j < size; ++j) { matrix[i * size + j] = timeModel->at(times[i] - times[j]); } matrix[i * size + i] += timeModel->noiseRatio; } std::vector> result; if (!invertMatrix(matrix, size)) { return result; } result.assign(size, std::complex(0.0, 0.0)); for (std::size_t i = 0; i < size; ++i) { for (std::size_t j = 0; j < size; ++j) { result[i] += timeModel->at(times[j]) * matrix[j * size + i]; } } return result; }; std::vector> uncached; const std::vector>* solved = &uncached; if (timeWeights) { auto found = timeWeights->find(times); if (found == timeWeights->end()) { found = timeWeights->emplace(times, solve()).first; } solved = &found->second; } else { uncached = solve(); } if (solved->size() == size) { std::complex estimate(0.0, 0.0); for (std::size_t i = 0; i < size; ++i) { estimate += (*solved)[i] * values[i]; } return static_cast>(estimate); } } } if (phaseCorrections && weights) { double sumWeight = 0.0; double sumTime = 0.0; double sumTimeSquared = 0.0; std::complex sumChannel(0.0, 0.0); std::complex sumTimeChannel(0.0, 0.0); // Observations are chronological; only the twelve symbols either side // of the target contribute. const auto first = std::lower_bound( observations.begin(), observations.end(), targetSequenceSymbol - window, [](const TimedPilot& observation, int symbol) { return observation.sequenceSymbol < symbol; } ); for (auto observationIt = first; observationIt != observations.end(); ++observationIt) { const TimedPilot& observation = *observationIt; const int time = observation.sequenceSymbol - targetSequenceSymbol; if (time > window) { break; } const double weight = (*weights)[static_cast(std::abs(time))]; const std::complex channel = corrected(observation); sumWeight += weight; sumTime += weight * time; sumTimeSquared += weight * time * time; sumChannel += weight * channel; sumTimeChannel += weight * time * channel; } const double denominator = sumWeight * sumTimeSquared - sumTime * sumTime; if (sumWeight > 1.0e-20 && std::abs(denominator) > 1.0e-20) { return static_cast>( (sumTimeSquared * sumChannel - sumTime * sumTimeChannel) / denominator ); } if (sumWeight > 1.0e-20) { return static_cast>(sumChannel / sumWeight); } } if (upper == observations.begin()) { return corrected(*upper); } if (upper == observations.end()) { return corrected(observations.back()); } if (upper->sequenceSymbol == targetSequenceSymbol) { return corrected(*upper); } const TimedPilot& right = *upper; const TimedPilot& left = *(upper - 1); const float fraction = static_cast( targetSequenceSymbol - left.sequenceSymbol ) / static_cast(right.sequenceSymbol - left.sequenceSymbol); const std::complex leftChannel = corrected(left); const std::complex rightChannel = corrected(right); return leftChannel + fraction * (rightChannel - leftChannel); } // FFT plans by size and direction, built once per thread rather than per // symbol. kissfft>& fftPlan(int size, bool inverse) { thread_local std::map, std::unique_ptr>>> plans; auto& plan = plans[{size, inverse}]; if (!plan) { plan = std::make_unique>>(size, inverse); } return *plan; } bool dftInterpolate( const ModeParameters& parameters, int carrierCount, int pilotSpacing, const std::vector>& pilots, std::vector>& interpolated, float* noiseRatio = nullptr ) { if (carrierCount <= 0 || pilotSpacing <= 0 || pilots.size() < 2U) { return false; } const int shortLength = static_cast(pilots.size()); const int longLength = carrierCount + pilotSpacing - 1; if (longLength < shortLength) { return false; } std::vector> delay(static_cast(shortLength)); fftPlan(shortLength, true).transform(pilots.data(), delay.data()); // The guard interval in delay taps of the pilot transform. const int guardTaps = static_cast(std::floor( static_cast(carrierCount) * parameters.guardTimeUnits / parameters.usefulTimeUnits )); if (guardTaps < 1) { return false; } // Paths arrive from about zero delay (the FFT window backoff is phase // corrected) up to the guard interval. Keep delays within the guard either // side, which also keeps the leakage of off-grid paths. When the guard is // more than half the pilot transform span (modes C and D, whose pilot // spacing is wide for their guard), a symmetric window would fold echoes // beyond half the span onto negative delays: keep the whole guard as // positive delay and the remaining taps as early ones instead. const bool longGuard = guardTaps > shortLength / 2; const int positiveLimit = longGuard ? std::min(shortLength - 1, guardTaps) : guardTaps; const int negativeLimit = longGuard ? shortLength - positiveLimit - 1 : guardTaps - 1; const auto signedDelay = [&](int tap) { return longGuard ? (tap < positiveLimit ? tap : tap - shortLength) : (tap <= shortLength / 2 ? tap : tap - shortLength); }; const auto kept = [&](int tap) { const int signedTap = signedDelay(tap); return signedTap < positiveLimit && -signedTap <= negativeLimit; }; // A path between the taps of this transform leaks into every tap, and // truncating to the guard window leaves an error that grows and shrinks // with the path's fractional position (1.6 dB of MER on a steady BBC // channel as the timing moved by single samples). Shift the strongest // kept path onto the grid (its fractional position by Jacobsen's // estimator) and restore the shift on the interpolated response. double alignment = 0.0; { int peak = 0; for (int tap = 1; tap < shortLength; ++tap) { if (!kept(tap)) { continue; } if (std::norm(delay[static_cast(tap)]) > std::norm(delay[static_cast(peak)])) { peak = tap; } } const auto at = [&](int tap) { return std::complex(delay[static_cast((tap % shortLength + shortLength) % shortLength)]); }; const std::complex denominator = 2.0 * at(peak) - at(peak - 1) - at(peak + 1); if (std::abs(denominator) > 1.0e-20) { alignment = std::clamp(std::real((at(peak - 1) - at(peak + 1)) / denominator), -0.5, 0.5); } if (alignment != 0.0) { std::vector> aligned = pilots; for (int i = 0; i < shortLength; ++i) { aligned[static_cast(i)] *= std::polar(1.0f, static_cast(2.0 * Pi * alignment * i / shortLength)); } fftPlan(shortLength, true).transform(aligned.data(), delay.data()); } } std::vector delayPowers(delay.size()); std::transform( delay.begin(), delay.end(), delayPowers.begin(), [](const std::complex& value) { return std::norm(value); } ); std::vector sortedPowers = delayPowers; const auto lowerQuartile = sortedPowers.begin() + static_cast(sortedPowers.size() / 4U); std::nth_element(sortedPowers.begin(), lowerQuartile, sortedPowers.end()); constexpr float ExponentialLowerQuartile = 0.28768207245f; // A finite occupied band spreads off-grid physical paths across delay // bins. Use the lower quartile as a conservative noise observation and // leave most of that deterministic leakage intact. constexpr float DelayNoiseScale = 0.1f; const float noiseFloor = DelayNoiseScale * *lowerQuartile / ExponentialLowerQuartile; const float averagePower = std::accumulate( delayPowers.begin(), delayPowers.end(), 0.0f ) / static_cast(delayPowers.size()); if (noiseRatio) { *noiseRatio = averagePower > 1.0e-20f ? noiseFloor / averagePower : 0.0f; } std::vector> padded( static_cast(longLength), {0.0f, 0.0f} ); for (int tap = 0; tap < shortLength; ++tap) { if (!kept(tap)) { continue; } const int signedTap = signedDelay(tap); const float power = delayPowers[static_cast(tap)]; const float wienerGain = power > 1.0e-20f ? std::max(0.0f, 1.0f - noiseFloor / power) : 0.0f; const int paddedTap = signedTap >= 0 ? signedTap : longLength + signedTap; if (paddedTap >= 0 && paddedTap < longLength) { padded[static_cast(paddedTap)] = wienerGain * delay[static_cast(tap)]; } } std::vector> transformed(static_cast(longLength)); fftPlan(longLength, false).transform(padded.data(), transformed.data()); interpolated.resize(static_cast(carrierCount)); for (int i = 0; i < carrierCount; ++i) { interpolated[static_cast(i)] = transformed[static_cast(i)] / static_cast(shortLength) * std::polar(1.0f, static_cast(-2.0 * Pi * alignment * i / (static_cast(pilotSpacing) * shortLength))); } return true; } // Mode E places each symbol's gain references 16 carriers apart and steps // them by 4 carriers a symbol. Its Doppler (up to about 130 Hz in the annex B // channels) is too fast to interpolate the pilots over the 4-symbol pattern, // so each symbol is interpolated from its own reference cells, which resolve // delays over a span of N/16 samples (140 us). Linear interpolation is exact // only for a flat channel; a frequency-domain Wiener interpolator follows the // echoes within that span and averages the noise over the pilots. Its // correlation model is a uniform delay profile over a window chosen from the // pilots (see selectFrequencyWiener). Echoes beyond the span need the // time/frequency estimator instead (see longEchoFraction). struct SymbolPilots { std::vector carriers; std::vector> values; std::vector amplitudes; }; struct FrequencyWienerModel { bool valid = false; double centre = 0.0; // samples of the useful period double width = 0.0; // samples of the useful period double noiseRatio = 0.0; // noise to channel power, for a pilot of FrequencyWienerReferenceAmplitude }; constexpr int FrequencyWienerSpacing = 16; constexpr float FrequencyWienerReferenceAmplitude = 1.41421356f; constexpr double FrequencyWienerNoiseBias = 0.3; // Echo energy beyond the per-symbol span, relative to that within it, above // which mode E interpolates in time: about 0.003-0.023 (median) on the annex B // channels 7 to 11 and 0.05-0.074 on the SFN channel, static or moving. constexpr double ModeELongEchoFraction = 0.035; std::complex uniformDelayCorrelation(double lag, double centre, double width, int usefulSamples) { const double x = lag * width / usefulSamples; const double sinc = std::abs(x) < 1.0e-9 ? 1.0 : std::sin(Pi * x) / (Pi * x); return std::polar(sinc, -2.0 * Pi * lag * centre / usefulSamples); } // Interpolation from one symbol's reference cells to every carrier: the // estimate at carrier k is cross(k) * inverse * pilots. The coefficients // (inverse * pilots) are solved once per symbol, which costs less than // forming the weights when a pilot pattern serves few symbols. With a narrow // delay window at high SNR the matrix is ill-conditioned (condition numbers // near 1e5), so the inverse, the coefficients and their sum stay in double. struct FrequencyWienerFilter { std::size_t pilotCount = 0; std::vector> inverse; // pilotCount x pilotCount std::vector> cross; // carrier-major, carriers x pilotCount void coefficients(const std::vector>& pilots, std::vector>& result) const { result.assign(pilotCount, {0.0, 0.0}); for (std::size_t i = 0; i < pilotCount; ++i) { const std::complex* row = inverse.data() + i * pilotCount; std::complex sum(0.0, 0.0); for (std::size_t j = 0; j < pilotCount; ++j) { sum += row[j] * std::complex(pilots[j]); } result[i] = sum; } } std::complex value(int carrierIndex, const std::vector>& coefficients) const { const std::complex* row = cross.data() + static_cast(carrierIndex) * pilotCount; std::complex sum(0.0, 0.0); for (std::size_t j = 0; j < pilotCount; ++j) { sum += row[j] * coefficients[j]; } return static_cast>(sum); } }; bool invertMatrix(std::vector>& matrix, std::size_t size) { std::vector> inverse(size * size, {0.0, 0.0}); for (std::size_t i = 0; i < size; ++i) { inverse[i * size + i] = 1.0; } for (std::size_t column = 0; column < size; ++column) { std::size_t pivot = column; for (std::size_t row = column + 1; row < size; ++row) { if (std::abs(matrix[row * size + column]) > std::abs(matrix[pivot * size + column])) { pivot = row; } } if (std::abs(matrix[pivot * size + column]) < 1.0e-12) { return false; } if (pivot != column) { for (std::size_t k = 0; k < size; ++k) { std::swap(matrix[pivot * size + k], matrix[column * size + k]); std::swap(inverse[pivot * size + k], inverse[column * size + k]); } } const std::complex scale = 1.0 / matrix[column * size + column]; for (std::size_t k = 0; k < size; ++k) { matrix[column * size + k] *= scale; inverse[column * size + k] *= scale; } for (std::size_t row = 0; row < size; ++row) { if (row == column) { continue; } const std::complex factor = matrix[row * size + column]; if (factor == std::complex(0.0, 0.0)) { continue; } for (std::size_t k = 0; k < size; ++k) { matrix[row * size + k] -= factor * matrix[column * size + k]; inverse[row * size + k] -= factor * inverse[column * size + k]; } } } matrix.swap(inverse); return true; } bool buildFrequencyWiener( const SymbolPilots& pilots, const FrequencyWienerModel& model, int usefulSamples, int minimumCarrier, int carrierCount, FrequencyWienerFilter& filter ) { const std::size_t size = pilots.carriers.size(); if (size < 2U || carrierCount < 1) { return false; } // The model depends only on the carrier lag. std::vector> correlation(static_cast(2 * carrierCount - 1)); for (int lag = -(carrierCount - 1); lag < carrierCount; ++lag) { correlation[static_cast(lag + carrierCount - 1)] = uniformDelayCorrelation(lag, model.centre, model.width, usefulSamples); } const auto at = [&](int lag) { return correlation[static_cast(lag + carrierCount - 1)]; }; std::vector> matrix(size * size); for (std::size_t i = 0; i < size; ++i) { for (std::size_t j = 0; j < size; ++j) { matrix[i * size + j] = at(pilots.carriers[i] - pilots.carriers[j]); } const double amplitude = pilots.amplitudes[i]; matrix[i * size + i] += model.noiseRatio * FrequencyWienerReferenceAmplitude * FrequencyWienerReferenceAmplitude / std::max(1.0e-6, amplitude * amplitude); } if (!invertMatrix(matrix, size)) { return false; } filter.pilotCount = size; filter.inverse = matrix; filter.cross.resize(static_cast(carrierCount) * size); for (int index = 0; index < carrierCount; ++index) { for (std::size_t j = 0; j < size; ++j) { filter.cross[static_cast(index) * size + j] = at(minimumCarrier + index - pilots.carriers[j]); } } return true; } // Whether a symbol's reference cells are only its gain references, evenly // spaced (the symbols without time or AFS references). bool regularPilots(const SymbolPilots& pilots) { if (pilots.carriers.size() < 4U) { return false; } for (std::size_t i = 1; i < pilots.carriers.size(); ++i) { if (pilots.carriers[i] - pilots.carriers[i - 1] != FrequencyWienerSpacing) { return false; } } return true; } // Chooses the delay window and noise term. A tapered periodogram of each // regular symbol's pilots gives the delay profile over the span the pilot // spacing resolves; for each candidate width the window sits where it // captures most of the profile's energy above its floor. The width and noise // term are those whose interpolation of each symbol best predicts the pilots // of the symbols one and two either side, which lie 4, 8 and 12 carriers from // its own: the positions interpolation fills. A common phase is fitted to each // prediction (residual frequency offset), and the channel's change over those // symbols adds the same error to every candidate. FrequencyWienerModel selectFrequencyWiener( const std::vector& symbols, int usefulSamples, int minimumCarrier, int carrierCount ) { FrequencyWienerModel best; const double span = static_cast(usefulSamples) / FrequencyWienerSpacing; constexpr double DelayStep = 0.25; const int delayBins = static_cast(std::lround(span / DelayStep)); std::vector profile(static_cast(delayBins), 0.0); std::vector regular; for (std::size_t index = 0; index < symbols.size(); ++index) { if (regularPilots(symbols[index])) { regular.push_back(index); } } if (regular.size() < 4U) { return best; } // Periodogram phasors by carrier and delay bin. std::vector> phasors(static_cast(carrierCount) * static_cast(delayBins)); for (int index = 0; index < carrierCount; ++index) { for (int bin = 0; bin < delayBins; ++bin) { const double delay = -span / 2.0 + bin * DelayStep; phasors[static_cast(index) * static_cast(delayBins) + static_cast(bin)] = std::polar(1.0, 2.0 * Pi * (minimumCarrier + index) * delay / usefulSamples); } } for (std::size_t index : regular) { const SymbolPilots& pilots = symbols[index]; const std::size_t count = pilots.carriers.size(); double taperPower = 0.0; std::vector taper(count); for (std::size_t i = 0; i < count; ++i) { const double s = std::sin(Pi * (static_cast(i) + 0.5) / static_cast(count)); taper[i] = s * s; taperPower += taper[i] * taper[i]; } std::vector> sums(static_cast(delayBins), {0.0, 0.0}); for (std::size_t i = 0; i < count; ++i) { const int carrierIndex = pilots.carriers[i] - minimumCarrier; if (carrierIndex < 0 || carrierIndex >= carrierCount) { continue; } const std::complex value = taper[i] * std::complex(pilots.values[i]); const std::complex* row = phasors.data() + static_cast(carrierIndex) * static_cast(delayBins); for (int bin = 0; bin < delayBins; ++bin) { sums[static_cast(bin)] += value * row[bin]; } } for (int bin = 0; bin < delayBins; ++bin) { profile[static_cast(bin)] += std::norm(sums[static_cast(bin)]) / taperPower; } } std::vector sorted = profile; const auto floorPosition = sorted.begin() + static_cast(sorted.size() / 5U); std::nth_element(sorted.begin(), floorPosition, sorted.end()); const double floor = *floorPosition; std::vector excess(profile.size()); for (std::size_t bin = 0; bin < profile.size(); ++bin) { excess[bin] = std::max(0.0, profile[bin] - floor); } const double regularCount = static_cast(regular.size()); double zeroLag = 0.0; double zeroCount = 0.0; for (std::size_t index : regular) { for (std::size_t i = 0; i < symbols[index].values.size(); ++i) { if (std::abs(symbols[index].amplitudes[i] - FrequencyWienerReferenceAmplitude) >= 0.01f) { continue; } zeroLag += std::norm(std::complex(symbols[index].values[i])); zeroCount += 1.0; } } if (zeroCount < 1.0) { return best; } zeroLag /= zeroCount; // The periodogram's floor is the noise per pilot (the taper is // normalized); an estimate only, refined by the validation below. const double floorNoise = floor / regularCount; const double floorRatio = zeroLag > floorNoise ? floorNoise / (zeroLag - floorNoise) : 1.0; static constexpr double Widths[] = {1.0, 2.0, 3.0, 4.0, 6.0, 8.0, 11.0, 14.0, 18.0, 22.0}; static constexpr double NoiseScales[] = {0.1, 0.3, 1.0, 3.0}; // Every second symbol as a target is enough to rank the candidates. constexpr std::size_t ValidationStride = 2; // The window position capturing most excess energy for a width. const auto centreFor = [&](double width) { const int widthBins = std::max(1, static_cast(std::lround(width / DelayStep))); double captured = 0.0; for (int k = 0; k < widthBins; ++k) { captured += excess[static_cast(k % delayBins)]; } double bestCaptured = captured; int bestStart = 0; for (int start = 1; start < delayBins; ++start) { captured += excess[static_cast((start + widthBins - 1) % delayBins)] - excess[static_cast(start - 1)]; if (captured > bestCaptured) { bestCaptured = captured; bestStart = start; } } return std::remainder(-span / 2.0 + (bestStart + (widthBins - 1) / 2.0) * DelayStep, span); }; // Squared error of the predicted neighbour pilots, after a common phase. const auto validationError = [&](const FrequencyWienerModel& candidate) { std::map, FrequencyWienerFilter> filters; std::vector> coefficients; double score = 0.0; for (std::size_t r = 0; r < regular.size(); r += ValidationStride) { const std::size_t index = regular[r]; const SymbolPilots& pilots = symbols[index]; auto found = filters.find(pilots.carriers); if (found == filters.end()) { FrequencyWienerFilter filter; if (!buildFrequencyWiener(pilots, candidate, usefulSamples, minimumCarrier, carrierCount, filter)) { return std::numeric_limits::max(); } found = filters.emplace(pilots.carriers, std::move(filter)).first; } const FrequencyWienerFilter& filter = found->second; filter.coefficients(pilots.values, coefficients); for (int offset : {-2, -1, 1, 2}) { const long neighbour = static_cast(index) + offset; if (neighbour < 0 || neighbour >= static_cast(symbols.size())) { continue; } const SymbolPilots& other = symbols[static_cast(neighbour)]; if (!regularPilots(other)) { continue; } std::complex cross(0.0, 0.0); double predictedPower = 0.0; double measuredPower = 0.0; for (std::size_t j = 0; j < other.carriers.size(); ++j) { const int carrierIndex = other.carriers[j] - minimumCarrier; if (carrierIndex < 0 || carrierIndex >= carrierCount) { continue; } const std::complex predicted(filter.value(carrierIndex, coefficients)); const std::complex measured(other.values[j]); cross += measured * std::conj(predicted); predictedPower += std::norm(predicted); measuredPower += std::norm(measured); } score += measuredPower + predictedPower - 2.0 * std::abs(cross); } } return score; }; const auto candidateFor = [&](double width, double centre, double noiseScale) { FrequencyWienerModel candidate; candidate.valid = true; candidate.centre = centre; candidate.width = width; candidate.noiseRatio = std::clamp(floorRatio * noiseScale, 1.0e-4, 10.0); return candidate; }; // The width at the periodogram's noise estimate, then the noise term at // that width. double bestScore = std::numeric_limits::max(); for (double width : Widths) { const FrequencyWienerModel candidate = candidateFor(width, centreFor(width), 1.0); const double score = validationError(candidate); if (score < bestScore) { bestScore = score; best = candidate; } } if (!best.valid) { return best; } const FrequencyWienerModel widthChoice = best; for (double noiseScale : NoiseScales) { if (noiseScale == 1.0) { continue; } const FrequencyWienerModel candidate = candidateFor(widthChoice.width, widthChoice.centre, noiseScale); const double score = validationError(candidate); if (score < bestScore) { bestScore = score; best = candidate; } } return best; } // Replaces the per-symbol linear estimates of a mode E context with frequency // Wiener interpolation from the same reference cells. bool refineFrequencyWiener( Mode mode, int occupancy, int firstFrameInSuperframe, int integerCarrierOffset, const std::vector>>>& fftFrames, std::vector& estimates, FrequencyWienerModel& model ) { const ModeParameters& parameters = Parameters::forMode(mode); const SpectrumParameters* spectrum = Parameters::forSpectrum(mode, occupancy); if (!spectrum) { return false; } const int usefulSamples = parameters.usefulSamples(parameters.internalSampleRate); const int carrierCount = spectrum->maximumCarrier - spectrum->minimumCarrier + 1; std::vector symbols(estimates.size()); std::vector>*> bins(estimates.size(), nullptr); for (std::size_t frame = 0; frame < fftFrames.size(); ++frame) { const int framePhase = (firstFrameInSuperframe + static_cast(frame)) % parameters.framesPerSuperframe; for (int symbol = 0; symbol < parameters.symbolsPerFrame; ++symbol) { const std::size_t index = frame * static_cast(parameters.symbolsPerFrame) + static_cast(symbol); const std::vector& references = CellMap::referenceCells(mode, occupancy, symbol, framePhase); const std::vector>& expected = referenceValues(mode, occupancy, symbol, framePhase); const auto& symbolBins = fftFrames[frame][static_cast(symbol)]; SymbolPilots& pilots = symbols[index]; for (std::size_t i = 0; i < references.size(); ++i) { pilots.carriers.push_back(references[i].carrier); pilots.values.push_back(binForCarrier(symbolBins, references[i].carrier + integerCarrierOffset) / expected[i]); pilots.amplitudes.push_back(references[i].amplitude); } bins[index] = &symbolBins; } } model = selectFrequencyWiener(symbols, usefulSamples, spectrum->minimumCarrier, carrierCount); // The time smoothing that follows (estimateFrame) averages noise as // well, so interpolate in frequency with less smoothing than the // per-symbol validation chooses: on the mode E sweeps a third of its // noise term gave 2% more superframes, a tenth or a wider window no more. model.noiseRatio *= FrequencyWienerNoiseBias; if (!model.valid) { return false; } std::map, FrequencyWienerFilter> filters; std::vector> coefficients; for (std::size_t index = 0; index < estimates.size(); ++index) { const SymbolPilots& pilots = symbols[index]; std::vector key = pilots.carriers; for (float amplitude : pilots.amplitudes) { key.push_back(static_cast(std::lround(amplitude * 1000.0f))); } auto found = filters.find(key); if (found == filters.end()) { FrequencyWienerFilter filter; if (!buildFrequencyWiener(pilots, model, usefulSamples, spectrum->minimumCarrier, carrierCount, filter)) { return false; } found = filters.emplace(std::move(key), std::move(filter)).first; } const FrequencyWienerFilter& filter = found->second; filter.coefficients(pilots.values, coefficients); EqualizedSymbol& output = estimates[index]; output.minimumCarrier = spectrum->minimumCarrier; output.maximumCarrier = spectrum->maximumCarrier; output.channel.assign(static_cast(carrierCount), {0.0f, 0.0f}); output.cells.assign(static_cast(carrierCount), {0.0f, 0.0f}); for (int carrierIndex = 0; carrierIndex < carrierCount; ++carrierIndex) { const std::complex channel = filter.value(carrierIndex, coefficients); output.channel[static_cast(carrierIndex)] = channel; const float power = std::norm(channel); output.cells[static_cast(carrierIndex)] = power > 1.0e-12f ? binForCarrier(*bins[index], spectrum->minimumCarrier + carrierIndex + integerCarrierOffset) * std::conj(channel) / power : std::complex(0.0f, 0.0f); } double residualPower = 0.0; double referencePower = 0.0; for (std::size_t j = 0; j < pilots.carriers.size(); ++j) { const std::complex predicted = output.channel[ static_cast(pilots.carriers[j] - spectrum->minimumCarrier)]; residualPower += std::norm(pilots.values[j] - predicted); referencePower += std::norm(pilots.values[j]); } output.pilotResidualPower = referencePower > 1.0e-20 ? static_cast(residualPower / referencePower) : 0.0f; } return true; } } // namespace bool EqualizedSymbol::contains(int carrier) const { return carrier >= minimumCarrier && carrier <= maximumCarrier && cells.size() == static_cast(maximumCarrier - minimumCarrier + 1); } const std::complex& EqualizedSymbol::cell(int carrier) const { if (!contains(carrier)) { throw std::out_of_range("DRM carrier is outside equalized symbol"); } return cells[static_cast(carrier - minimumCarrier)]; } bool ChannelEstimator::estimate( Mode mode, int occupancy, int symbol, int frameInSuperframe, int integerCarrierOffset, const std::vector>& fftBins, EqualizedSymbol& result ) { const SpectrumParameters* spectrum = Parameters::forSpectrum(mode, occupancy); const int expectedFFTSize = Parameters::forMode(mode).usefulSamples( Parameters::forMode(mode).internalSampleRate ); if (!spectrum || static_cast(fftBins.size()) < expectedFFTSize) { return false; } const std::vector& pilots = CellMap::referenceCells( mode, occupancy, symbol, frameInSuperframe ); if (pilots.size() < 2) { return false; } // Mode, occupancy, symbol and frame are within the cell-map ranges here. const std::vector>& expectedValues = referenceValues( mode, occupancy, symbol, frameInSuperframe ); struct Estimate { int carrier; std::complex value; }; std::vector pilotEstimates; pilotEstimates.reserve(pilots.size()); for (std::size_t index = 0; index < pilots.size(); ++index) { const PilotCell& pilot = pilots[index]; const std::complex& expected = expectedValues[index]; pilotEstimates.push_back({ pilot.carrier, binForCarrier(fftBins, pilot.carrier + integerCarrierOffset) / expected }); } result.minimumCarrier = spectrum->minimumCarrier; result.maximumCarrier = spectrum->maximumCarrier; const std::size_t cellCount = static_cast( spectrum->maximumCarrier - spectrum->minimumCarrier + 1 ); result.channel.resize(cellCount); result.cells.resize(cellCount); std::size_t upper = 0; for (int carrier = spectrum->minimumCarrier; carrier <= spectrum->maximumCarrier; ++carrier) { while (upper < pilotEstimates.size() && pilotEstimates[upper].carrier < carrier) { ++upper; } std::complex channel; if (upper == 0) { channel = pilotEstimates.front().value; } else if (upper == pilotEstimates.size()) { channel = pilotEstimates.back().value; } else { const Estimate& left = pilotEstimates[upper - 1]; const Estimate& right = pilotEstimates[upper]; const float fraction = static_cast(carrier - left.carrier) / static_cast(right.carrier - left.carrier); channel = left.value + fraction * (right.value - left.value); } const std::size_t outputIndex = static_cast(carrier - spectrum->minimumCarrier); result.channel[outputIndex] = channel; const float power = std::norm(channel); result.cells[outputIndex] = power > 1.0e-12f ? binForCarrier(fftBins, carrier + integerCarrierOffset) * std::conj(channel) / power : std::complex(0.0f, 0.0f); } double residualPower = 0.0; double referencePower = 0.0; std::size_t residualCount = 0; for (std::size_t i = 1; i + 1 < pilotEstimates.size(); ++i) { const Estimate& left = pilotEstimates[i - 1]; const Estimate& middle = pilotEstimates[i]; const Estimate& right = pilotEstimates[i + 1]; const float fraction = static_cast(middle.carrier - left.carrier) / static_cast(right.carrier - left.carrier); const std::complex predicted = left.value + fraction * (right.value - left.value); residualPower += std::norm(middle.value - predicted); referencePower += std::norm(middle.value); ++residualCount; } result.pilotResidualPower = residualCount == 0 || referencePower <= 1.0e-20 ? 0.0f : static_cast(residualPower / referencePower); return true; } bool ChannelEstimationContext::prepare( Mode mode, int occupancy, int firstFrameInSuperframe, int integerCarrierOffset, const std::vector>>>& fftFrames ) { m_mode = mode; m_occupancy = occupancy; m_firstFrameInSuperframe = firstFrameInSuperframe; m_integerCarrierOffset = integerCarrierOffset; m_frames = &fftFrames; m_valid = false; m_initial.clear(); m_wienerState = 0; m_pilotGrid.clear(); m_pilotSpacing = 0; m_histories.clear(); m_timeWienerWeights.clear(); m_temporalCorrelation = 0.8f; m_pilotPeriod = 0; m_phaseRate = 0.0; m_pilotCorrelation = 1.0; m_pilotLag1 = 1.0; m_haveDelayWindow = false; m_timeInterpolation = 0; m_delayCentre = 0.0; m_delayWidth = 0.0; const ModeParameters& parameters = Parameters::forMode(mode); const SpectrumParameters* spectrum = Parameters::forSpectrum(mode, occupancy); if (!spectrum || fftFrames.empty() || firstFrameInSuperframe < 0 || firstFrameInSuperframe >= parameters.framesPerSuperframe) { return false; } for (const auto& frame : fftFrames) { if (static_cast(frame.size()) != parameters.symbolsPerFrame) { return false; } } const std::size_t frameSymbols = static_cast(parameters.symbolsPerFrame); m_initial.resize(fftFrames.size() * frameSymbols); for (std::size_t frame = 0; frame < fftFrames.size(); ++frame) { const int framePhase = (firstFrameInSuperframe + static_cast(frame)) % parameters.framesPerSuperframe; for (int symbol = 0; symbol < parameters.symbolsPerFrame; ++symbol) { const std::size_t sequenceIndex = frame * frameSymbols + static_cast(symbol); if (!ChannelEstimator::estimate( mode, occupancy, symbol, framePhase, integerCarrierOffset, fftFrames[frame][static_cast(symbol)], m_initial[sequenceIndex])) { m_initial.clear(); return false; } } } if (mode == Mode::E) { FrequencyWienerModel model; if (refineFrequencyWiener(mode, occupancy, firstFrameInSuperframe, integerCarrierOffset, fftFrames, m_initial, model)) { m_haveDelayWindow = true; m_delayCentre = model.centre; m_delayWidth = model.width; } } m_valid = true; return true; } bool ChannelEstimationContext::estimateFrame( int targetFrame, std::vector& result, int temporalRadius ) const { if (!m_valid || targetFrame < 0 || static_cast(targetFrame) >= m_frames->size() || temporalRadius < 0) { return false; } const auto& fftFrames = *m_frames; const std::vector& initial = m_initial; const ModeParameters& parameters = Parameters::forMode(m_mode); const SpectrumParameters* spectrum = Parameters::forSpectrum(m_mode, m_occupancy); const std::size_t sequenceSymbols = initial.size(); result.assign( initial.begin() + static_cast(targetFrame * parameters.symbolsPerFrame), initial.begin() + static_cast((targetFrame + 1) * parameters.symbolsPerFrame) ); const std::size_t cellCount = initial.front().channel.size(); for (int symbol = 0; symbol < parameters.symbolsPerFrame; ++symbol) { EqualizedSymbol& output = result[static_cast(symbol)]; std::vector> phaseCorrections( sequenceSymbols, {1.0f, 0.0f} ); const int targetSequenceSymbol = targetFrame * parameters.symbolsPerFrame + symbol; const int first = std::max(0, targetSequenceSymbol - temporalRadius); const int last = std::min( static_cast(sequenceSymbols) - 1, targetSequenceSymbol + temporalRadius ); for (int adjacent = first; adjacent <= last; ++adjacent) { if (adjacent == targetSequenceSymbol) { continue; } std::complex correlation(0.0, 0.0); for (std::size_t index = 0; index < cellCount; ++index) { correlation += static_cast>( initial[static_cast(adjacent)].channel[index] * std::conj(initial[static_cast(targetSequenceSymbol)].channel[index]) ); } if (std::abs(correlation) > 1.0e-20) { phaseCorrections[static_cast(adjacent)] = std::polar(1.0f, static_cast(-std::arg(correlation))); } } for (int carrier = spectrum->minimumCarrier; carrier <= spectrum->maximumCarrier; ++carrier) { std::complex channel(0.0f, 0.0f); float totalWeight = 0.0f; for (int adjacent = first; adjacent <= last; ++adjacent) { const float weight = 1.0f / (1.0f + std::abs(adjacent - targetSequenceSymbol)); channel += weight * phaseCorrections[static_cast(adjacent)] * initial[static_cast(adjacent)].channel[ static_cast(carrier - spectrum->minimumCarrier) ]; totalWeight += weight; } channel /= totalWeight; const std::size_t index = static_cast(carrier - spectrum->minimumCarrier); output.channel[index] = channel; const float power = std::norm(channel); output.cells[index] = power > 1.0e-12f ? binForCarrier( fftFrames[static_cast(targetFrame)][static_cast(symbol)], carrier + m_integerCarrierOffset ) * std::conj(channel) / power : std::complex(0.0f, 0.0f); } } return true; } bool ChannelEstimationContext::prepareWiener() { if (m_wienerState != 0) { return m_wienerState > 0; } m_wienerState = -1; const Mode mode = m_mode; const int occupancy = m_occupancy; const auto& fftFrames = *m_frames; const ModeParameters& parameters = Parameters::forMode(mode); const SpectrumParameters* spectrum = Parameters::forSpectrum(mode, occupancy); for (const auto& frame : fftFrames) { for (const auto& bins : frame) { if (static_cast(bins.size()) < parameters.usefulSamples( parameters.internalSampleRate)) { return false; } } } std::vector pilotGrid; for (int symbol = 0; symbol < parameters.symbolsPerFrame; ++symbol) { const std::vector& carriers = CellMap::gainReferenceCarriers( mode, occupancy, symbol ); pilotGrid.insert(pilotGrid.end(), carriers.begin(), carriers.end()); } std::sort(pilotGrid.begin(), pilotGrid.end()); pilotGrid.erase(std::unique(pilotGrid.begin(), pilotGrid.end()), pilotGrid.end()); if (pilotGrid.size() < 2U || pilotGrid.front() != spectrum->minimumCarrier) { return false; } const int pilotSpacing = pilotGrid[1] - pilotGrid[0]; if (pilotSpacing <= 0) { return false; } // The time-interpolated grid is regular except where an unused carrier // (mode D's DC) displaces a gain reference; fill those positions later. const std::vector unused = CellMap::unusedCarriers(mode); std::vector regularGrid; for (int carrier = pilotGrid.front(); carrier <= pilotGrid.back(); carrier += pilotSpacing) { regularGrid.push_back(carrier); } for (int carrier : pilotGrid) { if ((carrier - pilotGrid.front()) % pilotSpacing != 0) { return false; } } for (int carrier : regularGrid) { const bool present = std::binary_search(pilotGrid.begin(), pilotGrid.end(), carrier); if (!present && std::find(unused.begin(), unused.end(), carrier) == unused.end()) { return false; } } std::vector> histories(regularGrid.size()); for (std::size_t frame = 0; frame < fftFrames.size(); ++frame) { const int framePhase = (m_firstFrameInSuperframe + static_cast(frame)) % parameters.framesPerSuperframe; for (int symbol = 0; symbol < parameters.symbolsPerFrame; ++symbol) { const int sequenceSymbol = static_cast(frame) * parameters.symbolsPerFrame + symbol; const std::vector& carriers = CellMap::gainReferenceCarriers( mode, occupancy, symbol ); const std::vector& references = CellMap::referenceCells( mode, occupancy, symbol, framePhase ); for (int carrier : carriers) { const auto reference = std::find_if( references.begin(), references.end(), [carrier](const PilotCell& pilot) { return pilot.carrier == carrier; } ); if (reference == references.end()) { return false; } histories[static_cast((carrier - regularGrid.front()) / pilotSpacing)].push_back({ sequenceSymbol, binForCarrier( fftFrames[frame][static_cast(symbol)], carrier + m_integerCarrierOffset ) / pilotValue(*reference) }); } } } const std::vector& initial = m_initial; const std::size_t sequenceSymbols = initial.size(); std::vector temporalCorrelations; temporalCorrelations.reserve(sequenceSymbols > 0 ? sequenceSymbols - 1U : 0U); for (std::size_t adjacent = 1; adjacent < sequenceSymbols; ++adjacent) { std::complex correlation(0.0, 0.0); double leftPower = 0.0; double rightPower = 0.0; for (std::size_t index = 0; index < initial[adjacent].channel.size(); ++index) { const std::complex left = initial[adjacent - 1U].channel[index]; const std::complex right = initial[adjacent].channel[index]; correlation += right * std::conj(left); leftPower += std::norm(left); rightPower += std::norm(right); } if (leftPower > 1.0e-20 && rightPower > 1.0e-20) { temporalCorrelations.push_back(static_cast( std::abs(correlation) / std::sqrt(leftPower * rightPower) )); } } float temporalCorrelation = 0.8f; if (!temporalCorrelations.empty()) { const auto middle = temporalCorrelations.begin() + static_cast(temporalCorrelations.size() / 2U); std::nth_element( temporalCorrelations.begin(), middle, temporalCorrelations.end() ); temporalCorrelation = std::clamp(*middle, 0.35f, 0.95f); } m_pilotGrid = std::move(regularGrid); m_pilotSpacing = pilotSpacing; m_histories = std::move(histories); m_temporalCorrelation = temporalCorrelation; // The common phase advance per symbol (residual carrier offset), from // successive observations of the same pilot carriers a pilot period // apart. { int gap = 0; for (const auto& history : m_histories) { for (std::size_t k = 1; k < history.size(); ++k) { const int step = history[k].sequenceSymbol - history[k - 1].sequenceSymbol; if (step > 0 && (gap == 0 || step < gap)) { gap = step; } } } // A carrier displaced by k whole bins advances by 2 pi k G / N per // symbol (G guard, N useful samples): the NCO removes only the // fractional offset. Remove that known rotation first, so that only // the small residual is measured and cannot wrap over the gap. const double integerRotation = std::remainder( 2.0 * Pi * m_integerCarrierOffset * parameters.guardTimeUnits / parameters.usefulTimeUnits, 2.0 * Pi); std::complex advance(0.0, 0.0); for (const auto& history : m_histories) { for (std::size_t k = 1; k < history.size(); ++k) { if (history[k].sequenceSymbol - history[k - 1].sequenceSymbol != gap) { continue; } advance += std::complex(history[k].channel) * std::conj(std::complex(history[k - 1].channel)) * std::polar(1.0, -integerRotation * gap); } } m_pilotPeriod = gap; m_phaseRate = integerRotation + (gap > 0 && std::abs(advance) > 1.0e-20 ? std::arg(advance) / gap : 0.0); // Correlation of each pilot carrier's observations at one and two // observation lags, with the common rotation removed. Noise only // lowers lag zero, so their ratio estimates the channel's own // correlation at one lag. std::complex lag1(0.0, 0.0), lag2(0.0, 0.0); double power1 = 0.0, power2 = 0.0; for (const auto& history : m_histories) { for (std::size_t k = 1; k < history.size(); ++k) { if (history[k].sequenceSymbol - history[k - 1].sequenceSymbol != gap) { continue; } const std::complex a(history[k - 1].channel), b(history[k].channel); lag1 += b * std::conj(a) * std::polar(1.0, -m_phaseRate * gap); power1 += std::abs(a) * std::abs(b); if (k >= 2 && history[k - 1].sequenceSymbol - history[k - 2].sequenceSymbol == gap) { const std::complex c(history[k - 2].channel); lag2 += b * std::conj(c) * std::polar(1.0, -m_phaseRate * 2 * gap); power2 += std::abs(c) * std::abs(b); } } } const double rho1 = power1 > 1.0e-20 ? std::abs(lag1) / power1 : 1.0; const double rho2 = power2 > 1.0e-20 ? std::abs(lag2) / power2 : 1.0; m_pilotCorrelation = rho1 > 1.0e-6 ? std::clamp(rho2 / rho1, 0.0, 1.0) : 0.0; m_pilotLag1 = rho1; } m_wienerState = 1; return true; } bool ChannelEstimationContext::estimateFrameWiener( int targetFrame, std::vector& result, ChannelWienerStatistics* statistics ) { if (!m_valid || targetFrame < 0 || static_cast(targetFrame) >= m_frames->size() || !prepareWiener()) { return false; } // Mode E interpolates each symbol from its own pilots unless the channel // holds echoes beyond the delay span they resolve (see longEchoFraction). if (m_mode == Mode::E) { if (m_timeInterpolation == 0) { m_timeInterpolation = longEchoFraction() >= ModeELongEchoFraction ? 1 : -1; } if (m_timeInterpolation < 0) { return false; } } const Mode mode = m_mode; const int occupancy = m_occupancy; const auto& fftFrames = *m_frames; const ModeParameters& parameters = Parameters::forMode(mode); const SpectrumParameters* spectrum = Parameters::forSpectrum(mode, occupancy); if (!spectrum) { return false; } const int carrierCount = spectrum->maximumCarrier - spectrum->minimumCarrier + 1; const float temporalCorrelation = m_temporalCorrelation; const TemporalWeights weights = temporalWeights(temporalCorrelation); // Mode E's linear time regression spans as much of each pilot's history // as the channel stays correlated over: twelve symbols either side when // it is nearly static, down to one pilot period when it fades quickly. // The A-D Wiener filter weights the twelve symbols either side itself. const int pilotPeriod = std::max(1, m_pilotPeriod); const int window = mode != Mode::E || m_pilotCorrelation >= 0.995 ? 12 : m_pilotCorrelation >= 0.95 ? std::min(12, 2 * pilotPeriod) : std::min(12, pilotPeriod); if (statistics) { statistics->temporalCorrelation = temporalCorrelation; statistics->delayNoiseRatio = 0.0f; } double totalNoiseRatio = 0.0; int noiseRatioCount = 0; result.assign( static_cast(parameters.symbolsPerFrame), EqualizedSymbol() ); // A-D regress each pilot carrier's observations by Wiener interpolation in // time: a Gaussian Doppler spectrum fitted to the pilots' noise-free // correlation one pilot period apart, and the noise ratio from that against // the measured correlation (noise lowers only the latter). It weights the // twelve symbols either side itself. Mode E (its SFN channel is the one // that interpolates in time) lost 0.5% of the sweep and keeps the linear // fit. // // The lag-two to lag-one ratio is rho(P) for an exponential correlation // but rho(P)^3 for a Gaussian one. In fast fading the noise ratio then // came out at its floor and the filter followed the pilots' noise; a floor // of 0.003 instead of 0.001 lifted the BBC recording's worst 1% of // superframes from 7.7 to 8.4 dB MER and the A-D sweep by 16 frames. // Consistent models were worse where it counts, in decoded frames: the // cube root (or 2/3) with the noise from the pilots' second differences // gave 10.3 dB there but cost 0.6% of the A-D sweep and 5% of Kuwait // 10:02's frames; higher floors cost Kuwait 3-5%. TimeWienerModel timeModel; timeModel.period = pilotPeriod; timeModel.periodCorrelation = std::clamp(m_pilotCorrelation, 0.05, 0.9999); timeModel.noiseRatio = std::clamp( timeModel.periodCorrelation / std::max(1.0e-6, m_pilotLag1) - 1.0, 3.0e-3, 10.0 ); const TimeWienerModel* timeWiener = mode == Mode::E ? nullptr : &timeModel; TimeWienerWeights& timeWeights = m_timeWienerWeights; for (int symbol = 0; symbol < parameters.symbolsPerFrame; ++symbol) { const int targetSequenceSymbol = targetFrame * parameters.symbolsPerFrame + symbol; const PhaseCorrections phaseCorrections(m_phaseRate, targetSequenceSymbol); std::vector> timeInterpolated; std::vector haveEstimate; timeInterpolated.reserve(m_pilotGrid.size()); haveEstimate.reserve(m_pilotGrid.size()); for (const std::vector& history : m_histories) { if (history.empty()) { // Only an unused carrier can be absent (checked when prepared). timeInterpolated.emplace_back(0.0f, 0.0f); haveEstimate.push_back(false); continue; } timeInterpolated.push_back(estimateTimedPilot( history, targetSequenceSymbol, &phaseCorrections, &weights, window, timeWiener, &timeWeights )); haveEstimate.push_back(true); } for (std::size_t index = 0; index < timeInterpolated.size(); ++index) { if (haveEstimate[index]) { continue; } const bool left = index > 0 && haveEstimate[index - 1]; const bool right = index + 1 < timeInterpolated.size() && haveEstimate[index + 1]; if (!left && !right) { return false; } timeInterpolated[index] = left && right ? 0.5f * (timeInterpolated[index - 1] + timeInterpolated[index + 1]) : left ? timeInterpolated[index - 1] : timeInterpolated[index + 1]; } std::vector> channel; float noiseRatio = 0.0f; if (!dftInterpolate( parameters, carrierCount, m_pilotSpacing, timeInterpolated, channel, &noiseRatio)) { return false; } totalNoiseRatio += noiseRatio; ++noiseRatioCount; EqualizedSymbol& output = result[static_cast(symbol)]; output.minimumCarrier = spectrum->minimumCarrier; output.maximumCarrier = spectrum->maximumCarrier; output.channel = std::move(channel); output.cells.resize(static_cast(carrierCount)); for (int carrier = spectrum->minimumCarrier; carrier <= spectrum->maximumCarrier; ++carrier) { const std::size_t index = static_cast(carrier - spectrum->minimumCarrier); const float power = std::norm(output.channel[index]); output.cells[index] = power > 1.0e-12f ? binForCarrier( fftFrames[static_cast(targetFrame)][static_cast(symbol)], carrier + m_integerCarrierOffset ) * std::conj(output.channel[index]) / power : std::complex(0.0f, 0.0f); } double residualPower = 0.0; double referencePower = 0.0; const std::vector& targetPilots = CellMap::gainReferenceCarriers( mode, occupancy, symbol ); // The frequency interpolation's residual at the symbol's own pilot // carriers, against the time-interpolated grid it was given. for (int carrier : targetPilots) { const std::complex measured = timeInterpolated[ static_cast((carrier - m_pilotGrid.front()) / m_pilotSpacing) ]; const std::complex predicted = output.channel[ static_cast(carrier - spectrum->minimumCarrier) ]; residualPower += std::norm(measured - predicted); referencePower += std::norm(measured); } output.pilotResidualPower = referencePower > 1.0e-20 ? static_cast(residualPower / referencePower) : 0.0f; // The raw pilot observations of this symbol against the final // estimate: their error power is N0 + F |H|^2, receiver noise plus // error that scales with the channel (a transmitter's own modulation // error, estimation error). The estimate includes each pilot with some // weight, which shrinks both terms alike, so their ratio is unbiased. // A boosted pilot (amplitude 2, not sqrt(2)) carries half a normal // one's noise but, at the band edge, the most interpolation error; // scaling its error to a normal pilot's noise or dropping it cost // Kuwait 10:02 46 frames, so all gain pilots count alike. for (int carrier : targetPilots) { const auto& history = m_histories[static_cast((carrier - m_pilotGrid.front()) / m_pilotSpacing)]; const auto found = std::lower_bound(history.begin(), history.end(), targetSequenceSymbol, [](const ChannelPilotObservation& observation, int value) { return observation.sequenceSymbol < value; }); if (found == history.end() || found->sequenceSymbol != targetSequenceSymbol) { continue; } const std::complex predicted = output.channel[static_cast(carrier - spectrum->minimumCarrier)]; output.pilotErrors.push_back({carrier, std::norm(predicted), std::norm(found->channel - predicted)}); } } if (statistics && noiseRatioCount > 0) { statistics->delayNoiseRatio = static_cast( totalNoiseRatio / noiseRatioCount ); } return true; } double ChannelEstimationContext::longEchoFraction() { if (!m_valid || !m_haveDelayWindow || !prepareWiener()) { return -1.0; } const ModeParameters& parameters = Parameters::forMode(m_mode); const int usefulSamples = parameters.usefulSamples(parameters.internalSampleRate); const int length = static_cast(m_pilotGrid.size()); if (length < 8 || m_pilotSpacing <= 0) { return -1.0; } const TemporalWeights weights = temporalWeights(m_temporalCorrelation); const int pilotPeriod = std::max(1, m_pilotPeriod); const int window = m_pilotCorrelation >= 0.995 ? 12 : m_pilotCorrelation >= 0.95 ? std::min(12, 2 * pilotPeriod) : std::min(12, pilotPeriod); std::vector profile(static_cast(length), 0.0); std::vector> grid(static_cast(length)); std::vector> delay(static_cast(length)); const int sequenceSymbols = static_cast(m_initial.size()); for (int target = 0; target < sequenceSymbols; ++target) { const PhaseCorrections phaseCorrections(m_phaseRate, target); for (std::size_t index = 0; index < m_histories.size(); ++index) { grid[index] = m_histories[index].empty() ? std::complex(0.0f, 0.0f) : estimateTimedPilot(m_histories[index], target, &phaseCorrections, &weights, window); } fftPlan(length, true).transform(grid.data(), delay.data()); for (int tap = 0; tap < length; ++tap) { profile[static_cast(tap)] += std::norm(delay[static_cast(tap)]); } } std::vector sorted = profile; const auto floorPosition = sorted.begin() + static_cast(sorted.size() / 4U); std::nth_element(sorted.begin(), floorPosition, sorted.end()); const double floor = *floorPosition; // The grid spans N / spacing samples of delay; time-interpolation errors // repeat with the pilot pattern across the grid, so they image the // channel at multiples of N / 16 samples (the per-symbol span). const double tapDelay = static_cast(usefulSamples) / (m_pilotSpacing * length); const double span = static_cast(usefulSamples) / m_pilotSpacing; const double imagePeriod = static_cast(usefulSamples) / FrequencyWienerSpacing; const double halfWindow = m_delayWidth / 2.0 + 1.5 * tapDelay; double inWindow = 0.0; double outside = 0.0; for (int tap = 0; tap < length; ++tap) { const double excess = std::max(0.0, profile[static_cast(tap)] - floor); const double delaySamples = tap * tapDelay; bool image = false; bool main = false; for (double shift = 0.0; shift < span - 1.0e-9; shift += imagePeriod) { if (std::abs(std::remainder(delaySamples - m_delayCentre - shift, span)) <= halfWindow) { (shift == 0.0 ? main : image) = true; } } if (main) { inWindow += excess; } else if (!image) { outside += excess; } } return inWindow > 1.0e-20 ? outside / inWindow : -1.0; } PilotTimingEstimate PilotTimingTracker::compare( const EqualizedSymbol& previous, const EqualizedSymbol& current, int usefulSamples ) { PilotTimingEstimate result; if (usefulSamples <= 0 || previous.minimumCarrier != current.minimumCarrier || previous.maximumCarrier != current.maximumCarrier || previous.channel.size() != current.channel.size() || previous.channel.size() < 3U) { return result; } double sumWeight = 0.0; double sumX = 0.0; double sumY = 0.0; double sumXX = 0.0; double sumXY = 0.0; double previousPhase = 0.0; bool havePhase = false; struct Observation { double carrier; double phase; double weight; }; std::vector observations; observations.reserve(previous.channel.size()); for (std::size_t index = 0; index < previous.channel.size(); ++index) { const std::complex left = previous.channel[index]; const std::complex right = current.channel[index]; const double weight = std::sqrt(std::norm(left) * std::norm(right)); if (weight <= 1.0e-12) { continue; } double phase = std::arg(right * std::conj(left)); if (havePhase) { while (phase - previousPhase > Pi) { phase -= 2.0 * Pi; } while (phase - previousPhase < -Pi) { phase += 2.0 * Pi; } } previousPhase = phase; havePhase = true; const double carrier = previous.minimumCarrier + static_cast(index); observations.push_back({carrier, phase, weight}); sumWeight += weight; sumX += weight * carrier; sumY += weight * phase; sumXX += weight * carrier * carrier; sumXY += weight * carrier * phase; } const double denominator = sumWeight * sumXX - sumX * sumX; if (observations.size() < 3U || sumWeight <= 1.0e-20 || std::abs(denominator) <= 1.0e-20) { return result; } const double slope = (sumWeight * sumXY - sumX * sumY) / denominator; const double intercept = (sumY - slope * sumX) / sumWeight; std::complex residualCorrelation(0.0, 0.0); for (const Observation& observation : observations) { residualCorrelation += observation.weight * std::polar( 1.0, observation.phase - intercept - slope * observation.carrier ); } result.timingShiftSamples = slope * usefulSamples / (2.0 * Pi); result.commonPhaseRadians = std::remainder(intercept, 2.0 * Pi); result.coherence = static_cast( std::abs(residualCorrelation) / sumWeight ); result.valid = std::isfinite(result.timingShiftSamples) && std::isfinite(result.commonPhaseRadians) && result.coherence >= 0.5f && std::abs(result.timingShiftSamples) <= usefulSamples / 8.0; return result; } } // namespace modemdrm