Skip to content

burst-pipeline — a burst waveform end to end

Get the code

The files are listed at the bottom of this page. The tabs there show the project on the development branch; this fetches the project's folder from the latest release, which can differ from them until the next release:

curl -fsSL https://github.com/doppler-dsp/doppler/archive/refs/tags/v0.88.0.tar.gz \
  | tar -xz --strip-components=2 doppler-0.88.0/example-projects/burst-pipeline

Or browse example-projects/burst-pipeline/.

Describe a frame, generate the train, sweep it, write a BLUE file, read it back. Copy this directory as the starting point for a downstream that generates realistic burst traffic and then does something with it.

Every number it prints is measured on the machine that runs it, with units. Every claim it makes is checked, and a failed check exits non-zero — an example that validates nothing still exits 0, so this one does not rely on you reading the output.

What it demonstrates

  1. A frame the caller builds. A wfm_frame_desc_t assembled from dp_wfm_frame_add_field / add_derived / add_stage — fields in wire order, a CRC-16 stage covering a span it names — attached to a source. No wfmgen flag spells this layout.

  2. repeats is not shorthand for listing segments. Sixty bursts listed as sixty segments carry identical noise: a listed segment restarts the source from its declared seed. One segment with repeats = 60 fixes the signal and draws fresh AWGN per instance. Both are checked, in both directions, because the difference is invisible in a plot and fatal to a Monte-Carlo run.

  3. Where the declared SNR actually shows up. SNR is a property of a source, and the floor it implies runs through the whole segment — the gap is the channel, not digital silence. Measured with the library's own estimators: ≈11.5 dB data-aided and ≈11.6 dB blind, against 12 dB declared.

  4. What a Plan does and does not cache. It caches the clean signal and redraws the noise every point, so the saving is the signal's share of the work — modest for a low-duty burst train, larger as the on-time fraction or the per-sample signal cost grows. Both timings are printed and neither is asserted: a ratio is a property of the machine. What is asserted is that a cached render is byte-identical to composing the scene. That check is not decoration: a cache that is fast because it quietly renders less is not something a stopwatch can catch.

  5. BLUE round-trips. cf32 out and back is byte-exact, both halves timed with the consumer's own work inside the read loop.

  6. And then it receives. The consumer does not know where the bursts are: it demaps the whole record, gaps included, and searches for the frame's sync marker. Two things keep that honest rather than circular — the marker is rebuilt from the frame's own declaration (dp_wfm_seq_bits over the same generated field the transmitter used, not a second copy that can drift), and the tolerance is derived by dp_syncword_max_errors_for from how much stream is searched rather than guessed. Each frame the search lands on is checked with dp_wfm_frame_desc_crc_ok, which needs no payload truth, so the result is a frame error rate a receiver could compute on a capture it did not generate.

Build and run

You need doppler installed somewhere CMake can find it:

cmake --install /path/to/doppler/build --prefix ~/.local
make PREFIX=~/.local run

Drop PREFIX= for a system install. make help lists the knobs; make clean removes build/. make run builds and runs both link modes — the static one is where a missing transitive dependency shows up.

Which call to reach for

Use the library's measurement rather than writing your own — one fewer local convention to drift from the one doppler ships.

you want call
SNR of a burst, knowing what was sent dp_snr_data_aided_db
SNR of a burst, knowing nothing about it dp_snr_m2m4_db
to tell a silent gap from a noisy one dp_snr_m2m4_db — NaN on a block with zero power
to find a marker in a bit stream dp_syncword_find — offset, polarity and Hamming distance in one answer
to choose how many bit errors to tolerate dp_syncword_max_errors_for, dp_syncword_pfa
to know whether a received frame is intact dp_wfm_frame_desc_crc_ok — no payload truth needed

Pick the marker length for the window you search, not by eye

dp_syncword_max_errors_for answers "how many bit errors may I accept?" and it needs to know how much stream you are searching, because the search returns the first acceptable offset — every offset ahead of the real one is its own chance to hit first. Over this scene's search window, one burst period (4433 bits; the run prints it), a 31-bit marker is refused outright: even demanding an exact match leaves a false-frame probability above 1e-6. A 63-bit marker takes that from 2⁻³¹ to 2⁻⁶³ and leaves room for 7 bit errors at Pfa 1.4e-10.

A refusal comes back as -1. Check for it: casting -1 into the unsigned tolerance the search takes turns impossible into accept anything, and a receiver that does it will lock onto noise and report frames that were never sent.

The search works in bits, so give it bits

dp_syncword_find answers in the symbol domain: which bit the marker starts on, in which polarity, at what Hamming distance. It cannot resolve timing finer than a symbol and is not meant to — recovering a fractional-sample offset is a timing loop's job (symsync, ratesync).

