Skip to content

802.11 non-HT OFDM (wlan)

doppler.wlan is the 802.11 half of OFDM. The modulator turns constellation points and a layout into samples and names no standard; wlan turns an 802.11 rate and a PSDU into the layout and the points it takes. It is a table of PPDU formats and a builder from a format and a frame body, so the rules about what goes in each field are written once, and a caller builds a frame without knowing them. For the 802.11 presets from wfmgen, start with the preset guide.

What is held, and what is not. Every number the design page (docs/design/wlan.md) marks checked against the text of IEEE Std 802.11-2024 is a constant in wlan or a literal in dp_wlan_tables_standard(). The unit test holds the RATE bits, both training sequences, the constellation encodings and the 2/3 and 3/4 puncture masks against their printed form. The second interleaver permutation is held by its worked 16-QAM example and by a re-derivation of Eq. (17-16) and (17-17) for every index of all four pairs, which is the printed equation implemented a second time, not a printed table. The 127 pilot signs are held against the ones ETSI TS 101 475 V1.3.1, 5.6, prints; that the SIGNAL field takes the first is not checked. All eight rates are compared with a chain written in the test: every Data bit, at five lengths, and with no window, every point, each SIGNAL and Data symbol's 64 M useful samples, and two 64 M blocks of each training field, at M = 1 (at the least and the most padding and at 1 and 4095 octets, among others) and at M = 2 (100 octets). At those lengths and M, each prefix and the L-STF's ten periods are held against the PPDU's own samples. The ten periods are our own rule. None of the documents it uses is IEEE's, and some only repeat IEEE's values, so it supports the rules and doesn't show that the library meets IEEE 802.11. The 5/6 code rate is Clause 19's and is not checked. A release has a downstream record, whose results and limits are in section 7 of the design page. It doesn't cover this commit, so matching IEEE 802.11 isn't certified here. An earlier result, v0.84.0's, is Annex I.1's worked example at 36 Mb/s, run downstream (#3148). This tree holds none of Annex I's tables, so the example is not a test here.

A rate

A non-HT rate is a constellation and a BCC code rate, the two columns of the rate table that do not change with the channel clocking. Wlan holds one and says what follows from it:

from doppler.wlan import Wlan

FRAME = 1500  # the PSDU, in octets
RATES = [
    ("bpsk", "r12"),
    ("qpsk", "r12"),
    ("qam16", "r12"),
    ("qam16", "r34"),
    ("qam64", "r34"),
]


def airtime_us(w: Wlan, psdu_len: int) -> float:
    """The preamble (L-STF, L-LTF: 16 us), the SIGNAL symbol (4 us) and the
    Data symbols (4 us each) of a frame at this rate, at 20 MHz."""
    return 16.0 + 4.0 + 4.0 * w.n_sym(psdu_len)


rows = []
for constellation, code_rate in RATES:
    w = Wlan(constellation=constellation, code_rate=code_rate)
    rows.append((constellation, code_rate, w.rate_mbps, w.n_sym(FRAME)))

rate_mbps is the data rate at 20 MHz as Table 17-6 prints it (6, 9, 12, 18, 24, 36, 48 or 54; 0 for the eight pairs the table does not list), and n_dbps / 4 says the same. Wlan(mbps=N) names a rate by that number instead, and Wlan() is 36 Mb/s, 16-QAM at 3/4, Annex I's rate. A pair is given whole or not at all: Wlan(constellation="qam16") alone is refused. The object holds no clocking, so mbps is the 20 MHz rate; the same PPDU played at 10 or 5 MHz carries half or a quarter of it. In C, dp_wlan_mbps() and dp_wlan_rate_from_mbps() read the table both ways. n_bpsc is bits per subcarrier, n_cbps the coded bits and n_dbps the data bits in one OFDM symbol, and n_sym(psdu_len) the symbols a PSDU of that many octets fills: its bits, with the 16-bit SERVICE field and six tail bits, padded to a whole number of symbols. The airtime at 20 MHz is a 20 us preamble and SIGNAL field and 4 us a symbol; the 10 and 5 MHz clockings double and quadruple every duration and change nothing else.

The minimal MAC

