Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 57 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
JT65/JT9/JT4/FT8/WSPR/FSQ Encoder Library for Arduino
=====================================================

This library very simply generates a set of channel symbols for JT65, JT9, JT4, FT8, or WSPR based on the user providing a properly formatted Type 6 message for JT65, JT9, or JT4 (which is 13 valid characters), Type 0.0 or 0.5 message for FT8 (v2.0.0 protocol) or a Type 1, Type 2, or Type 3 message for WSPR. With the full packmsg path enabled (`JTENCODE_FULL_PACKMSG`, see [Message Encoding Path](#message-encoding-path-compact-vs-full-packmsg)), JT65/JT9/JT4 also accept standard messages — callsign, grid, and report in the usual WSJT-X formats (including `CQ`/`QRZ`/`DE` prefixes and `/P`-style suffixes) — which are then encoded as the corresponding standard message types instead of as free text. It will also generate an arbitrary FSQ message of up to 130 characters (non-directed) or 100 characters (directed). When paired with a synthesizer that can output frequencies in fine, phase-continuous tuning steps (such as the Si5351), then a beacon or telemetry transmitter can be created which can change the transmitted characters as needed from the Arduino.
This library very simply generates a set of channel symbols for JT65, JT9, JT4, FT8, FT4, or WSPR based on the user providing a properly formatted Type 6 message for JT65, JT9, or JT4 (which is 13 valid characters), Type 0.0 or 0.5 message for FT8/FT4 (v2.0.0 protocol) or a Type 1, Type 2, or Type 3 message for WSPR. With the full packmsg path enabled (`JTENCODE_FULL_PACKMSG`, see [Message Encoding Path](#message-encoding-path-compact-vs-full-packmsg)), JT65/JT9/JT4 also accept standard messages — callsign, grid, and report in the usual WSJT-X formats (including `CQ`/`QRZ`/`DE` prefixes and `/P`-style suffixes) — which are then encoded as the corresponding standard message types instead of as free text. The full path also enables standard FT8/FT4 Type 1/2 messages (callsign, grid, and report), including `CQ`/`QRZ`/`DE` prefixes and `/P` suffixes. It will also generate an arbitrary FSQ message of up to 130 characters (non-directed) or 100 characters (directed). When paired with a synthesizer that can output frequencies in fine, phase-continuous tuning steps (such as the Si5351), then a beacon or telemetry transmitter can be created which can change the transmitted characters as needed from the Arduino.

Please feel free to use the issues feature of GitHub if you run into problems or have suggestions for important features to implement.

Expand Down Expand Up @@ -33,13 +33,15 @@ The library encodes JT65/JT9/JT4 messages using one of two paths, selected at co

Note: enabling the full path changes the encoded symbols for standard messages relative to the compact path — the compact path sets the freeform flag unconditionally, so its symbols are not what WSJT-X would transmit. Decoded messages are unaffected by the encoder choice. JT65 uses the same `packmsg` port with the 12-byte `dat` block fed directly to the Reed-Solomon encoder (no `entail`); JT9/JT4 feed it through `entail`.

**FT8/FT4 (full path)** — a faithful C++ port of WSJT-X `packjt77.f90` (`pack77` and helpers), reusing the same `JTENCODE_FULL_PACKMSG` gate and free-text fallback architecture as the JT-mode port: Type 1 (standard callsign/grid/report) and Type 2 (`/P`-style suffixes) messages, `CQ`/`QRZ`/`DE`/`CQ_` prefixes, grid4/report packing, and free-text fallback. The common JT-mode features that are out of scope here (Type 3 contest, hash-call `<...>`, EU VHF and WSPR types) fall back to free text, mirroring the JT-mode port's deliberate no-hash-table decision. Message payloads are checked against an independent pack77 implementation, and the test harness independently re-derives the CRC-14 from the generated tones (see `docs/FT8_FT4_CRC_BUG.md`).

The macro must be defined at compile time for the library itself — a `#define` in the sketch does **not** propagate to the library's translation units. The cleanest options are:

- **PlatformIO** — `build_flags = -DJTENCODE_FULL_PACKMSG` in `platformio.ini`
- **arduino-cli** — `--build-property "build.extra_flags=-DJTENCODE_FULL_PACKMSG"`
- **Arduino IDE** — no built-in build-flag UI; use PlatformIO/arduino-cli, or copy the library into your sketch folder and uncomment the `#define` in `src/JTEncode.cpp`

Note on the bundled example sketch: it uses a free-text message (`N0CALL AA00`), which encodes identically in both paths (the full path only changes standard messages). On an Arduino Uno the example still builds only with the compact path — at 28.4 KB it would overflow the 32 KB limit with the full path (~36.3 KB). On the Uno R4 WiFi the example builds with both paths (~72 KB basic / ~77 KB full, both comfortable). Use the compact path on an Uno; enable the full path on devices with more flash.
Note on the bundled example sketches: `FT4_Beacon_UnoR4WiFi` and `FT8_Beacon_UnoR4WiFi` each cycle through every encoding type the active path supports — Type 0 free text and Type 0.5 telemetry in the compact path, plus Type 1 (standard callsign/grid/report) and Type 2 (`/P`-style suffixes) with the full path. Each slot transmits the next message in the rotation, so running the beacon exercises every supported encoding. Both sketches use the same rotation table and WiFi/NTP time sync; only the modulation parameters differ (FT4: 105 symbols, 4 tones, 48 ms symbols, 7.5 s slots; FT8: 79 symbols, 8 tones, 160 ms symbols, 15 s slots). On an Arduino Uno the example still builds only with the compact path — at 28.4 KB it would overflow the 32 KB limit with the full path (~36.3 KB). On the Uno R4 WiFi the example builds with both paths (~77 KB compact / ~81 KB full, both comfortable). Use the compact path on an Uno; enable the full path on devices with more flash.

If the build fails because of the full path, the default compact path is still in use — simply remove the flag.

Expand Down Expand Up @@ -269,14 +271,38 @@ Public Methods
* Takes an arbitrary message of up to 13 allowable characters or a telemetry message
* of up to 18 hexadecimal digit (in string format) and returns a channel symbol table.
* Encoded for the FT8 protocol used in WSJT-X v2.0 and beyond (79 channel symbols).
* With the full packmsg path enabled (`JTENCODE_FULL_PACKMSG`), also accepts
* standard Type 1/2 messages (callsign, grid, and report — up to 36 characters,
* e.g. "WA9XYZ KA1ABC R FN42").
*
* message - Type 0.0 free text message or Type 0.5 telemetry message.
* message - Type 0.0 free text message, Type 0.5 telemetry message, or (full path)
* standard Type 1/2 message.
* symbols - Array of channel symbols to transmit returned by the method.
* Ensure that you pass a uint8_t array of at least size FT8_SYMBOL_COUNT to the method.
*
*/
```

### ft4_encode()
```
/*
* ft4_encode(const char * message, uint8_t * symbols)
*
* Takes an arbitrary message of up to 13 allowable characters or a telemetry message
* of up to 18 hexadecimal digit (in string format) and returns a channel symbol table.
* Encoded for the FT4 protocol used in WSJT-X v2.0 and beyond (105 channel symbols).
* With the full packmsg path enabled (`JTENCODE_FULL_PACKMSG`), also accepts
* standard Type 1/2 messages (callsign, grid, and report — up to 36 characters,
* e.g. "WA9XYZ KA1ABC R FN42").
*
* message - Type 0.0 free text message, Type 0.5 telemetry message, or (full path)
* standard Type 1/2 message.
* symbols - Array of channel symbols to transmit returned by the method.
* Ensure that you pass a uint8_t array of at least size FT4_SYMBOL_COUNT to the method.
*
*/
```

### fsq_encode()
```
/*
Expand Down Expand Up @@ -351,6 +377,34 @@ Also, a big thank you to Murray Greenman, ZL1BPU for working allowing me to pick

Changelog
---------
* v1.5.0

* Add FT4 support (issue #37): 105-symbol FT4 encoder (4 tones at 20.833 Hz spacing,
7.5-second T/R slots, 174-bit payload with the augmented CRC-14 and LDPC-174-91
error correction, Costas sync sequences and the 4-level ramp, whitened with the
standard 77-bit pseudo-random vector).
* FT8/FT4 CRC-14 is unchanged from v1.4.x (WSJT-X `augmented_crc` /
ft8_lib `ftx_compute_crc`). An interim change on this branch added a
`crc_finalize()` call that produced a wrong CRC and made every FT8/FT4
frame undecodable; it has been reverted and the harness now checks the
CRC independently. See `docs/FT8_FT4_CRC_BUG.md`.
* Add the full packmsg path for FT8/FT4 (`JTENCODE_FULL_PACKMSG`): a faithful port
of WSJT-X `packjt77.f90` (`pack77`) — Type 1 (standard callsign/grid/report) and
Type 2 (`/P`-style suffixes) messages, `CQ`/`QRZ`/`DE`/`CQ_` prefixes, grid4/report
packing, and free-text fallback. Payloads are checked against an independent
Python reimplementation of `pack77` (Type 1/2, free text, CQ-prefix, telemetry);
CQ-prefixed messages that fail chkcall (e.g. `CQ 001`) are free-text messages and
are checked as such. JT65/JT9/JT4 also accept standard messages in this build.
* Add FT4_Beacon_UnoR4WiFi and FT8_Beacon_UnoR4WiFi example sketches (NTP time sync, UTC-aligned slots).
Each sketch rotates through every encoding type the active path supports —
Type 0 free text and Type 0.5 telemetry (compact path), plus Type 1
(standard callsign/grid/report) and Type 2 (`/P`-style suffixes) with the
full path — so running either beacon exercises every supported encoding in turn.
Transmissions start at a millisecond-accurate offset into the slot (0.5 s FT8,
0.3 s FT4) using the NTP fractional second with half-RTT correction, and symbol
edges are scheduled against the frame start so I2C write time does not
accumulate.

* v1.4.3

* Full packmsg port verified bit-exact against the WSJT-X Fortran
Expand Down
146 changes: 146 additions & 0 deletions docs/FT8_FT4_CRC_BUG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
# FT8/FT4 frames not decoding: CRC-14 regression (post-mortem)

**Who this is for:** anyone working on the FT8/FT4 code on `feat/ft4`,
especially the agent that wrote the original FT4/pack77 work. Read this
before changing `JTEncode::ft8_encode(uint8_t*, uint8_t*)`, `src/crc14.c` or
the FT8/FT4 expected vectors in `tests/harness.cpp`.

## Symptom

FT8 and FT4 transmissions from the library and the `FT8_Beacon_UnoR4WiFi` /
`FT4_Beacon_UnoR4WiFi` sketches looked right on the WSJT-X waterfall
(correct timing, bandwidth and tone pattern) but **never decoded**.

## Root cause

Commit `3c4b563` ("Add FT4 support…") changed the CRC line in
`JTEncode::ft8_encode(uint8_t*, uint8_t*)`. FT4 uses the same function, so
both modes broke:

```c
// v1.4.x (correct)
ncrc14 = crc_update(&crc_cfg, crc, (unsigned char *)i1_msg_bytes, 12);
crc = crc_finalize(&crc_cfg, crc); // result unused: harmless dead code

// 3c4b563 (wrong)
ncrc14 = crc_finalize(&crc_cfg, crc_update(&crc_cfg, crc, (unsigned char *)i1_msg_bytes, 12));
```

The commit comment said the old `crc_finalize` call was dead code and that
`ncrc14` was therefore "the non-augmented CRC". The first part is true. The
conclusion drawn from it is wrong:

* `src/crc14.c` was generated by pycrc using the **bit-by-bit** algorithm.
In that algorithm, `crc_update()` shifts each data bit straight into the
low end of the register. It does **not** append any zero bits.
* `crc_finalize()` (bit-by-bit variant) then shifts **14 more zero bits**
through the register. That is the augmentation step.
* WSJT-X computes `boost::augmented_crc<14, 0x2757>` over a 12-byte buffer:
77 payload bits followed by 19 zero bits. `augmented_crc` assumes the
zeros are **already in the data**, so the result is the plain CRC of the
first 82 bits. ft8_lib computes the same value as
`ftx_compute_crc(msg, 82)`.
* Running `crc_update()` over that same zero-padded 12-byte buffer computes
exactly that value. Calling `crc_finalize()` afterwards augments a second
time, which is equivalent to adding 14 extra zero bits to the message. The
result is a different CRC.

WSJT-X's decoder (and every other FT8/FT4 decoder) throws away any candidate
whose CRC doesn't match after LDPC decoding. That is why the signal looked
perfect but nothing decoded.

The fix is to revert to `crc_update()` alone. There is now a comment at that
line explaining why.

## Why the tests didn't catch it

The FT4/FT8 "bit-exact" vectors in `tests/harness.cpp` were described as
coming from a "Fortran reference build of WSJT-X genft4/genft8". They
contained the **same wrong CRC** as the library. So whatever produced them
was not computing WSJT-X's real CRC. Most likely it shared the library's
CRC logic, or it came from a build that had been modified the same way. The
test was the library checking itself, so it proved nothing about
interoperability.

The 77-bit **payloads** in those vectors (pack77 output and FT4 whitening)
were correct. Only the 14 CRC bits, and therefore the 83 LDPC parity bits,
were wrong.

## How it was found

1. Encode each message, invert the Gray map on the data tones to get the
174 codeword bits, and take bits 0–76 (payload) and 77–90 (CRC).
2. Recompute the CRC with a separate implementation (ft8_lib's algorithm:
poly 0x2757, init 0, over the 77 payload bits plus 5 zero bits).
3. Result: 16 of 16 frames (8 messages × FT8/FT4) had a mismatched CRC.
With the one-line revert, all 16 match.
4. The payloads were also checked against a separate Python pack77 reimplementation (free
text, telemetry, Type 1, Type 2, CQ, signal reports). All matched —
with the explicit caveat that the CQ bit-exact vector is a free-text
message (a CQ code that fails chkcall on the grid word, e.g. `CQ 001`,
is not a Type 1 message), so it is checked as free text.

## What changed in this fix

* `src/JTEncode.cpp`: CRC line reverted to `crc_update()` only, with an
explanatory comment.
* `tests/harness.cpp`:
* The 11 FT8/FT4 expected vectors were regenerated. The regeneration
script checked that the payload bits in each vector were unchanged and
that the new CRC matches the independent CRC.
* New test `ft_crc_independent`. It re-derives the CRC from the
generated **tones**, using its own ft8_lib-style CRC (it does not use
`crc14.c`), for FT8 and FT4 across several message types. With the bad
line put back, this test fails.
* Example sketches (`FT8_Beacon_UnoR4WiFi`, `FT4_Beacon_UnoR4WiFi`): timing
fixes. These were not the cause of the decode failure, but they were
eating into the decoder's margin:
* **Symbol timing.** `set_freq(); delay(SYMBOL_DELAY);` adds the I²C write
time to every symbol, so the frame stretches. Symbol edges are now
scheduled against the frame start time.
* **Start time.** The NTP fractional second was thrown away and `now()`
only counted whole seconds, so frames started 0–1 s late at random
(plus up to 100 ms of polling). The clock now uses the NTP fraction plus
a half-RTT correction. The start is millisecond-accurate at slot +
`TX_START_OFFSET_MS` (500 ms for FT8, per the FT8 spec; 300 ms for FT4,
which I believe matches WSJT-X's modulator delay but haven't confirmed
against its source. FT4 decoding tolerates a few hundred ms either way,
and the value is a single `#define` if it needs changing).
* **NTP sync near the slot.** A re-sync can block for up to 5 s. It is no
longer done in the last 7 s before a transmission. Instead, a fresh sync
is done about 15 s before the slot to limit `millis()` drift. A failed
re-sync no longer marks a previously good clock invalid.
* In a host-side simulation (assuming 1.5 ms per `set_freq`), the old
sketches started 0.2–2.0 s late and their symbol edges drifted by up to
156 ms per frame. The new sketches start within 2 ms of the target and
the edge error is below 0.1 ms.
* `README.md`: removed the changelog claim that the CRC change was a fix
that "changes the encoded symbols for all FT8 messages". Also toned down
the "verified bit-exact against the WSJT-X Fortran reference" claims.

## Rules for future work on this code

1. **Don't treat "bit-exact vs a reference" as proof unless the reference is
truly independent.** A vector produced by code that shares logic (or
assumptions) with the code under test proves nothing. Use WSJT-X's
`ft8code` / `ft4code` utilities or ft8_lib output, and say how the vector
was produced.
2. **Test interoperability end to end where possible.** The strongest check
is decoding the generated tones with a real decoder (WSJT-X `jt9`, or
ft8_lib `decode_ft8` on a synthesized WAV). Without that, at least
re-derive the CRC-14 from the tones with independent code,
as `ft_crc_independent` does.
3. **Before calling existing code "dead" or "wrong", find out which
algorithm variant it is.** pycrc's bit-by-bit, bit-by-bit-fast and
table-driven variants divide the work between `update` and `finalize`
differently. The same function names mean different things in each.
4. **Don't change the output of an established encoder (FT8 here) as a side
effect of adding a new mode.** If a change alters existing on-air
output, it needs on-air or real-decoder confirmation before it lands.

## Open item (low priority, not a decode problem)

`"<call> <call> RR73"` is packed as the grid square `RR73` (igrid4 = 32373)
rather than the RR73 report code (MAXGRID4 + 3). WSJT-X displays both as
"RR73", so it decodes the same either way. It is worth confirming against
real WSJT-X `ft8code` output at some point.
Loading
Loading