Skip to content

File f32_to_i16_core.h

FileList > f32_to_i16 > f32_to_i16_core.h

Go to the source code of this file

Scale-and-saturate float-to-int16 converter. More...

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

Classes

Type Name
struct f32_to_i16_state_t
F32ToI16 state.

Public Functions

Type Name
f32_to_i16_state_t * f32_to_i16_create (float scale)
Create a f32_to_i16 instance.
void f32_to_i16_destroy (f32_to_i16_state_t * state)
Destroy a f32_to_i16 instance and release all memory.
void f32_to_i16_get_state (const f32_to_i16_state_t * state, void * blob)
void f32_to_i16_reset (f32_to_i16_state_t * state)
Reset f32_to_i16 to its post-create state.
int f32_to_i16_set_state (f32_to_i16_state_t * state, const void * blob)
size_t f32_to_i16_state_bytes (const f32_to_i16_state_t * state)
JM_FORCEINLINE JM_HOT int16_t f32_to_i16_step (f32_to_i16_state_t * state, float x)
Process one input sample.
void f32_to_i16_steps (f32_to_i16_state_t * state, const float * input, int16_t * output, size_t n)
Process a block of float samples to int16.

Macros

Type Name
define F32_TO_I16_STATE_MAGIC [**DP\_FOURCC**](dp__state_8h.md#define-dp_fourcc) ('F','2','1','6')
define F32_TO_I16_STATE_VERSION 1u

Detailed Description

Multiplies the input by scale, rounds to the nearest integer, and saturates (clamps) the result to the int16 range [-32768, 32767]. The default scale of 32768.0 maps a normalised [-1, +1] float to the full Q15 integer range, making it the natural pair for I16ToF32. A sticky clipped flag is raised on any sample that saturates and is cleared only by reset().

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

>>> from doppler.cvt import F32ToI16
>>> import numpy as np
>>> obj = F32ToI16(scale=32768.0)
>>> obj.step(0.5)
16384
>>> obj.step(-0.5)
-16384
>>> obj.clipped
False
>>> obj.step(1.0)
32767
>>> obj.clipped
True
>>> obj.reset()
>>> obj.clipped
False
>>> x = np.array([0.5, -0.5, 1.0], dtype=np.float32)
>>> obj.steps(x).tolist()
[16384, -16384, 32767]

Public Functions Documentation

function f32_to_i16_create

Create a f32_to_i16 instance.

f32_to_i16_state_t * f32_to_i16_create (
    float scale
) 

Allocates state and stores scale. The clipped flag is initialised to 0. Returns NULL only on malloc failure; no parameter validation is performed (any finite float is a valid scale).

Parameters:

  • scale Multiply factor applied before rounding and saturation (default: 32768.0f). Use 32768.0 to convert a normalised [-1, +1] signal to full Q15 range.

Returns:

Heap-allocated state, or NULL on allocation failure.

Note:

Caller must call f32_to_i16_destroy() when done.


function f32_to_i16_destroy

Destroy a f32_to_i16 instance and release all memory.

void f32_to_i16_destroy (
    f32_to_i16_state_t * state
) 

Parameters:

  • state May be NULL.

function f32_to_i16_get_state

void f32_to_i16_get_state (
    const f32_to_i16_state_t * state,
    void * blob
) 

function f32_to_i16_reset

Reset f32_to_i16 to its post-create state.

void f32_to_i16_reset (
    f32_to_i16_state_t * state
) 

Clears the sticky clipped flag. The scale is preserved.

Parameters:

  • state Must be non-NULL.

function f32_to_i16_set_state

int f32_to_i16_set_state (
    f32_to_i16_state_t * state,
    const void * blob
) 

function f32_to_i16_state_bytes

size_t f32_to_i16_state_bytes (
    const f32_to_i16_state_t * state
) 

function f32_to_i16_step

Process one input sample.

JM_FORCEINLINE  JM_HOT int16_t f32_to_i16_step (
    f32_to_i16_state_t * state,
    float x
) 

Computes round(x * scale), saturates to [-32768, 32767], and sets the sticky clipped flag if saturation occurred.

Parameters:

  • state Must be non-NULL.
  • x Normalised float input sample.

Returns:

Saturated int16 output in [-32768, 32767].


function f32_to_i16_steps

Process a block of float samples to int16.

void f32_to_i16_steps (
    f32_to_i16_state_t * state,
    const float * input,
    int16_t * output,
    size_t n
) 

Applies step() to every element. The clipped flag is updated cumulatively across the block — a single saturating sample raises it for the entire call. Accepts an optional pre-allocated output array; allocates a fresh one when output is NULL.

Parameters:

  • state Must be non-NULL.
  • input Input float32 array; must contain at least n elements.
  • output Output int16 array; must contain at least n elements.
  • n Number of samples to process.
>>> from doppler.cvt import F32ToI16
>>> import numpy as np
>>> x = np.array([0.0, 0.5, -1.0, 0.999], dtype=np.float32)
>>> F32ToI16().steps(x).tolist()   # default scale=32768 -> full-scale int16
[0, 16384, -32768, 32735]

Macro Definition Documentation

define F32_TO_I16_STATE_MAGIC

#define F32_TO_I16_STATE_MAGIC `DP_FOURCC ('F','2','1','6')`

define F32_TO_I16_STATE_VERSION

#define F32_TO_I16_STATE_VERSION `1u`


The documentation for this class was generated from the following file native/inc/f32_to_i16/f32_to_i16_core.h