Search code examples
algorithmembeddedlookup-tablesfixed-point

How to find offset in a sine lookup table (LUT) using fixed point arithmetic


I am generating a LUT dividing 0-2PI into 512 fragments and then finding sine of these values and storing them as Q1.31 values.

Example:

LUT[0] = 0

LUT[1] = sin((1/512) * 2*PI) * (2^31)
..
..

LUT[511] = sin((511/512) * 2*PI) * (2^31)

My inputs are also values in the Q1.31 format.

My question is how do I go about using the LUT i.e. what is the algorithm to find the offset values in the table when I get a random value as an input to figure out the sine value

Example:

int sample_input = 0.125 * (2^31) //0.125radians in Q31 format = 268435456

Solution

  • Note that 0.125radians * (2^31) approach is not suitable for angles > 1 radian. Probably, you wanted to normalize angle (0.125radians/2Pi) * (2^31)
    Anyway, you'll need to map 2^31 range to 2^9 - so just divide by 2^22. Example:

    Angle = 0.125 radians.
    0.125 /(2*Pi) * 2^31 = 42 722 829
    42 722 829 / 2^22 = 10
    Result = Lut[10] = 262 874 923