From 7fcf03533797d6dd157e2f50e2ce96d4ed0f55f8 Mon Sep 17 00:00:00 2001 From: Steve Pinkham Date: Fri, 13 Nov 2026 01:29:50 -0500 Subject: [PATCH] edac: correct Golay(24,12) words that are not at the start of the message (#2631) Golay24.checkAndCorrect indexed the word from bit 0 (getInt(0, 22), xor(12, ...), flip(index)) and took the parity over the whole message, so it was only right for a message that is exactly one 24-bit word. P25 TDULC words 2-12 and the DMR voice superframe words at 24 and 48 were "corrected" in the wrong bits. Decode any other word in a 24-bit copy and write it back. Co-authored-by: Claude Opus 5.5 --- .../java/io/github/dsheirer/edac/Golay24.java | 23 +++++ .../dsheirer/edac/Golay24OffsetTest.java | 85 +++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 src/test/java/io/github/dsheirer/edac/Golay24OffsetTest.java diff --git a/src/main/java/io/github/dsheirer/edac/Golay24.java b/src/main/java/io/github/dsheirer/edac/Golay24.java index 0453a591..00363d43 100644 --- a/src/main/java/io/github/dsheirer/edac/Golay24.java +++ b/src/main/java/io/github/dsheirer/edac/Golay24.java @@ -68,6 +68,29 @@ public class Golay24 */ public static int checkAndCorrect(CorrectedBinaryMessage message, int startIndex) { + //The correction below indexes the word from bit 0 and takes the parity over the whole message, so it is only + //correct for a message that is exactly the 24-bit word. Decode any other word in a 24-bit copy. + if(startIndex != 0 || message.size() != 24) + { + CorrectedBinaryMessage word = new CorrectedBinaryMessage(24); + int original = message.getInt(startIndex, startIndex + 23); + word.load(0, 24, original); + int result = checkAndCorrectWord(word); + int corrected = word.getInt(0, 23); + message.load(startIndex, 24, corrected); + message.incrementCorrectedBitCount(Integer.bitCount(original ^ corrected)); + return result; + } + + return checkAndCorrectWord(message); + } + + /** + * Error detection and correction of a message that is exactly one 24-bit word. + */ + private static int checkAndCorrectWord(CorrectedBinaryMessage message) + { + int startIndex = 0; boolean parityError = message.cardinality() % 2 != 0; int syndrome = getSyndrome(message, startIndex); diff --git a/src/test/java/io/github/dsheirer/edac/Golay24OffsetTest.java b/src/test/java/io/github/dsheirer/edac/Golay24OffsetTest.java new file mode 100644 index 00000000..1819eba6 --- /dev/null +++ b/src/test/java/io/github/dsheirer/edac/Golay24OffsetTest.java @@ -0,0 +1,85 @@ +/* + * ***************************************************************************** + * 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 + * **************************************************************************** + */ + +package io.github.dsheirer.edac; + +import io.github.dsheirer.bits.CorrectedBinaryMessage; +import java.util.Random; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Tests Golay(24,12) correction of the data bits of words that are not at the start of a 24-bit message. + */ +public class Golay24OffsetTest +{ + private static int encode(int data) + { + int checksum = 0; + + for(int bit = 0; bit < 12; bit++) + { + if((data & (1 << (11 - bit))) != 0) + { + checksum ^= Golay24.CHECKSUMS[bit]; + } + } + + int word23 = (data << 11) | checksum; + return (word23 << 1) | (Integer.bitCount(word23) & 1); + } + + @Test + public void correctsUpToThreeErrorsInEveryWordOfALongerMessage() + { + Random random = new Random(1); + + for(int trial = 0; trial < 2000; trial++) + { + int[] words = new int[3]; + CorrectedBinaryMessage message = new CorrectedBinaryMessage(72); + + for(int w = 0; w < 3; w++) + { + words[w] = encode(random.nextInt(4096)); + int received = words[w]; + + for(int e = random.nextInt(4); e > 0; e--) + { + received ^= 1 << random.nextInt(24); + } + + message.load(w * 24, 24, received); + } + + for(int w = 0; w < 3; w++) + { + Golay24.checkAndCorrect(message, w * 24); + } + + for(int w = 0; w < 3; w++) + { + //The 12 data bits. (The core correction can leave the overall parity bit wrong when it corrects other + //bits, which no caller reads.) + assertEquals(words[w] >> 12, message.getInt(w * 24, w * 24 + 11), "trial " + trial + " word " + w); + } + } + } +}