lf nrz demod: start the bitstream at the first edge

The samples before the first level change were counted as bits, and where no
edge arrives inside ten clocks the long-run flush fired first and invented ten
of them out of the quiet lead-in. The count sat on a rounding boundary, so one
sample of jitter in that edge added or dropped a leading bit and rotated the
rest of the word - while startIdx, derived from the same count, held i % clk and
could name no sample.

A level change can only fall on a bit boundary, so start there and report it.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Matthew CarrollandClaude Opus 5 committed 2026-09-10 14:57:11 -07:00
1 parent 0884083530
commit 1ebcf225f1
2 files changed
+20 -6

No files matched your search

+19 -6
View File
@@ -1902,18 +1902,31 @@ int nrzRawDemod(uint8_t *dest, size_t *size, int *clk, const int *invert, int *s
if (dest[i] <= low) bit = 0;
dest[i] = bit;
}
//now demod based on clock (rf/32 = 32 1's for one 1 bit, 32 0's for one 0 bit)
// start on the first level change: it is the earliest sample known to be a
// bit boundary. counting the samples before it as bits rotated the stream
// whenever that edge moved by one, and left startIdx naming no sample
size_t lastBit = 0;
size_t numBits = 0;
bool anchored = false;
for (i = 21; i < *size - 20; i++) {
bool edge = (dest[i] != dest[i - 1]);
if (anchored == false) {
if (edge == false) {
continue;
}
*startIdx = (int)i;
if (g_debugMode == 2) prnt("DEBUG NRZ: startIdx %i", *startIdx);
lastBit = i - 1;
anchored = true;
continue;
}
//if transition detected or large number of same bits - store the passed bits
if (dest[i] != dest[i - 1] || (i - lastBit) == (10 * *clk)) {
if (edge || (i - lastBit) == (10 * *clk)) {
memset(dest + numBits, dest[i - 1] ^ *invert, (i - lastBit + (*clk / 4)) / *clk);
numBits += (i - lastBit + (*clk / 4)) / *clk;
if (lastBit == 0) {
*startIdx = i - (numBits * *clk);
if (g_debugMode == 2) prnt("DEBUG NRZ: startIdx %i", *startIdx);
}
lastBit = i - 1;
}
}