Oakfield API Documentation 1.0.0
Numerical core APIs
Loading...
Searching...
No Matches
sim_noise_source.h File Reference

Deterministic RNG streams and reusable noise update helpers. More...

#include "field.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
Include dependency graph for sim_noise_source.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  SimNoiseSourceRng
 Mutable deterministic RNG state with cached normal sample. More...
 

Macros

#define SIM_NOISE_SOURCE_STREAM_STIMULUS_WHITE   0x85EBCA77C2B2AE63ULL
 
#define SIM_NOISE_SOURCE_STREAM_STOCHASTIC   0x000000000BAD5EEDULL
 
#define SIM_NOISE_SOURCE_STREAM_ORNSTEIN   0xD8D12A6C9B4E41B7ULL
 

Typedefs

typedef struct SimNoiseSourceRng SimNoiseSourceRng
 Mutable deterministic RNG state with cached normal sample.
 

Functions

void sim_noise_source_seed (SimNoiseSourceRng *rng, uint64_t initstate, uint64_t initseq)
 Seed a deterministic RNG stream.
 
double sim_noise_source_uniform (SimNoiseSourceRng *rng)
 Draw a uniform variate in [0, 1).
 
void sim_noise_source_normal_pair (SimNoiseSourceRng *rng, double *n0, double *n1)
 Draw a pair of independent standard-normal variates.
 
double sim_noise_source_normal (SimNoiseSourceRng *rng)
 Draw one standard-normal variate.
 
double sim_noise_source_gaussian_gain (double sigma, double dt, bool scale_by_dt)
 Compute the finite gain used by Gaussian white-noise updates.
 
SimResult sim_noise_source_ensure_capacity (double **real_state, SimComplexDouble **complex_state, size_t *capacity, size_t count, bool is_complex)
 Ensure reusable temporal-noise state storage has at least count entries.
 
void sim_noise_source_apply_white_real (double *dst, size_t count, double mean, double gain, SimNoiseSourceRng *rng)
 Add real-valued white Gaussian noise to an array in place.
 
void sim_noise_source_apply_white_complex (SimComplexDouble *dst, size_t count, double mean_re, double mean_im, double gain_re, double gain_im, SimNoiseSourceRng *rng)
 Add complex white Gaussian noise to an array in place.
 
void sim_noise_source_temporal_params (double sigma, double tau, double alpha, double dt, double *out_decay, double *out_variance)
 Derive decay and innovation scale for temporal correlated noise.
 
void sim_noise_source_apply_temporal_real (double *dst, double *noise_state, size_t count, double output_scale, double decay, double variance, SimNoiseSourceRng *rng)
 Advance real temporal-noise state and add it to a destination array.
 
void sim_noise_source_apply_temporal_complex (SimComplexDouble *dst, SimComplexDouble *noise_state, size_t count, double output_scale, double decay, double variance, SimNoiseSourceRng *rng)
 Advance complex temporal-noise state and add it to a destination array.
 
void sim_noise_source_apply_mean_reverting_real (double *dst, size_t count, double mean, double decay, double variance, SimNoiseSourceRng *rng)
 Apply a real mean-reverting update in place.
 
void sim_noise_source_apply_mean_reverting_complex (SimComplexDouble *dst, size_t count, double mean, double decay, double variance, SimNoiseSourceRng *rng)
 Apply a complex mean-reverting update in place.
 

Detailed Description

Deterministic RNG streams and reusable noise update helpers.

This header exposes the shared stochastic primitives used by stimulus and noise operators. Streams are explicitly seeded, output buffers are caller-owned, and helper functions clamp non-finite parameters to deterministic fallbacks rather than propagating NaN through update loops.

Typedef Documentation

◆ SimNoiseSourceRng

Mutable deterministic RNG state with cached normal sample.

The state/inc pair implements the underlying uniform stream. The spare fields cache the second Box-Muller output so repeated scalar-normal calls consume the same sequence as paired normal generation.

Function Documentation

◆ sim_noise_source_apply_mean_reverting_complex()

void sim_noise_source_apply_mean_reverting_complex ( SimComplexDouble dst,
size_t  count,
double  mean,
double  decay,
double  variance,
SimNoiseSourceRng rng 
)

Apply a complex mean-reverting update in place.

The same real mean is applied to both real and imaginary channels, with independent Gaussian innovations when variance is nonzero.

Parameters
[in,out]dstComplex values to update in place.
countNumber of entries in dst.
meanReversion target for both channels.
decayDistance-from-mean multiplier.
varianceOptional standard-normal innovation multiplier.
rngRNG state to advance when variance is nonzero.

◆ sim_noise_source_apply_mean_reverting_real()

void sim_noise_source_apply_mean_reverting_real ( double *  dst,
size_t  count,
double  mean,
double  decay,
double  variance,
SimNoiseSourceRng rng 
)

Apply a real mean-reverting update in place.

Each element becomes mean + decay * (x - mean) plus optional Gaussian innovation. Non-finite parameters fall back to mean=0, decay=1, variance=0.

Parameters
[in,out]dstReal values to update in place.
countNumber of entries in dst.
meanReversion target.
decayDistance-from-mean multiplier.
varianceOptional standard-normal innovation multiplier.
rngRNG state to advance when variance is nonzero.

◆ sim_noise_source_apply_temporal_complex()

void sim_noise_source_apply_temporal_complex ( SimComplexDouble dst,
SimComplexDouble noise_state,
size_t  count,
double  output_scale,
double  decay,
double  variance,
SimNoiseSourceRng rng 
)

Advance complex temporal-noise state and add it to a destination array.

Real and imaginary state channels use independent normal innovations.

