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

Bandlimited Fourier waveform kernels (BLIT / PolyBLEP / miniBLEP). More...

#include <complex.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include "oakfield/field.h"
Include dependency graph for math/fourier.h:

Go to the source code of this file.

Classes

struct  SimComplexDouble
 Explicit in-memory representation for a complex double scalar (re, im) More...
 

Macros

#define SIM_FOURIER_ASSUME_VALID   0
 Skip hot-path argument clamps when inputs are validated by caller.
 
#define SIM_FOURIER_MINIBLEP_SPAN   4.0
 Two-sided BLIT support window (in units of phase increment): wider -> cleaner stopband.
 
#define SIM_FOURIER_MINIBLEP_MAX_WINDOW   0.5
 Maximum miniBLEP window in normalized phase (0..1).
 
#define SIM_FOURIER_INTEGRATOR_LEAK   1e-9
 Small leak to keep BLIT integrators numerically bounded.
 

Typedefs

typedef struct SimComplexDouble SimComplexDouble
 Fallback ABI-compatible complex double if not defined in field.h.
 

Functions

double sim_fourier_dirichlet (double phase_radians, int harmonic_count)
 Normalized Dirichlet kernel D_M(φ) = sin(Mφ/2)/(M sin(φ/2)).
 
SimComplexDouble sim_fourier_dirichlet_complex (SimComplexDouble phase_radians, int harmonic_count)
 Complex Dirichlet kernel.
 
double sim_fourier_blit (double phase_radians, int harmonic_count)
 DC-corrected BLIT for saw/triangle integration: D_M(φ) − 1/M.
 
SimComplexDouble sim_fourier_blit_complex (SimComplexDouble phase_radians, int harmonic_count)
 Complex BLIT (DC-corrected).
 
double sim_fourier_saw_blit (double phase_radians, double phase_increment_radians, int harmonic_count, double *state)
 Bandlimited saw from BLIT integration.
 
SimComplexDouble sim_fourier_saw_blit_complex (SimComplexDouble phase_radians, double phase_increment_radians, int harmonic_count, SimComplexDouble *state)
 Complex BLIT saw (integrates complex BLIT).
 
double sim_fourier_square_blit (double phase_radians, double phase_increment_radians, int harmonic_count, double duty, double *state)
 Bandlimited square via phase-shifted BLIT difference and integration.
 
SimComplexDouble sim_fourier_square_blit_complex (SimComplexDouble phase_radians, double phase_increment_radians, int harmonic_count, double duty, SimComplexDouble *state)
 Complex BLIT square.
 
double sim_fourier_triangle_blit (double phase_radians, double phase_increment_radians, int harmonic_count, double *velocity_state, double *position_state)
 Triangle from double-integrated BLIT square (velocity + position states).
 
SimComplexDouble sim_fourier_triangle_blit_complex (SimComplexDouble phase_radians, double phase_increment_radians, int harmonic_count, SimComplexDouble *velocity_state, SimComplexDouble *position_state)
 Complex BLIT triangle (double integration).
 
double sim_fourier_polyblep (double phase, double dphase)
 Two-point PolyBLEP step correction (subtract from naive saw/square).
 
SimComplexDouble sim_fourier_polyblep_complex (double phase, double dphase)
 Complex PolyBLEP (imaginary part = 0).
 
double sim_fourier_polyblamp (double phase, double dphase)
 PolyBLAMP (integral of PolyBLEP) for slope/kink smoothing.
 
SimComplexDouble sim_fourier_polyblamp_complex (double phase, double dphase)
 Complex PolyBLAMP.
 
double sim_fourier_miniblep (double phase, double dphase)
 Table-driven miniBLEP (windowed-sinc step) for high stopband rejection.
 
SimComplexDouble sim_fourier_miniblep_complex (double phase, double dphase)
 Complex miniBLEP.
 
double sim_fourier_miniblamp (double phase, double dphase)
 miniBLAMP (integrated miniBLEP) for slope discontinuities (triangle).
 
SimComplexDouble sim_fourier_miniblamp_complex (double phase, double dphase)
 Complex miniBLAMP.
 

Detailed Description

