title: An algorithm to detect a certain frequency?
body: <p>Using Goertzel algorithm I am detecting a certain frequency that goes into the iphone microphone (I use buffer samples). </p>
  
  <p>Its working, but it has many strange stability problems when values are changed. (they are constant at a spectrum app on same device,but not with Goertzel algorithm )</p>
  
  <p>I would like to use another way, in <code>C</code> , to detect a certain frequency, or the energy in a range of frequencies, (I don't know if <strong>FFT</strong> is good and accurate, if yes I need a good algorithm).
  If you have a function that ONLY gets samples and length and return energy at a spectrum of frequencies, or at a certain known frequency, that can help. 
  I need a serious one, maybe a second order filter.</p>
  
  <p>This is my Goertzel :</p>
  
  <pre><code>float goertzel_mag(int16_t* data ,int SAMPLING_RATE ,double TARGET_FREQUENCY,int numSamples )
  {
      int     k,i;
      float   floatnumSamples;
      float   omega,sine,cosine,coeff,q0,q1,q2,magnitude,real,imag;
  
      float   scalingFactor = numSamples / 2.0; // -2
  
      floatnumSamples = (float) numSamples;
      k = (int) (0.5 + ((floatnumSamples * TARGET_FREQUENCY) / SAMPLING_RATE));
      omega = (2.0 * M_PI * k) / floatnumSamples;
      sine = sin(omega);
      cosine = cos(omega);
      coeff = 2.0 * cosine;
      q0=0;
      q1=0;
      q2=0;
  
      for(i=0; i&lt;numSamples; i++)
      {
          q0 = coeff * q1 - q2 + data[i];
          q2 = q1;
          q1 = q0;
      }
  
  
      real = (q1 - q2 * cosine) / scalingFactor;
      imag = (q2 * sine) / scalingFactor;
  
      //double theta = atan2 ( imag, real); //PHASE
      magnitude = sqrtf(real*real + imag*imag);
      return magnitude;
  }
  </code></pre>