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) Clear the sticky clip flag, starting a fresh saturation history. |
| 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) Scale one float sample by scale , round, and saturate to int16. |
| 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.
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:
scaleMultiply 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.
Parameters:
stateMay be NULL.
function f32_to_i16_get_state¶
function f32_to_i16_reset¶
Clear the sticky clip flag, starting a fresh saturation history.
Zeroes clipped so a subsequent clipped query reflects only samples seen after this call; the immutable scale is preserved. Call it at a buffer or segment boundary so a saturation on one block does not leak into the next.
Parameters:
stateMust be non-NULL.
>>> from doppler.cvt import F32ToI16
>>> c = F32ToI16()
>>> c.step(9.0) # out of range -> saturates, latches clipped
32767
>>> c.reset() # forget the clip history
>>> c.clipped
False
function f32_to_i16_set_state¶
function f32_to_i16_state_bytes¶
function f32_to_i16_step¶
Scale one float sample by scale , round, and saturate to int16.
Computes round(x * scale), clamps to the int16 range [-32768, 32767], and latches the sticky clipped flag if the scaled value fell outside that range before clamping. At the default scale of 32768 a normalised [-1, +1] input maps to the full Q15 code range.
Parameters:
stateMust be non-NULL.xInput sample, normally a normalised float in[-1, +1].
Returns:
Saturated int16 code in [-32768, 32767].
>>> from doppler.cvt import F32ToI16
>>> c = F32ToI16(scale=32768.0) # normalised float -> full-scale Q15
>>> c.step(0.5) # 0.5 * 32768
16384
>>> c.step(2.0) # beyond +1.0 -> saturates to max
32767
>>> c.clipped # sticky flag latched by the clip
True
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:
stateMust be non-NULL.inputInput float32 array; must contain at leastnelements.outputOutput int16 array; must contain at leastnelements.nNumber 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() # scale=32768 -> full-scale i16
[0, 16384, -32768, 32735]
Macro Definition Documentation¶
define F32_TO_I16_STATE_MAGIC¶
define F32_TO_I16_STATE_VERSION¶
The documentation for this class was generated from the following file native/inc/f32_to_i16/f32_to_i16_core.h