That boundary is why this example runs at one sample per symbol. The frame, the search and the CRC are all questions about symbols; oversampling would add a pulse shape to match and a sample phase to recover, neither of which changes any answer here. Hand the searcher a demapped bit stream, take the bit offset it returns, and let a timing loop own the samples.

Take the polarity with the offset

A hit carries inverted. A BPSK stream can arrive complemented, and a receiver that takes the offset without the polarity hands its frame decoder inverted bits that fail the CRC for a reason that has nothing to do with the channel.

Two things that will bite you

Both are comments in main.c too, because they cost a build rather than a read:

  • A downstream that does its own maths links -lm itself. This program calls log10() and fabs(); without m on the link line the static target fails at link time. doppler's own libm use comes with the imported target — yours does not.
  • dp_wfm_writer_destroy is dp_wfm_writer_close. The header says C callers may use either name. Calling both, as a create/destroy pair invites, closes the FILE twice and segfaults inside ferror(). Call one, and check its status.

The files

cmake_minimum_required(VERSION 3.16)
project(doppler_burst_pipeline C)

# A burst waveform end to end: describe a frame, generate the train, sweep it
# through a Plan, write BLUE, read it back and time both halves.
#
# Point CMake at an install prefix (a `cmake --install` tree or an extracted
# release tarball) with
#   -DCMAKE_PREFIX_PATH=/path/to/doppler-install
# or rely on a system install. Frame description, composition, the Plan cache
# and the BLUE reader/writer are all in the pure-C core, with no stream/NATS
# seam — so the core alone is enough and no optional component is needed.
#
# `m` is on the link line because THIS program calls log10() and fabs() to
# report its own measurements. doppler's own libm use is its business and
# comes with the imported target; a downstream that does maths of its own
# links libm for that reason, exactly as it would without doppler. Dropped
# here it fails at the link, not at runtime, which is how you find out.
# <!-- doc-version:start -->
find_package(doppler 0.88.0 REQUIRED)
# <!-- doc-version:end -->

add_executable(burst_pipeline_shared main.c)
target_link_libraries(burst_pipeline_shared PRIVATE doppler::doppler m)

if(TARGET doppler::doppler-static)
    add_executable(burst_pipeline_static main.c)
    target_link_libraries(burst_pipeline_static PRIVATE doppler::doppler-static m)
endif()
# example-projects/burst-pipeline — a downstream project's own Makefile.
#
# This is the file a copier runs first, so it is deliberately thin: CMake is
# the build system, and this wraps the two commands you would otherwise type.
# `make help` lists everything.
#
# Nothing here is doppler-specific tooling. Copy the directory, keep this
# file, and it keeps working against whatever prefix you install doppler to.

BUILD_DIR        ?= build
CMAKE_BUILD_TYPE ?= Release
# Where doppler is installed. Empty means "a system install CMake can already
# find"; set it to a `cmake --install` tree or an extracted release tarball:
#   make PREFIX=~/.local run
PREFIX           ?=

CMAKE_FLAGS := -DCMAKE_BUILD_TYPE=$(CMAKE_BUILD_TYPE)
ifneq ($(strip $(PREFIX)),)
CMAKE_FLAGS += -DCMAKE_PREFIX_PATH=$(abspath $(PREFIX))
endif

.PHONY: all build run clean help
.DEFAULT_GOAL := build

all: build

# Configure every time rather than gating on CMakeCache.txt: re-running
# configure is cheap and idempotent, while a cached one silently ignores a
# changed PREFIX — which is the mistake this target exists to prevent.
build:
    cmake -S . -B $(BUILD_DIR) $(CMAKE_FLAGS)
    cmake --build $(BUILD_DIR)

# BOTH link modes, because the static one is where a missing transitive
# dependency actually shows up. The program self-validates and exits non-zero
# on any failed check, so `make run` succeeding is the whole test.
run: build
    $(BUILD_DIR)/burst_pipeline_shared
    @if [ -x $(BUILD_DIR)/burst_pipeline_static ]; then \
        echo; echo "── again, linked statically ──"; \
        $(BUILD_DIR)/burst_pipeline_static; \
    else \
        echo "note: no static target — this install ships no doppler::doppler-static"; \
    fi

# The program writes its output beside itself when run in place, so clean
# takes that too -- otherwise `make clean` leaves the interesting half behind.
clean:
    rm -rf $(BUILD_DIR) burst_train.blue

help:
    @echo "burst-pipeline — a burst waveform end to end"
    @echo
    @echo "  make build              configure and compile"
    @echo "  make run                build, then run both link modes"
    @echo "  make clean              remove $(BUILD_DIR)/"
    @echo
    @echo "  PREFIX=<dir>            where doppler is installed"
    @echo "  CMAKE_BUILD_TYPE=<t>    default Release"
    @echo "  BUILD_DIR=<dir>         default build"