The generator wraps the caller's octets as one MPDU and does nothing more: the whole PSDU as given, an MPDU with its FCS appended, or a header and a body with their FCS. The header is the caller's raw octets. The FCS is CRC-32 over every octet before it, appended least significant octet first, so the CRC over a whole frame is one constant. The C example shows it:

/**
 * wlan_mac_demo.c -- the 802.11 minimal MAC: a frame body to a PSDU.
 *
 * The generator wraps the caller's octets as one MPDU and does nothing more
 * (docs/design/wlan.md, "The minimal MAC"): a header and a body in, the
 * header, the body and the frame check sequence out. The FCS is CRC-32 over
 * every octet before it, appended least significant octet first, so the CRC
 * over a whole frame is the same constant whatever the frame holds.
 *
 * Shows:
 *   1. A 24-octet header and a body wrapped into a PSDU
 *   2. The FCS against the CRC-32 catalogue check value
 *   3. The property the wire order gives: any frame with its FCS appended
 *      has one CRC, a receiver's test for "frame intact"
 *
 * Build and run:
 *   make build
 *   ./build/native/examples/wlan_mac_demo
 */
#include "doppler/wlan/wlan_core.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int
main (void)
{
  printf ("=== doppler 802.11 minimal MAC demo ===\n\n");
  int ok = 1;

  /* 1. A header and a body, wrapped. The header is raw octets the caller
   *    gives: the generator does not build one. */
  uint8_t header[24], body[100], psdu[128];
  for (int i = 0; i < 24; i++)
    header[i] = (uint8_t)(i * 5 + 1);
  for (int i = 0; i < 100; i++)
    body[i] = (uint8_t)(i * 7 + 3);
  const size_t n
      = dp_wlan_mpdu (WLAN_MAC_DATA, header, 24, body, 100, psdu, sizeof psdu);
  printf ("--- 1. %zu octets in, a %zu-octet PSDU out ---\n", (size_t)124, n);
  ok &= (n == 128);
  ok &= (memcmp (psdu, header, 24) == 0);
  ok &= (memcmp (psdu + 24, body, 100) == 0);

  /* how many OFDM symbols it fills at 36 Mb/s: 16-QAM, rate 3/4 */
  dp_wlan_state_t *w = dp_wlan_create (2, 2, 0u, 0, 1);
  if (!w)
    return 1;
  printf (
      "    at 16-QAM rate 3/4 (%u data bits per symbol): %zu OFDM symbols\n\n",
      (unsigned)w->n_dbps, dp_wlan_n_sym (w, n));
  ok &= (dp_wlan_n_sym (w, n) == 8);
  dp_wlan_destroy (w);

  /* 2. The FCS, against the catalogue check value of CRC-32/ISO-HDLC over
   *    the nine ASCII digits: CBF43926, appended least significant first. */
  const uint8_t digits[9] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
  uint8_t       check[16];
  const size_t  nc
      = dp_wlan_mpdu (WLAN_MAC_FCS, NULL, 0, digits, 9, check, sizeof check);
  printf ("--- 2. The FCS ---\n    123456789 -> %02X %02X %02X %02X "
          "(the check value CBF43926, least significant octet first)\n\n",
          check[9], check[10], check[11], check[12]);
  ok &= (nc == 13 && check[9] == 0x26 && check[10] == 0x39 && check[11] == 0xF4
         && check[12] == 0xCB);

  /* 3. Any frame with its FCS appended has the same CRC. */
  const dp_crc_t model = DP_CRC_32_ISO_HDLC;
  const uint64_t a     = dp_crc_bytes (&model, psdu, n);
  uint8_t        other[128];
  for (int i = 0; i < 100; i++)
    body[i] = (uint8_t)(i * 11 + 9);
  dp_wlan_mpdu (WLAN_MAC_DATA, header, 24, body, 100, other, sizeof other);
  const uint64_t b = dp_crc_bytes (&model, other, 128);
  printf ("--- 3. The CRC over a whole frame ---\n    %08llX and %08llX: ",
          (unsigned long long)a, (unsigned long long)b);
  printf ("%s\n\n", a == b ? "equal, whatever the frame" : "DIFFERENT");
  ok &= (a == b);

  if (!ok)
    {
      fprintf (stderr, "FAILED\n");
      return 1;
    }
  printf ("OK\n");
  return 0;
}

