4

I'm developing a kind of guitar tuner.

I have a function that gives me the FFT, and the values of the FFt for each frequency.

How do I get the musical note from there? Do I have to chose the highest peak?

 for(y=0; y<maxY; y++){


    CGFloat yFract = (CGFloat)y / (CGFloat)(maxY - 1);          

    CGFloat fftIdx = yFract * ((CGFloat)fftLength);
    double fftIdx_i,fftIdx_f;
    fftIdx_f = modf(fftIdx, &fftIdx_i);

    SInt8 fft_l, fft_r;
    CGFloat fft_l_fl, fft_r_fl;
    CGFloat interpVal;

    fft_l = (fftData[(int)fftIdx_i] & 0xFF000000) >> 24;
    fft_r = (fftData[(int)fftIdx_i + 1] & 0xFF000000) >> 24;


    fft_l_fl = (CGFloat)(fft_l + 80) / 64.;
    fft_r_fl = (CGFloat)(fft_r + 80) / 64.;
    interpVal = fft_l_fl * (1. - fftIdx_f) + fft_r_fl * fftIdx_f;
    interpVal = CLAMP(0., interpVal, 1.);


    drawBuffers[0][y] = (interpVal * 120);
    //NSLog(@"The magnitude for %f Hz is %f.", (yFract * hwSampleRate * .5), (interpVal * 120));

}

Thanks a lot if you can help.

Julien.

Neva
  • 1,340
  • 9
  • 11
  • There are already a lot of questions and answers on SO about pitch detection for musical instruments using FFT etc - you might want to go back and read some of these as they cover a lot of the "FAQs" for this kind of task. It seems a lot of people are trying to write guitar tuners for iPhone and other mobile devices... – Paul R Mar 16 '11 at 12:49

1 Answers1

3

This is a non-trivial problem, for several reasons:

  • The peak may not correspond to the fundamental harmonic (it may even be missing).
  • The fundamental harmonic will probably not land precisely on the centre of an FFT bin, so its energy will be spread across multiple bins. You would need to do interpolation to estimate the actual frequency.
  • Unless you perform some kind of windowing, you will get "spectral leakage" effects, which will smear your spectrum all over the place, making it hard to discern details.

I appreciate that this doesn't really answer your question, but it should highlight the fact that this is actually a pretty tricky thing to do well.

Oliver Charlesworth
  • 252,669
  • 29
  • 530
  • 650
  • Thanks for the quick answer. I'm already applying a weighting window. How do you find the fundamental harmonic? – Neva Mar 16 '11 at 12:25
  • "You would need to do interpolation" + "you will get "spectral leakage" " = difficult problem. Have a look at "slepian windowing" for robust spectral estimation. – Alexandre C. Mar 16 '11 at 13:42