Skip to content

File ofdm_core.h

FileList > doppler > ofdm > ofdm_core.h

Go to the source code of this file

A general OFDM modulator: constellation points and a layout in, baseband samples out. More...

  • #include "doppler/clib_common.h"
  • #include "doppler/dp_state.h"
  • #include "doppler/fft/fft_core.h"
  • #include "doppler/jm_perf.h"

Classes

Type Name
struct dp_ofdm_state_t
Ofdm state: the layout (configuration) and the position in it (state).
struct ofdm_class_t
One kind of OFDM symbol. The arrays are owned by the modulator, copied in by dp_ofdm_add_class() anddp_ofdm_add_fixed() .
struct ofdm_fixed_t
One entry of a class's fixed pattern.
struct ofdm_run_t
A run of OFDM symbols of one class.

Public Functions

Type Name
int dp_ofdm_add_class (dp_ofdm_state_t * state, uint32_t n_fft, const uint8_t * subcarrier_map, size_t subcarrier_map_len, uint32_t useful, uint32_t cyclic_prefix, uint32_t window, double scale, uint32_t n_phase)
Append one kind of OFDM symbol and return its index.
int dp_ofdm_add_fixed (dp_ofdm_state_t * state, uint32_t class_index, uint32_t phase, int32_t k, double _Complex value)
Append one fixed entry to a class: a pilot or a training value.
int dp_ofdm_add_polarity (dp_ofdm_state_t * state, uint32_t class_index, const float _Complex * polarity, size_t polarity_len)
Give a class a polarity sequence.
int dp_ofdm_add_run (dp_ofdm_state_t * state, uint32_t class_index, uint32_t count, uint32_t cyclic_prefix, uint32_t polarity_offset, uint32_t first_phase)
Append a run of OFDM symbols of one class and return its index.
int dp_ofdm_add_subcarrier_gain (dp_ofdm_state_t * state, uint32_t class_index, const float _Complex * gain, size_t gain_len)
Give a class a complex multiplier per subcarrier.
int dp_ofdm_check (dp_ofdm_state_t * state)
The plain form of dp_ofdm_check_why() : 0 if the layout is usable, a negative value if not, with no sentence. What a C caller that does not print the reason calls.
int dp_ofdm_check_why (dp_ofdm_state_t * state, const char ** why)
Check the whole layout and derive what the render uses; say why when it is not usable. This is the binding's check .
dp_ofdm_state_t * dp_ofdm_create (void)
Create an empty modulator (no classes, no runs).
void dp_ofdm_destroy (dp_ofdm_state_t * state)
Destroy a modulator and release all memory.
size_t dp_ofdm_finish (dp_ofdm_state_t * state, size_t n, float _Complex * out, size_t max_out)
Return the window tail still held and clear it.
size_t dp_ofdm_finish_max_out (dp_ofdm_state_t * state, size_t n)
Capacity dp_ofdm_finish() needs forn samples.
void dp_ofdm_get_state (const dp_ofdm_state_t * state, void * blob)
Write the state into blob .
size_t dp_ofdm_modulate (dp_ofdm_state_t * state, const float _Complex * in, size_t n_in, float _Complex * out, size_t max_out)
The plain form of dp_ofdm_modulate_why() .
size_t dp_ofdm_modulate_max_out (dp_ofdm_state_t * state, size_t n_in)
Samples a render of n_in points writes.
int64_t dp_ofdm_modulate_why (dp_ofdm_state_t * state, const float _Complex * in, size_t n_in, float _Complex * out, size_t max_out, const char ** why)
Render OFDM symbols from constellation points, or say why not.
size_t dp_ofdm_points_remaining (dp_ofdm_state_t * state)
Constellation points the rest of the layout consumes.
void dp_ofdm_reset (dp_ofdm_state_t * state)
Return to the first OFDM symbol, keeping the layout.
int dp_ofdm_set_state (dp_ofdm_state_t * state, const void * blob)
Restore the state; nonzero rejects blob .
size_t dp_ofdm_state_bytes (const dp_ofdm_state_t * state)
Bytes in the blob dp_ofdm_get_state() writes.

Macros