Bandlimited Fourier waveform kernels (BLIT / PolyBLEP / miniBLEP).

This header provides reusable kernels for research-grade saw, square, and triangle oscillators:

  • BLIT (Dirichlet kernel) + integration for analytical Fourier-series control.
  • PolyBLEP / PolyBLAMP for real-time anti-aliased steps and slope changes.
  • miniBLEP / miniBLAMP (windowed-sinc table) for higher-stopband rejection.

All routines use double precision. Complex variants use SimComplexDouble for ABI stability and mirror the real APIs.

Macro Definition Documentation

◆ SIM_FOURIER_ASSUME_VALID

#define SIM_FOURIER_ASSUME_VALID   0

Skip hot-path argument clamps when inputs are validated by caller.

0 = clamp/guard (default), 1 = assume phase/duty in-range; debug builds assert.

◆ SIM_FOURIER_MINIBLEP_SPAN

#define SIM_FOURIER_MINIBLEP_SPAN   4.0

Two-sided BLIT support window (in units of phase increment): wider -> cleaner stopband.

The miniBLEP/miniBLAMP kernels are stretched over SIM_FOURIER_MINIBLEP_SPAN phase increments but capped at SIM_FOURIER_MINIBLEP_MAX_WINDOW to avoid consuming an entire period near Nyquist.

Function Documentation

◆ sim_fourier_dirichlet()

double sim_fourier_dirichlet ( double  phase_radians,
int  harmonic_count 
)

Normalized Dirichlet kernel D_M(φ) = sin(Mφ/2)/(M sin(φ/2)).

Parameters
phase_radiansInstantaneous phase in radians (wrap to 2π is optional).
harmonic_countN = number of positive harmonics below Nyquist.

Implementation detail:

  • We internally build an odd Dirichlet length M = 2N + 1 to preserve 2π periodicity.
  • DC correction terms use this M (see sim_fourier_blit).
    Returns
    BLIT sample with DC gain of 1.

◆ sim_fourier_miniblep()

double sim_fourier_miniblep ( double  phase,
double  dphase 
)

Table-driven miniBLEP (windowed-sinc step) for high stopband rejection.

Parameters
phaseNormalized phase in [0,1).
dphaseNormalized phase increment (f / fs).
Returns
Correction to subtract from a naive discontinuity.

◆ sim_fourier_polyblep()

double sim_fourier_polyblep ( double  phase,
double  dphase 
)

Two-point PolyBLEP step correction (subtract from naive saw/square).

Parameters
phaseNormalized phase in [0,1).
dphaseNormalized phase increment per sample (f / fs).

◆ sim_fourier_saw_blit()

double sim_fourier_saw_blit ( double  phase_radians,
double  phase_increment_radians,
int  harmonic_count,
double *  state 
)

Bandlimited saw from BLIT integration.

Parameters
phase_radiansPhase in radians.
phase_increment_radiansPer-sample phase advance (2π f / fs).
harmonic_countNumber of partials (see sim_fourier_dirichlet).
[in,out]stateSingle-pole integrator state (initialize to 0).
Returns
Saw sample (approximately unity peak-to-peak).

◆ sim_fourier_square_blit()

double sim_fourier_square_blit ( double  phase_radians,
double  phase_increment_radians,
int  harmonic_count,
double  duty,
double *  state 
)

Bandlimited square via phase-shifted BLIT difference and integration.

Parameters
phase_radiansCurrent oscillator phase.
phase_increment_radiansPhase increment per output sample.
harmonic_countNumber of harmonics to include.
dutyDuty cycle in [0,1]; 0.5 yields a symmetric square.
stateIntegrator state for the BLIT accumulator.

◆ sim_fourier_triangle_blit()

double sim_fourier_triangle_blit ( double  phase_radians,
double  phase_increment_radians,
int  harmonic_count,
double *  velocity_state,
double *  position_state 
)

Triangle from double-integrated BLIT square (velocity + position states).

Parameters
phase_radiansCurrent oscillator phase.
phase_increment_radiansPhase increment per output sample.
harmonic_countNumber of harmonics to include.
velocity_stateFirst integrator (square accumulator), init to 0.
position_stateSecond integrator (triangle accumulator), init to 0.