Skip to content

Commit 15e824f

Browse files
committed
Added missing Gray coding
1 parent 20e0549 commit 15e824f

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

AudioCoder/src/main/java/org/operatorfoundation/audiocoder/mfsk/MFSKEncoder.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ object MFSKEncoder
146146
toneIndex = (toneIndex shl 1) or (if (bit) 1 else 0)
147147
}
148148

149-
symbols[symbolIndex] = toneIndex
149+
symbols[symbolIndex] = toneIndex xor (toneIndex shr 1)
150150
}
151151

152152
return symbols

AudioCoder/src/main/java/org/operatorfoundation/audiocoder/mfsk/MFSKStation.kt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,10 @@ class MFSKStation(
189189
val energies = DoubleArray(configuration.mode.toneCount) { i ->
190190
GoertzelFilter.energy(samples, toneFrequencies[i], configuration.sampleRate)
191191
}
192+
192193
// maxByOrNull is safe, toneCount is always >= 8 by MFSKMode's design.
193-
val winnerToneIndex = energies.indices.maxByOrNull { energies[it] }!!
194+
val rawToneIndex = energies.indices.maxByOrNull { energies[it] }!!
195+
val winnerToneIndex = grayDecode(rawToneIndex)
194196

195197
// Extract bitsPerSymbol bits from the winner, MSB-first,
196198
// and feed each into the Varicode decoder.
@@ -303,4 +305,17 @@ class MFSKStation(
303305

304306
return buffer
305307
}
308+
309+
private fun grayDecode(gray: Int): Int
310+
{
311+
var n = gray
312+
var mask = n shr 1
313+
314+
while (mask != 0) {
315+
n = n xor mask
316+
mask = mask shr 1
317+
}
318+
319+
return n
320+
}
306321
}

0 commit comments

Comments
 (0)