-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#ifndef XTRIG_H | ||
#define XTRIG_H | ||
|
||
#include <stdio.h> | ||
#include <math.h> | ||
|
||
#define LUT_BIN_SIZE 0.00174533 // in radians | ||
#define HALF_PI (M_PI / 2.0) | ||
#define LUT_SIZE (int)(HALF_PI/ LUT_BIN_SIZE + 1) | ||
|
||
/** sine lookup table (LUT) with sampled values from 0 to pi/2 */ | ||
extern double sine_lookup[LUT_SIZE]; | ||
|
||
/** Initialize lookup tables - must be called before xsin or xcos */ | ||
void init_lookup_tables(); | ||
/** fast sine */ | ||
double xsin(double angle); | ||
/** fast cosine */ | ||
double xcos(double angle); | ||
|
||
#endif // XTRIG_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include "xtrig.h" | ||
|
||
double sine_lookup[LUT_SIZE]; | ||
|
||
void init_lookup_tables() { | ||
for (int i = 0; i < LUT_SIZE; i++) | ||
sine_lookup[i] = sin(i * LUT_BIN_SIZE); | ||
} | ||
|
||
/** Get the index of an angle in the LUT */ | ||
static int get_lookup_index(double rad) { | ||
return (int) round(rad / LUT_BIN_SIZE); | ||
} | ||
|
||
double xsin(double rad) { | ||
// use the period and symmetry to compute based off [0, pi/2] | ||
rad = fmod(rad, 2*M_PI); | ||
if (rad < 0) rad += 2*M_PI; | ||
if (rad < HALF_PI) | ||
return sine_lookup[get_lookup_index(rad)]; | ||
else if (rad < M_PI) | ||
return sine_lookup[get_lookup_index(M_PI - rad)]; | ||
else if (rad < 3*HALF_PI) | ||
return -sine_lookup[get_lookup_index(rad - M_PI)]; | ||
else | ||
return -sine_lookup[get_lookup_index(2*M_PI - rad)]; | ||
} | ||
|
||
double xcos(double rad) { | ||
return xsin(HALF_PI - rad); | ||
} | ||
|