05_16_07_approximation_variants¶
Fast Approximation Variants¶
Status: ⏸️ NOT STARTED Priority: MEDIUM Dependencies: TAREA 0 (Variant Framework) Estimated Effort: 2-3 weeks
🎯 Purpose¶
Approximation variants trade precision for speed using: - Fast math functions: Fast sin/cos/exp/log (5-10x faster) - Lookup tables: Pre-computed values - Polynomial approximations: Faster than accurate functions - Quality presets: User-selectable speed vs quality
📋 Planned Features¶
1. Fast Math Functions¶
class FastMathVariant : public IVariant {
public:
// 5-10x faster than std::sin
float fastSin(float x); // ~0.1% error
float fastCos(float x);
float fastExp(float x);
float fastLog(float x);
float fastPow(float x, float y);
};
2. Lookup Table Oscillators¶
class LookupTableOscillator : public IVariant {
public:
bool process(const float* input, float* output, size_t numSamples) override;
private:
std::vector<float> sineTable_; // Pre-computed
double phase_;
};
3. Polynomial Approximations¶
// Fast tanh approximation (neural network activation)
inline float fastTanh(float x) {
// Rational approximation: tanh(x) ≈ x(27 + x²)/(27 + 9x²)
float x2 = x * x;
return x * (27.0f + x2) / (27.0f + 9.0f * x2);
}
4. Quality Presets¶
enum class ApproximationQuality {
FASTEST, // 10x faster, ~1% error
FAST, // 5x faster, ~0.1% error
BALANCED, // 2x faster, ~0.01% error
ACCURATE // Baseline (std::sin, etc.)
};
📊 Performance vs Accuracy¶
| Function | Accurate | Fast Approx | Speedup | Max Error |
|---|---|---|---|---|
| sin/cos | std::sin | 7th poly | 8x | 0.001 |
| exp | std::exp | 5th poly | 10x | 0.01 |
| log | std::log | 4th poly | 8x | 0.01 |
| tanh | std::tanh | rational | 12x | 0.001 |
| pow | std::pow | exp(y*log(x)) | 5x | 0.05 |
🎯 Use Cases¶
- Oscillators: Wavetable lookup (10x faster)
- Distortion: Fast tanh/sigmoid (5-10x faster)
- Envelopes: Fast exp decay (5x faster)
- LFOs: Fast sin/cos (8x faster)
- Neural Networks: Fast activation functions
Status: ⏸️ Good for creative effects Priority: MEDIUM - Quality vs speed trade-off