Generating a high SNR sinusoide with lut on a DSP I'm generating a sinus by using a lut method on a DSPic33f. My sample rate is 48 kHz, so I saved 12000 of the first value (unsigned int, 16 bits) and use trigonometric formulas to calculate the other parts of the sinusoid. I can change the frequency of this sine by turning a rotary encoder. Every 2 ms, a timer interrupt read the value of the rotary encoder, and the value (frequency in Hz) will be the new index to read my lut.

A bit of the code for you to understand :

ANGLE += FREQUENCY_VALUE;
if(ANGLE &lt; SINE_TABLE_LEN)
{
    index  = ANGLE;
    sign = 1;
}
else if((ANGLE &gt;= SINE_TABLE_LEN) &amp;&amp; (ANGLE &lt; SINE_TABLE_LEN2))
{
    index = SINE_TABLE_LEN2 - ANGLE;
    sign = 1;
}
else if((ANGLE &gt;= SINE_TABLE_LEN2) &amp;&amp; (ANGLE &lt; SINE_TABLE_LEN3))
{
     index = ANGLE - SINE_TABLE_LEN2;
     sign = -1;
}
else if((ANGLE &gt;= SINE_TABLE_LEN3) &amp;&amp; (ANGLE &lt; SINE_TABLE_LEN4))
{
     index = SINE_TABLE_LEN4 - ANGLE;
     sign = -1;
}
else
{
     ANGLE -= SINE_TABLE_LEN4;
     index = ANGLE;
     sign = 1;
}
audio_sample_l = sign * sine_lut[index-1];
audio_sample_r = audio_sample_l;


I initialize my variable ANGLE at 0 and FREQUENCY_VALUE at 1. When a plot the fft of my sinus I get some odd harmonic noise that I can not explain. When I compare the output samples with the one from my lut, they are the same (no index error). The problem may come from the fact that I do not start at the beginning of my lut but at the previous sample to avoid phase changes. There may be some rounded error also. My third lead is the initialization values and the index calculation (test in the loop also). What do you think ?



Anyone have some ideas to change that ?
Trace your logic when ANGLE is incremented to SINE_TABLE_LEN4 -- you'll find you are accessing below the buffer.  You should really adopt a zero-based index scheme when coding in C.