I am sampling a sine wave at 48 kHz, the frequency range of my sine wave can vary from 0 to 20000 Hz with a step of about 100 Hz. I am using a lookup table approach. So I genera
If you want high precision, you can use a trig identities to have both a small LUT, and clean sine waves.
static float gTrigSinHi[256], gTrigSinLo[256];
static float gTrigCosHi[256], gTrigCosLo[256];
////////////////////////////////////////
// Sets up the fast trig functions
void FastTrigInit()
{
unsigned i;
for(i = 0; i < 256; ++i)
{
gTrigSinHi[i] = sin(2.0 * M_PI / 0xFFFF * (i << 8));
gTrigSinLo[i] = sin(2.0 * M_PI / 0xFFFF * (i << 0));
gTrigCosHi[i] = cos(2.0 * M_PI / 0xFFFF * (i << 8));
gTrigCosLo[i] = cos(2.0 * M_PI / 0xFFFF * (i << 0));
}
}
////////////////////////////////////////
// Implements sin as
// sin(u+v) = sin(u)*cos(v) + cos(u)*sin(v)
float FastSin(unsigned short val)
{
unsigned char hi = (val >> 8);
unsigned char lo = (val & 0xFF);
return gTrigSinHi[hi] * gTrigCosLo[lo]
+ gTrigCosHi[hi] * gTrigSinLo[lo];
}
////////////////////////////////////////
// Implements cos as
// cos(u+v) = cos(u)*cos(v) - sin(u)*sin(v)
float FastCos(unsigned short val)
{
unsigned char hi = (val >> 8);
unsigned char lo = (val & 0xFF);
return gTrigCosHi[hi] * gTrigCosLo[lo]
- gTrigSinHi[hi] * gTrigSinLo[lo];
}