[sdiy] OT - Linear interpolation in C
Olivier Gillet
ol.gillet at gmail.com
Sun Jan 30 17:11:26 CET 2011
There's no way in C to express the fact that a 16 bits int is indeed a
8.8 fixed point value. So you treat it as a "full range" int and shift
by whatever is the number of fractional bits. In your example, the
interpolation "fader" is an 8-bits unsigned int which represents a
value between 0 and 1, so when multiplying you must shift the result
by 8.
This gives the following line:
value = ((255 - phase.frac) * table[phase.index] + phase.frac *
table[phase.index + 1]) >> 8;
(In this example, phase is a union of a 4 bytes struct and a 32 bit
unsigned int - you use the 32 bit unsigned int when doing
incrementation/reset, you use the struct for accessing the individual
bytes, I find it more readable than shifting and masking).
If you want to use the first 8 bits for wavetable index, the next 16
for interpolation, this would become:
value = ((65535 - phase.frac) * table[phase.index] + phase.frac *
table[phase.index + 1]) >> 16;
However, such vital and low-level things as interpolated table
lookups, oscillator functions etc. are worth doing in inline assembly.
The 200 or so lines of assembly in the Shruthi code are all fixed
point primitive operations, with different types of operand (size and
signed/unsigned).
Olivier
More information about the Synth-diy
mailing list