decodeWithECCount method

int decodeWithECCount(
  1. List<int> received,
  2. int twoS
)

Decodes given set of received codewords, which include both data and error-correction codewords. Really, this means it uses Reed-Solomon to detect and correct errors, in-place, in the input.

@param received data and error-correction codewords @param twoS number of error-correction codewords available @return the number of errors corrected @throws ReedSolomonException if decoding fails for any reason

Implementation

int decodeWithECCount(List<int> received, int twoS) {
  final poly = GenericGFPoly(_field, received);
  final syndromeCoefficients = Int32List(twoS);
  bool noError = true;
  for (int i = 0; i < twoS; i++) {
    final eval = poly.evaluateAt(_field.exp(i + _field.generatorBase));
    syndromeCoefficients[syndromeCoefficients.length - 1 - i] = eval;
    if (eval != 0) {
      noError = false;
    }
  }
  if (noError) {
    return 0;
  }
  final syndrome = GenericGFPoly(_field, syndromeCoefficients);
  final sigmaOmega =
      _runEuclideanAlgorithm(_field.buildMonomial(twoS, 1), syndrome, twoS);
  final sigma = sigmaOmega[0];
  final omega = sigmaOmega[1];
  final errorLocations = _findErrorLocations(sigma);
  final errorMagnitudes = _findErrorMagnitudes(omega, errorLocations);
  for (int i = 0; i < errorLocations.length; i++) {
    final position = received.length - 1 - _field.log(errorLocations[i]);
    if (position < 0) {
      throw ReedSolomonException('Bad error location');
    }
    received[position] =
        GenericGF.addOrSubtract(received[position], errorMagnitudes[i]);
  }
  return errorLocations.length;
}