File boxcar_core.h¶
FileList > boxcar > boxcar_core.h
Go to the source code of this file
Boxcar (rectangular) moving-average filter — cf32, fixed window. More...
#include "doppler/clib_common.h"#include "doppler/dp_state.h"#include "doppler/jm_perf.h"
Classes¶
| Type | Name |
|---|---|
| struct | dp_boxcar_state_t Boxcar moving-average state (cf32). |
Public Functions¶
| Type | Name |
|---|---|
| dp_boxcar_state_t * | dp_boxcar_create (size_t len, double gain) Create a boxcar instance. |
| void | dp_boxcar_destroy (dp_boxcar_state_t * s) Destroy a boxcar instance. |
| JM_FORCEINLINE double | dp_boxcar_get_gain (const dp_boxcar_state_t * s) Current output gain. |
| void | dp_boxcar_get_state (const dp_boxcar_state_t * s, void * blob) Serialize the full state into blob . |
| void | dp_boxcar_init (dp_boxcar_state_t * s, size_t len, double gain) Initialise a boxcar in place (no allocation). |
| void | dp_boxcar_reset (dp_boxcar_state_t * s) Clear the window (zero the ring and the running sum); keep the configured length and gain. |
| JM_FORCEINLINE void | dp_boxcar_set_gain (dp_boxcar_state_t * s, double gain) Set the output gain; refresh the cached scale. |
| int | dp_boxcar_set_state (dp_boxcar_state_t * s, const void * blob) Restore state from a blob written by dp_boxcar_get_state() . |
| size_t | dp_boxcar_state_bytes (const dp_boxcar_state_t * s) Serialized-state byte size. |
| JM_FORCEINLINE int | dp_boxcar_state_ok (const dp_boxcar_state_t * s) A boxcar state a blob may carry: a window that fits, a write index inside it. |
| JM_FORCEINLINE JM_HOT float _Complex | dp_boxcar_step (dp_boxcar_state_t * s, float _Complex x) Slide the window by one sample; return the gained moving average. |
| void | dp_boxcar_steps (dp_boxcar_state_t * s, const float _Complex * x, float _Complex * out, size_t n) Filter a block: write the gained moving average of each sample. |
Macros¶
| Type | Name |
|---|---|
| define | BOXCAR_MAX_LEN 64 |
| define | BOXCAR_STATE_MAGIC [**DP\_FOURCC**](dp__state_8h.md#define-dp_fourcc) ('B', 'O', 'X', 'C') |
| define | BOXCAR_STATE_VERSION 1u |
Detailed Description¶
A sliding-window moving average over the last len complex samples: one output per input sample (no rate change). Each step adds the new sample and subtracts the sample leaving the window, so it is O(1) per sample regardless of window length (a running window sum, not a re-summed convolution). The output is the window mean times an optional output gain: gain·(Σ window)/ len. Because the step must multiply by 1/len anyway, the gain is folded into a single cached scale = gain/len, so applying it is free — a composing loop (e.g. a carrier arm with an AGC) can push its gain into the boxcar and avoid a second multiply.
The delay ring is a fixed in-struct array (BOXCAR_MAX_LEN), so the state is pointer-free POD: it embeds by value into a composing object (a carrier loop's I/Q arm, a smoother ahead of a detector) and serializes as a whole-struct snapshot. A window longer than BOXCAR_MAX_LEN is rejected at create/init time. (A bounded window sum also stays numerically clean — unlike a never-reset CIC integrator-comb, whose integrator drifts in float.)
Until the ring fills (the first len-1 samples after a reset) the ring holds zeros, so the average is taken over a partial window and the output ramps in.
>>> import numpy as np
>>> from doppler.filter import MovingAverage
>>> ma = MovingAverage(2) # 2-sample window, unit gain
>>> ma.steps(np.ones(3, np.complex64)).real.tolist()
[0.5, 1.0, 1.0]
>>> ma2 = MovingAverage(2, gain=2.0) # gain folded into the mean
>>> ma2.steps(np.ones(3, np.complex64)).real.tolist()
[1.0, 2.0, 2.0]
Public Functions Documentation¶
function dp_boxcar_create¶
Create a boxcar instance.
Parameters:
lenWindow length (1 .. BOXCAR_MAX_LEN; default 4).gainOutput gain (default 1.0).
Returns:
Heap state, or NULL when len is 0 or above BOXCAR_MAX_LEN, or on allocation failure.
Note:
Caller must call dp_boxcar_destroy() when done.
function dp_boxcar_destroy¶
Destroy a boxcar instance.
Parameters:
sMay be NULL.
function dp_boxcar_get_gain¶
Current output gain.
Parameters:
sBoxcar state. Must be non-NULL.
Returns:
The gain last set by dp_boxcar_init(), dp_boxcar_create() or dp_boxcar_set_gain() (a linear multiplier on the window mean).
function dp_boxcar_get_state¶
Serialize the full state into blob .
Writes the envelope header and a whole-struct snapshot (window length, gain, ring, running sum and write index).
Parameters:
sBoxcar state. Must be non-NULL.blobDestination; at least dp_boxcar_state_bytes() bytes.
function dp_boxcar_init¶
Initialise a boxcar in place (no allocation).
Parameters:
sState to initialise. Must be non-NULL.lenWindow length in samples; clamped to[1, BOXCAR_MAX_LEN](0 becomes 1).gainOutput gain (folded into the averaging scale).
function dp_boxcar_reset¶
Clear the window (zero the ring and the running sum); keep the configured length and gain.
Returns the filter to its just-constructed state: the delay ring and the running window sum are zeroed while len and gain are preserved, so the next len-1 outputs ramp in over a partial window exactly as they did on a fresh instance. Call it at a segment boundary so samples from one capture do not average into an unrelated next one.
Parameters:
sBoxcar state. Must be non-NULL.
function dp_boxcar_set_gain¶
Set the output gain; refresh the cached scale.
Parameters:
sBoxcar state. Must be non-NULL.gainNew output gain (folded intoscale = gain / len).
function dp_boxcar_set_state¶
Restore state from a blob written by dp_boxcar_get_state() .
The whole struct, including the configured length and gain, is replaced by the snapshot, but only when it is one dp_boxcar_state_ok() accepts: the payload is decoded into a temporary first, so nothing is changed if the envelope or the payload is refused.
Parameters:
sBoxcar state to overwrite. Must be non-NULL.blobSource blob of dp_boxcar_state_bytes() bytes.
Returns:
DP_OK, or DP_ERR_INVALID if the envelope (magic, version, endianness, size) does not match or the payload is refused.
function dp_boxcar_state_bytes¶
Serialized-state byte size.
Parameters:
sBoxcar state (unused: the size is fixed).
Returns:
sizeof(dp_state_hdr_t) + sizeof(dp_boxcar_state_t); the same for every instance.
function dp_boxcar_state_ok¶
A boxcar state a blob may carry: a window that fits, a write index inside it.
dp_boxcar_step() writes ring[pos] BEFORE it wraps pos, so a pos at or past BOXCAR_MAX_LEN writes past the struct, and a len above it lets pos walk there (#2142). set_state, and any composition that embeds a boxcar by value, refuses a snapshot this rejects.
The all-zero state (len == 0, pos == 0) passes: an embedding holds it until its init runs, and it round-trips like any other. Stepping it writes ring[0] only.
Parameters:
sDecoded boxcar state. Must be non-NULL.
Returns:
Non-zero when len <= BOXCAR_MAX_LEN and pos is below len (below 1 when len is 0).
function dp_boxcar_step¶
Slide the window by one sample; return the gained moving average.
O(1): add x, drop the sample leaving the window, return acc · scale (= gain · acc / len) — one multiply.
Parameters:
sBoxcar state. Must be non-NULL.xOne input sample.
Returns:
The gained window mean after admitting x.
>>> from doppler.filter import MovingAverage
>>> ma = MovingAverage(2) # 2-sample sliding window, unit gain
>>> [round(ma.step(v).real, 4) for v in (1 + 0j, 3 + 0j, 3 + 0j)]
[0.5, 2.0, 3.0]
function dp_boxcar_steps¶
Filter a block: write the gained moving average of each sample.
void dp_boxcar_steps (
dp_boxcar_state_t * s,
const float _Complex * x,
float _Complex * out,
size_t n
)
Applies dp_boxcar_step() to each input sample in turn, so the window sum and ring carry across the block exactly as they would sample by sample — a stream can be processed in frames of any size with no seam. Immediately after a reset the first len-1 outputs average over a partial (still filling) window and ramp in.
Parameters:
sBoxcar state. Must be non-NULL.xInput samples.outOutput (gained window means); may aliasx.nNumber of samples.
Macro Definition Documentation¶
define BOXCAR_MAX_LEN¶
Maximum window length in samples (64). The delay ring is a fixed in-struct array so the state stays pointer-free POD (embed-by-value + whole-struct serialization). dp_boxcar_create() rejects a longer window (returns NULL); dp_boxcar_init() clamps it to this value.
define BOXCAR_STATE_MAGIC¶
FourCC type tag ("BOXC") stamped in a boxcar state blob's header.
define BOXCAR_STATE_VERSION¶
Layout version of the boxcar state blob; a mismatch is rejected.
The documentation for this class was generated from the following file native/inc/doppler/boxcar/boxcar_core.h