The stages from Python

Wlan reaches each stage of the chain, so a frame can be built and inspected a stage at a time. Every method is the C function of the same name over the object's rate and the standard tables.

import numpy as np
from doppler.wlan import Wlan

w = Wlan(constellation="qam16", code_rate="r34")        # 36 Mb/s
header = np.arange(24, dtype=np.uint8)                  # the caller's header
body = np.arange(10, dtype=np.uint8)
psdu = w.mpdu(body, header)                             # + the 4-octet FCS
assert psdu.shape == (38,)

sig = w.signal_bits(len(psdu))                          # 24 bits
data = w.data_bits(psdu, 93)                            # n_sym * n_dbps bits
points = w.points(psdu, 93)                             # 48 + 48 * n_sym
assert sig.shape == (24,)
assert data.shape == (w.n_sym(len(psdu)) * w.n_dbps,)
assert points.shape == (w.points_max_out(len(psdu)),)

x = w.ppdu(psdu, 93)                                    # the samples
assert x.shape == (w.ppdu_max_out(len(psdu)),)

A refusal is a ValueError that says why: a scrambler state outside 1 to 127, a PSDU outside 1 to 4095 octets, or a rate Table 17-6 has no row for.

The PPDU

A whole frame, from a PSDU to samples, is one call, dp_wlan_ppdu(). The steps, in order, with the example that runs all of them:

  1. Wrap the body as a PSDU (dp_wlan_mpdu(), above), or bring one.
  2. Choose the rate (dp_wlan_rate_t: bits per subcarrier and a code rate) and ask dp_wlan_rate_n_sym() how many Data OFDM symbols the PSDU fills.
  3. Size the buffer. dp_wlan_ppdu_len(n_sym, window, oversample) is the whole PPDU in samples, oversample * (160 + 160 + 80 + 80 * n_sym) plus the window's tail; the caller allocates it, and a buffer one sample short is a refusal, not a truncation.
  4. Render. dp_wlan_ppdu(tables, &rate, psdu, len, state, window, oversample, out, cap) runs the SERVICE, tail and pad assembly, the scrambler (state is its register, 1 to 127), the punctured BCC, the interleaver, the mapper, the pilots and the OFDM modulator, and writes the L-STF, the L-LTF, the SIGNAL field and the Data field. It returns the samples written, or 0, changing nothing, if any stage refuses.
  5. Check it. The example tests the length, that the L-STF repeats every 16 samples, that the L-LTF's two long symbols are alike, and the power of each field: exactly one for the two training fields (the library's scale is 1/sqrt 52, one unit over the 52 occupied subcarriers) and, for each SIGNAL or Data symbol, the energy of its own grid over 52, which Parseval ties to the points it was built from. A Data symbol's power is not one: a 16-QAM symbol's 48 points have a mean power of one only on average over the constellation, and the cyclic prefix repeats only the body's last 16 samples. The Data symbols are compared with the library's own points, so that check holds the renderer to the mapper and nothing more. The unit test holds the mapper to the encodings that TS 101 475 V1.3.1, 5.5, prints, and the samples to points made without it, at all eight rates; the training fields' exact one holds the library to its own scale, unit mean power over the 52 occupied subcarriers. That is the library's convention, not the standard's: Annex I's printed time-domain samples are the same math with the transform's own 1/64, so they equal the library's times sqrt(52)/64. The unit tests hold that factor, not a printed value: a table whose three scales are 1/64 renders the Annex's scaling. The chain was also compared downstream, outside this repository, against the Annex I.1 worked example with that factor applied, time-domain stage included (recorded on #3148); no test in this tree runs it.
