I have used the following:
public static double Index2Freq(int i, double samples, int nFFT) {
return (double) i * (samples / nFFT / 2.);
}
public static int Freq2Index(double freq, double samples, int nFFT) {
return (int) (freq / (samples / nFFT / 2.0));
}
The inputs are:
i
: Bin to accesssamples
: Sampling rate in Hertz (i.e. 8000 Hz, 44100Hz, etc.)nFFT
: Size of the FFT vector