# How sine wave is generated using lookup tables

From this document I am quoting P2,L7

So what is the incremental distance if we are using a 64 element look-up table instead of a sine function based on 2π? incremental distance = 2k*64/48k = 8/3. In this case increment represents a virtual phase increment or more directly a table index increment.

The radian increment statement I am clear with. Now I am having a basic doubt of how can it be incremented to table index of 8/3 on each sample to be calculated.

Following, some new doubts arise.

1. Is lookup table method applicable for limited number of Fs, which can be indexed from the lookup table ?
2. Consider a lookup table of size 256, generated with resolution of π/24. Is it popssible to generate sine value for, say sin(π/3) from lookup table ?

Lookup tables are incredibly useful.

In audio, when you're doing a chorus or flange effect, or a synthesizer with a modulating envelope, you want to be able to cycle through a sine wave in a lookup table and not just keep track of some infinitely growing index n.

You'll want to use some mathematical program like MATLAB, Octave, or Mathematica to generate as many points as is practical (I wouldn't go with less than 512 or 1024 points) and store that in an array. This is your lookup table.

For a fractional index, what you'll end up doing is linearly interpolating around the nearest two indices at that fractional value. Let's say your index is 8/3. You've called up your index in a 1024 point table four times. What do you do? Now you're at index 32/3.

You'll need to find the lower index floor(32/3) and the higher one ceil(32/3). You'll need some conditionals in case you're at the end. I'll leave that exercise to you.

So now you've got your neighbor indices, you'll evaluate the interpolated value at that fractional index using the linear interpolation formula.

y = x*y2 + (1-x)*y1;

where x is the difference between your fractional index and the nearest index rounded down, y1 is the nearest value of the lookup table rounded down (you found the index earlier, now just use it to index into the lookup array) and y2 is the same thing for the nearest value rounded up.

You would track the index in a variable and increment it each loop of your program by 8/3 and do a wraparound when necessary (when you go beyond the length of the lookup table).

The trickiest thing is keeping track of the indices. The nearest value rounded up may be the 0th index.

• Thanks Anthony. Can you say some more on why should i take 32/3 instead of 8/3, as you said in Let's say your index is 8/3. You've called up your index in a 1024 point table four times. What do you do? Now you're at index 32/3. – nmxprime May 18 '15 at 6:39
• Because 4*8/3 is 32/3. It is just an example. – panthyon May 18 '15 at 14:10
• My Question is, why should 4 be multiplied, – nmxprime May 20 '15 at 11:45
• I'm off by one. if you have passed through 4 iterations of your loop (really 5 if you start at zero), it means you must have incremented four times. Like if fracIdx = 0, then during that iteration you will write fracIdx += fracIdx; on your fifth iteration it will end up that fracIdx = 4*fracIdx if you start at zero. – panthyon May 20 '15 at 14:16

So in general look up tables works as follows: Let's say you have a sample rate of 48 kHz and a table of size 64 (covering all the range from 0 to 2*pi). If you step through the table with a step size of 1, you get one cycle in 64 samples so the frequency would be 48kHz/64 = 750 Hz.

For a step size of 2 you would get 1500 Hz, 3 would be 2250 Hz etc. You can only create exact sine waves that are an integral multiple of the fundamental frequency (sampleRat/tableSize).

You can certainly try other frequencies as well, but you end up with a non-integer step size so you either have to round the step index or do some interpolation between table values.

In general table look up is a terrible way of creating sine waves. There are other oscillator algorithms that are much cheaper, more flexible and require almost no memory. See for example How to implement a digital oscillator?