Skip to content

File f32_to_i16u32_core.h

FileList > doppler > f32_to_i16u32 > f32_to_i16u32_core.h

Go to the source code of this file

Scale-and-saturate float to Q15-in-uint32 converter. More...

  • #include "doppler/clib_common.h"
  • #include "doppler/dp_state.h"
  • #include "doppler/jm_perf.h"
  • #include <math.h>

Classes

Type Name
struct dp_f32_to_i16u32_state_t
F32ToI16U32 state.

Public Functions

Type Name
dp_f32_to_i16u32_state_t * dp_f32_to_i16u32_create (float scale)
Create a f32_to_i16u32 instance.
void dp_f32_to_i16u32_destroy (dp_f32_to_i16u32_state_t * state)
Destroy a f32_to_i16u32 instance and release all memory.
void dp_f32_to_i16u32_get_state (const dp_f32_to_i16u32_state_t * state, void * blob)
Snapshot the whole state struct (scale and clipped) into blob .
void dp_f32_to_i16u32_reset (dp_f32_to_i16u32_state_t * state)
Clear the sticky clip flag, starting a fresh saturation history.
int dp_f32_to_i16u32_set_state (dp_f32_to_i16u32_state_t * state, const void * blob)
Restore the whole state struct from blob .
size_t dp_f32_to_i16u32_state_bytes (const dp_f32_to_i16u32_state_t * state)
Bytes dp_f32_to_i16u32_get_state() writes (envelope + payload).
JM_FORCEINLINE JM_HOT uint32_t dp_f32_to_i16u32_step (dp_f32_to_i16u32_state_t * state, float x)
Scale one float sample to a saturated Q15 code packed in a uint32.
void dp_f32_to_i16u32_steps (dp_f32_to_i16u32_state_t * state, const float * input, uint32_t * output, size_t n)
Process a block of float samples to Q15-in-uint32.

Macros

