mirror of
https://github.com/DSheirer/sdrtrunk.git
synced 2026-11-20 04:27:48 -05:00
#2516 DMR Decoder enhancements. Architecture now shares common C4FM framework like P25P1 and NXDN. Adds soft decoding and enhanced/corrected error detection and correction. (#2517)
Co-authored-by: denny <dennis.sheirer@gmail.com>
This commit is contained in:
288
src/main/java/io/github/dsheirer/edac/soft/SoftBlockCode.java
Normal file
288
src/main/java/io/github/dsheirer/edac/soft/SoftBlockCode.java
Normal file
@@ -0,0 +1,288 @@
|
||||
/*
|
||||
* *****************************************************************************
|
||||
* Copyright (C) 2014-2026 Dennis Sheirer
|
||||
*
|
||||
* 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, either 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 for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
* ****************************************************************************
|
||||
*/
|
||||
|
||||
package io.github.dsheirer.edac.soft;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.function.IntPredicate;
|
||||
|
||||
/**
|
||||
* Soft decision decoder for short binary block codes, defined by the full set of valid codewords.
|
||||
*
|
||||
* Codewords are represented as integers where bit i (LSB = 0) holds codeword bit position i. Soft bit values are
|
||||
* log-likelihood ratios where a positive value favors a 1 bit and the magnitude indicates the reliability.
|
||||
*
|
||||
* Provides Chase-Pyndiah soft-in/soft-out decoding: tests the codewords nearest to the hard decision with the least
|
||||
* reliable bits flipped and produces extrinsic soft information for iterative (ie product code) decoding.
|
||||
*/
|
||||
public class SoftBlockCode
|
||||
{
|
||||
private static final int MAX_LENGTH = 17;
|
||||
private final int mLength;
|
||||
private final int[] mCodewords;
|
||||
private final int[] mDecodeTable;
|
||||
|
||||
/**
|
||||
* Constructs an instance.
|
||||
* @param length of the code in bits (maximum 17).
|
||||
* @param codewords set of all valid codewords.
|
||||
*/
|
||||
public SoftBlockCode(int length, int[] codewords)
|
||||
{
|
||||
if(length < 1 || length > MAX_LENGTH)
|
||||
{
|
||||
throw new IllegalArgumentException("Code length must be in range 1 - " + MAX_LENGTH);
|
||||
}
|
||||
|
||||
mLength = length;
|
||||
mCodewords = codewords.clone();
|
||||
|
||||
//Single bit error correction table: maps each word within distance 1 of a codeword to that codeword, or -1.
|
||||
mDecodeTable = new int[1 << length];
|
||||
Arrays.fill(mDecodeTable, -1);
|
||||
|
||||
for(int codeword: codewords)
|
||||
{
|
||||
mDecodeTable[codeword] = codeword;
|
||||
|
||||
for(int i = 0; i < length; i++)
|
||||
{
|
||||
mDecodeTable[codeword ^ (1 << i)] = codeword;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an instance by testing every possible word of the specified length to identify the valid codewords.
|
||||
* @param length of the code in bits (maximum 17).
|
||||
* @param isCodeword test that indicates if the word (bit i = codeword position i) is a valid codeword.
|
||||
* @return soft block code.
|
||||
*/
|
||||
public static SoftBlockCode fromCodewordTest(int length, IntPredicate isCodeword)
|
||||
{
|
||||
int count = 0;
|
||||
int[] candidates = new int[1 << length];
|
||||
|
||||
for(int word = 0; word < (1 << length); word++)
|
||||
{
|
||||
if(isCodeword.test(word))
|
||||
{
|
||||
candidates[count++] = word;
|
||||
}
|
||||
}
|
||||
|
||||
return new SoftBlockCode(length, Arrays.copyOf(candidates, count));
|
||||
}
|
||||
|
||||
/**
|
||||
* Code length in bits
|
||||
*/
|
||||
public int getLength()
|
||||
{
|
||||
return mLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* Valid codewords
|
||||
*/
|
||||
public int[] getCodewords()
|
||||
{
|
||||
return mCodewords;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates if the word is a valid codeword.
|
||||
*/
|
||||
public boolean isCodeword(int word)
|
||||
{
|
||||
return mDecodeTable[word] == word;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hard decision of the soft bits.
|
||||
* @param softBits array
|
||||
* @param indices of the code bits in the soft bits array
|
||||
* @return hard decision word.
|
||||
*/
|
||||
public static int getHardDecision(float[] softBits, int[] indices)
|
||||
{
|
||||
int word = 0;
|
||||
|
||||
for(int i = 0; i < indices.length; i++)
|
||||
{
|
||||
if(softBits[indices[i]] > 0)
|
||||
{
|
||||
word |= (1 << i);
|
||||
}
|
||||
}
|
||||
|
||||
return word;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the reliability metric for the candidate codeword: the sum of the soft bit magnitudes for each bit
|
||||
* where the candidate differs from the hard decision. Lower values are more likely.
|
||||
*/
|
||||
private static float getMetric(int candidate, int hard, float[] magnitudes)
|
||||
{
|
||||
float metric = 0.0f;
|
||||
int difference = candidate ^ hard;
|
||||
|
||||
while(difference != 0)
|
||||
{
|
||||
int bit = Integer.numberOfTrailingZeros(difference);
|
||||
metric += magnitudes[bit];
|
||||
difference &= difference - 1;
|
||||
}
|
||||
|
||||
return metric;
|
||||
}
|
||||
|
||||
/**
|
||||
* Chase-Pyndiah soft-in/soft-out decode. Generates test patterns by flipping each combination of the least
|
||||
* reliable bits in the hard decision, decodes each with the single bit error correction table and selects the
|
||||
* most likely candidate codeword as the decision. For each bit, the extrinsic value is the metric difference
|
||||
* between the decision and the best competing candidate that differs at that bit, less the soft input, or beta
|
||||
* (signed by the decision) when there is no competing candidate. When no test pattern decodes to a codeword, the
|
||||
* extrinsic values are zero.
|
||||
*
|
||||
* @param input soft bits array
|
||||
* @param indices of the code bits in the input and output arrays
|
||||
* @param output array to receive the extrinsic soft values at the code bit indices
|
||||
* @param leastReliableCount number of least reliable bits to use for test patterns
|
||||
* @param beta reliability to assign to a bit when there is no competing candidate codeword.
|
||||
* @return decision codeword, or -1 if no test pattern decoded to a codeword.
|
||||
*/
|
||||
public int decodeChase(float[] input, int[] indices, float[] output, int leastReliableCount, float beta)
|
||||
{
|
||||
float[] magnitudes = new float[mLength];
|
||||
|
||||
for(int i = 0; i < mLength; i++)
|
||||
{
|
||||
magnitudes[i] = Math.abs(input[indices[i]]);
|
||||
}
|
||||
|
||||
int hard = getHardDecision(input, indices);
|
||||
|
||||
//Identify the least reliable bit positions using selection sort over the small code length
|
||||
int p = Math.min(leastReliableCount, mLength);
|
||||
int[] leastReliable = new int[p];
|
||||
int used = 0;
|
||||
|
||||
for(int x = 0; x < p; x++)
|
||||
{
|
||||
int minIndex = -1;
|
||||
|
||||
for(int i = 0; i < mLength; i++)
|
||||
{
|
||||
if((used & (1 << i)) == 0 && (minIndex < 0 || magnitudes[i] < magnitudes[minIndex]))
|
||||
{
|
||||
minIndex = i;
|
||||
}
|
||||
}
|
||||
|
||||
leastReliable[x] = minIndex;
|
||||
used |= (1 << minIndex);
|
||||
}
|
||||
|
||||
//Decode each test pattern into a unique candidate codeword
|
||||
int[] candidates = new int[1 << p];
|
||||
float[] metrics = new float[1 << p];
|
||||
int candidateCount = 0;
|
||||
int decision = -1;
|
||||
float decisionMetric = Float.MAX_VALUE;
|
||||
|
||||
for(int pattern = 0; pattern < (1 << p); pattern++)
|
||||
{
|
||||
int test = hard;
|
||||
|
||||
for(int b = 0; b < p; b++)
|
||||
{
|
||||
if((pattern & (1 << b)) != 0)
|
||||
{
|
||||
test ^= (1 << leastReliable[b]);
|
||||
}
|
||||
}
|
||||
|
||||
int candidate = mDecodeTable[test];
|
||||
|
||||
if(candidate < 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
boolean duplicate = false;
|
||||
|
||||
for(int c = 0; c < candidateCount; c++)
|
||||
{
|
||||
if(candidates[c] == candidate)
|
||||
{
|
||||
duplicate = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(!duplicate)
|
||||
{
|
||||
float metric = getMetric(candidate, hard, magnitudes);
|
||||
candidates[candidateCount] = candidate;
|
||||
metrics[candidateCount] = metric;
|
||||
candidateCount++;
|
||||
|
||||
if(metric < decisionMetric)
|
||||
{
|
||||
decisionMetric = metric;
|
||||
decision = candidate;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for(int i = 0; i < mLength; i++)
|
||||
{
|
||||
if(decision < 0)
|
||||
{
|
||||
output[indices[i]] = 0.0f;
|
||||
continue;
|
||||
}
|
||||
|
||||
int mask = 1 << i;
|
||||
float sign = (decision & mask) != 0 ? 1.0f : -1.0f;
|
||||
float competitorMetric = Float.MAX_VALUE;
|
||||
|
||||
for(int c = 0; c < candidateCount; c++)
|
||||
{
|
||||
if(((candidates[c] ^ decision) & mask) != 0 && metrics[c] < competitorMetric)
|
||||
{
|
||||
competitorMetric = metrics[c];
|
||||
}
|
||||
}
|
||||
|
||||
if(competitorMetric < Float.MAX_VALUE)
|
||||
{
|
||||
output[indices[i]] = sign * (competitorMetric - decisionMetric) - input[indices[i]];
|
||||
}
|
||||
else
|
||||
{
|
||||
output[indices[i]] = sign * beta;
|
||||
}
|
||||
}
|
||||
|
||||
return decision;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,278 @@
|
||||
/*
|
||||
* *****************************************************************************
|
||||
* Copyright (C) 2014-2026 Dennis Sheirer
|
||||
*
|
||||
* 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, either 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 for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
* ****************************************************************************
|
||||
*/
|
||||
|
||||
package io.github.dsheirer.edac.soft;
|
||||
|
||||
import io.github.dsheirer.bits.CorrectedBinaryMessage;
|
||||
|
||||
/**
|
||||
* Iterative soft decision decoder for product codes where each row is a codeword of a short block code and each
|
||||
* column has even parity (ie the final row is the column parity row). Bits are ordered row by row.
|
||||
*
|
||||
* Alternately decodes the rows using Chase-Pyndiah decoding and the even parity columns using min-sum decoding,
|
||||
* exchanging extrinsic soft information, until the decisions form a valid product codeword. Rejects decisions
|
||||
* where the correction cost (sum of the normalized channel soft bit magnitudes for the corrected bits) exceeds the
|
||||
* maximum, since a low correction cost indicates a reliable decision.
|
||||
*/
|
||||
public class SoftProductCodeDecoder
|
||||
{
|
||||
private static final int CHASE_LEAST_RELIABLE_BITS = 4;
|
||||
//Each half iteration decodes either all rows or all columns, starting with the rows. Alpha scales the extrinsic
|
||||
//information fed to the next half iteration and beta is the reliability assigned to row bits without a competing
|
||||
//candidate codeword.
|
||||
private static final float[] SOFT_ALPHA = new float[]{0.0f, 0.2f, 0.3f, 0.5f, 0.7f, 0.9f, 1.0f, 1.0f};
|
||||
private static final float[] SOFT_BETA = new float[]{0.2f, 0.4f, 0.6f, 0.8f, 1.0f, 1.0f, 1.0f, 1.0f};
|
||||
private final SoftBlockCode mRowCode;
|
||||
private final int mRowCount;
|
||||
private final int mColumnCount;
|
||||
private final int mLength;
|
||||
private final int[][] mRowIndexes;
|
||||
private final float mMaxCorrectionCost;
|
||||
|
||||
/**
|
||||
* Constructs an instance
|
||||
* @param rowCode for each row, where the code length is the column count
|
||||
* @param rowCount number of rows, including the column parity row
|
||||
* @param maxCorrectionCost maximum correction cost for a decision, or Float.MAX_VALUE for no limit
|
||||
*/
|
||||
public SoftProductCodeDecoder(SoftBlockCode rowCode, int rowCount, float maxCorrectionCost)
|
||||
{
|
||||
mRowCode = rowCode;
|
||||
mRowCount = rowCount;
|
||||
mColumnCount = rowCode.getLength();
|
||||
mLength = mRowCount * mColumnCount;
|
||||
mMaxCorrectionCost = maxCorrectionCost;
|
||||
mRowIndexes = new int[mRowCount][mColumnCount];
|
||||
|
||||
for(int row = 0; row < mRowCount; row++)
|
||||
{
|
||||
for(int column = 0; column < mColumnCount; column++)
|
||||
{
|
||||
mRowIndexes[row][column] = row * mColumnCount + column;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Decodes the (de-interleaved) soft bits.
|
||||
* @param channel soft bits ordered row by row where a positive value favors a 1 bit.
|
||||
* @return decoded message with the corrected bit count set, or null if decoding doesn't converge or the correction
|
||||
* cost exceeds the maximum.
|
||||
*/
|
||||
public CorrectedBinaryMessage decode(float[] channel)
|
||||
{
|
||||
if(channel == null || channel.length != mLength)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
//Normalize the channel soft bits to a mean magnitude of 1 for the alpha and beta settings
|
||||
float sum = 0.0f;
|
||||
|
||||
for(float softBit: channel)
|
||||
{
|
||||
sum += Math.abs(softBit);
|
||||
}
|
||||
|
||||
if(sum <= 0.0f)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
float scale = mLength / sum;
|
||||
float[] received = new float[mLength];
|
||||
|
||||
for(int i = 0; i < mLength; i++)
|
||||
{
|
||||
received[i] = channel[i] * scale;
|
||||
}
|
||||
|
||||
float[] extrinsic = new float[mLength];
|
||||
float[] input = new float[mLength];
|
||||
float[] output = new float[mLength];
|
||||
|
||||
for(int halfIteration = 0; halfIteration < SOFT_ALPHA.length; halfIteration++)
|
||||
{
|
||||
float alpha = SOFT_ALPHA[halfIteration];
|
||||
|
||||
for(int i = 0; i < mLength; i++)
|
||||
{
|
||||
input[i] = received[i] + alpha * extrinsic[i];
|
||||
}
|
||||
|
||||
CorrectedBinaryMessage decoded = new CorrectedBinaryMessage(mLength);
|
||||
|
||||
if(halfIteration % 2 == 0)
|
||||
{
|
||||
boolean allDecoded = true;
|
||||
|
||||
for(int row = 0; row < mRowCount; row++)
|
||||
{
|
||||
int word = mRowCode.decodeChase(input, mRowIndexes[row], output, CHASE_LEAST_RELIABLE_BITS,
|
||||
SOFT_BETA[halfIteration]);
|
||||
allDecoded &= word >= 0;
|
||||
|
||||
for(int column = 0; column < mColumnCount; column++)
|
||||
{
|
||||
if((word & (1 << column)) != 0)
|
||||
{
|
||||
decoded.set(mRowIndexes[row][column]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(!allDecoded)
|
||||
{
|
||||
decoded = null;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
decodeColumns(input, output);
|
||||
|
||||
for(int i = 0; i < mLength; i++)
|
||||
{
|
||||
if(input[i] + output[i] > 0)
|
||||
{
|
||||
decoded.set(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Check for a valid product codeword from the decisions of this half iteration
|
||||
if(decoded != null && isValid(decoded))
|
||||
{
|
||||
int corrected = 0;
|
||||
float cost = 0.0f;
|
||||
|
||||
for(int i = 0; i < mLength; i++)
|
||||
{
|
||||
if(decoded.get(i) != (received[i] > 0))
|
||||
{
|
||||
corrected++;
|
||||
cost += Math.abs(received[i]);
|
||||
}
|
||||
}
|
||||
|
||||
if(cost > mMaxCorrectionCost)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
decoded.setCorrectedBitCount(corrected);
|
||||
return decoded;
|
||||
}
|
||||
|
||||
//Normalize the extrinsic information to a mean magnitude of 1 for the next half iteration
|
||||
float extrinsicSum = 0.0f;
|
||||
|
||||
for(float value: output)
|
||||
{
|
||||
extrinsicSum += Math.abs(value);
|
||||
}
|
||||
|
||||
float extrinsicScale = extrinsicSum > 0.0f ? mLength / extrinsicSum : 0.0f;
|
||||
|
||||
for(int i = 0; i < mLength; i++)
|
||||
{
|
||||
extrinsic[i] = output[i] * extrinsicScale;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates if each row is a row codeword and each column has even parity.
|
||||
*/
|
||||
private boolean isValid(CorrectedBinaryMessage message)
|
||||
{
|
||||
int parity = 0;
|
||||
|
||||
for(int row = 0; row < mRowCount; row++)
|
||||
{
|
||||
int word = 0;
|
||||
|
||||
for(int column = 0; column < mColumnCount; column++)
|
||||
{
|
||||
if(message.get(mRowIndexes[row][column]))
|
||||
{
|
||||
word |= (1 << column);
|
||||
}
|
||||
}
|
||||
|
||||
if(!mRowCode.isCodeword(word))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
parity ^= word;
|
||||
}
|
||||
|
||||
return parity == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Min-sum (max-log) soft-in/soft-out decoding of the even parity columns. The extrinsic value for each bit is
|
||||
* the product of the signs and the minimum magnitude of the other bits in the column.
|
||||
* @param input soft bits
|
||||
* @param output to receive the extrinsic values.
|
||||
*/
|
||||
private void decodeColumns(float[] input, float[] output)
|
||||
{
|
||||
for(int column = 0; column < mColumnCount; column++)
|
||||
{
|
||||
float sign = 1.0f;
|
||||
float minimum1 = Float.MAX_VALUE;
|
||||
float minimum2 = Float.MAX_VALUE;
|
||||
int minimumRow = -1;
|
||||
|
||||
for(int row = 0; row < mRowCount; row++)
|
||||
{
|
||||
float value = input[mRowIndexes[row][column]];
|
||||
|
||||
if(value < 0)
|
||||
{
|
||||
sign = -sign;
|
||||
}
|
||||
|
||||
float magnitude = Math.abs(value);
|
||||
|
||||
if(magnitude < minimum1)
|
||||
{
|
||||
minimum2 = minimum1;
|
||||
minimum1 = magnitude;
|
||||
minimumRow = row;
|
||||
}
|
||||
else if(magnitude < minimum2)
|
||||
{
|
||||
minimum2 = magnitude;
|
||||
}
|
||||
}
|
||||
|
||||
for(int row = 0; row < mRowCount; row++)
|
||||
{
|
||||
int index = mRowIndexes[row][column];
|
||||
float value = input[index];
|
||||
float otherSign = value < 0 ? -sign : sign;
|
||||
output[index] = otherSign * (row == minimumRow ? minimum2 : minimum1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user