/**
 * wlan_ppdu_demo.c -- an 802.11a/g PPDU, rendered to samples in one call.
 *
 * The steps a caller takes, in the order they happen:
 *
 *   1. dp_wlan_mpdu()          wrap a body as a PSDU, FCS included
 *   2. dp_wlan_rate_n_sym()    how many Data OFDM symbols that PSDU fills at
 *                              the chosen rate
 *   3. dp_wlan_ppdu_len()      how many samples the whole PPDU occupies: the
 *                              CALLER allocates the buffer
 *   4. dp_wlan_ppdu()          the render: L-STF, L-LTF, SIGNAL and the Data
 *                              field, from the PSDU, the rate, the scrambler
 *                              register state and the standard's tables
 *   5. check what came out     the length, the fields' structure, the power
 *   6. oversample              the same PPDU at 2x the rate, by a larger
 *                              transform, held to the 20 MHz one
 *
 * dp_wlan_ppdu() does everything between a PSDU and its samples: the SERVICE,
 * tail and pad assembly, the scrambler, the punctured BCC, the interleaver,
 * the constellation mapper, the pilots and the OFDM modulator. It returns 0,
 * changing nothing, if any stage refuses: a rate the standard does not have, a
 * scrambler state outside 1..127, a buffer too small for the PPDU.
 *
 * What is checked here, each in the language of the standard rather than of
 * this code: the PPDU is 160 + 160 + 80 + 80 * N_SYM samples; the L-STF is
 * ten repetitions of one 16-sample pattern; the L-LTF's two long symbols are
 * identical, preceded by the double guard interval; and the power of each
 * field is what this library's scale (1/sqrt 52, #3148) makes it: exactly one
 * for the two training fields, and for each SIGNAL or Data symbol the energy
 * of its own grid over 52, which Parseval ties to the points it was built
 * from. The scale is the library's convention (unit mean power over the 52
 * occupied subcarriers, #3148); Annex I's printed samples are the same math
 * with the transform's own 1/64, so they equal ours times sqrt(52)/64.
 *
 * Build and run:
 *   make build
 *   ./build/native/examples/wlan_ppdu_demo
 */
#include "doppler/wlan/wlan_core.h"
#include <complex.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef float _Complex cf;

/* |x|^2 */
static double
pw (cf x)
{
  return (double)crealf (x) * crealf (x) + (double)cimagf (x) * cimagf (x);
}

