title: Generate sine signal in C without using the standard function
body: <p>I want to generate a sine signal in C without using the standard function sin() in order to trigger sine shaped changes in the brightness of a LED. My basic idea was to use a lookup table with 40 points and interpolation.</p>
  
  <p>Here's my first approach:</p>
  
  <pre><code>const int sine_table[40] = {0, 5125, 10125, 14876, 19260, 23170, 26509, 29196,
  31163, 32364, 32767,  32364, 31163, 29196, 26509, 23170, 19260, 14876, 10125,
  5125, 0, -5126, -10126,-14877, -19261, -23171, -26510, -29197, -31164, -32365,
  -32768, -32365, -31164, -29197, -26510, -23171, -19261, -14877, -10126, -5126};
  
  int i = 0;
  int x1 = 0;
  int x2 = 0;
  float y = 0;
  
  float sin1(float phase)
  {
      x1 = (int) phase % 41;
      x2 = x1 + 1;
      y = (sine_table[x2] - sine_table[x1])*((float) ((int) (40*0.001*i*100) % 4100)/100 - x1) + sine_table[x1];
      return y;
  }
  
  int main()
  {
      while(1)
      {
      printf("%f      ", sin1(40*0.001*i)/32768);
      i = i + 1;
      }
  }
  </code></pre>
  
  <p>Unfortunately, this function sometimes returns values far bigger than 1. Furthermore, the interpolation doesn't seem to be good (I used this to create sine shaped brightness changes of a LED, but these are very unsmoooth).</p>
  
  <p>Does anybody have a better idea to implement a sine generator in C?</p>