title: How to work with complex numbers in C without the complex.h library in a non-C99 compliant program?
body: <p>I'm trying to create a Discrete Fourier Transform in C for a university project, which is a guitar tuner. The university basically forces us to use CodeVisionAVR because it works well with the ATmega168p they gave us. CVAVR works only in C, and is non-C99 compliant, which means <code>&lt;complex.h&gt;</code>, will not work. If i am to create a vector of let's say 800 samples for the guitar tuner, the DFT requires working with complex numbers, respectively with the real and imaginary parts of the input.</p>
  
  <p>This is the code that i'm trying to modify for the task.</p>
  
  <pre><code>float read_voltage(unsigned char channel) 
  {
      channel &amp;= 0b00000111;
      ADMUX = ADMUX_NOCHANNEL | channel;
      ADCSRA |= 0b01000000; 
      while (ADCSRA &amp; 0b01000000); 
      ADCSRA |= 0b00010000; 
      return (float)(ADCH) * 0.0275;
  }
  
  #define M_PI 3.14159265358979323486
  #define N 800
  
  float computeDft()
  {
      // time and frequency domain data arrays 
      float x[N];    
      float X[N];
      float Xre[N] = {0};
      float Xim[N] = {0}; 
      size_t n = sizeof(x);
      int k;            
      int t;
      int i = 0;   
      while(i&lt;N)
      {
          x[i] = read_voltage(0b00000000);         
          i++;
      }
  
      for (k = 0; k &lt; n; k++) 
      {  // For each output element
          for (t = 0; t &lt; n; t++) 
          {  // For each input element
              double angle = 2 * M_PI * t * k / n;
              Xre[t] +=  Xre[t] * cos(angle) + Xim[t] * sin(angle);
              Xim[t] += -Xre[t] * sin(angle) + Xim[t] * cos(angle);
          }    
          X[k] = Xre[k] + Xim[k]; 
      }   
      return X[k];
  }
  </code></pre>
  
  <p>The problem is that X[k] is actually equal to the complex Xre[k] + jXim[k], not just adding the 2 numbers together.</p>