/*
 * main.c — a burst waveform, end to end: describe, generate, write, receive.
 *
 * Copy this directory as the starting point for a downstream that generates
 * realistic burst traffic and then does something with it. Five steps, each
 * checked, each exiting non-zero if the check fails:
 *
 *   1. THE FRAME. A `wfm_frame_desc_t` the caller builds — fields in wire
 *      order, a stage covering a span it names — put on a source. No wfmgen
 *      flag spells this layout.
 *
 *   2. THE BURST TRAIN, listed then declared. Sixty bursts as sixty listed
 *      segments are NOT the same waveform as one segment with `repeats = 60`:
 *      a listed segment restarts the source from its seed, so all sixty carry
 *      identical noise, while `repeats` draws fresh noise per instance. The
 *      difference is invisible in a plot and fatal to a Monte-Carlo run.
 *
 *   3. SNR ACROSS THE SCENE. SNR is a property of a SOURCE, and the noise
 *      floor it implies runs through the whole segment — including the gaps,
 *      where the signal is off. So a gap is the channel, not digital silence.
 *
 *   4. PREPARE ONCE, SWEEP MANY. `dp_wfm_plan_prepare` caches the clean signal
 *      and `dp_wfm_plan_at` re-weights it. Both timings are printed and
 * NEITHER is asserted: a Plan caches the signal but redraws the noise, so what
 *      it saves is the signal's share of the work, and that depends on the
 *      scene and the machine. What IS asserted is that a cached render is
 *      byte-identical to composing the scene — a cache that is fast because
 *      it quietly does less is not something a stopwatch can catch.
 *
 *   5. WRITE IT, THEN RECEIVE IT. Out to a BLUE file, back in through the
 *      reader, and then found the way a receiver would have to: demodulate
 *      the whole record, gaps included, and search for the frame's sync
 *      marker. The marker is rebuilt from the frame's own declaration rather
 *      than held as a second copy, and the search tolerance is derived by
 *      `dp_syncword_max_errors_for` rather than guessed. Each frame found is
 *      checked with `dp_wfm_frame_desc_crc_ok`, which needs no payload truth —
 *      a frame error rate a receiver could compute on someone else's capture.
 *
 * Measurement comes from the library where the library has it:
 * `dp_snr_data_aided_db` / `dp_snr_m2m4_db` for SNR, `syncword_*` for the
 * search and its threshold arithmetic. Every number printed is measured on the
 * machine that runs it and carries its units.
 *
 * Builds against either link mode; see CMakeLists.txt (find_package) and the
 * pkg-config commands in docs/install/c.md.
 */
#include <complex.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "doppler/mpsk/mpsk_core.h"
#include "doppler/snr/snr_core.h"
#include "doppler/syncword/syncword_core.h"
#include "doppler/wfm/wfm_compose.h"
#include "doppler/wfm/wfm_frame.h"
#include "doppler/wfm/wfm_plan.h"
#include "doppler/wfm_reader/wfm_reader_core.h"
#include "doppler/wfm_synth/wfm_synth_core.h"
#include "doppler/wfm_writer/wfm_writer_core.h"

#define FS 1.0e6 /* sample rate, Hz                                  */
/* ONE SAMPLE PER SYMBOL. The frame, the sync search and the CRC are all
   questions about SYMBOLS, so this example stays in that domain: a sample is
   a symbol, and the receive half is a demapper.

   Oversampling would add a pulse shape to match and a sample phase to
   recover. Both are a timing loop's job (`symsync`, `ratesync`) and neither
   changes any answer here, so carrying them would only put a second subject
   in front of the first. */
#define SPS 1
#define SEED 7u
#define HDR_BITS 16u  /* the caller's own header                          */
#define PAY_BITS 240u /* payload                                          */
/* A GENERATED sync field rather than a literal: declaring a `kind` means the
   description PRODUCES the marker, so the receiver rebuilds the same bits
   from the same declaration (`dp_wfm_seq_bits`) instead of holding a second
   copy that can drift. A literal field works too, for a marker you must match.

   LENGTH IS THE PROPERTY THAT MATTERS, because the search is a Hamming
   distance over bits: what makes a marker safe is being unlikely to appear by
   chance anywhere in the window searched, which is 2^-63 here and 2^-31 at
   half the length. (An m-sequence's two-valued autocorrelation is why it
   suits a sample-domain correlator; that is not what this is.)
   `dp_syncword_max_errors_for` puts a number on it — see section 7. */
#define SYNC_BITS 63u
#define SYNC_REG_BITS 6u
#define FRAME_BITS (SYNC_BITS + HDR_BITS + PAY_BITS + WFM_FRAME_CRC_BITS)
#define N_SYMS FRAME_BITS /* one BPSK symbol per bit */
#define TOTAL_BITS (TOTAL / (size_t)SPS)