int
main (void)
{
  printf ("=== doppler 802.11 PPDU demo ===\n\n");
  int ok = 1;

  /* 1. A 24-octet header and a 100-octet body, wrapped as one PSDU. */
  uint8_t header[24], body[100], psdu[128];
  for (int i = 0; i < 24; i++)
    header[i] = (uint8_t)(i * 5 + 1);
  for (int i = 0; i < 100; i++)
    body[i] = (uint8_t)(i * 7 + 3);
  const size_t len
      = dp_wlan_mpdu (WLAN_MAC_DATA, header, 24, body, 100, psdu, sizeof psdu);
  ok &= (len == 128);

  /* 2-3. 16-QAM at code rate 3/4 (36 Mb/s): how many Data symbols, how many
     samples. The buffer is the caller's; the library never allocates it. */
  const dp_wlan_tables_t *tables = dp_wlan_tables_standard ();
  const dp_wlan_rate_t    rate   = { 4, WLAN_CODE_3_4 };
  const size_t            n_sym  = dp_wlan_rate_n_sym (&rate, len);
  const size_t            n      = dp_wlan_ppdu_len (n_sym, 0u, 1u);
  printf (
      "--- 1-3. a %zu-octet PSDU at 16-QAM rate 3/4: %zu Data OFDM symbols, "
      "%zu samples\n",
      len, n_sym, n);
  ok &= (n_sym == 8 && n == 160 + 160 + 80 + 80 * n_sym);
  cf *ppdu = malloc (n * sizeof *ppdu);
  if (!ppdu)
    return 1;

  /* 4. The render. The scrambler register starts at 93 (any of 1..127; the
     standard's example uses 1011101b). Zero window: no transition shaping. */
  const size_t got
      = dp_wlan_ppdu (tables, &rate, psdu, len, 93, 0u, 1u, ppdu, n);
  printf ("--- 4. dp_wlan_ppdu wrote %zu samples\n", got);
  ok &= (got == n);

  /* 5a. The L-STF: ten repetitions of one 16-sample pattern. */
  double stf = 0;
  for (size_t i = 0; i + 16 < 160; i++)
    stf = fmax (stf, cabs ((double _Complex)ppdu[i] - ppdu[i + 16]));
  printf ("--- 5. L-STF periodic with period 16: worst difference %.1e\n",
          stf);
  ok &= (stf < 1e-5);

  /* 5b. The L-LTF: a 32-sample double guard, then two identical 64-sample
     long symbols. */
  double ltf = 0;
  for (size_t i = 0; i < 64; i++)
    ltf = fmax (ltf, cabs ((double _Complex)ppdu[160 + 32 + i]
                           - ppdu[160 + 32 + 64 + i]));
  printf ("    L-LTF's two long symbols alike: worst difference %.1e\n", ltf);
  ok &= (ltf < 1e-5);

  /* 5c. Power, field by field, each against what it must be.
   *
   * The training fields are deterministic, and this library's scale
   * (1/sqrt 52, one unit for the 52 occupied subcarriers) makes their mean
   * power exactly one. A SIGNAL or Data OFDM symbol is not constant: its 64
   * body samples have, by Parseval, a mean power equal to the grid's energy
   * over 52, that is (the sum of |point|^2 over its 48 data points, plus 4
   * for the four unit pilots) / 52, so each is held to THAT, computed from
   * the points dp_wlan_points() returns for the same PSDU, with no tolerance
   * for chance. (The 80-sample symbol's power differs from its body's by the
   * cyclic prefix, which repeats only the body's last 16 samples; and a
   * 16-QAM payload's mean |point|^2 is one only on average over the
   * constellation, 8 symbols of it are not.)
   *
   * What this holds, and what it does not: the Data bodies are compared with
   * the library's OWN points, so that check ties the renderer (the layout,
   * the pilots, the transform and the scale) to the mapper by Parseval, and
   * nothing more. test_wlan_core.c holds the mapper to the encodings that
   * ETSI TS 101 475 V1.3.1, 5.5, prints, and, with no window at M = 1
   * and 2, the fields' samples at all eight rates to points made without it.
   * Neither shows that the library meets IEEE 802.11; Annex I.1's comparison
   * was run downstream (#3148). The two training fields reading exactly one
   * holds the library to its own convention, unit power, and nothing more:
   * Annex I's printed samples equal ours times sqrt(52)/64, which the unit
   * tests hold with a table whose three scales are 1/64. */
  double p_stf = 0, p_ltf = 0;
  for (size_t i = 0; i < 160; i++)
    {
      p_stf += pw (ppdu[i]);
      p_ltf += pw (ppdu[160 + i]);
    }
  p_stf /= 160.0;
  p_ltf /= 160.0;
  printf ("    mean power: L-STF %.6f, L-LTF %.6f (both exactly 1)\n", p_stf,
          p_ltf);
  ok &= (fabs (p_stf - 1.0) < 1e-6 && fabs (p_ltf - 1.0) < 1e-6);

  cf *pts = malloc ((48 + 48 * n_sym) * sizeof *pts);
  if (!pts)
    return 1;
  ok &= (dp_wlan_points (tables, &rate, psdu, len, 93, pts, 48 + 48 * n_sym)
         == 48 + 48 * n_sym);
  double worst = 0, data_got = 0, data_want = 0;
  for (size_t s = 0; s < 1 + n_sym; s++) /* SIGNAL, then the Data symbols */
    {
      double grid = 4.0, body = 0;
      for (size_t k = 0; k < 48; k++)
        grid += pw (pts[48 * s + k]);
      for (size_t i = 0; i < 64; i++)
        body += pw (ppdu[320 + 80 * s + 16 + i]);
      body /= 64.0;
      grid /= 52.0;
      worst = fmax (worst, fabs (body - grid));
      if (s == 0)
        printf ("    SIGNAL body %.6f (BPSK and four pilots: exactly 1)\n",
                body);
      else
        {
          data_got += body;
          data_want += grid;
        }
    }
  printf ("    Data bodies: mean power %.4f, from the points %.4f (a "
          "16-QAM payload of %zu symbols; the constellation's own mean is "
          "1)\n",
          data_got / (double)n_sym, data_want / (double)n_sym, n_sym);
  printf ("    every body against its grid's energy: worst difference "
          "%.1e\n",
          worst);
  ok &= (worst < 1e-5);
  free (pts);

  /* A refusal changes nothing and returns 0: a scrambler state of 0 is not
     one the register can hold. */
  cf spare[1] = { 0 };
  ok &= (dp_wlan_ppdu (tables, &rate, psdu, len, 0, 0u, 1u, spare, 1) == 0);
  ok &= (dp_wlan_ppdu (tables, &rate, psdu, len, 93, 0u, 1u, ppdu, n - 1)
         == 0);
  printf ("    a zero scrambler state, and a buffer one sample short, are "
          "refused with 0\n");

  /* 6. The same PPDU at twice the rate, 40 MHz: a transform twice as long,
     the same subcarriers, not a resampler. Every second sample is the 20 MHz
     PPDU's, to float rounding: the relative l2 error is held to 1.5e-3, the
     tolerance and statistic of the unit test (test_wlan_core.c, WLAN_FFT_TOL:
     the FFT's own 5e-4 per component, for the two transforms compared). */
  const size_t n2 = dp_wlan_ppdu_len (n_sym, 0u, 2u);
  cf          *p2 = malloc (n2 * sizeof *p2);
  if (!p2)
    return 1;
  ok &= (dp_wlan_ppdu (tables, &rate, psdu, len, 93, 0u, 2u, p2, n2) == n2);
  double num = 0, den = 0;
  for (size_t i = 0; i < n; i++)
    {
      const double d = cabs ((double _Complex)p2[2 * i] - ppdu[i]);
      num += d * d;
      den += pw (ppdu[i]);
    }
  const double dec = sqrt (num / den);
  printf (
      "--- 6. at 2x oversampling: %zu samples, every second one the 20 MHz "
      "PPDU's, relative l2 difference %.1e\n",
      n2, dec);
  ok &= (n2 == 2 * n && dec < 1.5e-3);
  free (p2);

  free (ppdu);
  printf ("\nwlan_ppdu_demo: %s\n", ok ? "OK" : "FAILED");
  return ok ? 0 : 1;
}

