File lockdet_core.h¶
FileList > doppler > lockdet > lockdet_core.h
Go to the source code of this file
Portable lock detector — level + time hysteresis over any scalar lock metric, embeddable in every loop that makes a lock decision. More...
#include "doppler/clib_common.h"#include "doppler/dp_state.h"#include "doppler/jm_perf.h"#include "doppler/util/util_core.h"#include <math.h>
Classes¶
| Type | Name |
|---|---|
| struct | dp_lockdet_rule_t A lock detector's RULE: its levels and verify counts, without its running count or decision. |
| struct | dp_lockdet_state_t Lock-detector state (embeddable by value; pointer-free POD). |
Public Functions¶
| Type | Name |
|---|---|
| void | dp_lockdet_configure (dp_lockdet_state_t * state, double up_thresh, double down_thresh, uint32_t n_up, uint32_t n_down) Re-tune thresholds and verify counts; preserve the decision. |
| dp_lockdet_state_t * | dp_lockdet_create (double up_thresh, double down_thresh, uint32_t n_up, uint32_t n_down) Create a lockdet instance. |
| void | dp_lockdet_destroy (dp_lockdet_state_t * state) Destroy a lockdet instance and release all memory. |
| void | dp_lockdet_get_state (const dp_lockdet_state_t * state, void * blob) Serialize the detector state into blob . |
| void | dp_lockdet_init (dp_lockdet_state_t * state, double up_thresh, double down_thresh, uint32_t n_up, uint32_t n_down) Initialise a lock detector in place (no allocation). |
| void | dp_lockdet_reset (dp_lockdet_state_t * state) Drop the lock and clear the verify counter; keep the config. Returns the detector to the unlocked state with an empty verify run, as if freshly constructed with the same thresholds. Call it at a segment boundary so a decision made on one capture does not leak into an unrelated next one. |
| JM_FORCEINLINE dp_lockdet_rule_t | dp_lockdet_rule (const dp_lockdet_state_t * s) The rule a lock detector decides by, read from its state. |
| int | dp_lockdet_set_state (dp_lockdet_state_t * state, const void * blob) Restore a state written by dp_lockdet_get_state() . |
| size_t | dp_lockdet_state_bytes (const dp_lockdet_state_t * state) Serialized-state byte size. |
| JM_FORCEINLINE int | dp_lockdet_state_ok (const dp_lockdet_state_t * s) A lock-detector state a blob may carry: a decision that is 0 or 1. |
| JM_FORCEINLINE JM_HOT int | dp_lockdet_step (dp_lockdet_state_t * state, double x) Feed one look of the lock metric; return the current decision. |
| void | dp_lockdet_steps (dp_lockdet_state_t * state, const double * x, int * out, size_t n) Run a block of lock-metric looks through the detector. Applies dp_lockdet_step() to each look in turn, so the decision flag and the in-flight verify run carry across the block exactly as they would look by look — a signal can be processed in frames of any size with no seam. |
Macros¶
| Type | Name |
|---|---|
| define | LOCKDET_STATE_MAGIC [**DP\_FOURCC**](dp__state_8h.md#define-dp_fourcc)('L', 'K', 'D', 'T')State-blob magic for the lock detector (FourCC "LKDT"). |
| define | LOCKDET_STATE_VERSION 1uState-blob layout version for the lock detector. |
Detailed Description¶
A tracking loop that computes a lock statistic (a CFAR ratio, a coherence metric, an error variance) still needs a decision rule: when is the statistic "high enough, long enough" to declare lock, and "low enough, long enough" to drop it? This component is that rule, factored out once:
- Level hysteresis: separate declare (
up_thresh) and drop (down_thresh) thresholds. Withup_thresh >= down_threshthe band between them is sticky in both directions — a metric wobbling around a single threshold cannot chatter the flag. - Time hysteresis:
n_upconsecutive looks aboveup_threshto declare,n_downconsecutive looks belowdown_threshto drop. A single contrary look resets the run (consecutive, not cumulative), so the verify counts compose probabilistically. At per-look false-alarm rate p the false-declare rate per look isp^n_up * (1 - p) / (1 - p^n_up), whose reciprocal is exactly det_verify_delay(p, n_up), the mean looks to a declare.p^n_upalone is the p -> 0 limit of that, and is what dp_det_verify_count() sizes against correct to 0.001% at p = 1e-5, 10% at p = 0.1, and +87% at p = 0.5 with n_up = 4. Use it as the budget (it errs high, so it over-provisions n_up) and dp_det_verify_delay() for the number a caller actually observes. Measured across p from 0.1 to 0.5 and n_up from 1 to 4: native/validation/lockdet_verify.c.(Both the formula and those ranges are written without an indented block or square brackets on purpose: mkdoxy renders this comment into markdown, where an indented line is swallowed into the paragraph before it and a barep in [0.1, 0.5]parses as a link reference and fails the strict docs build.) - Non-finite looks: a NaN look is a miss in both states — it never advances a declare, and while locked it advances the drop run like any other miss, so a metric that goes NaN drops the lock after
n_downrather than holding it lit. An unknown lock is not a lock. The policy is not implemented here: the look is passed through util_core.h's dp_saturate(), whosenan_toparameter documents a lock statistic as the caller that wants the floor. Only NaN is unordered — the infinities are ordinary looks (+inf a hit, -inf a miss), and the exclusive edges are unchanged.
The state struct is public so a tracker embeds it by value (no heap) and drives it with dp_lockdet_init()/dp_lockdet_step() — e.g. the DLL steps one on its CFAR statistic each N-look decision, the MPSK receiver steps one on the carrier lock metric each recovered symbol. dp_lockdet_create() is the heap path used by the Python wrapper. Pointer-free POD: it rides an embedding composer's whole-struct state snapshot with no extra packing.
Lifecycle: create -> (step / steps / configure / reset)* -> destroy
dp_lockdet_state_t d;
dp_lockdet_init (&d, 1.5, 1.2, 2, 3); // declare: 2 looks > 1.5
dp_lockdet_reset (&d); // cnt = 0, locked = 0
double metric = 2.0; // one lock-metric look
int locked = dp_lockdet_step (&d, metric); // one look -> current flag
Public Functions Documentation¶
function dp_lockdet_configure¶
Re-tune thresholds and verify counts; preserve the decision.
void dp_lockdet_configure (
dp_lockdet_state_t * state,
double up_thresh,
double down_thresh,
uint32_t n_up,
uint32_t n_down
)
The current locked flag survives (a live lock is not dropped by a re-tune); the in-flight verify counter is cleared so the next run is counted entirely under the new config.
Parameters:
stateMust be non-NULL.up_threshDeclare threshold (hit when metric > up_thresh).down_threshDrop threshold (miss when metric < down_thresh).n_upConsecutive hits to declare; clamped to >= 1.n_downConsecutive misses to drop; clamped to >= 1.
function dp_lockdet_create¶
Create a lockdet instance.
dp_lockdet_state_t * dp_lockdet_create (
double up_thresh,
double down_thresh,
uint32_t n_up,
uint32_t n_down
)
Parameters:
up_threshDeclare threshold (hit when metric > up_thresh).down_threshDrop threshold (miss when metric < down_thresh).n_upConsecutive hits to declare; clamped >= 1 (default 1).n_downConsecutive misses to drop; clamped >= 1 (default 1).
Returns:
Heap-allocated state, or NULL on allocation failure.
Note:
Caller must call dp_lockdet_destroy() when done.
function dp_lockdet_destroy¶
Destroy a lockdet instance and release all memory.
Parameters:
stateMay be NULL.
function dp_lockdet_get_state¶
Serialize the detector state into blob .
A whole-struct snapshot: thresholds, run lengths, the verify-run counter and the lock flag.
Parameters:
stateMust be non-NULL.blobCaller-owned buffer of at least dp_lockdet_state_bytes() bytes.
function dp_lockdet_init¶
Initialise a lock detector in place (no allocation).
void dp_lockdet_init (
dp_lockdet_state_t * state,
double up_thresh,
double down_thresh,
uint32_t n_up,
uint32_t n_down
)
Stores the thresholds and verify counts (each count clamped to >= 1; a count of 1 means no time hysteresis on that side). Does not touch cnt / locked, so it doubles as a reconfigure that preserves the current decision. Use this for a dp_lockdet_state_t embedded by value; dp_lockdet_create() is calloc + dp_lockdet_init().
Parameters:
stateMust be non-NULL.up_threshDeclare threshold (hit when metric > up_thresh).down_threshDrop threshold (miss when metric < down_thresh); choose <= up_thresh for level hysteresis.n_upConsecutive hits to declare; clamped to >= 1.n_downConsecutive misses to drop; clamped to >= 1.
function dp_lockdet_reset¶
Drop the lock and clear the verify counter; keep the config. Returns the detector to the unlocked state with an empty verify run, as if freshly constructed with the same thresholds. Call it at a segment boundary so a decision made on one capture does not leak into an unrelated next one.
Parameters:
stateMust be non-NULL.
function dp_lockdet_rule¶
The rule a lock detector decides by, read from its state.
The one reader every embedding object's get_*lock_rule() returns through, so the four fields are copied in one place.
Parameters:
sMust be non-NULL.
Returns:
Its thresholds and verify counts.
dp_lockdet_state_t d;
dp_lockdet_init (&d, 0.85, 0.78, 8, 32);
dp_lockdet_rule_t r = dp_lockdet_rule (&d);
// r.up_thresh 0.85, r.down_thresh 0.78, r.n_up 8, r.n_down 32
function dp_lockdet_set_state¶
Restore a state written by dp_lockdet_get_state() .
The envelope is validated and the payload decoded into a temporary and checked (dp_lockdet_state_ok) first; on rejection state is untouched. Being a whole-struct snapshot, it also restores the sender's thresholds and run lengths.
Parameters:
stateMust be non-NULL.blobBlob from dp_lockdet_get_state().
Returns:
DP_OK, or DP_ERR_INVALID on a magic, version, endian or size mismatch or a payload the object's check refuses.
function dp_lockdet_state_bytes¶
Serialized-state byte size.
Parameters:
stateMust be non-NULL (its contents do not affect the size).
Returns:
Bytes: the dp_state_hdr_t envelope plus the whole struct.
function dp_lockdet_state_ok¶
A lock-detector state a blob may carry: a decision that is 0 or 1.
step() only ever stores 0 or 1, so a decision of 7 is a forged blob, not a state: it surfaced as locked and read back through every embedder (#2142). The by-value embedders that refuse a snapshot this rejects are carrier_nda, costas and symsync. dll, ratesync, mpsk_receiver and the async sym_lockdet do not yet; they are on #2142's checklist. The counts are not checked: init() clamps them to >= 1, but an embedding holds the all-zero state until its init runs, and that round-trips (a count of 0 steps as 1).
Parameters:
sDecoded detector state. Must be non-NULL.
Returns:
Non-zero when locked is 0 or 1.
function dp_lockdet_step¶
Feed one look of the lock metric; return the current decision.
Unlocked: a hit (x > up_thresh) advances the verify run and the n_up-th consecutive hit declares lock; any miss resets the run. Locked: a miss (x < down_thresh) advances the run and the n_down-th consecutive miss drops the lock; any hit (x >= down_thresh) resets it. A metric inside the [down_thresh, up_thresh] band is sticky — it neither advances a declare nor a drop.
A non-finite look is a miss in both states: it never advances a declare, and while locked it advances the drop run like any other miss. An unknown lock is not a lock, which is the rule util_core.h states for lock statistics generally. So a metric that goes NaN drops the lock after n_down looks rather than holding it lit indefinitely.
Parameters:
stateMust be non-NULL.xLock metric for this look. Non-finite counts as a miss.
Returns:
Decision after this look (1 = locked, 0 = not).
>>> from doppler.detection import LockDet
>>> d = LockDet(up_thresh=1.5, down_thresh=1.2, n_up=2, n_down=3)
>>> [d.step(2.0), d.step(2.0)] # declared on the 2nd straight hit
[0, 1]
>>> d.step(1.3) # in the hysteresis band: stays up
1
>>> [d.step(1.0), d.step(1.0), d.step(1.0)] # 3rd straight miss drops
[1, 1, 0]
function dp_lockdet_steps¶
Run a block of lock-metric looks through the detector. Applies dp_lockdet_step() to each look in turn, so the decision flag and the in-flight verify run carry across the block exactly as they would look by look — a signal can be processed in frames of any size with no seam.
Parameters:
stateComponent state (mutated). Must be non-NULL.xLock-metric looks, one scalar per look (length >= n).outPer-look decision output, 0 or 1 (length >= n).nNumber of looks to process.
Macro Definition Documentation¶
define LOCKDET_STATE_MAGIC¶
State-blob magic for the lock detector (FourCC "LKDT").
define LOCKDET_STATE_VERSION¶
State-blob layout version for the lock detector.
The documentation for this class was generated from the following file native/inc/doppler/lockdet/lockdet_core.h