Parameters
[in,out]dstDestination values incremented by output_scale * state.
[in,out]noise_statePersistent per-element complex noise state.
countNumber of entries in both arrays.
output_scaleScale applied to updated channels before adding to dst.
decayAutoregressive decay factor.
varianceMultiplier for fresh standard-normal innovations.
rngRNG state to advance.

◆ sim_noise_source_apply_temporal_real()

void sim_noise_source_apply_temporal_real ( double *  dst,
double *  noise_state,
size_t  count,
double  output_scale,
double  decay,
double  variance,
SimNoiseSourceRng rng 
)

Advance real temporal-noise state and add it to a destination array.

Parameters
[in,out]dstDestination values incremented by output_scale * state.
[in,out]noise_statePersistent per-element real noise state.
countNumber of entries in both arrays.
output_scaleScale applied to the updated state before adding to dst.
decayAutoregressive decay factor.
varianceMultiplier for fresh standard-normal innovation.
rngRNG state to advance.

◆ sim_noise_source_apply_white_complex()

void sim_noise_source_apply_white_complex ( SimComplexDouble dst,
size_t  count,
double  mean_re,
double  mean_im,
double  gain_re,
double  gain_im,
SimNoiseSourceRng rng 
)

Add complex white Gaussian noise to an array in place.

Real and imaginary channels draw independent standard-normal samples. Non- finite means or gains are treated as zero.

Parameters
[in,out]dstComplex destination values to increment.
countNumber of entries in dst.
mean_reReal-channel deterministic offset.
mean_imImaginary-channel deterministic offset.
gain_reReal-channel normal multiplier.
gain_imImaginary-channel normal multiplier.
rngRNG state to advance.

◆ sim_noise_source_apply_white_real()

void sim_noise_source_apply_white_real ( double *  dst,
size_t  count,
double  mean,
double  gain,
SimNoiseSourceRng rng 
)

Add real-valued white Gaussian noise to an array in place.

Non-finite mean and gain are treated as zero. The destination is unchanged when dst, rng, or count is empty.

Parameters
[in,out]dstReal destination values to increment.
countNumber of entries in dst.
meanDeterministic offset added to each element.
gainMultiplier for independent N(0, 1) samples.
rngRNG state to advance.

◆ sim_noise_source_ensure_capacity()

SimResult sim_noise_source_ensure_capacity ( double **  real_state,
SimComplexDouble **  complex_state,
size_t *  capacity,
size_t  count,
bool  is_complex 
)

Ensure reusable temporal-noise state storage has at least count entries.

The selected real or complex state array is allocated or grown with new slots zero-initialized. Passing count zero frees both arrays and resets capacity.

Parameters
[in,out]real_statePointer to caller-owned real state storage.
[in,out]complex_statePointer to caller-owned complex state storage.
[in,out]capacityCurrent and resulting number of allocated entries.
countRequired element count; zero releases both arrays.
is_complexSelects which state array must be grown for nonzero count.
Returns
SIM_RESULT_OK, SIM_RESULT_INVALID_ARGUMENT, or SIM_RESULT_OUT_OF_MEMORY.

◆ sim_noise_source_gaussian_gain()

double sim_noise_source_gaussian_gain ( double  sigma,
double  dt,
bool  scale_by_dt 
)

Compute the finite gain used by Gaussian white-noise updates.

Non-finite sigma or dt values are treated as zero. When scale_by_dt is true, the gain is multiplied by sqrt(abs(dt)).

Parameters
sigmaNoise amplitude.
dtSimulation timestep.
scale_by_dtWhether to apply sqrt(abs(dt)) scaling.
Returns
Finite gain value, or 0.0 if the computed gain is non-finite.

◆ sim_noise_source_normal()

double sim_noise_source_normal ( SimNoiseSourceRng rng)

Draw one standard-normal variate.

Parameters
rngRNG state to advance.
Returns
A cached or newly sampled N(0, 1) value, or 0.0 when rng is NULL.

◆ sim_noise_source_normal_pair()

void sim_noise_source_normal_pair ( SimNoiseSourceRng rng,
double *  n0,
double *  n1 
)

Draw a pair of independent standard-normal variates.

Uses Box-Muller sampling with the first uniform clamped away from zero to avoid log(0). Output pointers may be NULL.

Parameters
rngRNG state to advance; NULL writes zeroes to non-NULL outputs.
[out]n0Receives the first N(0, 1) sample when non-NULL.
[out]n1Receives the second N(0, 1) sample when non-NULL.

◆ sim_noise_source_seed()

void sim_noise_source_seed ( SimNoiseSourceRng rng,
uint64_t  initstate,
uint64_t  initseq 
)

Seed a deterministic RNG stream.

Parameters
rngRNG state to initialize; NULL is ignored.
initstateInitial generator state value.
initseqStream selector; converted to an odd increment internally.

◆ sim_noise_source_temporal_params()

void sim_noise_source_temporal_params ( double  sigma,
double  tau,
double  alpha,
double  dt,
double *  out_decay,
double *  out_variance 
)

Derive decay and innovation scale for temporal correlated noise.

The update convention is state = decay * state + variance * N(0, 1). Invalid dt disables innovation, invalid tau is clamped to a tiny positive value, and alpha is bounded below by 0.1.

Parameters
sigmaInnovation amplitude before temporal normalization.
tauPositive correlation timescale.
alphaPositive stretch exponent, clamped to at least 0.1.
dtSimulation timestep.
[out]out_decayReceives decay factor when non-NULL.
[out]out_varianceReceives normal multiplier when non-NULL.

◆ sim_noise_source_uniform()

double sim_noise_source_uniform ( SimNoiseSourceRng rng)

Draw a uniform variate in [0, 1).

Parameters
rngRNG state to advance.
Returns
Uniform double in [0, 1), or 0.0 when rng is NULL.