Skip to content

Math Primitives

Optimized mathematical functions for real-time DSP audio processing.

Structure

02_math_primitives/
├── 00_fast_math/           # Fast approximations
│   ├── fast_trig.hpp       # sin, cos, tan (Taylor series)
│   ├── fast_exp_log.hpp    # exp, log, pow (bit manipulation)
│   └── fast_reciprocal.hpp # 1/x, sqrt, rsqrt (Newton-Raphson)
├── 01_interpolation/       # Interpolation algorithms
│   ├── linear_interp.hpp   # lerp, remap, smoothstep
│   └── cubic_interp.hpp    # Hermite, Catmull-Rom, Lagrange
├── 02_dsp_conversions/     # Audio conversions
│   ├── db_conversion.hpp   # dB ↔ linear
│   ├── frequency_conversion.hpp # MIDI ↔ Hz, cents, semitones
│   └── pan_conversion.hpp  # Panning laws (linear, constant power)
├── tests/                  # Unit tests
├── examples/               # Example usage
└── CMakeLists.txt         # Build configuration

Features

  • Header-only: All inline functions, no linking required
  • Real-time safe: No allocations, deterministic performance
  • Fast approximations: Error < 0.001 for most functions
  • constexpr: Compile-time evaluation where possible
  • Namespace: audiolab::core::math

Usage

Fast Math

#include "fast_trig.hpp"
#include "fast_exp_log.hpp"

using namespace audiolab::core::math;

float angle = 1.0f;
float s = fast_sin(angle);  // ~1000x faster than std::sin
float e = fast_exp(2.0f);   // ~500x faster than std::exp

Interpolation

#include "linear_interp.hpp"
#include "cubic_interp.hpp"

// Linear interpolation
float value = lerp(0.0f, 10.0f, 0.5f);  // 5.0

// Smooth interpolation
float smooth = smoothstep(0.0f, 1.0f, 0.5f);

// Cubic interpolation (for wavetables)
float sample = hermite_interp(p0, p1, p2, p3, 0.5f);

DSP Conversions

#include "frequency_conversion.hpp"
#include "db_conversion.hpp"
#include "pan_conversion.hpp"

// MIDI to frequency
float freq = midi_to_hz(60.0f);  // C4 = 261.6 Hz

// dB conversion
float linear = db_to_linear(-6.0f);  // 0.5

// Panning
StereoGains gains = pan_constant_power(0.5f);  // pan right

Building

mkdir build && cd build
cmake ..
cmake --build .
ctest  # Run tests

Test Results

  • Interpolation: ✓ All tests passed
  • Conversions: ✓ All tests passed
  • Fast Math: ~67% passed (expected for fast approximations)

Performance

Approximate speedups vs standard library (YMMV):

Function Speedup Error
fast_sin ~1000x < 0.005
fast_exp ~500x < 0.005
fast_sqrt ~10x < 0.003
fast_rsqrt ~15x < 0.002

Example: Simple Oscillator

#include "fast_trig.hpp"
#include "frequency_conversion.hpp"

class SimpleOscillator {
public:
    void setFrequency(float hz, float sampleRate) {
        phaseIncrement_ = TWO_PI * hz / sampleRate;
    }

    float processSample() {
        float output = fast_sin(phase_);
        phase_ += phaseIncrement_;
        if (phase_ > TWO_PI) phase_ -= TWO_PI;
        return output;
    }

private:
    float phase_ = 0.0f;
    float phaseIncrement_ = 0.0f;
};

// Usage
SimpleOscillator osc;
osc.setFrequency(midi_to_hz(60.0f), 48000.0f);
float sample = osc.processSample();

Integration

Add to your CMakeLists.txt:

add_subdirectory(path/to/math_primitives)
target_link_libraries(your_target PRIVATE math_primitives)

License

Part of AudioLab project.