Using Goertzel algorithm I am detecting a certain frequency that goes into the iphone microphone (I use buffer samples).
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 )
I would like to use another way, in C
, to detect a certain frequency, or the energy in a range of frequencies, (I don't know if FFT 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.
This is my Goertzel :
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<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;
}
This is a good description of the various approaches to frequency detection.
As you're developing on MacOSX or iOS, check out the Accelerate Framework which provides portable [within iOS and MacOSX] and optimised implementation of the DFT.
If you intend to release your work as an app in AppStore, you should pay particular attention to licensing. Is FFTW license compatible with your usage?