#define BURST_ON (FRAME_BITS * (unsigned)SPS) /* one frame, exactly       */
#define BURST_OFF 4096u                       /* gap between bursts       */
/* A lead-in, so the first burst does not start at sample 0 and the search has
   somewhere to look before it. */
#define DELAY_SAMPLES 2u
#define N_BURSTS 60u
#define PERIOD ((size_t)(DELAY_SAMPLES + BURST_ON + BURST_OFF))
#define TOTAL (PERIOD * N_BURSTS)

#define SNR_DB 12.0 /* declared per-source SNR, in Es/N0 mode          */
#define N_SWEEP 24u /* operating points in the sweep                   */
#define READ_BLOCK 4096u

static int failures = 0;

/** @brief Report one named check; the first failure sets the exit status. */
static void
check (int ok, const char *what)
{
  printf ("  [%s] %s\n", ok ? "ok" : "FAIL", what);
  if (!ok)
    failures++;
}

/** @brief Monotonic seconds. CLOCK_MONOTONIC, so it is not NTP's to move. */
static double
now_s (void)
{
  struct timespec t;
  clock_gettime (CLOCK_MONOTONIC, &t);
  return (double)t.tv_sec + (double)t.tv_nsec * 1e-9;
}

/** @brief Drain a composer into `out`, returning what it produced. */
static size_t
drain (dp_wfm_compose_state_t *c, float complex *out, size_t cap)
{
  size_t n = 0, got;
  while (n < cap && (got = dp_wfm_compose_execute (c, out + n, cap - n)) > 0)
    n += got;
  return n;
}