Type Name
define F32_TO_I16U32_STATE_MAGIC [**DP\_FOURCC**](dp__state_8h.md#define-dp_fourcc) ('F','U','3','2')
define F32_TO_I16U32_STATE_VERSION 1u

Detailed Description

Converts a float to a saturated int16, then zero-extends the 16-bit two's complement bit pattern into the lower 16 bits of a uint32 (upper 16 bits are always zero). The upper bits leave room for a consumer that accumulates in place. The CIC does not take this format: it takes CF32 and encodes to UQ16 internally (cic_core.h).

input +1.0 → int16 32767 → uint32 0x00007FFF input -1.0 → int16 -32768 → uint32 0x00008000

The default scale of 32768.0 maps [-1, +1] float to Q15 range. A sticky clipped flag is raised on saturation and cleared only by reset().

Lifecycle: create -> (step / steps / reset)* -> destroy

>>> from doppler.cvt import F32ToI16U32
>>> import numpy as np
>>> obj = F32ToI16U32(scale=32768.0)
>>> hex(obj.step(-1.0))
'0x8000'
>>> hex(obj.step(1.0))
'0x7fff'
>>> obj.step(0.0)
0
>>> x = np.array([-1.0, 0.0, 1.0], dtype=np.float32)
>>> obj.steps(x).tolist()
[32768, 0, 32767]

Public Functions Documentation

function dp_f32_to_i16u32_create

Create a f32_to_i16u32 instance.

dp_f32_to_i16u32_state_t * dp_f32_to_i16u32_create (
    float scale
) 

Stores scale and initialises the sticky clipped flag to 0.

Parameters:

  • scale Multiply factor applied before quantisation and saturation (default: 32768.0f). Use 32768.0 to convert normalised [-1, +1] samples to Q15 packed into a uint32. Must be positive and finite.

Returns:

Heap-allocated state, or NULL if scale is not positive and finite, or allocation fails.

Note:

Caller must call dp_f32_to_i16u32_destroy() when done.


function dp_f32_to_i16u32_destroy

Destroy a f32_to_i16u32 instance and release all memory.

void dp_f32_to_i16u32_destroy (
    dp_f32_to_i16u32_state_t * state
) 

Parameters:

  • state May be NULL.

function dp_f32_to_i16u32_get_state

Snapshot the whole state struct (scale and clipped) into blob .

void dp_f32_to_i16u32_get_state (
    const dp_f32_to_i16u32_state_t * state,
    void * blob
) 

Parameters:


function dp_f32_to_i16u32_reset

Clear the sticky clip flag, starting a fresh saturation history.

void dp_f32_to_i16u32_reset (
    dp_f32_to_i16u32_state_t * state
) 

Zeroes clipped so a subsequent clipped query reflects only samples seen after this call; scale is preserved. Call it at a buffer or segment boundary so a saturation on one block does not leak into the next.

Parameters:

  • state Must be non-NULL.
>>> from doppler.cvt import F32ToI16U32
>>> c = F32ToI16U32()
>>> c.step(5.0)          # out of range -> saturates, latches clipped
32767
>>> c.reset()            # forget the clip history
>>> c.clipped
False

function dp_f32_to_i16u32_set_state

Restore the whole state struct from blob .

int dp_f32_to_i16u32_set_state (
    dp_f32_to_i16u32_state_t * state,
    const void * blob
) 

The envelope (magic, version, endianness, size) is validated first; on success both scale and clipped are overwritten from the blob, so the instance takes the source's scale too.

Parameters:

Returns:

DP_OK, or DP_ERR_INVALID if the envelope rejects.


function dp_f32_to_i16u32_state_bytes

Bytes dp_f32_to_i16u32_get_state() writes (envelope + payload).

size_t dp_f32_to_i16u32_state_bytes (
    const dp_f32_to_i16u32_state_t * state
) 

Parameters:

  • state State; not read (the size is fixed).

Returns:

sizeof(dp_state_hdr_t) + sizeof(dp_f32_to_i16u32_state_t).


function dp_f32_to_i16u32_step

Scale one float sample to a saturated Q15 code packed in a uint32.

JM_FORCEINLINE  JM_HOT uint32_t dp_f32_to_i16u32_step (
    dp_f32_to_i16u32_state_t * state,
    float x
) 

Computes round(x * scale), saturates to [-32768, 32767], then zero-extends the 16-bit two's-complement pattern into the lower 16 bits of a uint32 (upper 16 bits are always zero). Latches the sticky clipped flag on saturation. A NaN input maps to 0x8000 (-32768) without setting clipped.

A NaN input counts as clipped: it latches clipped and returns the negative full-scale code, -32768 (0x8000).

Parameters:

  • state Must be non-NULL.
  • x Input sample, normally a normalised float in [-1, +1].

Returns:

Q15 code in the low 16 bits of a uint32; e.g. -32768 -> 0x8000.

>>> from doppler.cvt import F32ToI16U32
>>> c = F32ToI16U32(scale=32768.0)
>>> c.step(0.5)              # 0.5 -> Q15 16384, upper 16 bits zero
16384
>>> hex(c.step(-1.0))        # -32768 as an unsigned low-16 pattern
'0x8000'

function dp_f32_to_i16u32_steps

Process a block of float samples to Q15-in-uint32.

void dp_f32_to_i16u32_steps (
    dp_f32_to_i16u32_state_t * state,
    const float * input,
    uint32_t * output,
    size_t n
) 

Applies step() to every element. The clipped flag is updated cumulatively across the block. In C the caller supplies output; the Python binding accepts an optional pre-allocated out array and allocates a fresh one when it is omitted.

Parameters:

  • state Must be non-NULL.
  • input Input float32 array; must contain at least n elements.
  • output Output uint32 array; must be non-NULL and contain at least n elements.
  • n Number of samples to process.
>>> from doppler.cvt import F32ToI16U32
>>> import numpy as np
>>> F32ToI16U32().steps(
...     np.array([0.0, 0.5], dtype=np.float32)).tolist()
[0, 16384]

Macro Definition Documentation

define F32_TO_I16U32_STATE_MAGIC

#define F32_TO_I16U32_STATE_MAGIC `DP_FOURCC ('F','U','3','2')`

Envelope magic ("FU32") that tags an f32_to_i16u32 state blob.


define F32_TO_I16U32_STATE_VERSION

#define F32_TO_I16U32_STATE_VERSION `1u`

Payload layout version of an f32_to_i16u32 state blob.



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