Skip to content

File hbdecim_core.h

FileList > doppler > hbdecim > hbdecim_core.h

Go to the source code of this file

Halfband 2:1 decimator for CF32 IQ samples. More...

  • #include "doppler/clib_common.h"
  • #include "doppler/dp_state.h"

Classes

Type Name
struct hbdecim_state_t
State of a CF32 halfband 2:1 decimator.

Public Functions

Type Name
size_t dp_hbdecim_consumed (const hbdecim_state_t * r)
Input samples the last dp_hbdecim_execute() took.
hbdecim_state_t * dp_hbdecim_create (size_t num_taps, const float * h)
Allocate and initialise a halfband 2:1 decimator.
double dp_hbdecim_dc_gain (const hbdecim_state_t * r)
The filter's response to a constant input, from its own taps.
void dp_hbdecim_destroy (hbdecim_state_t * r)
Free the coefficient copy, both delay lines and the state.
size_t dp_hbdecim_execute (hbdecim_state_t * r, const float _Complex * in, size_t num_in, float _Complex * out, size_t max_out)
Decimate a block of CF32 samples by 2.
double dp_hbdecim_get_group_delay (const hbdecim_state_t * r)
The decimator's group delay, in its INPUT samples: where in the input timeline an output sample's content sits.
size_t dp_hbdecim_get_num_taps (const hbdecim_state_t * r)
FIR branch length N.
double dp_hbdecim_get_rate (const hbdecim_state_t * r)
Output-to-input sample-rate ratio (fixed by design).
void dp_hbdecim_get_state (const hbdecim_state_t * r, void * blob)
Serialize r's mutable state intoblob .
void dp_hbdecim_reset (hbdecim_state_t * r)
Zero both delay lines and drop any pending sample.
int dp_hbdecim_set_state (hbdecim_state_t * r, const void * blob)
Restore mutable state from blob (same num_taps).
size_t dp_hbdecim_state_bytes (const hbdecim_state_t * r)
Bytes dp_hbdecim_get_state() writes forr (envelope + payload).

Macros