int
main (void)
{
  printf ("=== a burst waveform, end to end ===\n\n");

  /* Borrowed below, so they outlive every compose call. */
  static uint8_t hdr[HDR_BITS], pay[PAY_BITS];
  for (unsigned i = 0; i < HDR_BITS; i++)
    hdr[i] = (uint8_t)((0x5C5Cu >> (HDR_BITS - 1u - i)) & 1u);
  for (unsigned i = 0; i < PAY_BITS; i++)
    pay[i] = (uint8_t)((i * 7u + 1u) & 1u);

  /* ── 1. the frame ───────────────────────────────────────────────────── */
  printf ("--- 1. The frame this waveform carries ---\n");

  wfm_seq_t sy = { 0 };
  sy.kind      = WFM_SEQ_PN;
  sy.len       = SYNC_BITS;
  sy.reg_bits  = SYNC_REG_BITS;
  sy.seed      = 1u; /* 0 would select 1 anyway; said out loud */
  sy.poly      = 0u; /* 0 selects the maximal-length polynomial for reg_bits */

  wfm_frame_desc_t d;
  wfm_seq_t h = { .kind = WFM_SEQ_LITERAL, .bits = hdr, .len = HDR_BITS };
  wfm_seq_t p = { .kind = WFM_SEQ_LITERAL, .bits = pay, .len = PAY_BITS };
  memset (&d, 0, sizeof d);
  /* The cover names its ends and REACHES the derived field, which is what
     wires that field's producer — so the CRC's position and the fact that a
     CRC fills it are one declaration rather than two that can disagree.
     Naming the ends is also why inserting the sync field AHEAD of them moved
     nothing here: `first_field`/`n_fields` would both have had to shift.
     The sync marker and the header sit outside the cover — a receiver finds
     them before it can check anything. */
  int built
      = dp_wfm_frame_add_field (&d, "sync", &sy, 0u) == 0
        && dp_wfm_frame_add_field (&d, "hdr", &h, 0u) == 1
        && dp_wfm_frame_add_field (&d, "payload", &p, 0u) == 2
        && dp_wfm_frame_add_derived (&d, "crc", WFM_FRAME_CRC_BITS) == 3
        && dp_wfm_frame_add_stage (&d, WFM_STAGE_CRC16, "payload", "crc") == 0;
  check (built, "a generated sync marker, a header, a payload and a derived "
                "CRC-16 over a named span");
  if (!built)
    return 1;

  wfm_frame_desc_layout_t lay;
  check (dp_wfm_frame_desc_layout (&d, &lay) == 0
             && lay.frame_bits == FRAME_BITS,
         "the description lays out at the length the burst is sized from");
  printf ("  %zu frame bits x %d sps = %u samples of burst, "
          "%u samples of gap\n\n",
          lay.frame_bits, SPS, BURST_ON, BURST_OFF);

  float complex *listed   = calloc (TOTAL, sizeof *listed);
  float complex *declared = calloc (TOTAL, sizeof *declared);
  float complex *swept    = calloc (TOTAL, sizeof *swept);
  float complex *readback = calloc (TOTAL, sizeof *readback);
  if (!listed || !declared || !swept || !readback)
    {
      fprintf (stderr, "out of memory\n");
      return 1;
    }

  /* ── 2. the burst train, listed then declared ───────────────────────── */
  printf ("--- 2. Sixty bursts: listed, then declared ---\n");

  /* THE SOURCE, spelled out: these are the fields a framed BPSK burst needs
     and there is no shorter honest way to say it. `= { 0 }` then named
     members, never a positional initialiser list — wfm_source_t carries
     30-odd members and a positional list shifts silently the moment one is
     inserted. The payload's bits are the description's `payload` field, so
     the source carries no payload of its own: the frame is the whole frame,
     and a source with both is refused (doppler#1683). */
  wfm_source_t src = { 0 };
  src.type         = WFM_SYNTH_BITS;
  src.modulation   = WFM_BITMOD_BPSK;
  src.sps          = SPS;
  src.snr          = SNR_DB;
  src.snr_mode     = WFM_SNR_ESNO;
  src.seed         = SEED;
  src.frame        = &d; /* borrowed; the description outlives compose */
  /* No on-time on the segments below: a carried frame of fixed bits sets
     its own length -- BURST_ON, one frame -- so `num_samples` stays 0
     (derive), and a count beside it is refused (doppler#1729). */

  /* The naive shape: one segment per burst. It works, and it is sixty copies
     of one fact — change the gap and you change it sixty times. */
  wfm_segment_t *many = calloc (N_BURSTS, sizeof *many);
  if (!many)
    {
      fprintf (stderr, "out of memory\n");
      return 1;
    }
  for (unsigned i = 0; i < N_BURSTS; i++)
    {
      many[i].sources       = &src;
      many[i].n_sources     = 1u;
      many[i].fs            = FS;
      many[i].off_samples   = BURST_OFF;
      many[i].delay_samples = DELAY_SAMPLES;
    }

  double                  t0 = now_s ();
  dp_wfm_compose_state_t *c  = dp_wfm_compose_create (many, N_BURSTS, 0, 0);
  size_t                  n_listed = c ? drain (c, listed, TOTAL) : 0;
  dp_wfm_compose_destroy (c);
  const double t_listed = now_s () - t0;

  /* The declared shape: ONE segment that plays `repeats` times. Each
     instance is delay + on-time + gap, the signal is fixed across them and
     the AWGN is fresh per instance — a burst train from one declaration. */
  wfm_segment_t one = { 0 };
  one.sources       = &src;
  one.n_sources     = 1u;
  one.fs            = FS;
  one.off_samples   = BURST_OFF;
  one.delay_samples = DELAY_SAMPLES;
  one.repeats       = N_BURSTS;

  t0                = now_s ();
  c                 = dp_wfm_compose_create (&one, 1u, 0, 0);
  size_t n_declared = c ? drain (c, declared, TOTAL) : 0;
  dp_wfm_compose_destroy (c);
  const double t_declared = now_s () - t0;

  check (n_listed == TOTAL && n_declared == TOTAL,
         "both shapes produce the whole train");
  printf ("  %u segments: %.1f ms   |   1 segment x %u repeats: %.1f ms\n",
          N_BURSTS, t_listed * 1e3, N_BURSTS, t_declared * 1e3);

  /* AND THEY ARE NOT THE SAME WAVEFORM — the reason to reach for `repeats`
     is not brevity. A listed segment is a fresh segment: it restarts the
     source from its declared `seed`, so all sixty draw the SAME noise, which
     is sixty bursts that look independent and are not. `repeats` says "the
     same burst, again": signal fixed across instances, AWGN fresh per
     instance. */
  const size_t period = PERIOD;
  /* Instance k is [delay | on | off], so a burst starts DELAY_SAMPLES in. */
  const size_t burst0 = DELAY_SAMPLES;
  check (memcmp (listed + burst0, listed + burst0 + period,
                 BURST_ON * sizeof *listed)
             == 0,
         "listed: every burst carries IDENTICAL noise — one seed, sixty "
         "fresh segments");
  check (memcmp (declared + burst0, declared + burst0 + period,
                 BURST_ON * sizeof *declared)
             != 0,
         "repeats: each instance draws its own noise, as a burst train "
         "should");
  printf ("  so `repeats` is not shorthand for the listed form — it is the "
          "one\n  that gives sixty INDEPENDENT bursts.\n\n");

  /* ── 3. SNR across the scene ────────────────────────────────────────── */
  printf ("--- 3. Where the declared %.0f dB SNR actually shows up ---\n",
          SNR_DB);

  /* Two estimators, for the two situations a caller is in: `dp_snr_m2m4_db` is
     blind (moments only, nothing known about what was sent), while
     `dp_snr_data_aided_db` strips the known transmitted sign. Both read
     symbols, which at one sample per symbol is the burst as composed. */
  static uint8_t       tx_bits[FRAME_BITS];
  const float complex *tx_sym = declared + burst0; /* a sample IS a symbol */

  const size_t n_tx = dp_wfm_frame_assemble (&d, NULL, tx_bits, FRAME_BITS);
  check (n_tx == FRAME_BITS,
         "the description assembles the bits the burst carries");

  const double snr_blind = dp_snr_m2m4_db (tx_sym, N_SYMS);
  const double snr_da = dp_snr_data_aided_db (tx_sym, N_SYMS, tx_bits, n_tx);

  /* THE GAP IS NOT SILENCE, and the estimator is what says so: it returns
     NaN for a block with zero power, so a digitally silent gap would fail
     this check rather than quietly reading as a very good SNR. What the gap
     actually holds is the segment's noise floor — the source's AWGN keeps
     running while the signal stops. */
  const double snr_gap
      = dp_snr_m2m4_db (declared + burst0 + BURST_ON, BURST_OFF);
  check (!isnan (snr_gap),
         "the GAP is not silent — it carries the segment's noise floor");
  check (snr_gap < snr_blind - 6.0,
         "and it is NOISE, not signal — the blind estimator separates them");

  check (fabs (snr_da - SNR_DB) < 1.5,
         "data-aided Es/N0 recovers the SNR the source declared");
  printf ("  declared %.0f dB   data-aided %.2f dB   blind (M2M4) %.2f dB   "
          "gap %.2f dB\n",
          SNR_DB, snr_da, snr_blind, snr_gap);
  printf ("  snr_mode is Es/N0, so the declaration is per transmitted "
          "symbol;\n"
          "  every instance of the burst sees the same declaration.\n\n");

  /* ── 4. prepare once, sweep many ────────────────────────────────────── */
  printf ("--- 4. A %u-point SNR sweep, re-composed then cached ---\n",
          N_SWEEP);

  char *spec
      = dp_wfm_spec_to_json (&one, 1u, 0, 0, WFM_SEED_ADVANCE_NONE, 0.0);
  if (!spec)
    {
      fprintf (stderr, "dp_wfm_spec_to_json failed\n");
      return 1;
    }

  /* Naive: every operating point re-runs the whole scene — the framing, the
     bit mapping, the pulse, for bursts that did not change. */
  t0 = now_s ();
  for (unsigned k = 0; k < N_SWEEP; k++)
    {
      wfm_source_t s2            = src;
      s2.snr                     = 4.0 + (double)k;
      wfm_segment_t g            = one;
      g.sources                  = &s2;
      dp_wfm_compose_state_t *cc = dp_wfm_compose_create (&g, 1u, 0, 0);
      if (cc)
        (void)drain (cc, swept, TOTAL);
      dp_wfm_compose_destroy (cc);
    }
  const double t_recompose = now_s () - t0;

  /* Cached: prepare renders and keeps each source's clean ON-time once; a
     point is then a re-weighted sum plus fresh noise. */
  t0                     = now_s ();
  wfm_plan_t  *plan      = dp_wfm_plan_prepare (spec);
  const double t_prepare = now_s () - t0;
  check (plan != NULL, "the scene is in scope for a Plan");

  double t_sweep  = 0.0;
  int    all_full = 1;
  if (plan)
    {
      t0 = now_s ();
      for (unsigned k = 0; k < N_SWEEP; k++)
        /* The return is the length of THIS draw. Discarding it is how a Plan
           that renders nothing still looks like a fast sweep. */
        if (dp_wfm_plan_at (plan, 4.0 + (double)k, 1234u + k, swept) != TOTAL)
          all_full = 0;
      t_sweep = now_s () - t0;
    }
  check (plan != NULL && all_full,
         "every point of the sweep rendered the whole train");

  /* THE CHECK THAT MATTERS, and it is not the clock. A cache is worth having
     only if it renders what compose would have: at the scene's own SNR and
     anchor seed, `dp_wfm_plan_at` reproduces `wfm_compose` to the bit
     (wfm_plan.h). A speed ratio asserts nothing useful in its place — it is
     a property of the machine, and it cannot tell "faster" from "did less".
     Byte-identity can. */
  int exact = 0;
  if (plan)
    {
      float complex *ref = calloc (TOTAL, sizeof *ref);
      if (!ref)
        {
          fprintf (stderr, "out of memory\n");
          return 1;
        }
      dp_wfm_compose_state_t *ref_c = dp_wfm_compose_create (&one, 1u, 0, 0);
      const size_t            n_ref = ref_c ? drain (ref_c, ref, TOTAL) : 0;
      dp_wfm_compose_destroy (ref_c);
      const size_t n_plan = dp_wfm_plan_at (
          plan, SNR_DB, dp_wfm_plan_anchor_seed (plan), swept);
      exact = n_ref == TOTAL && n_plan == TOTAL
              && memcmp (ref, swept, TOTAL * sizeof *swept) == 0;
      free (ref);
    }
  check (exact, "a cached render is byte-identical to composing the scene");

  free (spec);

  printf ("  re-composing each point : %7.1f ms\n", t_recompose * 1e3);
  printf ("  prepare once            : %7.1f ms\n", t_prepare * 1e3);
  printf ("  then %2u cached renders  : %7.1f ms\n", N_SWEEP, t_sweep * 1e3);
  if (t_sweep > 0.0)
    printf ("  -> %.1fx on the sweep itself, on this machine\n",
            t_recompose / t_sweep);
  printf ("  a Plan caches the SIGNAL, not the noise: the AWGN is redrawn\n"
          "  every point either way. So the saving is the signal's share of\n"
          "  the work — modest for a low-duty burst train like this one\n"
          "  (%u on, %u off), and larger as the on-time fraction or the\n"
          "  per-sample signal cost (RRC, spreading, long codes) grows.\n"
          "  Measure it for YOUR scene; that is why both numbers are here.\n",
          BURST_ON, BURST_OFF);
  printf ("\n");

  /* ── 5. write it to a BLUE file ─────────────────────────────────────── */
  printf ("--- 5. Out to a BLUE type-1000 file ---\n");

  const char *path = "burst_train.blue";
  t0               = now_s ();
  /* sample_type 0 is cf32 — the composer's own type, so the file is a
     lossless copy and the readback below can be compared exactly. `total`
     lets the 512-byte header carry the length up front. */
  dp_wfm_writer_state_t *w = dp_wfm_writer_create (
      path, FS, WFM_FT_BLUE, 0, 0, 0.0, TOTAL, 0.0, 0.0, false);
  size_t wrote = w ? dp_wfm_writer_write (w, declared, TOTAL) : 0;
  /* close() FINALISES AND FREES, and `dp_wfm_writer_destroy` is the same
     function under a second name -- "C callers may use either". Calling both,
     as a create/destroy pair invites, closes the FILE twice; it segfaults in
     ferror() on the second pass. One call, and check its status: BLUE patches
     its length and flushes here, so a failure at close is a failed capture. */
  int          wrc     = w ? dp_wfm_writer_close (w) : -1;
  const double t_write = now_s () - t0;

  check (wrote == TOTAL && wrc == 0, "the whole train reached the file");
  printf ("  %zu samples in %.1f ms  (%.1f Msample/s)\n\n", wrote,
          t_write * 1e3, (double)wrote / t_write / 1e6);

  /* ── 6. a consumer, reading as fast as it is given samples ──────────── */
  printf ("--- 6. A consumer reading it back, block by block ---\n");

  t0                         = now_s ();
  dp_wfm_reader_state_t *r   = dp_wfm_reader_create (path, 0, 0);
  size_t                 got = 0, bursts = 0;
  double                 acc = 0.0;
  if (r)
    {
      /* Read in blocks and do the work IN the loop, so the timing below
         covers reading AND processing rather than reading alone. The work
         here is running total energy — trivial, but real: drop it and the
         number becomes the reader's throughput rather than a consumer's. */
      size_t n;
      while (
          (n = dp_wfm_reader_read (r, READ_BLOCK, readback + got, TOTAL - got))
          > 0)
        {
          for (size_t i = 0; i < n; i++)
            {
              const float complex v = readback[got + i];
              acc += (double)crealf (v) * crealf (v)
                     + (double)cimagf (v) * cimagf (v);
            }
          got += n;
          if (got >= TOTAL)
            break;
        }
      dp_wfm_reader_destroy (r);
    }
  const double t_read = now_s () - t0;

  check (got == TOTAL, "the consumer read every sample that was written");
  check (memcmp (readback, declared, TOTAL * sizeof *declared) == 0,
         "and cf32 through BLUE is byte-exact — what went in came back");
  printf ("  %zu samples in %.1f ms  (%.1f Msample/s)\n\n", got, t_read * 1e3,
          (double)got / t_read / 1e6);

  /* ── 7. finding the bursts, by their sync marker ────────────────────── */
  printf ("--- 7. Finding each burst by its sync marker ---\n");

  /* Demap the WHOLE record, gaps included: the gaps demap to noise bits,
     which is the point — on a real capture nothing announces a burst, and the
     searcher has to survive the space between them. `dp_mpsk_demap` at m=2 is
     the element-wise inverse of the BPSK mapping the source used. */
  static uint8_t rx_bits[TOTAL_BITS];
  dp_mpsk_demap (readback, TOTAL_BITS, rx_bits, 2);

  /* THE MARKER COMES FROM THE DESCRIPTION, not from a constant here. The
     field declared a generated sequence, so the receiver materialises the
     same bits from the same declaration — one statement of what the marker
     is, and the two ends cannot drift apart. */
  static uint8_t marker[SYNC_BITS];
  check (dp_wfm_seq_bits (&sy, marker, SYNC_BITS) == SYNC_BITS,
         "the receiver rebuilds the marker from the frame's own declaration");

  dp_syncword_state_t *sw = dp_syncword_create (marker, SYNC_BITS);
  if (!sw)
    {
      fprintf (stderr, "dp_syncword_create failed\n");
      return 1;
    }

  /* THE TOLERANCE IS DERIVED, not guessed. `max_errors` is a function of how
     much stream is searched rather than of how long the marker is: the
     search reports the FIRST acceptable offset, so every offset ahead of the
     true one is an independent chance to false-hit first. Ask for a false
     frame probability and let the library answer. */
  const size_t window_bits = PERIOD / (size_t)SPS;
  const double pfa_target  = 1e-6;
  const int max_err = dp_syncword_max_errors_for (sw, window_bits, pfa_target);
  /* -1 means no tolerance clears the target — the marker is too short for
     the window. It is a REFUSAL, and casting it to the unsigned tolerance
     the search takes would turn "impossible" into "accept anything", which
     is how a receiver locks onto noise and reports sixty happy frames. */
  check (max_err >= 0,
         "a tolerance exists for this marker over this search window");
  if (max_err < 0)
    {
      printf ("  %u-bit marker is too short to search %zu bits at Pfa "
              "%.0e\n\n",
              SYNC_BITS, window_bits, pfa_target);
      dp_syncword_destroy (sw);
      goto done;
    }
  printf ("  %u-bit marker, %zu-bit window, Pfa <= %.0e  ->  tolerate %d "
          "bit error(s)\n",
          SYNC_BITS, window_bits, pfa_target, max_err);
  printf ("  (at that tolerance the actual Pfa is %.2e)\n",
          dp_syncword_pfa (sw, (uint32_t)max_err));

  /* Walk the record one burst at a time, and CHECK each frame the search
     lands on. dp_wfm_frame_desc_crc_ok needs the description and the received
     bits and no payload truth at all, so this is the frame error rate a
     receiver can compute on a capture it did not generate. */
  size_t         pos = 0, at_expected = 0;
  unsigned       crc_pass = 0, inverted = 0;
  static uint8_t frame[FRAME_BITS];

  for (unsigned k = 0; k < N_BURSTS && pos < TOTAL_BITS; k++)
    {
      const syncword_hit_t hit = dp_syncword_find (
          sw, rx_bits + pos, TOTAL_BITS - pos, (uint32_t)max_err);
      if (!hit.found)
        break;
      const size_t at = pos + hit.offset;

      /* The sync field is field 0, so where the marker starts IS where the
         frame starts — in bits, which is the unit the search deals in. */
      if (at == (size_t)k * window_bits + DELAY_SAMPLES / (size_t)SPS)
        at_expected++;
      inverted += (unsigned)(hit.inverted != 0);

      if (at + FRAME_BITS <= TOTAL_BITS)
        {
          /* A hit carries its POLARITY, and a receiver that took the offset
             without it would hand the frame decoder inverted bits that fail
             the CRC for the wrong reason. */
          for (size_t j = 0; j < FRAME_BITS; j++)
            frame[j]
                = hit.inverted ? (uint8_t)!rx_bits[at + j] : rx_bits[at + j];
          if (dp_wfm_frame_desc_crc_ok (&d, frame) == 1)
            crc_pass++;
        }
      bursts++;
      pos = at + FRAME_BITS;
    }
  dp_syncword_destroy (sw);

  check (bursts == N_BURSTS, "every burst is found by its marker");
  check (at_expected == N_BURSTS,
         "and each lands on the exact BIT the scene's geometry predicts");
  check (crc_pass == N_BURSTS,
         "every recovered frame passes its CRC — a frame error rate that "
         "needs no payload truth");
  printf ("  %zu burst(s) found, %zu at the exact predicted bit, "
          "%u inverted, %u/%u CRC pass\n",
          bursts, at_expected, inverted, crc_pass, N_BURSTS);
  printf ("  mean power %.4f over the record\n\n",
          got ? acc / (double)got : 0.0);

done:
  (void)remove (path);
  free (many);
  free (listed);
  free (declared);
  free (swept);
  free (readback);
  dp_wfm_plan_destroy (plan);

  if (failures)
    {
      printf ("=== %d check(s) FAILED ===\n", failures);
      return 1;
    }
  printf ("=== all checks passed ===\n");
  return 0;
}