diff --git a/README.md b/README.md index 16eff18..dfc8144 100644 --- a/README.md +++ b/README.md @@ -204,6 +204,13 @@ Public Methods * symbols - Array of channel symbols to transmit returned by the method. * Ensure that you pass a uint8_t array of at least size JT4_SYMBOL_COUNT to the method. * + * JT4 transmits a 206-symbol frame: 206 data symbols interleaved with the + * 206-bit pseudo-random sync vector from WSJT-X gen4 (sync in the LSB, data + * in the MSB, so each symbol is 0-3 for the 4-FSK tones). If the message + * contains a negative report (a '-' in the report field — the 9th character + * or later — followed by a digit 0-3), the sync vector is inverted, exactly + * as WSJT-X does. + * */ ``` @@ -315,6 +322,31 @@ Also, a big thank you to Murray Greenman, ZL1BPU for working allowing me to pick Changelog --------- +* v1.4.3 + + * Fix the JT4 encoder to produce frames that match WSJT-X. The previous + implementation was wrong in several ways: it used a 207-symbol + `JT4_SYMBOL_COUNT` (the JT4 frame is 206 symbols), it filled a + 206-bit buffer and then shifted everything right by one symbol + (`memmove` + a hardcoded `s[0] = 0`), it used a truncated 126-entry + sync vector (several entries were garbled), and it put the sync in the + MSB / data in the LSB. + * The sync vector is now the full 207-entry table from WSJT-X `gen4.f90` + (`npr`, with an inert leading element), applied as + `tone = 2*data + sync` (data MSB, sync LSB), which is the correct + 4-FSK mapping. + * Negative reports now invert the sync vector, matching WSJT-X: when the + prepped message has a `-` in the report field followed by a digit + `0`-`3`, the sync bits are complemented. + * Output is bit-exact against a Python reimplementation of the WSJT-X + `gen4`/`encode4` pipeline for positive, negative, and non-report + messages (`NT7S`, `G0ABC M0SUP`, `G0ABC M0SUP -05`, `CQ DX`, + `AAAAAAA -05`, ...). + * Harness now has 20 checks (was 17): JT4 frame length/tone range, + bit-exact JT4 reference output, and JT4 inverted-sync coverage. + * Fixed the `JT4_SYMBOL_COUNT` doc typo in `jt4_encode` and the example + comment (both said 207 symbols). + * v1.4.2 * Fix #32 — 2m/6m frequency overflow in the example sketch: `freq` was an diff --git a/examples/Si5351JTDemo/Si5351JTDemo.ino b/examples/Si5351JTDemo/Si5351JTDemo.ino index fc60452..e821bf2 100644 --- a/examples/Si5351JTDemo/Si5351JTDemo.ino +++ b/examples/Si5351JTDemo/Si5351JTDemo.ino @@ -90,7 +90,7 @@ char call[] = "N0CALL"; char loc[] = "AA00"; int8_t dbm = 27; // Sized for fsq_dir_encode's documented maximum (2 symbols per char + terminator); -// also covers jt4_encode (207 symbols) and every other mode. +// also covers jt4_encode (206 symbols) and every other mode. uint8_t tx_buffer[320]; enum mode cur_mode = DEFAULT_MODE; uint8_t symbol_count; diff --git a/library.properties b/library.properties index 342d486..c4f91dd 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Etherkit JTEncode -version=1.4.2 +version=1.4.3 author=Jason Milldrum maintainer=Jason Milldrum sentence=Generate JT65, JT9, JT4, FT8, WSPR, and FSQ symbols on your Arduino. diff --git a/src/JTEncode.cpp b/src/JTEncode.cpp index c33730d..274394c 100644 --- a/src/JTEncode.cpp +++ b/src/JTEncode.cpp @@ -164,7 +164,7 @@ void JTEncode::jt9_encode(const char * msg, uint8_t * symbols) * * message - Plaintext Type 6 message. * symbols - Array of channel symbols to transmit returned by the method. - * Ensure that you pass a uint8_t array of at least size JT9_SYMBOL_COUNT to the method. + * Ensure that you pass a uint8_t array of at least size JT4_SYMBOL_COUNT to the method. * */ void JTEncode::jt4_encode(const char * msg, uint8_t * symbols) @@ -185,18 +185,16 @@ void JTEncode::jt4_encode(const char * msg, uint8_t * symbols) // Convolutional Encoding // --------------------- - uint8_t s[JT4_SYMBOL_COUNT]; + uint8_t s[JT4_BIT_COUNT]; convolve(c, s, 13, JT4_BIT_COUNT); // Interleaving // ------------ jt9_interleave(s); - memmove(s + 1, s, JT4_BIT_COUNT); - s[0] = 0; // Append a 0 bit to start of sequence - // Merge with sync vector - // ---------------------- - jt4_merge_sync_vector(s, symbols); + // Merge with sync vector (with inverted sync for negative reports) + // --------------------------------------------------------------- + jt4_merge_sync_vector(message, s, symbols); } /* @@ -1511,25 +1509,52 @@ void JTEncode::jt9_merge_sync_vector(uint8_t * g, uint8_t * symbols) } } -void JTEncode::jt4_merge_sync_vector(uint8_t * g, uint8_t * symbols) +void JTEncode::jt4_merge_sync_vector(const char * message, uint8_t * g, uint8_t * symbols) { + // 207-entry sync vector (WSJT-X gen4: npr(2:) is the 206-bit stream used + // for symbols 0..205; npr(1) is inert and kept as a leading element). + static const uint8_t sync_vector[JT4_SYMBOL_COUNT + 1] PROGMEM = + { 0, 0, 0, 0, 1, 1, 0, 0, + 0, 1, 1, 0, 1, 1, 0, 0, + 1, 0, 1, 0, 0, 0, 0, 0, + 0, 0, 1, 1, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 1, 0, 1, 1, 0, 1, 1, 0, + 1, 0, 1, 1, 1, 1, 1, 0, + 1, 0, 0, 0, 1, 0, 0, 1, + 0, 0, 1, 1, 1, 1, 1, 0, + 0, 0, 1, 0, 1, 0, 0, 0, + 1, 1, 1, 1, 0, 1, 1, 0, + 0, 1, 0, 0, 0, 1, 1, 0, + 1, 0, 1, 0, 1, 0, 1, 0, + 1, 1, 1, 1, 1, 0, 1, 0, + 1, 0, 1, 1, 0, 1, 0, 1, + 0, 1, 1, 1, 0, 0, 1, 0, + 1, 1, 0, 1, 1, 1, 1, 0, + 0, 0, 0, 1, 1, 0, 1, 1, + 0, 0, 0, 1, 1, 1, 0, 1, + 1, 1, 0, 1, 1, 1, 0, 0, + 1, 0, 0, 0, 1, 1, 0, 1, + 1, 0, 0, 1, 0, 0, 0, 1, + 1, 1, 1, 1, 1, 0, 0, 1, + 1, 0, 0, 0, 0, 1, 1, 0, + 0, 0, 1, 0, 1, 1, 0, 1, + 1, 1, 1, 0, 1, 0, 1,}; + + // Inverted sync for negative-report messages (WSJT-X gen4): a '-' at + // 0-indexed position >= 8 (1-indexed >= 9, i.e. in the report field) + // followed by one of '0'..'3' (the first digit of reports -01..-30). + const char * dash = strchr(message, '-'); + int i1 = (dash == NULL) ? -1 : (int)(dash - message); + bool invert = (i1 >= 8) && (i1 + 1 < 13) && (message[i1 + 1] >= '0' && message[i1 + 1] <= '3'); + uint8_t i; - static const uint8_t sync_vector[JT4_SYMBOL_COUNT] PROGMEM = - {0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, - 0, 0, 0, 0, 1, 1, 0, 0, 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,1 ,0 ,1 ,1, - 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, - 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, - 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, - 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, - 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, - 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, - 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, - 0, 1, 1, 1, 1, 0, 1, 0, 1}; - - for(i = 0; i < JT4_SYMBOL_COUNT; i++) - { - symbols[i] = pgm_read_byte(&sync_vector[i]) + (2 * g[i]); - } + for(i = 0; i < JT4_BIT_COUNT; i++) + { + uint8_t sync = pgm_read_byte(&sync_vector[i + 1]); + if(invert) sync = (uint8_t)(1 - sync); + symbols[i] = (uint8_t)(sync + 2 * g[i]); + } } void JTEncode::wspr_merge_sync_vector(uint8_t * g, uint8_t * symbols) diff --git a/src/JTEncode.h b/src/JTEncode.h index 8ebad94..22b3632 100644 --- a/src/JTEncode.h +++ b/src/JTEncode.h @@ -36,7 +36,7 @@ #define JT65_SYMBOL_COUNT 126 #define JT9_SYMBOL_COUNT 85 -#define JT4_SYMBOL_COUNT 207 +#define JT4_SYMBOL_COUNT 206 #define WSPR_SYMBOL_COUNT 162 #define FT8_SYMBOL_COUNT 79 @@ -246,7 +246,7 @@ class JTEncode void ft8_encode(uint8_t*, uint8_t*); void jt65_merge_sync_vector(uint8_t *, uint8_t *); void jt9_merge_sync_vector(uint8_t *, uint8_t *); - void jt4_merge_sync_vector(uint8_t *, uint8_t *); + void jt4_merge_sync_vector(const char * message, uint8_t *, uint8_t *); void wspr_merge_sync_vector(uint8_t *, uint8_t *); void ft8_merge_sync_vector(uint8_t*, uint8_t*); void convolve(uint8_t *, uint8_t *, uint8_t, uint8_t); diff --git a/tests/harness.cpp b/tests/harness.cpp index 16d9fc3..4c929f9 100644 --- a/tests/harness.cpp +++ b/tests/harness.cpp @@ -68,6 +68,76 @@ static int test_jt4_strcpy_overflow() return 0; } +static int test_jt4_frame_length() +{ + // 206-tone frame, all tones in {0..3}; catches uninitialized reads and + // wrong symbol-count assumptions. + JTEncode enc; + uint8_t symbols[JT4_SYMBOL_COUNT]; + memset(symbols, 0xFF, sizeof(symbols)); + enc.jt4_encode("NT7S", symbols); + for (int i = 0; i < JT4_SYMBOL_COUNT; i++) { + if (symbols[i] > 3) return 1; + } + return 0; +} + +// Bit-exact reference output for "NT7S" (Python reimplementation of the +// WSJT-X gen4/encode4 algorithm, same 72-bit packing as the library). +static int test_jt4_bit_exact() +{ + JTEncode enc; + uint8_t symbols[JT4_SYMBOL_COUNT]; + enc.jt4_encode("NT7S", symbols); + static const uint8_t expected[] = { + 2, 0, 0, 1, 1, 0, 0, 2, 3, 3, 0, 1, 3, 2, 2, 3, + 0, 3, 0, 2, 0, 2, 2, 2, 0, 1, 1, 0, 0, 2, 0, 2, + 0, 0, 0, 0, 2, 0, 0, 1, 2, 1, 1, 0, 1, 3, 2, 3, + 2, 1, 3, 1, 1, 1, 0, 3, 2, 2, 0, 1, 0, 0, 3, 0, + 0, 3, 3, 3, 1, 1, 0, 0, 2, 1, 0, 1, 2, 2, 2, 3, + 3, 1, 3, 2, 1, 1, 2, 2, 3, 2, 0, 2, 1, 1, 2, 1, + 0, 1, 2, 1, 0, 3, 2, 1, 3, 1, 3, 1, 2, 3, 0, 1, + 2, 3, 1, 2, 3, 0, 3, 2, 1, 3, 1, 0, 0, 3, 0, 1, + 3, 0, 1, 3, 3, 1, 2, 2, 0, 0, 3, 3, 2, 3, 1, 0, + 0, 0, 3, 3, 1, 2, 1, 3, 1, 0, 3, 3, 3, 0, 2, 3, + 2, 2, 2, 1, 3, 2, 1, 1, 0, 0, 1, 2, 2, 2, 3, 1, + 1, 1, 3, 3, 2, 0, 3, 1, 2, 0, 2, 2, 1, 1, 2, 0, + 2, 3, 2, 1, 1, 0, 3, 1, 3, 1, 2, 3, 0, 1, + }; + for (int i = 0; i < JT4_SYMBOL_COUNT; i++) { + if (symbols[i] != expected[i]) return 1; + } + return 0; +} + +// Negative-report message must trigger the inverted sync (WSJT-X gen4). +// Bit-exact reference for "AAAAAAA -05". +static int test_jt4_inverted_sync() +{ + JTEncode enc; + uint8_t symbols[JT4_SYMBOL_COUNT]; + enc.jt4_encode("AAAAAAA -05", symbols); + static const uint8_t expected[] = { + 1, 1, 1, 2, 0, 3, 3, 1, 0, 0, 1, 0, 0, 3, 3, 0, + 1, 2, 3, 3, 3, 1, 1, 3, 1, 2, 2, 1, 3, 1, 3, 1, + 3, 3, 3, 1, 1, 3, 1, 0, 1, 0, 0, 3, 0, 2, 1, 0, + 3, 0, 2, 0, 0, 0, 1, 2, 1, 1, 1, 2, 3, 1, 2, 1, + 1, 2, 2, 2, 2, 2, 1, 3, 3, 0, 3, 0, 3, 3, 1, 0, + 2, 0, 0, 3, 2, 0, 1, 3, 2, 1, 1, 3, 0, 2, 1, 0, + 3, 2, 3, 2, 1, 0, 3, 2, 2, 2, 2, 2, 3, 0, 3, 0, + 3, 0, 2, 3, 0, 3, 0, 1, 0, 2, 2, 3, 3, 0, 1, 0, + 0, 3, 0, 2, 0, 2, 1, 3, 3, 1, 2, 2, 3, 0, 2, 1, + 3, 3, 0, 0, 0, 3, 2, 2, 2, 3, 0, 0, 0, 1, 3, 0, + 3, 1, 1, 0, 0, 3, 2, 2, 3, 3, 2, 3, 1, 1, 0, 0, + 2, 2, 0, 2, 1, 1, 0, 0, 1, 1, 1, 1, 0, 2, 3, 1, + 1, 0, 3, 2, 0, 3, 2, 2, 2, 2, 1, 0, 1, 0, + }; + for (int i = 0; i < JT4_SYMBOL_COUNT; i++) { + if (symbols[i] != expected[i]) return 1; + } + return 0; +} + static int test_rs_inst_null_guard() { JTEncode enc; @@ -328,6 +398,9 @@ int main() {"fsq_encode_overflow", "fsq_encode sprintf overflows 155-byte buffer", test_fsq_encode_overflow}, {"jt65_strcpy_overflow", "jt65_encode strcpy into 14-byte message buffer", test_jt65_strcpy_overflow}, {"jt4_strcpy_overflow", "jt4_encode strcpy into 14-byte message buffer", test_jt4_strcpy_overflow}, + {"jt4_frame_length", "jt4_encode writes 206 tones, all in {0..3}", test_jt4_frame_length}, + {"jt4_bit_exact", "jt4_encode bit-exact vs WSJT-X reference (NT7S)", test_jt4_bit_exact}, + {"jt4_inverted_sync", "jt4_encode inverted sync for negative report", test_jt4_inverted_sync}, {"rs_inst_null_guard", "rs_encode derefs NULL codec when init fails", test_rs_inst_null_guard}, {"ft8_strcpy_overflow", "ft8_encode strcpy into 19-byte message buffer", test_ft8_strcpy_overflow}, {"wspr_callsign_strcpy_overflow", "wspr_encode strcpy into 13-byte call_ buffer", test_wspr_callsign_strcpy_overflow},