Type Name
define HBDECIM_STATE_MAGIC [**DP\_FOURCC**](dp__state_8h.md#define-dp_fourcc) ('H', 'B', 'D', 'C')
define HBDECIM_STATE_VERSION 1u

Detailed Description

Lifted from dp_hbdecim_cf32_t (c/src/hbdecim.c). Only the cf32 variant lives here; the real-input sibling (Architecture D2) is hbdecim_r2c_core.h, and the jm-bound Python face is HalfbandDecimator_core.h (doppler.resample.HalfbandDecimator).

Algorithm: two dual-write circular delay lines hold even-indexed and odd-indexed input samples separately. Per output sample: * Push x(2m) → even delay line. * Push x(2m+1) → odd delay line. * Compute symmetric FIR (N/2 paired multiplies) + scaled delay tap.

Which delay line carries the FIR is determined by N: N even (fir_on_even=1): FIR on even_dl; delay from odd_dl(centre). N odd (fir_on_even=0): FIR on odd_dl at offset +1; delay from even_dl(centre).

Coefficients are scaled by 0.5 inside dp_hbdecim_create — this is the polyphase identity normalisation; do not remove it.

Lifecycle:

hbdecim_state_t *r = dp_hbdecim_create(num_taps, h_fir);
size_t n = dp_hbdecim_execute(r, in, num_in, out, max_out);
dp_hbdecim_destroy(r);

Public Functions Documentation

function dp_hbdecim_consumed

Input samples the last dp_hbdecim_execute() took.

size_t dp_hbdecim_consumed (
    const hbdecim_state_t * r
) 

Equal to its num_in unless out filled first; then the caller resumes at in + consumed. 0 after create, reset and set_state.

Parameters:

  • r Must be non-NULL.

Returns:

Input samples taken by the last execute.


function dp_hbdecim_create

Allocate and initialise a halfband 2:1 decimator.

hbdecim_state_t * dp_hbdecim_create (
    size_t num_taps,
    const float * h
) 

The FIR branch must be symmetric (h[k] == h[N-1-k]): the filter reads only the first N/2 coefficients and applies each to a mirrored pair of delay-line samples. For odd N the centre coefficient h[N/2] is not read; the half-weight delay branch takes its place.

Parameters:

  • num_taps Length N of the FIR branch (the row from kaiser_prototype(phases=2) that has more than one significant coefficient). Must be > 0.
  • h FIR coefficients, length num_taps (float32). Copied internally and scaled by 0.5, so the caller keeps ownership. Must be non-NULL.

Returns:

A new state owned by the caller (free with dp_hbdecim_destroy()), with zeroed delay lines and no pending sample; NULL if num_taps is 0, h is NULL, or allocation fails.


function dp_hbdecim_dc_gain

The filter's response to a constant input, from its own taps.

double dp_hbdecim_dc_gain (
    const hbdecim_state_t * r
) 

A halfband decimator sums a symmetric FIR branch (each stored tap multiplies TWO delay-line samples) and a half-weight delay branch, so at DC the answer is 2*sum(h) + 0.5. Computed, not measured: a caller — or a gate — can ask what gain the object contributes without running a signal through it.

Parameters:

  • r State. Must be non-NULL.

Returns:

The DC gain. 1.0 for a correctly normalised halfband.

hbdecim_state_t *d = dp_hbdecim_create (ntaps, h);
printf ("%.4f\n", dp_hbdecim_dc_gain (d));  // 1.0000
dp_hbdecim_destroy (d);

function dp_hbdecim_destroy

Free the coefficient copy, both delay lines and the state.

void dp_hbdecim_destroy (
    hbdecim_state_t * r
) 

Parameters:


function dp_hbdecim_execute

Decimate a block of CF32 samples by 2.

size_t dp_hbdecim_execute (
    hbdecim_state_t * r,
    const float _Complex * in,
    size_t num_in,
    float _Complex * out,
    size_t max_out
) 

Processes input pairs (even, odd); one output per pair, counting a sample held from the previous call as the first of the block. A sample left unpaired at the end is buffered and consumed on the next call.

Output-buffer sizing: allocate at least (num_in + 1) / 2 samples. A shorter out drops nothing: the call stops at the pair boundary after the last output it has room for, and dp_hbdecim_consumed() says how much input it took, so the caller offers in + consumed again and the outputs resume exactly as one whole call gives (doppler#2643). A trailing sample is held only when it is the last input; with max_out 0 the call writes nothing and takes at most that one sample.

Parameters:

  • r Must be non-NULL.
  • in Input CF32 samples.
  • num_in Number of input samples.
  • out Output buffer.
  • max_out Capacity of out in samples.

Returns:

Number of output samples written: min((num_in + has_pending) / 2, max_out).


function dp_hbdecim_get_group_delay

The decimator's group delay, in its INPUT samples: where in the input timeline an output sample's content sits.

double dp_hbdecim_get_group_delay (
    const hbdecim_state_t * r
) 

Output m of a fresh stream carries the input at index 2 * m - group_delay. From the structure, not measured: output m is formed as inputs 2m and 2m + 1 arrive, and the filter's center tap is the delay branch, which reads num_taps - 1 inputs behind input 2m for either parity of num_taps (input 2m + 1 - N from the odd line when N is even, input 2m - (N - 1) from the even line when it is odd). The FIR branch is symmetric about that tap, so the delay is the same at every frequency.

Parameters:

  • r State. Must be non-NULL.

Returns:

num_taps - 1, in input samples.

const float h[4] = { -0.06f, 0.3f, 0.3f, -0.06f };
hbdecim_state_t *d = dp_hbdecim_create (4, h);
printf ("%.1f\n", dp_hbdecim_get_group_delay (d));  // 3.0
dp_hbdecim_destroy (d);

function dp_hbdecim_get_num_taps

FIR branch length N.

size_t dp_hbdecim_get_num_taps (
    const hbdecim_state_t * r
) 

Parameters:

  • r State. Must be non-NULL.

Returns:

The num_taps passed to dp_hbdecim_create().


function dp_hbdecim_get_rate

Output-to-input sample-rate ratio (fixed by design).

double dp_hbdecim_get_rate (
    const hbdecim_state_t * r
) 

Parameters:

  • r State; ignored.

Returns:

Always 0.5.


function dp_hbdecim_get_state

Serialize r's mutable state intoblob .

void dp_hbdecim_get_state (
    const hbdecim_state_t * r,
    void * blob
) 

Parameters:

  • r State. Must be non-NULL. Not modified.
  • blob Caller-owned buffer of at least dp_hbdecim_state_bytes(r) bytes.

function dp_hbdecim_reset

Zero both delay lines and drop any pending sample.

void dp_hbdecim_reset (
    hbdecim_state_t * r
) 

num_taps and coefficients are preserved; the next execute behaves as on a freshly created decimator.

Parameters:

  • r State. Must be non-NULL.

function dp_hbdecim_set_state

Restore mutable state from blob (same num_taps).

int dp_hbdecim_set_state (
    hbdecim_state_t * r,
    const void * blob
) 

The envelope (magic, version, endianness, size) is validated, and the payload's ring heads and flags checked, before anything is written; on rejection r is unchanged. Coefficients are not in the blob and are kept from create.

Parameters:

Returns:

DP_OK, or DP_ERR_INVALID if the blob's envelope or payload check refuses.


function dp_hbdecim_state_bytes

Bytes dp_hbdecim_get_state() writes forr (envelope + payload).

size_t dp_hbdecim_state_bytes (
    const hbdecim_state_t * r
) 

The size depends on num_taps (through the ring length even_cap), so a blob only fits an instance built with the same num_taps.

Parameters:

  • r State. Must be non-NULL.

Returns:

Blob size in bytes: header, both ring heads, the pending flag and sample, and both dual-write rings.


Macro Definition Documentation

define HBDECIM_STATE_MAGIC

#define HBDECIM_STATE_MAGIC `DP_FOURCC ('H', 'B', 'D', 'C')`

Envelope magic ("HBDC") that tags an hbdecim state blob.


define HBDECIM_STATE_VERSION

#define HBDECIM_STATE_VERSION `1u`

Payload layout version of an hbdecim state blob.



The documentation for this class was generated from the following file native/inc/doppler/hbdecim/hbdecim_core.h