Skip to content

File nco_core.h

FileList > inc > nco > nco_core.h

Go to the source code of this file

Pure phase-accumulator NCO. More...

  • #include "clib_common.h"

Classes

Type Name
struct nco_state_t

Public Functions

Type Name
nco_state_t * nco_create (float norm_freq, uint32_t nmax)
Create an NCO.
void nco_destroy (nco_state_t * nco)
void nco_execute_u32 (nco_state_t * nco, uint32_t * out, size_t n)
Output n raw 32-bit accumulator values.
void nco_execute_u32_ovf (nco_state_t * nco, uint32_t * out, uint8_t * carry, size_t n)
Output n raw accumulator values and per-sample overflow flags.
void nco_execute_u32_scaled (nco_state_t * nco, uint32_t * out, size_t n)
Output n accumulator values scaled to [0, nmax).
float nco_get_freq (const nco_state_t * nco)
uint32_t nco_get_phase (const nco_state_t * nco)
uint32_t nco_get_phase_inc (const nco_state_t * nco)
void nco_reset (nco_state_t * nco)
void nco_set_freq (nco_state_t * nco, float norm_freq)
void nco_set_phase (nco_state_t * nco, uint32_t phase)

Detailed Description

Lifted from dp_nco_t (c/src/nco.c) — phase-accumulator half only. The sine-LUT / complex-phasor output lives in lo_core (LO object).

The 32-bit unsigned accumulator advances by phase_inc each sample and wraps naturally at 2^32. Three output mappings are provided:

nco_execute_u32 raw accumulator value [0, 2^32) nco_execute_u32_scaled (uint64)phase * nmax >> 32 → [0, nmax) nco_execute_u32_ovf raw + per-sample carry/overflow flag

nmax=0 in nco_execute_u32_scaled is treated identically to nco_execute_u32 (returns raw accumulator unchanged).

Normalised-frequency → phase_inc conversion:

phase_inc = floor((norm_freq mod 1.0) × 2^32)

Negative frequencies fold correctly: −0.25 → phase_inc = 3×2^30.

nco_state_t *nco = nco_create(0.25f, 0);
uint32_t out[256];
nco_execute_u32(nco, out, 256);
nco_destroy(nco);

reset() zeroes phase only; norm_freq and nmax are unchanged.

Public Functions Documentation

function nco_create

Create an NCO.

nco_state_t * nco_create (
    float norm_freq,
    uint32_t nmax
) 

Parameters:

  • norm_freq Normalised frequency (cycles per sample). Any float; folded into [0, 1) internally.
  • nmax Wrap target for nco_execute_u32_scaled. Pass 0 to always return the raw accumulator.

Returns:

Heap-allocated state, or NULL on OOM.


function nco_destroy

void nco_destroy (
    nco_state_t * nco
) 

Free all resources. NULL is a no-op.


function nco_execute_u32

Output n raw 32-bit accumulator values.

void nco_execute_u32 (
    nco_state_t * nco,
    uint32_t * out,
    size_t n
) 

Parameters:

  • nco Must be non-NULL.
  • out Output buffer, length >= n (uint32_t).
  • n Number of samples.

function nco_execute_u32_ovf

Output n raw accumulator values and per-sample overflow flags.

void nco_execute_u32_ovf (
    nco_state_t * nco,
    uint32_t * out,
    uint8_t * carry,
    size_t n
) 

carry[i] == 1 when the accumulator wrapped on sample i (one input period elapsed), 0 otherwise. Useful for polyphase sample-clock generation — the carry marks when to consume the next input sample.

Parameters:

  • nco Must be non-NULL.
  • out Raw phase values, length >= n (uint32_t).
  • carry Overflow flags, length >= n (uint8_t).
  • n Number of samples.

function nco_execute_u32_scaled

Output n accumulator values scaled to [0, nmax).

void nco_execute_u32_scaled (
    nco_state_t * nco,
    uint32_t * out,
    size_t n
) 

Uses the branchless fixed-point identity: out[i] = (uint64_t)phase * nmax >> 32

Works for any nmax without division. When nco->nmax == 0, falls back to the raw accumulator (same as nco_execute_u32).

Parameters:

  • nco Must be non-NULL.
  • out Output buffer, length >= n (uint32_t).
  • n Number of samples.

function nco_get_freq

float nco_get_freq (
    const nco_state_t * nco
) 

function nco_get_phase

uint32_t nco_get_phase (
    const nco_state_t * nco
) 

function nco_get_phase_inc

uint32_t nco_get_phase_inc (
    const nco_state_t * nco
) 

function nco_reset

void nco_reset (
    nco_state_t * nco
) 

Zero phase accumulator. norm_freq and nmax are unchanged.


function nco_set_freq

void nco_set_freq (
    nco_state_t * nco,
    float norm_freq
) 

Update normalised frequency without disturbing the phase.


function nco_set_phase

void nco_set_phase (
    nco_state_t * nco,
    uint32_t phase
) 

Write phase accumulator (phase seek / sync).



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