Question

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;
}
Was it helpful?

Solution

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?

OTHER TIPS

Apple supplies the Accelerate Framework.
Accelerate Framework here
FFT here

It includes vDSP and several FFT routines including the categories:
1D Fast Fourier Transforms (Fixed Length)
1D Fast Fourier Transforms (In-Place Complex)
1D Fast Fourier Transforms (In-Place Real)
1D Fast Fourier Transforms (Out-of-Place Complex)
1D Fast Fourier Transforms (Out-of-Place Real)
1D Fast Fourier Transforms (Support Functions)
2D Fast Fourier Transforms (In-Place Complex)
2D Fast Fourier Transforms (In-Place Real)
2D Fast Fourier Transforms (Out-of-Place Complex)
2D Fast Fourier Transforms (Out-of-Place Real)
Discrete Fourier Transforms

The Accelerate Framework is all to often overlooked.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top