indexOfFrequency method

double indexOfFrequency(
  1. double freq,
  2. double samplesPerSecond
)

Returns the index in the FFT output that corresponds to the given frequency. This is the inverse of frequency.

samplesPerSecond is the sampling rate of the input signal in Hz. freq is also in Hz. The result is a double because the target freq might not exactly land on an FFT index. Decide whether to round, floor, or ceil the result based on your use case.

Implementation

double indexOfFrequency(double freq, double samplesPerSecond) {
  return freq * size / samplesPerSecond;
}