0

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;
}
Leigh
  • 28,765
  • 10
  • 55
  • 103
Curnelious
  • 1
  • 16
  • 76
  • 150
  • What is your specific question here? – Oliver Charlesworth Jan 01 '13 at 15:16
  • 1
    FFT is good; accuracy depends on your requirements, understanding of the algorithm, and the physics of your problem. – duffymo Jan 01 '13 at 15:18
  • my question was, to get a function ,a single function , that gets samples, and produce FFT. not just to suggest me the FFT, but a code sample of the same kind of a function that i showed here . i could not find one like that . – Curnelious Jan 01 '13 at 15:53
  • @Rant: If you need code for an FFT, then a Google search will help you; there are examples all over the place. Alternatively, you could just use an existing library, e.g. [FFTW](http://www.fftw.org/). – Oliver Charlesworth Jan 01 '13 at 16:03
  • 1
    @rant If you're going for an FFT approach, you'll need a fair amount of downstream processing of the results. This is a good description of how to use a SFFT to estimate frequency in each bin. There's quite a bit more to it - for instance dealing with bleed between bins and the noise floor. You'll also find real-world signals where the fundamental is missing, and you'll need to reconstruct this from the partials- in much the same way as the human auditory system is capable of. – marko Jan 01 '13 at 16:45
  • 1
    If you know the frequency who's magnitude you want to measure, the Goertzel algorithm is the right choice: FFT is computational overkill. However, I'm not clear from your description exactly what the problem was. You could also use a bandpass filter, but the order (and type) of the filter would depend on your requirements. This post might or might not help you: http://blog.bjornroche.com/2012/07/frequency-detection-using-fft-aka-pitch.html – Bjorn Roche Jan 02 '13 at 02:00
  • Your main problem is that you are not using the Goertzel algorithm correctly - you need to process the output rather than just using it "raw". – Paul R Jan 02 '13 at 07:53

2 Answers2

2

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?

marko
  • 9,029
  • 4
  • 30
  • 46
2

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.

zaph
  • 111,848
  • 21
  • 189
  • 228
  • What is the best method to calculate frequency by peak power? – arbel03 Dec 10 '15 at 15:07
  • I can't really help with that. – zaph Dec 10 '15 at 15:30
  • Ok, do you maybe know what parameters of AVAudioRecorder I need to pass to calculate a FFT of frequency? – arbel03 Dec 10 '15 at 15:34
  • The problem is I have used those routines only once several years ago, my experience was years prior and I had to write my own based on the *Numerical Recipes* book. Also although I have substantial audio experience when I was using FFTs it was in the medical realm. – zaph Dec 10 '15 at 16:04
  • I see, thanks anyways :) – arbel03 Dec 10 '15 at 16:12