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 "clib_common.h"#include "dp_state.h"#include "jm_perf.h"
Classes¶
| Type | Name |
|---|---|
| struct | boxcar_state_t Boxcar moving-average state (cf32). |
Public Functions¶
| Type | Name |
|---|---|
| boxcar_state_t * | boxcar_create (size_t len, double gain) Create a boxcar instance. |
| void | boxcar_destroy (boxcar_state_t * s) Destroy a boxcar instance. |
| JM_FORCEINLINE double | boxcar_get_gain (const boxcar_state_t * s) Current output gain. |
| void | boxcar_get_state (const boxcar_state_t * s, void * blob) Serialize the full state into blob . |
| void | boxcar_init (boxcar_state_t * s, size_t len, double gain) Initialise a boxcar in place (no allocation). |
| void | boxcar_reset (boxcar_state_t * s) Clear the window (zero the ring and the running sum); keep config. |
| JM_FORCEINLINE void | boxcar_set_gain (boxcar_state_t * s, double gain) Set the output gain; refresh the cached scale. |
| int | boxcar_set_state (boxcar_state_t * s, const void * blob) Restore state; DP_OK, or DP_ERR_INVALID if the envelope rejects. |
| size_t | boxcar_state_bytes (const boxcar_state_t * s) Serialized-state byte size. |
| JM_FORCEINLINE JM_HOT float complex | boxcar_step (boxcar_state_t * s, float complex x) Slide the window by one sample; return the gained moving average. |
| void | boxcar_steps (boxcar_state_t * s, const float complex * input, float complex * output, 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 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 on invalid length / allocation failure.
Note:
Caller must call boxcar_destroy() when done.
function boxcar_destroy¶
Destroy a boxcar instance.
Parameters:
sMay be NULL.
function boxcar_get_gain¶
Current output gain.
function boxcar_get_state¶
Serialize the full state into blob .
function boxcar_init¶
Initialise a boxcar in place (no allocation).
Parameters:
sState to initialise. Must be non-NULL.lenWindow length; clamped to [1, BOXCAR_MAX_LEN].gainOutput gain (folded into the averaging scale).
function boxcar_reset¶
Clear the window (zero the ring and the running sum); keep config.
function 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 boxcar_set_state¶
Restore state; DP_OK, or DP_ERR_INVALID if the envelope rejects.
function boxcar_state_bytes¶
Serialized-state byte size.
function 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.
function boxcar_steps¶
Filter a block: write the gained moving average of each sample.
void boxcar_steps (
boxcar_state_t * s,
const float complex * input,
float complex * output,
size_t n
)
Parameters:
sBoxcar state. Must be non-NULL.inputInput samples.outputOutput (gained window means); may aliasinput.nNumber of samples.
Macro Definition Documentation¶
define BOXCAR_MAX_LEN¶
define BOXCAR_STATE_MAGIC¶
define BOXCAR_STATE_VERSION¶
The documentation for this class was generated from the following file native/inc/boxcar/boxcar_core.h