OFDM¶
doppler.ofdm is a general OFDM modulator. It turns
constellation points into baseband samples for any OFDM signal a layout
can describe, and it names no standard: a standard's preset is a layout that
something else builds, and wfmgen's ofdm waveform type is one more caller.
For the 802.11 presets, start with the preset guide.
The design page says why the layout has the shape it has.
A layout is a list of classes, each one kind of OFDM symbol, and a list of runs, each a count of OFDM symbols of one class. A class says the transform length, which subcarriers may carry a data point (the others carry nothing), the fixed entries the modulator places itself (pilots, training values) with the pattern phase each belongs to, an optional polarity sequence that multiplies them, optional per-subcarrier gains, how many samples the body reads from the transform, the cyclic prefix, and the window.
Ofdm holds its layout and is built by calls: add_class, then
add_subcarrier_gain and add_polarity for the class's optional arrays,
add_fixed for each fixed entry, and add_run. modulate renders whole OFDM
symbols from the points it is given, taking from them exactly as many as each
symbol's data subcarriers need, and finish returns the window's last fall.
points_remaining says how many points the rest of the layout consumes, so a
whole layout renders in one call.
import numpy as np
from doppler.mpsk import mpsk_map
from doppler.ofdm import Ofdm
N, CP, WINDOW, N_SYM = 64, 16, 2, 10
used = np.zeros(N, np.uint8) # index k + N/2
used[N // 2 - 26 : N // 2 + 27] = 1
used[N // 2] = 0 # DC is null
PILOTS = (-21, -7, 7, 21)
ofdm = Ofdm()
cls = ofdm.add_class(N, used, cyclic_prefix=CP, window=WINDOW)
ofdm.add_polarity(cls, np.array([1, 1, 1, -1], np.complex64))
for k in PILOTS:
ofdm.add_fixed(cls, 0xFFFFFFFF, k, 1.0) # every pattern phase
ofdm.add_run(cls, N_SYM)
rng = np.random.default_rng(0)
n_points = ofdm.points_remaining() # 48 data subcarriers x 10 symbols
points = mpsk_map(rng.integers(0, 4, n_points).astype(np.uint8), 4)
samples = np.concatenate([ofdm.modulate(points), ofdm.finish()])
The script goes on to check what it made: a forward FFT of each OFDM symbol's body, with the cyclic prefix dropped, returns the grid that went in, with every data point on its subcarrier, every pilot at its value times the polarity, and nothing on the null subcarriers. That is the check to reach for with any layout.
What comes out. modulate and finish return a one-dimensional
complex64 array of samples, a new one on every call that the caller owns
(pass out= to write into a complex64 array of your own instead; from C the
caller always supplies the buffer and sizes it with modulate_max_out).
The samples are the unnormalized inverse transform of the grid, multiplied
by the class's scale (1.0 by default): a grid of unit entries gives a mean
power equal to the number of occupied subcarriers (52 in the example above),
not 1, so pass scale=1/np.sqrt(occupied) to add_class for unit power.
Refusals. A build call that cannot be honoured raises ValueError,
changing nothing. A layout that is not usable (no run, a window longer than an
OFDM symbol of its class, two fixed entries on one subcarrier in one phase)
is found out when it is checked, and check says which: it raises
ValueError with a sentence (the layout has no run) and returns None
when the layout is usable. modulate checks on first use and refuses the
same way: it raises ValueError with the sentence for an unusable layout,
for a point count that ends inside an OFDM symbol, and for one that runs past
the layout, leaving the modulator untouched in every case. A call that
renders nothing, because no points remain or because zero points were given
where the next OFDM symbol needs some, returns an empty array and is not an
error. A buffer too short is never the library's own refusal in Python: it
sizes the output itself, and an out= shorter than modulate_max_out is
rejected by the binding (out has N elements, need >= M) before the call.
In C the same refusal is a negative return from dp_ofdm_modulate_why() with
a sentence. Adding to a checked layout re-opens it and keeps the position and
the window tail, so a signal can be extended while it is rendered.
The same lifecycle from C, with the caller owning both buffers and the
refusal's reason read from dp_ofdm_check_why(). It checks itself and exits
non-zero if anything it prints is wrong:
/**
* ofdm_demo.c -- an OFDM signal from a layout, and the grid back out of it.
*
* The lifecycle a caller manages, in the order it happens:
*
* 1. dp_ofdm_create() the modulator, empty
* 2. dp_ofdm_add_class / _polarity / _fixed / _run
* the layout; each call COPIES what
* it is given, so the caller's arrays
* need not outlive the call
* 3. dp_ofdm_check_why() the layout is usable, or WHY it is
* not (modulate itself refuses an
* unusable layout with a bare 0)
* 4. dp_ofdm_points_remaining(), how many points to supply and how
* dp_ofdm_modulate_max_out() many samples will come out: the
* CALLER allocates both buffers
* 5. dp_ofdm_modulate(), dp_ofdm_finish()
* the samples, written into the
* caller's buffer; finish returns
* the window's last fall
* 6. dp_ofdm_destroy() frees the modulator, never the
* caller's buffers
*
* The layout is a generic one: a 64-point transform with 52 occupied
* subcarriers (DC and the band edges null), four pilots that follow a
* polarity sequence, a 16-sample cyclic prefix and a 2-sample window,
* carrying QPSK, ten OFDM symbols. The samples are the UNNORMALIZED inverse
* transform of the grid (the default scale is 1.0), so the mean power is the
* grid's energy, 52, not 1. A forward transform of each symbol's body, with
* the prefix dropped and divided by the transform length, returns the grid
* that went in: that is how the structure is checked here.
*
* Build and run:
* make build
* ./build/native/examples/ofdm_demo
*/
#include "doppler/ofdm/ofdm_core.h"
#include <complex.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 64u
#define CP 16u
#define WINDOW 2u
#define N_SYM 10u
#define TWO_PI 6.283185307179586476925286766559
typedef float _Complex cf;
/* Coefficient k of the forward sum over one body, divided by its length: the
inverse of the modulator's own transform, and nothing else. */
static double _Complex coef (const cf *body, int k)
{
double _Complex s = 0;
for (unsigned m = 0; m < N; m++)
s += (double _Complex)body[m]
* cexp (-I * TWO_PI * (double)k * (double)m / (double)N);
return s / (double)N;
}
int
main (void)
{
printf ("=== doppler OFDM modulator demo ===\n\n");
int ok = 1;
/* 1-2. Build the layout. The map is indexed k + N/2; the pilots are fixed
entries in every pattern phase, multiplied by the polarity sequence. */
static const int pilot_k[4] = { -21, -7, 7, 21 };
static const float pol[4] = { 1.0f, 1.0f, 1.0f, -1.0f };
uint8_t used[N];
memset (used, 0, sizeof used);
for (int k = -26; k <= 26; k++)
used[k + (int)N / 2] = (k != 0);
dp_ofdm_state_t *o = dp_ofdm_create ();
if (!o)
return 1;
const int cls = dp_ofdm_add_class (o, N, used, N, 0u, CP, WINDOW, 1.0, 1u);
cf polarity[4];
for (int i = 0; i < 4; i++)
polarity[i] = pol[i];
ok &= (cls == 0);
ok &= (dp_ofdm_add_polarity (o, 0u, polarity, 4u) == 0);
for (int i = 0; i < 4; i++)
ok &= (dp_ofdm_add_fixed (o, 0u, OFDM_ALL_PHASES, pilot_k[i], 1.0) == 0);
ok &= (dp_ofdm_add_run (o, 0u, N_SYM, OFDM_CP_DEFAULT, 0u, 0u) == 0);
/* 3. An unusable layout says why. A second modulator with a class and no
run is refused with a sentence, by check and by modulate_why alike;
plain modulate would return a bare 0. */
{
const char *why = NULL;
dp_ofdm_state_t *empty = dp_ofdm_create ();
dp_ofdm_add_class (empty, N, used, N, 0u, CP, WINDOW, 1.0, 1u);
const int bad = dp_ofdm_check_why (empty, &why);
printf ("--- 3. a layout with no run: check = %d (%s)\n", bad,
why ? why : "no reason");
ok &= (bad < 0 && why != NULL);
cf pts1[1] = { 0 }, out1[1];
const char *why2 = NULL;
ok &= (dp_ofdm_modulate_why (empty, pts1, 0u, out1, 0u, &why2) < 0
&& why2 == why);
dp_ofdm_destroy (empty);
}
const char *why = NULL;
ok &= (dp_ofdm_check_why (o, &why) == 0 && why == NULL);
/* 4. The caller sizes and owns both buffers. Points are QPSK, one per data
subcarrier: 52 occupied less 4 pilots, 48 a symbol. */
const size_t n_points = dp_ofdm_points_remaining (o);
const size_t n_out = dp_ofdm_modulate_max_out (o, n_points);
printf ("--- 4. %zu points in, %zu samples out (%u OFDM symbols of %u)\n",
n_points, n_out, N_SYM, N + CP);
ok &= (n_points == (size_t)48u * N_SYM);
ok &= (n_out == (size_t)N_SYM * (N + CP));
cf *points = malloc (n_points * sizeof *points);
cf *out = malloc ((n_out + WINDOW) * sizeof *out);
if (!points || !out)
return 1;
unsigned rng = 1234567u;
for (size_t i = 0; i < n_points; i++)
{
rng = rng * 1664525u + 1013904223u;
points[i] = (float)(((rng >> 16) & 1u) ? 0.70710678 : -0.70710678)
+ (float)(((rng >> 17) & 1u) ? 0.70710678 : -0.70710678) * I;
}
/* 5. Render. The whole layout in one call, then the window's last fall. */
const size_t got = dp_ofdm_modulate (o, points, n_points, out, n_out);
const size_t fin = dp_ofdm_finish (o, WINDOW, out + got, WINDOW);
printf ("--- 5. modulate wrote %zu samples, finish %zu\n", got, fin);
ok &= (got == n_out && fin == WINDOW);
ok &= (dp_ofdm_points_remaining (o) == 0);
/* modulate_why tells a refusal from a render of nothing: at the end of the
layout, no points is a valid 0 that leaves *why as it was set; one point
more than the layout has is refused, with the reason. */
{
const char *kept = "untouched", *reason = NULL;
const int64_t nothing
= dp_ofdm_modulate_why (o, points, 0u, out, 0u, &kept);
const int64_t refused
= dp_ofdm_modulate_why (o, points, 1u, out, n_out, &reason);
printf ("--- 5b. no points at the end: %lld, why %s; one point too many: "
"%lld (%s)\n",
(long long)nothing, kept, (long long)refused,
reason ? reason : "no reason");
ok &= (nothing == 0 && strcmp (kept, "untouched") == 0);
ok &= (refused < 0 && reason != NULL);
}
/* Invert it: each body's forward transform is the grid. */
int grid_ok = 1;
size_t drawn = 0;
for (unsigned s = 0; s < N_SYM; s++)
{
const cf *body = out + (size_t)s * (N + CP) + CP;
for (int k = -(int)N / 2; k < (int)N / 2; k++)
{
double _Complex want = 0;
int is_pilot = 0;
for (int i = 0; i < 4; i++)
if (k == pilot_k[i])
{
want = pol[s % 4];
is_pilot = 1;
}
if (!is_pilot && used[k + (int)N / 2])
want = points[drawn++];
grid_ok &= (cabs (coef (body, k) - want) < 1e-5);
}
}
printf ("--- the grid read back from the samples: %s\n",
grid_ok ? "every point, pilot and null where it was put" : "WRONG");
ok &= grid_ok && (drawn == n_points);
double p = 0;
for (size_t i = CP; i < n_out; i++)
p += (double)crealf (out[i]) * crealf (out[i])
+ (double)cimagf (out[i]) * cimagf (out[i]);
p /= (double)(n_out - CP);
printf ("--- mean power %.1f: the grid's energy (52), not 1; scale = 1/sqrt "
"(52) would give 1\n",
p);
ok &= (fabs (p - 52.0) < 2.0);
/* 6. Free the modulator; the two buffers are the caller's own. */
dp_ofdm_destroy (o);
free (points);
free (out);
printf ("\nofdm_demo: %s\n", ok ? "OK" : "FAILED");
return ok ? 0 : 1;
}
Resuming. The modulator is serializable: get_state and set_state
carry the position in the layout and the window tail, and the layout is the
reject key, so a blob restores only into a modulator built with the same
layout. A call renders whole OFDM symbols, so a render splits and resumes on
an OFDM-symbol boundary and is bit for bit the same as one call.