File carrier_mpsk_core.h¶
FileList > carrier_mpsk > carrier_mpsk_core.h
Go to the source code of this file
M-PSK carrier-tracking loop (integer-NCO de-rotation + decision PLL). More...
#include "doppler/clib_common.h"#include "doppler/dp_state.h"#include "doppler/jm_perf.h"#include "doppler/lo/lo_core.h"#include "doppler/loop_filter/loop_filter_core.h"#include "doppler/mpsk/mpsk_core.h"#include <math.h>
Classes¶
| Type | Name |
|---|---|
| struct | dp_carrier_mpsk_state_t M-PSK carrier loop state. |
Public Functions¶
| Type | Name |
|---|---|
| JM_FORCEINLINE JM_HOT void | carrier_mpsk_update (dp_carrier_mpsk_state_t * s, float _Complex P) Per-symbol carrier update: decision discriminator -> loop -> NCO. |
| JM_FORCEINLINE JM_HOT float _Complex | carrier_mpsk_wipeoff (dp_carrier_mpsk_state_t * s, float _Complex x) Per-sample carrier wipe-off: de-rotate x by the NCO, advance it. |
| int | dp_carrier_mpsk_configure (dp_carrier_mpsk_state_t * state, double bn, double zeta) Recompute the loop gains for a new (bn, zeta); keep the estimate. |
| dp_carrier_mpsk_state_t * | dp_carrier_mpsk_create (double bn, double zeta, double init_norm_freq, size_t tsamps, double bn_fll, int m) Create an M-PSK carrier loop instance. |
| void | dp_carrier_mpsk_destroy (dp_carrier_mpsk_state_t * state) Destroy an M-PSK carrier loop instance and release all memory. |
| double | dp_carrier_mpsk_get_bn (const dp_carrier_mpsk_state_t * state) PLL loop noise bandwidth in force, normalised to the symbol rate. |
| double | dp_carrier_mpsk_get_bn_fll (const dp_carrier_mpsk_state_t * state) FLL-assist bandwidth (0 = pure PLL). |
| double | dp_carrier_mpsk_get_last_error (const dp_carrier_mpsk_state_t * state) Last PLL phase-discriminator output, Im(P conj(ahat))/|P| . |
| double | dp_carrier_mpsk_get_lock_metric (const dp_carrier_mpsk_state_t * state) Decision-aligned lock metric. |
| int | dp_carrier_mpsk_get_m (const dp_carrier_mpsk_state_t * state) Constellation order M. |
| double | dp_carrier_mpsk_get_norm_freq (const dp_carrier_mpsk_state_t * state) Current carrier-NCO frequency estimate, cycles/sample. |
| void | dp_carrier_mpsk_get_state (const dp_carrier_mpsk_state_t * state, void * blob) Serialize the full loop state (config and running) into blob . |
| void | dp_carrier_mpsk_init (dp_carrier_mpsk_state_t * s, double bn, double zeta, double init_norm_freq, size_t tsamps, double bn_fll, int m) Initialise an M-PSK carrier loop in place (no allocation). |
| void | dp_carrier_mpsk_reset (dp_carrier_mpsk_state_t * state) Re-seed the loop to its create-time frequency/phase; keep config. |
| int | dp_carrier_mpsk_set_bn (dp_carrier_mpsk_state_t * state, double val) Retune bn, keeping zeta: dp_carrier_mpsk_configure() 's refusal and status. As a writable property a refusal raises ValueError and the value stays put (doppler#1987). |
| int | dp_carrier_mpsk_set_bn_fll (dp_carrier_mpsk_state_t * state, double val) Set the FLL-assist bandwidth, live; the loop estimate is kept. |
| void | dp_carrier_mpsk_set_norm_freq (dp_carrier_mpsk_state_t * state, double val) Re-seed the loop at a new carrier frequency. |
| int | dp_carrier_mpsk_set_state (dp_carrier_mpsk_state_t * state, const void * blob) Restore the full loop state (config and running) from blob . |
| size_t | dp_carrier_mpsk_state_bytes (const dp_carrier_mpsk_state_t * state) Serialized-state byte size. |
| size_t | dp_carrier_mpsk_steps (dp_carrier_mpsk_state_t * state, const float _Complex * x, size_t x_len, float _Complex * out, size_t max_out) Track the residual carrier over a block, one prompt per symbol. |
| size_t | dp_carrier_mpsk_steps_max_out (dp_carrier_mpsk_state_t * state) Output capacity hint for dp_carrier_mpsk_steps() . |
Macros¶
| Type | Name |
|---|---|
| define | CARRIER_MPSK_EPS 1e-12fGuard added to |P| in the discriminator so a zero prompt cannot divide by zero (not tunable). |
| define | CARRIER_MPSK_LOCK_ALPHA 0.1Per-symbol EMA weight of the decision-aligned lock metric (status diagnostic; ~10-symbol time constant). |
| define | CARRIER_MPSK_STATE_MAGIC [**DP\_FOURCC**](dp__state_8h.md#define-dp_fourcc)('C', 'M', 'P', 'K')Envelope magic ("CMPK") identifying a CarrierMpsk state blob. |
| define | CARRIER_MPSK_STATE_VERSION 1uLayout version of the CarrierMpsk state blob. |
Detailed Description¶
The M-ary generalization of the Costas loop (costas_core.h): per sample it de-rotates the input with the integer-phase lo NCO (carrier wipe-off); every tsamps samples it dumps the coherent integrate-and-dump prompt, runs a decision-directed M-PSK phase discriminator, filters the error through an embedded 2nd-order dp_loop_filter_state_t, and steers the NCO frequency + phase. It tracks a small residual carrier (bulk Doppler is removed upstream by acquisition); the steering NCO is lo, so the phase is bounded and exactly reproducible.
The discriminator slices the prompt to the nearest constellation point ahat = mpsk_slice(P, m) and uses e = Im(P * conj(ahat)) / |P| (= sin of the phase error near lock). At m = 2 this reduces exactly to the BPSK Costas discriminator. An optional decision-directed cross-product FLL assist (bn_fll > 0) widens the frequency pull-in.
The loop locks to one of m phases — an M-fold ambiguity on absolute phase. Resolve it downstream with differential demapping (mpsk_diff_demap) or a sync word; this loop only recovers the carrier and emits the prompts.
The block API (dp_carrier_mpsk_steps) is the Python face; the JM_FORCEINLINE carrier_mpsk_wipeoff()/carrier_mpsk_update() are the C composition API a receiver inlines into its own sample loop.
// QPSK carrier loop, 64 samples/symbol, FLL-assisted
dp_carrier_mpsk_state_t *c = dp_carrier_mpsk_create(0.05, 0.707, 0.0, 64, 0.01, 4);
float _Complex sym[16];
size_t k = dp_carrier_mpsk_steps(c, rx, rx_len, sym, 16);
double f = c->nco.norm_freq; // tracked residual carrier
dp_carrier_mpsk_destroy(c);
Public Functions Documentation¶
function carrier_mpsk_update¶
Per-symbol carrier update: decision discriminator -> loop -> NCO.
Slices the prompt P to the nearest M-PSK point ahat, forms the decision-directed phase error e = Im(P conj(ahat)) / |P|, optionally runs a decision-directed cross-product FLL on the data-wiped prompts, filters, and steers the NCO frequency + a proportional phase nudge. Updates the lock metric (decision-aligned Re(P conj(ahat))/|P|) and last_error. Inline.
Parameters:
sCarrier loop state. Must be non-NULL.PThe dumped integrate-and-dump prompt for this symbol.
function carrier_mpsk_wipeoff¶
Per-sample carrier wipe-off: de-rotate x by the NCO, advance it.
JM_FORCEINLINE JM_HOT float _Complex carrier_mpsk_wipeoff (
dp_carrier_mpsk_state_t * s,
float _Complex x
)
x * conj(lo_step(nco)) — strips the (tracked) carrier ahead of the matched-filter integrate-and-dump. Inline, zero call overhead.
Parameters:
sCarrier loop state. Must be non-NULL.xOne input sample.
Returns:
The de-rotated sample to feed the integrator.
function dp_carrier_mpsk_configure¶
Recompute the loop gains for a new (bn, zeta); keep the estimate.
Re-derives the proportional/integral gains of the embedded 2nd-order loop filter for the new noise bandwidth and damping, leaving the running frequency and phase estimate (the NCO and the loop integrator) untouched — a live lock survives a re-tune. Use it to widen the loop for fast pull-in and then narrow it for low-jitter tracking, mid-stream.
Parameters:
stateMust be non-NULL.bnLoop noise bandwidth, normalised to the symbol rate.zetaDamping factor (1.0 = critical; 0.707 usual).>>> from doppler.track import CarrierMpsk >>> c = CarrierMpsk(bn=0.02, zeta=0.707, init_norm_freq=0.01, ... tsamps=16, bn_fll=0.0, m=4) >>> round(c.bn, 3) 0.02 >>> c.configure(bn=0.05, zeta=1.0) # widen the loop mid-stream >>> round(c.bn, 3) 0.05 >>> round(c.norm_freq, 3) # frequency estimate preserved 0.01
Returns:
DP_OK, or DP_ERR_INVALID for a (bn, zeta) outside dp_loop_filter_params_ok(); a refusal changes nothing, so the bandwidth the getter reports is always the one the loop runs (doppler#2112).
function dp_carrier_mpsk_create¶
Create an M-PSK carrier loop instance.
dp_carrier_mpsk_state_t * dp_carrier_mpsk_create (
double bn,
double zeta,
double init_norm_freq,
size_t tsamps,
double bn_fll,
int m
)
Parameters:
bnLoop noise bandwidth (default 0.05), of the closed loop given a unit-slope detector (see LoopFilter). The detector's slope is 1 per radian at every signal level and falls below 1 as the SNR per symbol falls (about 0.6 at 0 dB for BPSK, and near 0 at 6 dB for 8-PSK), so the delivered loop is narrower at low SNR.zetaDamping factor (default 0.707).init_norm_freqSeed carrier frequency, cycles/sample (default 0.0).tsampsSamples per symbol (default 64); 0 is refused.bn_fllFLL-assist bandwidth (default 0.0 = pure PLL).mConstellation order M, 2/4/8 (default 4 = QPSK).
Returns:
Heap-allocated state, or NULL if m is not 2, 4 or 8, if tsamps is 0, if (bn, zeta) fails dp_loop_filter_params_ok(), if bn_fll fails dp_loop_filter_fll_bn_ok(), or on allocation failure.
Note:
Caller must call dp_carrier_mpsk_destroy() when done.
function dp_carrier_mpsk_destroy¶
Destroy an M-PSK carrier loop instance and release all memory.
Parameters:
stateMay be NULL.
function dp_carrier_mpsk_get_bn¶
PLL loop noise bandwidth in force, normalised to the symbol rate.
Parameters:
stateMust be non-NULL.
Returns:
The last bn accepted by create, configure or set_bn.
function dp_carrier_mpsk_get_bn_fll¶
FLL-assist bandwidth (0 = pure PLL).
Parameters:
stateMust be non-NULL.
Returns:
The value from create or the last set_bn_fll.
function dp_carrier_mpsk_get_last_error¶
Last PLL phase-discriminator output, Im(P conj(ahat))/|P| .
Roughly sin(phase error), dimensionless; a measure of loop stress.
Parameters:
stateMust be non-NULL.
Returns:
The last symbol's error; 0 after create, reset or set_norm_freq.
function dp_carrier_mpsk_get_lock_metric¶
Decision-aligned lock metric.
EMA (weight CARRIER_MPSK_LOCK_ALPHA per symbol) of Re(P conj(ahat))/|P|: near 1 when phase-locked, near 0 with no carrier. A status diagnostic, not a decision.
Parameters:
stateMust be non-NULL.
Returns:
The metric; 0 after create, reset or set_norm_freq.
function dp_carrier_mpsk_get_m¶
Constellation order M.
Parameters:
stateMust be non-NULL.
Returns:
2, 4 or 8 (fixed at create).
function dp_carrier_mpsk_get_norm_freq¶
Current carrier-NCO frequency estimate, cycles/sample.
Parameters:
stateMust be non-NULL.
Returns:
The seed frequency until the first symbol completes, then the loop's tracked estimate.
function dp_carrier_mpsk_get_state¶
Serialize the full loop state (config and running) into blob .
Parameters:
stateMust be non-NULL.blobCaller-owned buffer of at least dp_carrier_mpsk_state_bytes() bytes.
function dp_carrier_mpsk_init¶
Initialise an M-PSK carrier loop in place (no allocation).
void dp_carrier_mpsk_init (
dp_carrier_mpsk_state_t * s,
double bn,
double zeta,
double init_norm_freq,
size_t tsamps,
double bn_fll,
int m
)
Seeds the NCO at init_norm_freq and the loop integrator to the matching per-symbol frequency so de-rotation is correct from the first sample.
Parameters:
sState to initialise. Must be non-NULL.bnLoop noise bandwidth, normalised to the symbol rate.zetaDamping factor (1.0 = critical; 0.707 usual).init_norm_freqSeed carrier frequency, cycles/sample.tsampsSamples per symbol (the integrate-and-dump period); 0 is taken as 1.bn_fllFLL-assist bandwidth (0 = pure PLL).mConstellation order M (2, 4, 8); not validated here.
Unguarded, as the by-value path: the caller checks (bn, zeta) with dp_loop_filter_params_ok() first. Outside it the loop's gains go NaN (doppler#2103).
function dp_carrier_mpsk_reset¶
Re-seed the loop to its create-time frequency/phase; keep config.
Returns the NCO to the seed carrier passed at construction, zeroes the integrate-and-dump accumulator, the FLL history, and the lock/error diagnostics, and re-primes the loop integrator to the matching per-symbol frequency — the exact state a fresh dp_carrier_mpsk_create() leaves. The tuning (bn, zeta, bn_fll, tsamps, m) is untouched. Call it at a capture boundary so a lock reached on one segment does not bias an unrelated next one.
Parameters:
stateMust be non-NULL.>>> import numpy as np >>> from doppler.mpsk import mpsk_map >>> from doppler.track import CarrierMpsk >>> rng = np.random.default_rng(1) >>> sig = np.repeat( ... mpsk_map(rng.integers(0, 4, 100).astype(np.uint8), 4), ... 16).astype(np.complex64) >>> rx = (sig * np.exp(2j * np.pi * 0.003 * np.arange(len(sig))) ... ).astype(np.complex64) >>> c = CarrierMpsk(bn=0.04, zeta=0.707, init_norm_freq=0.0, ... tsamps=16, bn_fll=0.02, m=4) >>> _ = c.steps(rx) >>> round(c.norm_freq, 3) # loop pulled onto the residual carrier 0.003 >>> c.reset() # back to the create-time seed >>> round(c.norm_freq, 3) 0.0
function dp_carrier_mpsk_set_bn¶
Retune bn, keeping zeta: dp_carrier_mpsk_configure() 's refusal and status. As a writable property a refusal raises ValueError and the value stays put (doppler#1987).
Parameters:
stateMust be non-NULL.valNew loop noise bandwidth, normalised to the symbol rate.
Returns:
DP_OK, or DP_ERR_INVALID (nothing changed) for a value outside dp_loop_filter_params_ok().
function dp_carrier_mpsk_set_bn_fll¶
Set the FLL-assist bandwidth, live; the loop estimate is kept.
Re-derives the FLL gain as 4 * val; 0 is a pure PLL. A value outside dp_loop_filter_fll_bn_ok() (negative, NaN, or one whose gain overflows) is refused and nothing changes.
Parameters:
stateMust be non-NULL.valNew FLL-assist bandwidth, normalised to the symbol rate.
Returns:
DP_OK, or DP_ERR_INVALID when val is refused.
function dp_carrier_mpsk_set_norm_freq¶
Re-seed the loop at a new carrier frequency.
Not a nudge: it replaces the create-time seed (so a later reset() returns here), resets the loop filter, re-primes its integrator to val, and clears the I&D accumulator, FLL history and lock/error diagnostics. Tuning (bn, zeta, bn_fll, tsamps, m) is kept.
Parameters:
stateMust be non-NULL.valNew seed carrier frequency, cycles/sample.
function dp_carrier_mpsk_set_state¶
Restore the full loop state (config and running) from blob .
Parameters:
stateMust be non-NULL.blobA blob produced by dp_carrier_mpsk_get_state().
Returns:
DP_OK, or DP_ERR_INVALID if the envelope rejects (state then unchanged).
function dp_carrier_mpsk_state_bytes¶
Serialized-state byte size.
Parameters:
stateMust be non-NULL (the size does not depend on it).
Returns:
sizeof(dp_state_hdr_t) + sizeof(dp_carrier_mpsk_state_t).
function dp_carrier_mpsk_steps¶
Track the residual carrier over a block, one prompt per symbol.
size_t dp_carrier_mpsk_steps (
dp_carrier_mpsk_state_t * state,
const float _Complex * x,
size_t x_len,
float _Complex * out,
size_t max_out
)
The block form of the inline wipeoff/update pair: for each input sample it de-rotates by the carrier NCO and accumulates the coherent integrate-and-dump; every tsamps samples it dumps the prompt, runs the decision-directed M-PSK discriminator (slice to the nearest constellation point, error Im(P conj(ahat))/|P|, plus the optional cross-product FLL assist), filters the error, and steers the NCO frequency and phase. Exactly one de-rotated prompt is emitted per completed symbol; a trailing partial symbol is carried in the accumulator to the next call, so a stream can be fed in blocks of any length with no seam.
The loop locks to one of m carrier phases — an M-fold ambiguity on the absolute constellation orientation. Resolve it downstream (differential demapping or a sync word); this call only recovers the carrier and returns the prompts. At m = 2 it is exactly the BPSK Costas loop.
Parameters:
stateCarrier loop state (mutated). Must be non-NULL.xInput block, one complex baseband sample per element.x_lenNumber of input samples.outPrompt output buffer. Each prompt is the I&D sum divided bytsamps(the mean de-rotated sample).max_outCapacity ofout, in symbols. Symbols completed once it is full still steer the loop but are not written.
Returns:
Number of prompts written: one per completed integrate-and-dump period, (carried + x_len) / tsamps where carried is the partial symbol held from the previous call, capped at max_out. From Python, the prompt array.
>>> import numpy as np
>>> from doppler.mpsk import mpsk_map
>>> from doppler.track import CarrierMpsk
>>> rng = np.random.default_rng(0)
>>> sps = 16
>>> labels = rng.integers(0, 4, 400).astype(np.uint8)
>>> sig = np.repeat(mpsk_map(labels, 4), sps).astype(np.complex64)
>>> k = np.arange(len(sig))
>>> rx = (sig * np.exp(2j * np.pi * 0.002 * k)).astype(np.complex64)
>>> c = CarrierMpsk(bn=0.04, zeta=0.707, init_norm_freq=0.0,
... tsamps=sps, bn_fll=0.02, m=4)
>>> prompts = c.steps(rx) # one prompt per symbol
>>> prompts.shape
(400,)
>>> round(c.norm_freq, 4) # tracked the residual carrier 0.002
0.002
>>> round(c.lock_metric, 2) # decision-aligned lock metric -> 1
1.0
function dp_carrier_mpsk_steps_max_out¶
Output capacity hint for dp_carrier_mpsk_steps() .
steps() emits at most one prompt per input sample, so the binding sizes the buffer to the input length; this returns the "caller sizes it" sentinel.
Parameters:
stateUnused; may be any value.
Returns:
Always 0.
Macro Definition Documentation¶
define CARRIER_MPSK_EPS¶
Guard added to |P| in the discriminator so a zero prompt cannot divide by zero (not tunable).
define CARRIER_MPSK_LOCK_ALPHA¶
Per-symbol EMA weight of the decision-aligned lock metric (status diagnostic; ~10-symbol time constant).
define CARRIER_MPSK_STATE_MAGIC¶
Envelope magic ("CMPK") identifying a CarrierMpsk state blob.
define CARRIER_MPSK_STATE_VERSION¶
Layout version of the CarrierMpsk state blob.
The documentation for this class was generated from the following file native/inc/doppler/carrier_mpsk/carrier_mpsk_core.h