Type Name
define OFDM_ALL_PHASES 0xFFFFFFFFu
A fixed entry that applies in every pattern phase.
define OFDM_COUNT_MAX 65536u
Most classes, and most fixed entries per class, a layout holds.
define OFDM_CP_DEFAULT 0xFFFFFFFFu
A run's cyclic prefix that keeps its class's.
define OFDM_DATA 1u
A subcarrier map value: the subcarrier carries a data point.
define OFDM_NULL 0u
A subcarrier map value: the subcarrier carries nothing.
define OFDM_N_FFT_MAX 65536u
Largest transform length of a class.
define OFDM_N_PHASE_MAX 4096u
Largest pattern period of a class, in OFDM symbols.
define OFDM_STATE_MAGIC [**DP\_FOURCC**](dp__state_8h.md#define-dp_fourcc) ('O', 'F', 'D', 'M')
State blob magic ('O','F','D','M').
define OFDM_STATE_VERSION 1u
State blob layout version.

Detailed Description

The modulator names no standard and includes nothing of wfmgen. What it renders is described by a layout, a list of classes (one kind of OFDM symbol: a transform length, a subcarrier map, a fixed pattern of pilots or training values, a scale, a cyclic prefix and a window) and a list of runs (a count of OFDM symbols of one class). A standard's preset is a filled-in layout; the design page, docs/design/ofdm.md, owns the shape and what forced each field.

One OFDM symbol

Per OFDM symbol the modulator takes the data count of the symbol's pattern phase from the points, places them on the data subcarriers that no fixed entry displaces (in increasing subcarrier order), sets the fixed entries of the phase times the class's polarity element, multiplies the grid by the subcarrier gains, inverse transforms, multiplies by scale, reads useful samples cyclically from the transform output, prefixes the last cyclic_prefix of them, and overlap-adds the window.

With m the sample index into the transform output and k the subcarrier, the unnormalized inverse transform is x[m] = scale * sum_k grid[k] * exp(+2j*pi*k*m/n_fft). The body is x[m mod n_fft] for m < useful; the symbol is the last cyclic_prefix body samples followed by the body.

The window

The boundary between two OFDM symbols is a raised-cosine cross-fade of window samples, rise[j] = sin^2 (pi (j + 1/2) / (2 window)) and fall[j] = 1 - rise[j]. The later symbol's first window samples are weighted by rise; the earlier symbol is continued past its end by window samples of the transform output (x[(useful + j) mod n_fft]), weighted by fall, and overlap-added. The length used at a boundary is the later symbol's class window; the first symbol rises from silence over its own, and the last continues over its own. A window of 0 is no window: the output is exactly the symbols, back to back. The continued tail is what crosses a call, so it is state; dp_ofdm_finish returns the last one.

The calls

Build the layout with dp_ofdm_add_class, dp_ofdm_add_fixed and dp_ofdm_add_run; the first dp_ofdm_modulate (or dp_ofdm_check) checks it whole. Adding to a checked layout re-opens it (the position and the held window tail stay), so a signal can be extended while it is rendered. Then dp_ofdm_modulate renders whole OFDM symbols from the points it is given.

dp_ofdm_state_t *o = dp_ofdm_create ();
const uint8_t map[8] = { 0, 1, 1, 1, 0, 1, 1, 1 };   // DC and edge null
dp_ofdm_add_class (o, 8u, map, 8u, 0u, 0u, 0u, 1.0, 1u);
dp_ofdm_add_run (o, 0u, 2u, 0xFFFFFFFFu, 0u, 0u);
float _Complex pts[12], out[16];       // 6 data subcarriers x 2 symbols
for (int i = 0; i < 12; i++)
  pts[i] = 1.0f;
size_t n = dp_ofdm_modulate (o, pts, 12, out, 16);   // n == 16
dp_ofdm_destroy (o);

Public Functions Documentation

function dp_ofdm_add_class

Append one kind of OFDM symbol and return its index.

int dp_ofdm_add_class (
    dp_ofdm_state_t * state,
    uint32_t n_fft,
    const uint8_t * subcarrier_map,
    size_t subcarrier_map_len,
    uint32_t useful,
    uint32_t cyclic_prefix,
    uint32_t window,
    double scale,
    uint32_t n_phase
) 

Copies subcarrier_map. The optional per-subcarrier gains and the polarity sequence are added to the class by dp_ofdm_add_subcarrier_gain() and dp_ofdm_add_polarity(). Refuses (a negative return, nothing changed) what cannot be honoured; a sealed layout is re-opened.

Parameters:

  • state The modulator.
  • n_fft Transform length, 2 to OFDM_N_FFT_MAX.
  • subcarrier_map n_fft entries, index k + n_fft/2, each OFDM_DATA or OFDM_NULL.
  • subcarrier_map_len Must equal n_fft.
  • useful Body samples read cyclically from the transform; 0 reads as n_fft.
  • cyclic_prefix Default prefix, samples; at most useful.
  • window Window transition, samples; at most the symbol length.
  • scale Multiplies the transform output.
  • n_phase Period of the fixed pattern, at least 1.

Returns:

The class index, or a negative value if refused.

>>> import numpy as np
>>> from doppler.ofdm import Ofdm
>>> o = Ofdm()
>>> m = np.zeros(8, np.uint8); m[[1, 2, 3, 5, 6, 7]] = 1
>>> o.add_class(8, m, cyclic_prefix=2)
0
>>> o.add_class(8, m, cyclic_prefix=2, window=1)
1

function dp_ofdm_add_fixed

Append one fixed entry to a class: a pilot or a training value.

int dp_ofdm_add_fixed (
    dp_ofdm_state_t * state,
    uint32_t class_index,
    uint32_t phase,
    int32_t k,
    double _Complex value
) 

A fixed entry displaces data on its subcarrier in its phase (when the map has data there) and is multiplied by the class's polarity element.

Parameters:

  • state The modulator.
  • class_index An existing class.
  • phase The pattern phase it belongs to, below the class's n_phase, or OFDM_ALL_PHASES.
  • k Subcarrier, -(n_fft/2) .. n_fft - n_fft/2 - 1 in integer division: up to n_fft/2 - 1 at an even length, and n_fft/2 at an odd one.
  • value The value placed.

Returns:

0, or a negative value if refused.

>>> import numpy as np
>>> from doppler.ofdm import Ofdm
>>> m = np.zeros(8, np.uint8); m[[1, 2, 3, 5, 6, 7]] = 1
>>> o = Ofdm()
>>> o.add_class(8, m, cyclic_prefix=2)
0
>>> o.add_run(0, 1)
0
>>> o.points_remaining()      # six data subcarriers
6
>>> o.add_fixed(0, 2**32 - 1, 3, 1j)   # a pilot on a data subcarrier
>>> o.points_remaining()      # it displaces one
5

function dp_ofdm_add_polarity

Give a class a polarity sequence.

int dp_ofdm_add_polarity (
    dp_ofdm_state_t * state,
    uint32_t class_index,
    const float _Complex * polarity,
    size_t polarity_len
) 

OFDM symbol n of a run multiplies its fixed entries (never its data) by element (polarity_offset + n) mod polarity_len. Without it the scalar is 1.

Parameters:

  • state The modulator.
  • class_index An existing class without a sequence yet.
  • polarity The sequence.
  • polarity_len At least 1.

Returns:

0, or a negative value if refused.

>>> import numpy as np
>>> from doppler.ofdm import Ofdm
>>> z = np.zeros(8, np.uint8)               # no data subcarriers at all
>>> p = Ofdm(); p.add_class(8, z, cyclic_prefix=2)
0
>>> p.add_fixed(0, 2**32 - 1, 2, 1.0)      # one pilot, every phase
>>> p.add_polarity(0, np.array([1, -1], np.complex64))
>>> p.add_run(0, 2)
0
>>> x = p.modulate(np.empty(0, np.complex64))
>>> bool(np.allclose(x[2:10], -x[12:20]))   # the pilot flips sign
True

function dp_ofdm_add_run

Append a run of OFDM symbols of one class and return its index.

int dp_ofdm_add_run (
    dp_ofdm_state_t * state,
    uint32_t class_index,
    uint32_t count,
    uint32_t cyclic_prefix,
    uint32_t polarity_offset,
    uint32_t first_phase
) 

Parameters:

  • state The modulator.
  • class_index An existing class.
  • count OFDM symbols, at least 1.
  • cyclic_prefix Override of the class's prefix, or OFDM_CP_DEFAULT; at most the class's useful.
  • polarity_offset Polarity element of the run's first symbol.
  • first_phase Pattern phase of the run's first symbol.

Returns:

The run index, or a negative value if refused.

>>> import numpy as np
>>> from doppler.ofdm import Ofdm
>>> m = np.zeros(8, np.uint8); m[[1, 2, 3, 5, 6, 7]] = 1
>>> o = Ofdm()
>>> o.add_class(8, m, cyclic_prefix=2)
0
>>> o.add_run(0, 2)
0
>>> o.add_run(0, 3)
1

function dp_ofdm_add_subcarrier_gain

Give a class a complex multiplier per subcarrier.

int dp_ofdm_add_subcarrier_gain (
    dp_ofdm_state_t * state,
    uint32_t class_index,
    const float _Complex * gain,
    size_t gain_len
) 

Applied to the whole grid (data and fixed entries alike): a per-subband phase rotation, or any per-subcarrier amplitude. Without it the gain is 1 everywhere.

Parameters:

  • state The modulator.
  • class_index An existing class without gains yet.
  • gain n_fft multipliers, index k + n_fft/2.
  • gain_len Must equal the class's n_fft.

Returns:

0, or a negative value if refused.

>>> import numpy as np
>>> from doppler.ofdm import Ofdm
>>> m = np.zeros(8, np.uint8); m[[1, 2, 3, 5, 6, 7]] = 1
>>> o = Ofdm()
>>> o.add_class(8, m, cyclic_prefix=2)
0
>>> o.add_run(0, 1)
0
>>> pts = np.ones(6, np.complex64)
>>> one = o.modulate(pts)
>>> o.reset()
>>> o.add_subcarrier_gain(0, 2 * np.ones(8, np.complex64))
>>> bool(np.allclose(o.modulate(pts), 2 * one))
True

function dp_ofdm_check

The plain form of dp_ofdm_check_why() : 0 if the layout is usable, a negative value if not, with no sentence. What a C caller that does not print the reason calls.

int dp_ofdm_check (
    dp_ofdm_state_t * state
) 

Parameters:

  • state The modulator.

Returns:

0 if the layout is usable, a negative value if not.


function dp_ofdm_check_why

Check the whole layout and derive what the render uses; say why when it is not usable. This is the binding's check .

int dp_ofdm_check_why (
    dp_ofdm_state_t * state,
    const char ** why
) 

Refuses a layout with no run, a window longer than an OFDM symbol of its class, or two fixed entries on one subcarrier in one phase. Derives what the render uses (the data count per phase, the window weights, one inverse transform per distinct length). Idempotent once it has passed.

Parameters:

  • state The modulator.
  • why Where the refusal's sentence goes: a static string naming the condition that failed. Left as the caller set it when the layout is usable. May be NULL.

Returns:

0 if the layout is usable, a negative value if not.

>>> import numpy as np
>>> from doppler.ofdm import Ofdm
>>> o = Ofdm()
>>> m = np.zeros(8, np.uint8); m[[1, 2, 3]] = 1
>>> o.add_class(8, m)
0
>>> o.check()                # no run yet
Traceback (most recent call last):
    ...
ValueError: the layout has no run
>>> o.add_run(0, 1)
0
>>> o.check()                # usable: returns None

function dp_ofdm_create

Create an empty modulator (no classes, no runs).

dp_ofdm_state_t * dp_ofdm_create (
    void
) 

Returns:

Heap-allocated state, never NULL: running out of memory aborts.

Note:

Caller must call dp_ofdm_destroy() when done.


function dp_ofdm_destroy

Destroy a modulator and release all memory.

void dp_ofdm_destroy (
    dp_ofdm_state_t * state
) 

Parameters:

  • state May be NULL.

function dp_ofdm_finish

Return the window tail still held and clear it.

size_t dp_ofdm_finish (
    dp_ofdm_state_t * state,
    size_t n,
    float _Complex * out,
    size_t max_out
) 

After the last OFDM symbol this is the final fall of the window; with a window of 0, or before anything is rendered, there is none.

Parameters:

  • state The modulator.
  • n Samples asked for; the held tail is returned up to this.
  • out Receives the tail.
  • max_out Capacity of out.

Returns:

Samples written.

>>> import numpy as np
>>> from doppler.ofdm import Ofdm
>>> m = np.zeros(8, np.uint8); m[[1, 2, 3, 5, 6, 7]] = 1
>>> o = Ofdm()
>>> o.add_class(8, m, cyclic_prefix=2, window=2)
0
>>> o.add_run(0, 2)
0
>>> x = o.modulate(np.ones(12, np.complex64))
>>> o.finish().size          # the window's last fall
2
>>> o.finish().size          # and it is cleared
0

function dp_ofdm_finish_max_out

Capacity dp_ofdm_finish() needs forn samples.

size_t dp_ofdm_finish_max_out (
    dp_ofdm_state_t * state,
    size_t n
) 

Parameters:

  • state The modulator.
  • n Samples asked for.

Returns:

The held tail's length, at most n.


function dp_ofdm_get_state

Write the state into blob .

void dp_ofdm_get_state (
    const dp_ofdm_state_t * state,
    void * blob
) 

The blob holds the position (run index, OFDM symbols rendered of the run, the held window tail) and a hash of the layout, so it never restores into a different one.

Parameters:


function dp_ofdm_modulate

The plain form of dp_ofdm_modulate_why() .

size_t dp_ofdm_modulate (
    dp_ofdm_state_t * state,
    const float _Complex * in,
    size_t n_in,
    float _Complex * out,
    size_t max_out
) 

Parameters:

  • state The modulator.
  • in Constellation points.
  • n_in Number of points.
  • out Receives the samples.
  • max_out Capacity of out.

Returns:

Samples written, or 0 if the call is refused or writes none; a caller who must tell the two apart uses dp_ofdm_modulate_why().


function dp_ofdm_modulate_max_out

Samples a render of n_in points writes.

size_t dp_ofdm_modulate_max_out (
    dp_ofdm_state_t * state,
    size_t n_in
) 

Simulates the render from the current position: OFDM symbols are taken in layout order until the next one needs more points than remain. The result is zero if n_in ends inside an OFDM symbol or runs past the layout (that call is refused), for a layout that does not seal, and for no points when the next OFDM symbol needs some. Use it to size the output before the call.

Parameters:

  • state The modulator.
  • n_in Constellation points.

Returns:

Samples that call will write.

>>> import numpy as np
>>> from doppler.ofdm import Ofdm
>>> o = Ofdm()
>>> m = np.zeros(8, np.uint8); m[[1, 2, 3, 5, 6, 7]] = 1
>>> o.add_class(8, m, cyclic_prefix=2)
0
>>> o.add_run(0, 3)
0
>>> o.modulate_max_out(12)   # two OFDM symbols of 10 samples
20
>>> o.modulate_max_out(5)    # ends inside a symbol
0

function dp_ofdm_modulate_why

Render OFDM symbols from constellation points, or say why not.

int64_t dp_ofdm_modulate_why (
    dp_ofdm_state_t * state,
    const float _Complex * in,
    size_t n_in,
    float _Complex * out,
    size_t max_out,
    const char ** why
) 

Parameters:

  • state The modulator; sealed by this call if it is not.
  • in Constellation points, consumed in layout order.
  • n_in Number of points; must end on an OFDM symbol boundary.
  • out Receives the samples.
  • max_out Capacity of out. Short is a refusal, not a truncation.
  • why NULL, or receives a static sentence for a refusal and is left as the caller set it otherwise: an unusable layout (the sentence dp_ofdm_check_why() gives), points that end inside an OFDM symbol, more points than the layout has left, or an output buffer shorter than dp_ofdm_modulate_max_out().

Returns:

Samples written, which may be 0: no points at the end of the layout, or zero points mid-layout when the next OFDM symbol needs some. A refused call changes nothing and reports it by face: C returns a negative value and sets why; the Python binding raises ValueError with that sentence.

>>> import numpy as np
>>> from doppler.ofdm import Ofdm
>>> o = Ofdm()
>>> m = np.zeros(8, np.uint8); m[[1, 2, 3, 5, 6, 7]] = 1
>>> o.add_class(8, m, cyclic_prefix=2)
0
>>> o.add_run(0, 3)
0
>>> o.points_remaining()
18
>>> x = o.modulate(np.ones(12, np.complex64))   # two of the three
>>> x.size
20
>>> o.points_remaining()
6
>>> o.modulate(np.ones(5, np.complex64))        # inside a symbol
Traceback (most recent call last):
    ...
ValueError: the points end inside an OFDM symbol: pass a count that ends where one does (see points_remaining and modulate_max_out)

function dp_ofdm_points_remaining

Constellation points the rest of the layout consumes.

size_t dp_ofdm_points_remaining (
    dp_ofdm_state_t * state
) 

Zero for a layout that does not seal.

Parameters:

  • state The modulator.

Returns:

Points from the current position to the end of the layout.

>>> import numpy as np
>>> from doppler.ofdm import Ofdm
>>> m = np.zeros(8, np.uint8); m[[1, 2, 3, 5, 6, 7]] = 1
>>> o = Ofdm()
>>> o.add_class(8, m, cyclic_prefix=2)
0
>>> o.add_run(0, 3)
0
>>> o.points_remaining()
18

function dp_ofdm_reset

Return to the first OFDM symbol, keeping the layout.

void dp_ofdm_reset (
    dp_ofdm_state_t * state
) 

The next render starts from the post-seal state: the first run, no held tail. The layout, sealed or not, is unchanged.

Parameters:

  • state Must be non-NULL.
>>> import numpy as np
>>> from doppler.ofdm import Ofdm
>>> m = np.zeros(8, np.uint8); m[[1, 2, 3, 5, 6, 7]] = 1
>>> o = Ofdm()
>>> o.add_class(8, m, cyclic_prefix=2)
0
>>> o.add_run(0, 2)
0
>>> x = o.modulate(np.ones(12, np.complex64))
>>> o.reset()
>>> o.points_remaining()
12

function dp_ofdm_set_state

Restore the state; nonzero rejects blob .

int dp_ofdm_set_state (
    dp_ofdm_state_t * state,
    const void * blob
) 

Rejects a blob whose layout hash differs from this modulator's, whose position names no OFDM symbol of the layout, or whose held tail is not the length the position implies. The modulator is untouched on rejection.

Parameters:

Returns:

DP_OK, or DP_ERR_INVALID if rejected.


function dp_ofdm_state_bytes

Bytes in the blob dp_ofdm_get_state() writes.

size_t dp_ofdm_state_bytes (
    const dp_ofdm_state_t * state
) 

A function of the layout alone (the envelope, the position and one window-tail slot per sample of the largest window), so it does not change as the render advances.

Parameters:

  • state The modulator.

Returns:

Blob size in bytes.


Macro Definition Documentation

define OFDM_ALL_PHASES

A fixed entry that applies in every pattern phase.

#define OFDM_ALL_PHASES `0xFFFFFFFFu`


define OFDM_COUNT_MAX

Most classes, and most fixed entries per class, a layout holds.

#define OFDM_COUNT_MAX `65536u`


define OFDM_CP_DEFAULT

A run's cyclic prefix that keeps its class's.

#define OFDM_CP_DEFAULT `0xFFFFFFFFu`


define OFDM_DATA

A subcarrier map value: the subcarrier carries a data point.

#define OFDM_DATA `1u`


define OFDM_NULL

A subcarrier map value: the subcarrier carries nothing.

#define OFDM_NULL `0u`


define OFDM_N_FFT_MAX

Largest transform length of a class.

#define OFDM_N_FFT_MAX `65536u`


define OFDM_N_PHASE_MAX

Largest pattern period of a class, in OFDM symbols.

#define OFDM_N_PHASE_MAX `4096u`


define OFDM_STATE_MAGIC

State blob magic ('O','F','D','M').

#define OFDM_STATE_MAGIC `DP_FOURCC ('O', 'F', 'D', 'M')`


define OFDM_STATE_VERSION

State blob layout version.

#define OFDM_STATE_VERSION `1u`



The documentation for this class was generated from the following file native/inc/doppler/ofdm/ofdm_core.h