Sine wave look-up table generation


Here is a method for generating a sine look-up table in case you have little (a few kilobytes of) program memory. The idea goes like this: Let's say you have a sine wave lookup table of length 1024 with a 24-bit amplitude range. If you take the difference between successive samples, the range of the numbers is reduced. If you repeat the process a total of 3 times, the data will be dominated by the 3rd differential of the quantization noise and will have a range of -4 to 3, which can be conveniently stored in 3 bits only. This program below does the reverse, it generates the sine table from the 3-bit values, and also takes advantage of the symmetry properties of sine. So, instead of wasting 1024 program memory words for storing the original sine table, in the c-language implementation below, the compressed data takes only 35 24-bit words and the decompression code hopefully not too much more.

Here is a spreadsheet that illustrates the algorithm: sinetable.xls. The above implementation additionally embeds the seeds 0 and -3 in the magic data

9 thoughts on “Sine wave look-up table generation”

  1. Why are you using 24-bit words, when they’ll almost always be stored as 32-bit integers? Surely you could save another 25% by using the high byte of each word as well?

  2. Matthew: I used a processor with 24-bit program memory words when I wrote this. Yeah someone could write a 32-bit version…

  3. I have another solution. You know how you can approximate the cosine/sine using polynomials? And how every time you halve the range your polynomial covers you multiply the precision by 2^(degree of the polynomial + 1)? So you got your x = [0, 2^32-1] input that represents the angle in turns, do the symmetry thing to turn it into a value in the first quadrant, take the top N bits, use them as the index for your array of polynomial coefficients, compute the polynomial by doing (((c2 * xl >> fmt) + c1) * xl >> fmt) + c0; (for quadratic approximations), change the sign depending on the input quadrant, and there’s your result!

    If you go with quadratic approximations and 32 segments then you only need a 384 byte pre-computed table (32 segments, 3 coefficients per segment, coefficients are 32 bits) and you get max error of 1/1,450,000 (-123 dB), and on a modern computer it runs in about 15 cycles (I don’t use symmetry so it goes even faster). You can save more memory using a smaller table (at the cost of a max of 8x precision for each 2x decrement) or using higher degree polynomials (at the expense of CPU time).

  4. Hello
    Can you advise how to make 2 channels , DAC , Sine wave look-up table for 14.kHz with resolution 1mV for STM32T100 ?

  5. ted:
    1/ generate a sinewave from the code
    2/ play the sinewave through an audio device at a high enough sample rate to complete each cycle within 1/15000th of a second

    Assuming the output of this code is a full wave cycle (rather than the quarter wave used in e.g. the Yamaha OPL lookup tables), you’ll need to replay at about 15.36MHz.

    Obviously it’s only really worth doing this if you need to generate a signal for technical reasons rather than for audio. The ear can’t really determine between different wave shapes at that high a frequency (because what the ear actually picks up is the harmonic overtones caused by different timbres, rather than the wave shape itself, and any overtones for sounds above about 12kHz are inaudible to most people). In that case you’ll be more than well served by simply producing a repeating pattern of 0, +1, 0, -1 and replaying it at 60kHz, or even just +1, -1 replayed at 30kHz.

    If you want it to be aliased into a waveform of more standard frequency (say 32, 44.1 or 48kHz) then your best bet is to load up an audio editor such as Audacity that offers fundamental waveform generation and ask it to brew you up a 15kHz sine.

  6. Or go through the table at a different speed and use linear interpolation. There’s one extra sample at the end to facilitate this.

Leave a Reply to Olli Niemitalo Cancel reply

Your email address will not be published. Required fields are marked *