Oversampling. oversample is M, the output rate over the 20 MHz clock, 1 to 128: the PPDU is rendered by a transform M times longer, the same 52 subcarriers with the rest of the grid zero, not resampled. M = 1 is the PPDU above, bit for bit, and the signal at M is the same waveform sampled M times as finely (x_M[M n] = x_1[n] for the same scale, held to a tolerance in the unit test). window counts samples at the output rate, so the 100 ns default (T_TR checked) is 2 * M, which is what a caller who gives no window gets (window = 0 is no window); it may be at most 80 * M (an OFDM symbol, the modulator's limit), and above the 16 * M guard interval the cross-fade reaches into the symbol body. Outside the 52 subcarriers the spectrum is only the symbol boundaries' skirts, which a longer window lowers.

In Python the same PPDU is one call on Wlan, which holds the rate, the window and the oversampling as configuration and takes the PSDU and the scrambler's initial state:

import numpy as np
from doppler.wlan import Wlan

w = Wlan(constellation="qam16", code_rate="r34")  # 36 Mb/s at 20 MHz
x = w.ppdu(np.arange(100, dtype=np.uint8), 93)
# no window given: the 100 ns default, 2 samples at 20 MHz
assert w.window == 2
assert x.shape == (160 + 160 + 80 + 80 * w.n_sym(100) + w.window,)
w2 = Wlan(constellation="qam16", code_rate="r34", oversample=2)
x2 = w2.ppdu(np.arange(100, dtype=np.uint8), 93)
assert w2.window == 4  # still 100 ns, at twice the rate
assert x2.shape == (2 * (x.shape[0] - w.window) + w2.window,)  # twice as fine

A PSDU outside 1 to 4095 octets, a scrambler state outside 1 to 127, or an out= buffer that is too short raises ValueError saying which.

The stages are public if a caller wants one of them: dp_wlan_points() is the constellation points of a PPDU in layout order (the SIGNAL field's 48, then the Data field's), and dp_wlan_layout() builds the matching OFDM layout in a modulator, which is what the wfmgen ofdm waveform type uses. All of them take the tables the standard supplies, dp_wlan_tables_standard().

What it costs. A 1000-octet PSDU at the largest rate pair, 64-QAM rate 3/4 (54 Mb/s, 38 Data symbols), is timed stage by stage in native/benchmarks/wlan/ (the values are taken at each release's benchmark set): the interleaver, the mapper, the points, the layout and the whole PPDU.