DSPark 1.6.1
Header-only audio DSP framework in pure C++20 — zero dependencies
Loading...
Searching...
No Matches
Interpolation.h File Reference

Fractional-position interpolation for delay lines, tables and buffers. More...

#include "DspMath.h"
#include <cassert>
#include <cmath>
Include dependency graph for Interpolation.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Namespaces

namespace  dspark
 Main namespace for the DSPark framework.
 

Functions

template<FloatType T>
dspark::interpolateLinear (T y0, T y1, T frac) noexcept
 Linear interpolation between two adjacent samples.
 
template<FloatType T>
dspark::interpolateLinear (const T *buffer, int length, T position) noexcept
 Linear interpolation reading directly from a circular buffer.
 
template<FloatType T>
dspark::interpolateHermite (T y0, T y1, T y2, T y3, T frac) noexcept
 4-point, 3rd-order Hermite interpolation (optimized x-form).
 
template<FloatType T>
dspark::interpolateHermite (const T *buffer, int length, T position) noexcept
 
template<FloatType T>
dspark::interpolateCubic (const T *buffer, int length, T position) noexcept
 Alias of interpolateHermite (Catmull-Rom evaluated in Hermite form). Kept for backward compatibility.
 
template<FloatType T>
dspark::interpolateLagrange (T y0, T y1, T y2, T y3, T frac) noexcept
 4-point Lagrange interpolation from discrete samples.
 
template<FloatType T>
dspark::interpolateLagrange (const T *buffer, int length, T position) noexcept
 
template<FloatType T>
dspark::interpolateAllpass (T currentSample, T previousSample, T frac, T &state) noexcept
 Allpass interpolation (first-order Thiran) for fractional delay.
 

Detailed Description

Fractional-position interpolation for delay lines, tables and buffers.

4-point Lagrange interpolation reading directly from a circular buffer.

4-point Hermite interpolation reading directly from a circular buffer.

Free-function interpolators used across the framework (RingBuffer's interpolated reads, modulated delay lines). All of them are small scalar helpers that inline into the caller's loop; none allocates or locks.

Method Points Quality CPU Cost Use case
Linear 2 Low Ultra-Low LFOs, crossfades
Hermite 4 Good+ Low Modulated delays (default)
Lagrange 4 High Low Precision fractional reads
Allpass 2 Frequency-dep Low Static fractional delays

Polynomial accuracy: Linear reconstructs degree-1 signals exactly, Hermite (Catmull-Rom) degree 2, Lagrange degree 3. Hermite is the recommended default for modulated delay lines (chorus, vibrato, reverb modulation): it is stateless and C1-continuous across segments, so fast frac changes never destabilise anything, unlike the recursive Allpass interpolator.

Two overload families:

  • Raw-sample overloads (y0..y3, frac): no bounds logic at all, the fastest path. The container handles buffer wrapping (RingBuffer uses these).
  • (buffer, length, position) overloads: treat the buffer as CIRCULAR; neighbours needed beyond either end wrap around to the other end. Meant for ring buffers and periodic tables. On a plain linear buffer, positions within one sample of the edges (4-point methods) blend in samples from the opposite end.

Threading: all functions are pure and re-entrant; the Allpass state lives in the caller. Real-time safe: no allocation, no locks, no modulo; the only floating-point division is the Allpass coefficient (one per call).

Dependencies: DspMath.h (FloatType concept).

Interpolates between buffer[floor(position)] and the next sample; the two outer neighbours wrap around the buffer ends (circular contract, see notes).

Parameters
bufferPointer to the start of the audio buffer.
lengthSize of the buffer in samples (must be > 0).
positionAbsolute read position.
Returns
Interpolated sample.
Precondition
0 <= position < length. Violations assert in debug builds; release builds clamp to the nearest valid sample (NaN reads position 0).

Definition in file Interpolation.h.