File Resampler_core.h¶
FileList > doppler > Resampler > Resampler_core.h
Go to the source code of this file
Continuously-variable polyphase resampler, CF32 IQ. More...
#include <stdint.h>#include "doppler/resamp/resamp_core.h"
Public Types¶
| Type | Name |
|---|---|
| typedef resamp_state_t | dp_Resampler_state_t |
Public Functions¶
| Type | Name |
|---|---|
| dp_Resampler_state_t * | dp_Resampler_create (double rate) Create a Resampler with the built-in 4096×19 Kaiser bank. The bank provides ~60 dB alias rejection with 0.4/0.6 pass/stop normalised cutoffs. Pass rate >= 1.0 to interpolate (upsample); pass rate < 1.0 to decimate (downsample). For a custom bank use dp_Resampler_create_custom() instead (from Python, pass bank=). |
| dp_Resampler_state_t * | dp_Resampler_create_banks (double rate, const float * bank, size_t num_phases, size_t num_taps, const float * dbank, size_t dbank_phases, size_t dbank_taps) Create a Resampler over an optional bank and an optional second bank: the constructor Python's Resampler(rate, bank, dbank) calls, each absent array arriving as NULL. |
| dp_Resampler_state_t * | dp_Resampler_create_custom (size_t num_phases, size_t num_taps, const float * bank, double rate) Create a Resampler with a user-supplied polyphase bank. |
| void | dp_Resampler_destroy (dp_Resampler_state_t * state) Free the bank, the delay lines and the state itself. |
| size_t | dp_Resampler_execute (dp_Resampler_state_t * state, const float _Complex * x, size_t x_len, float _Complex * out, size_t max_out) Resample a block of CF32 samples at the fixed base rate. Uses the dual-mode polyphase engine: output-driven for rate >= 1 (interpolation), input-driven transposed-form for rate < 1 (decimation). State carries over between calls, so contiguous blocks produce the same result as one large block. |
| int64_t | dp_Resampler_execute_ctrl (dp_Resampler_state_t * state, const float _Complex * x, size_t x_len, const double * ctrl, size_t ctrl_len, float _Complex * out, size_t max_out) Resample with per-sample additive rate deviations. Effective rate for sample i is base_rate + ctrl[i] . Uses a unified double-precision accumulator that handles both interpolation and decimation in a single code path — suitable for Doppler-shift simulation and fractional-sample timing correction. ctrl and x must have the same length. |
| size_t | dp_Resampler_execute_ctrl_max_out (dp_Resampler_state_t * state) Output-capacity hint for dp_Resampler_execute_ctrl() . |
| int64_t | dp_Resampler_execute_ctrl_pair (dp_Resampler_state_t * state, const float _Complex * x, size_t x_len, const double * ctrl, size_t ctrl_len, float _Complex * out, float _Complex * out1, uint32_t * out2, size_t max_out) dp_Resampler_execute_ctrl() , also returning each output's second-bank value and the arm both were read at (dp_resamp_execute_ctrl_pair() ). |
| size_t | dp_Resampler_execute_ctrl_pair_max_out (dp_Resampler_state_t * state) Output-capacity hint for dp_Resampler_execute_ctrl_pair() . |
| size_t | dp_Resampler_execute_max_out (dp_Resampler_state_t * state) Output-capacity hint for dp_Resampler_execute() . |
| size_t | dp_Resampler_get_consumed (const dp_Resampler_state_t * state) Input samples the last execute() or execute_ctrl() took. |
| double | dp_Resampler_get_ctrl_acc (const dp_Resampler_state_t * state) The control accumulator's fractional phase, in [0, 1). |
| double | dp_Resampler_get_delay (const dp_Resampler_state_t * state) Group delay of the interpolator, in input samples: the delay of execute_ctrl() at every rate, and of execute() at rate >= 1. |
| double | dp_Resampler_get_execute_delay (const dp_Resampler_state_t * state) Group delay of execute(), in input samples, on the path its rate runs ( dp_resamp_get_execute_delay() ). |
| size_t | dp_Resampler_get_num_phases (const dp_Resampler_state_t * state) Number of polyphase branches in the filter bank. Always a power of two. The built-in bank has 4096 phases giving sub-sample timing resolution of 1/4096 of an input sample period. |
| size_t | dp_Resampler_get_num_taps (const dp_Resampler_state_t * state) Taps per polyphase branch. Total prototype filter length is num_phases * num_taps - 1. The built-in bank uses 19 taps per branch. |
| double | dp_Resampler_get_rate (const dp_Resampler_state_t * state) Get / set the output-to-input sample rate ratio. The setter recomputes the phase increment immediately. On the same side of 1.0 the delay line and phase accumulator are preserved, so in-stream rate changes are glitch-free. Crossing 1.0 switches the engine between its interpolation and decimation modes (the doctest below goes 0.5 -> 1.5); the mode taking over was never advanced, so the crossing resets the state as reset() does, and the output from there on is what a fresh Resampler at the new rate gives. A rate that is not finite and > 0 raises ValueError and leaves the rate as it was. |
| void | dp_Resampler_get_state (const dp_Resampler_state_t * state, void * blob) Serialize the resampler's phase + delay-line state into blob . |
| void | dp_Resampler_reset (dp_Resampler_state_t * state) Zero the delay line and phase accumulator. Rate and polyphase bank are preserved so the resampler can be resumed at the same ratio. Zeroing state eliminates transient artefacts when starting a new signal burst. The control accumulator read by dp_Resampler_get_ctrl_acc() returns to 0.0 as well. |
| int | dp_Resampler_set_rate (dp_Resampler_state_t * state, double rate) Set the output-to-input sample rate ratio (see dp_Resampler_get_rate() for the mid-stream semantics). |
| int | dp_Resampler_set_state (dp_Resampler_state_t * state, const void * blob) Restore state from blob . |
| size_t | dp_Resampler_state_bytes (const dp_Resampler_state_t * state) Serialized-state byte size (forwarded to the resamp leaf). |
Macros¶
| Type | Name |
|---|---|
| define | RESAMPLER_MAX_OUT 65536 |
Detailed Description¶
Thin adapter over resamp_core (resamp_state_t). Exposes a Resampler-prefixed API so the generated ext.c compiles without changes.
Lifecycle:
dp_Resampler_state_t *r = dp_Resampler_create(0.5);
float _Complex out[4096];
size_t n = dp_Resampler_execute(r, in, 1024, out, 1024);
dp_Resampler_destroy(r);
Output buffer sizing: execute: allocate dp_Resampler_execute_max_out() samples. execute_ctrl: same.
Public Types Documentation¶
typedef dp_Resampler_state_t¶
Resampler state: an alias of the resamp leaf's resamp_state_t, so every dp_Resampler_* call forwards to the matching dp_resamp_* one.
Public Functions Documentation¶
function dp_Resampler_create¶
Create a Resampler with the built-in 4096×19 Kaiser bank. The bank provides ~60 dB alias rejection with 0.4/0.6 pass/stop normalised cutoffs. Pass rate >= 1.0 to interpolate (upsample); pass rate < 1.0 to decimate (downsample). For a custom bank use dp_Resampler_create_custom() instead (from Python, pass bank=).
Parameters:
rateOutput-to-input sample rate ratio: finite and > 0 (dp_resamp_rate_ok()). Values >= 1.0 interpolate; values < 1.0 decimate. From Python it defaults to 1.0.
Returns:
Non-NULL on success, owned by the caller and released with dp_Resampler_destroy(); NULL if rate is outside dp_resamp_rate_ok(), or on OOM.
>>> from doppler.resample import Resampler
>>> import numpy as np
>>> r = Resampler(rate=2.0)
>>> r.num_phases, r.num_taps
(4096, 19)
>>> r.rate
2.0
function dp_Resampler_create_banks¶
Create a Resampler over an optional bank and an optional second bank: the constructor Python's Resampler(rate, bank, dbank) calls, each absent array arriving as NULL.
dp_Resampler_state_t * dp_Resampler_create_banks (
double rate,
const float * bank,
size_t num_phases,
size_t num_taps,
const float * dbank,
size_t dbank_phases,
size_t dbank_taps
)
bank NULL builds the built-in Kaiser bank, as dp_Resampler_create() does; otherwise it is dp_Resampler_create_custom()'s. dbank, when given, is attached as the second bank (dp_resamp_set_dbank()), read beside the first at the same arm by dp_Resampler_execute_ctrl_pair(), so its shape must be the bank's: the built-in one's (4096 x 19) when bank is NULL.
Parameters:
rateAs dp_Resampler_create().bankRow-majornum_phases x num_tapsfloats, or NULL.num_phasesThe bank's arms (0 with NULLbank).num_tapsThe bank's taps an arm (0 with NULLbank).dbankRow-major second bank, or NULL for none.dbank_phasesThe second bank's arms (0 with NULLdbank).dbank_tapsThe second bank's taps an arm (0 with NULLdbank).
Returns:
As dp_Resampler_create_custom(); also NULL when dbank's shape is not the bank's.
>>> from doppler.resample import Resampler
>>> import numpy as np
>>> b = np.ones((4, 3), np.float32)
>>> Resampler(rate=1.0, bank=b, dbank=2 * b).num_phases
4
function dp_Resampler_create_custom¶
Create a Resampler with a user-supplied polyphase bank.
dp_Resampler_state_t * dp_Resampler_create_custom (
size_t num_phases,
size_t num_taps,
const float * bank,
double rate
)
The bank is copied, so the caller keeps ownership of bank.
Parameters:
num_phasesNumber of polyphase branches; a power of two, at most 2^32 (any other count is refused: the branch select would read past the bank, and its 32-bit phase word cannot address more than 2^32 branches).num_tapsTaps per branch; > 0.bankRow-major float32 array, shape num_phases × num_taps.rateInitial output-to-input sample rate ratio; finite and > 0 (dp_resamp_rate_ok()).
Returns:
Non-NULL on success, owned by the caller and released with dp_Resampler_destroy(); NULL if num_phases is 0, not a power of two or above 2^32, num_taps is 0, bank is NULL, rate is outside dp_resamp_rate_ok(), or on OOM.
function dp_Resampler_destroy¶
Free the bank, the delay lines and the state itself.
Parameters:
stateResampler to free; NULL is a no-op.
function dp_Resampler_execute¶
Resample a block of CF32 samples at the fixed base rate. Uses the dual-mode polyphase engine: output-driven for rate >= 1 (interpolation), input-driven transposed-form for rate < 1 (decimation). State carries over between calls, so contiguous blocks produce the same result as one large block.
size_t dp_Resampler_execute (
dp_Resampler_state_t * state,
const float _Complex * x,
size_t x_len,
float _Complex * out,
size_t max_out
)
Parameters:
statePointer to a valid dp_Resampler_state_t.xCF32 input samples.x_lenNumber of input samples.outOutput buffer; must hold at least RESAMPLER_MAX_OUT samples.max_outCapacity ofoutin elements. Emission stops there, so the return value is the number actually written.
Returns:
CF32 output array; length is approximately x_len * rate, capped at max_out. A full out stops the call with input unread, never dropped: dp_Resampler_get_consumed() (Python: consumed) says where to resume.
>>> from doppler.resample import Resampler
>>> import numpy as np
>>> r = Resampler(rate=2.0)
>>> y = r.execute(np.zeros(128, dtype=np.complex64))
>>> y.shape, y.dtype
((256,), dtype('complex64'))
function dp_Resampler_execute_ctrl¶
Resample with per-sample additive rate deviations. Effective rate for sample i is base_rate + ctrl[i] . Uses a unified double-precision accumulator that handles both interpolation and decimation in a single code path — suitable for Doppler-shift simulation and fractional-sample timing correction. ctrl and x must have the same length.
int64_t dp_Resampler_execute_ctrl (
dp_Resampler_state_t * state,
const float _Complex * x,
size_t x_len,
const double * ctrl,
size_t ctrl_len,
float _Complex * out,
size_t max_out
)
Parameters:
statePointer to a valid dp_Resampler_state_t.xCF32 input samples.x_lenNumber of input samples.ctrlReal float64 array, same length as x; the per-sample rate addend. Anything numpy can safely widen to float64 is accepted (float32, a plain list); a complex array is refused rather than truncated.ctrl_lenNumber of control samples; at least x_len.outOutput buffer; must hold at least RESAMPLER_MAX_OUT samples.max_outCapacity ofoutin elements. Emission stops there, so the return value is the number actually written.
Returns:
CF32 output array; length depends on accumulated rate deviations, capped at max_out. In C, the count written (>= 0), or DP_ERR_INVALID (negative) when ctrl_len is shorter than x_len: nothing is written. 0 stays a valid, empty result, so the sign alone tells a refusal from it (doppler#1869). A full out stops the call with input unread, never dropped: dp_Resampler_get_consumed() (Python: consumed) says where to resume.
>>> from doppler.resample import Resampler
>>> import numpy as np
>>> r = Resampler(rate=1.0)
>>> x = np.zeros(64, dtype=np.complex64)
>>> ctrl = np.zeros(64)
>>> y = r.execute_ctrl(x, ctrl)
>>> y.shape, y.dtype
((64,), dtype('complex64'))
function dp_Resampler_execute_ctrl_max_out¶
Output-capacity hint for dp_Resampler_execute_ctrl() .
Parameters:
stateUnused.
Returns:
Always RESAMPLER_MAX_OUT.
function dp_Resampler_execute_ctrl_pair¶
dp_Resampler_execute_ctrl() , also returning each output's second-bank value and the arm both were read at (dp_resamp_execute_ctrl_pair() ).
int64_t dp_Resampler_execute_ctrl_pair (
dp_Resampler_state_t * state,
const float _Complex * x,
size_t x_len,
const double * ctrl,
size_t ctrl_len,
float _Complex * out,
float _Complex * out1,
uint32_t * out2,
size_t max_out
)
The output is dp_Resampler_execute_ctrl()'s from the same state and input, to a T-tap dot product's rounding (T * FLT_EPSILON * the arm's sum of |taps| * max |x|): both run one body, but -O3 with fast math reassociates each inlined copy's reduction in its own order (measured at 1 float32 ULP). Beside it, the second bank's value at the same arm over the same delay line (0 with no second bank), and the arm's index. What the derivative-bank timing detector reads (docs/design/ratesync-timing.md §9.1).
Parameters:
stateMust be non-NULL.xInput CF32 samples.x_lenNumber of input samples.ctrlRate deviations, parallel tox.ctrl_lenNumber of rate deviations; at leastx_len.outThe outputs.out1The second-bank values, parallel toout.out2The arm indices, parallel toout.max_outCapacity of each output array.
Returns:
The count written, or DP_ERR_INVALID (negative) when ctrl_len is shorter than x_len, as dp_Resampler_execute_ctrl().
>>> from doppler.resample import Resampler
>>> import numpy as np
>>> r = Resampler(rate=1.0)
>>> y, dy, arm = r.execute_ctrl_pair(np.ones(64, np.complex64),
... np.zeros(64))
>>> y.shape == dy.shape == arm.shape, float(abs(dy).max())
(True, 0.0)
function dp_Resampler_execute_ctrl_pair_max_out¶
Output-capacity hint for dp_Resampler_execute_ctrl_pair() .
Parameters:
stateUnused.
Returns:
Always RESAMPLER_MAX_OUT.
function dp_Resampler_execute_max_out¶
Output-capacity hint for dp_Resampler_execute() .
Parameters:
stateUnused.
Returns:
Always RESAMPLER_MAX_OUT.
function dp_Resampler_get_consumed¶
Input samples the last execute() or execute_ctrl() took.
Both calls stop when out is full and leave the rest of the input unread rather than dropping it. Python's default buffer is RESAMPLER_MAX_OUT, or len(x) if larger, so at a rate above 1 a long block or any block a steered rate stretches fills it before the input runs out. Equal to the input length when everything fit; otherwise offer x[consumed:] (and ctrl[consumed:]) to the next call and the output resumes bit-exact (doppler#2643). 0 after create and reset; a call refused for a short ctrl leaves it unchanged.
>>> from doppler.resample import Resampler
>>> import numpy as np
>>> r = Resampler(rate=2.0)
>>> x = np.ones(50000, dtype=np.complex64)
>>> y = r.execute_ctrl(x, np.zeros(50000))
>>> r.consumed < len(x)
True
>>> rest = r.consumed
>>> y2 = r.execute_ctrl(x[rest:], np.zeros(len(x) - rest))
>>> r.consumed == len(x) - rest
True
Parameters:
statePointer to a valid dp_Resampler_state_t.
Returns:
Input samples consumed by the last execute() or execute_ctrl().
function dp_Resampler_get_ctrl_acc¶
The control accumulator's fractional phase, in [0, 1).
The timing NCO's state, and the only way to see what a closed timing loop is doing to the sampling instant. floor(mu * num_phases) is the polyphase arm the NEXT output will read; a steady value means the loop has settled on a sampling phase, while one that slews and wraps means a residual RATE error it has not absorbed, one input interval per wrap.
Reports the CONTROL accumulator, so it stays 0.0 unless you are driving this object through execute_ctrl(): the free-running phase used by execute() is a separate accumulator with no accessor.
Parameters:
statePointer to a valid dp_Resampler_state_t.
Returns:
Control-accumulator phase as a fraction of one input interval.
function dp_Resampler_get_delay¶
Group delay of the interpolator, in input samples: the delay of execute_ctrl() at every rate, and of execute() at rate >= 1.
The prototype's centre plus the one input the pipeline holds back (dp_resamp_get_delay()): output k carries the input at k / rate + (accumulated ctrl) - delay. 10.5 for the built-in bank. Below unity execute() runs the decimator instead, whose delay is dp_Resampler_get_execute_delay()'s.
Parameters:
statePointer to a valid dp_Resampler_state_t.
Returns:
Group delay in input samples.
function dp_Resampler_get_execute_delay¶
Group delay of execute(), in input samples, on the path its rate runs ( dp_resamp_get_execute_delay() ).
At rate >= 1 it is the delay property's. Below unity execute() runs the decimator, whose delay is 8.5 / rate input samples for the built-in bank: output k of execute() carries the input at k / rate - execute_delay from a fresh stream. A caller mapping a position found on the output back to the input subtracts this.
>>> from doppler.resample import Resampler
>>> Resampler(rate=1.0).execute_delay
10.5
>>> Resampler(rate=0.5).execute_delay
17.0
Parameters:
statePointer to a valid dp_Resampler_state_t.
Returns:
Group delay in input samples.
function dp_Resampler_get_num_phases¶
Number of polyphase branches in the filter bank. Always a power of two. The built-in bank has 4096 phases giving sub-sample timing resolution of 1/4096 of an input sample period.
Parameters:
statePointer to a valid dp_Resampler_state_t.
Returns:
Number of polyphase arms.
function dp_Resampler_get_num_taps¶
Taps per polyphase branch. Total prototype filter length is num_phases * num_taps - 1. The built-in bank uses 19 taps per branch.
Parameters:
statePointer to a valid dp_Resampler_state_t.
Returns:
Taps per polyphase arm.
function dp_Resampler_get_rate¶
Get / set the output-to-input sample rate ratio. The setter recomputes the phase increment immediately. On the same side of 1.0 the delay line and phase accumulator are preserved, so in-stream rate changes are glitch-free. Crossing 1.0 switches the engine between its interpolation and decimation modes (the doctest below goes 0.5 -> 1.5); the mode taking over was never advanced, so the crossing resets the state as reset() does, and the output from there on is what a fresh Resampler at the new rate gives. A rate that is not finite and > 0 raises ValueError and leaves the rate as it was.
>>> from doppler.resample import Resampler
>>> r = Resampler(rate=0.5)
>>> r.rate
0.5
>>> r.rate = 1.5
>>> r.rate
1.5
Parameters:
statePointer to a valid dp_Resampler_state_t.
Returns:
Current output-to-input sample rate ratio.
function dp_Resampler_get_state¶
Serialize the resampler's phase + delay-line state into blob .
Parameters:
statePointer to a valid dp_Resampler_state_t.blobDestination; must hold dp_Resampler_state_bytes() bytes.
function dp_Resampler_reset¶
Zero the delay line and phase accumulator. Rate and polyphase bank are preserved so the resampler can be resumed at the same ratio. Zeroing state eliminates transient artefacts when starting a new signal burst. The control accumulator read by dp_Resampler_get_ctrl_acc() returns to 0.0 as well.
Parameters:
statePointer to a valid dp_Resampler_state_t.
>>> from doppler.resample import Resampler
>>> import numpy as np
>>> r = Resampler(rate=2.0)
>>> _ = r.execute(np.ones(64, dtype=np.complex64))
>>> r.reset()
>>> r.rate
2.0
function dp_Resampler_set_rate¶
Set the output-to-input sample rate ratio (see dp_Resampler_get_rate() for the mid-stream semantics).
Parameters:
statePointer to a valid dp_Resampler_state_t.rateNew ratio; finite and > 0 (dp_resamp_rate_ok()).
Returns:
DP_OK, or DP_ERR_INVALID if rate is refused (nothing changes).
function dp_Resampler_set_state¶
Restore state from blob .
Parameters:
stateResampler built with the same bank shape as the writer.blobBlob from dp_Resampler_get_state().
Returns:
DP_OK, or DP_ERR_INVALID if the blob is rejected.
function dp_Resampler_state_bytes¶
Serialized-state byte size (forwarded to the resamp leaf).
Parameters:
statePointer to a valid dp_Resampler_state_t.
Returns:
Size in bytes of the blob dp_Resampler_get_state() writes; the blob carries the resamp leaf's own envelope.
Macro Definition Documentation¶
define RESAMPLER_MAX_OUT¶
Output capacity, in samples, the binding pre-allocates for one call (returned by both *_max_out() hints).
The documentation for this class was generated from the following file native/inc/doppler/Resampler/Resampler_core.h