# inpulse / sin_table.c

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107``` ```#include "sin_table.h" #include #include #include static const uint8_t sin_table[] = { 0, 25, 50, 74, 98, 121, 142, 162, 181, 198, 213, 226, 237, 245, 251, 255, 255, }; int8_t sin_lookup( uint8_t theta ) { int sign = 1; if (theta == 0) { return 0; } else if (theta < 64) { // q1, upward slope, normal theta, positive sign } else if (theta == 64) { return 127; } else if (theta < 128) { theta = 128 - theta; // downward slope, still positive } else if (theta == 128) { return 0; } else if (theta < 192) { // q3, downward slope, negative side theta = theta - 128; sign = -1; } else if (theta == 192) { return -128; } else { // q4, upward slope, negative side theta = 256 - theta; sign = -1; } int16_t s1 = sin_table[(theta >> 2) + 0]; int16_t s2 = sin_table[(theta >> 2) + 1]; int8_t result = (s1 + ((s2 - s1) * (theta & 0x3)) / 4) / 2; fprintf(stderr, "theta=%d %d+%d => %d %d => %d\n", theta, theta >> 2, theta & 0x3, s1, s2, result ); if (sign == -1) return -result; else return result; } #if 0 int main(void) { unsigned theta; for (theta = 0 ; theta < 0x100 ; theta++) { double sx = sin_lookup(theta) / 128.0; double sf = sin(theta * 2 * M_PI / 256.0); printf("%.4f %.4f %.4f\n", sx, sf, sf - sx ); } } #endif ```