decodeMinSum method

LdpcDecodeResult decodeMinSum(
  1. List<int> logLikelihoodRatios, {
  2. required int llrWidth,
  3. required int maxIterations,
  4. int normalizationNumerator = 1,
  5. int normalizationShift = 0,
  6. int offset = 0,
})

Decodes quantized two's-complement LLRs with flooding min-sum.

Positive LLRs favor zero and negative LLRs favor one. The optional normalization factor is normalizationNumerator / 2^normalizationShift. offset is subtracted from each normalized check-node magnitude with a floor at zero. The result reports convergence, not a bounded-distance correction guarantee.

Implementation

LdpcDecodeResult decodeMinSum(
  List<int> logLikelihoodRatios, {
  required int llrWidth,
  required int maxIterations,
  int normalizationNumerator = 1,
  int normalizationShift = 0,
  int offset = 0,
}) {
  _validateMinSumConfiguration(
    llrWidth: llrWidth,
    maxIterations: maxIterations,
    normalizationNumerator: normalizationNumerator,
    normalizationShift: normalizationShift,
    offset: offset,
  );
  if (logLikelihoodRatios.length != length) {
    throw ArgumentError.value(
      logLikelihoodRatios,
      'logLikelihoodRatios',
      'Expected one LLR per codeword variable.',
    );
  }
  final minimum = -(1 << (llrWidth - 1));
  final maximum = (1 << (llrWidth - 1)) - 1;
  if (logLikelihoodRatios
      .any((value) => value < minimum || value > maximum)) {
    throw ArgumentError.value(
      logLikelihoodRatios,
      'logLikelihoodRatios',
      "Every LLR must fit in $llrWidth-bit two's-complement.",
    );
  }

  var hardCodeword = _hardDecision(logLikelihoodRatios);
  final inputSyndrome = syndrome(hardCodeword);
  if (inputSyndrome == BigInt.zero) {
    return LdpcDecodeResult(
      codeword: hardCodeword,
      message: extractMessage(hardCodeword),
      syndrome: BigInt.zero,
      inputHadError: false,
      converged: true,
      iterationsUsed: 0,
    );
  }

  var variableToCheck = [
    for (final edge in edges) logLikelihoodRatios[edge.variable],
  ];
  var finalSyndrome = inputSyndrome;
  for (var iteration = 1; iteration <= maxIterations; iteration++) {
    final checkToVariable = List<int>.filled(edgeCount, 0);
    for (var check = 0; check < parityBits; check++) {
      final incident = checkEdges[check];
      for (final edgeIndex in incident) {
        var negative = false;
        var minimumMagnitude = 1 << (llrWidth - 1);
        for (final otherEdge in incident) {
          if (otherEdge == edgeIndex) {
            continue;
          }
          final value = variableToCheck[otherEdge];
          negative ^= value < 0;
          minimumMagnitude = min(minimumMagnitude, value.abs());
        }
        final normalized =
            (minimumMagnitude * normalizationNumerator) >> normalizationShift;
        final magnitude = max(0, normalized - offset);
        checkToVariable[edgeIndex] = negative ? -magnitude : magnitude;
      }
    }

    final nextVariableToCheck = List<int>.filled(edgeCount, 0);
    final posterior = List<int>.filled(length, 0);
    for (var variable = 0; variable < length; variable++) {
      final incident = variableEdges[variable];
      posterior[variable] = _saturateSigned(
        logLikelihoodRatios[variable] +
            incident.fold(
              0,
              (sum, edgeIndex) => sum + checkToVariable[edgeIndex],
            ),
        llrWidth,
      );
      for (final edgeIndex in incident) {
        nextVariableToCheck[edgeIndex] = _saturateSigned(
          logLikelihoodRatios[variable] +
              incident.where((otherEdge) => otherEdge != edgeIndex).fold(
                    0,
                    (sum, otherEdge) => sum + checkToVariable[otherEdge],
                  ),
          llrWidth,
        );
      }
    }
    hardCodeword = _hardDecision(posterior);
    finalSyndrome = syndrome(hardCodeword);
    if (finalSyndrome == BigInt.zero) {
      return LdpcDecodeResult(
        codeword: hardCodeword,
        message: extractMessage(hardCodeword),
        syndrome: BigInt.zero,
        inputHadError: true,
        converged: true,
        iterationsUsed: iteration,
      );
    }
    variableToCheck = nextVariableToCheck;
  }

  return LdpcDecodeResult(
    codeword: hardCodeword,
    message: extractMessage(hardCodeword),
    syndrome: finalSyndrome,
    inputHadError: true,
    converged: false,
    iterationsUsed: maxIterations,
  );
}