0

My app. is displaying the peak frequency of input sound in RPM .. i have array of doubles contains the samples in time domain.

audioRecord.read(buffer, 0, 1024);

Then i did FFT on it .

transformer.ft(toTransform);

using this class Here

then i got the max magnitude of complex values which are the results of FFT

// block size = 1024

double magnitude[] = new double[blockSize / 2];

            for (int i = 0; i < magnitude.length; i++) {
                double R = toTransform[2 * i] * toTransform[2 * i];
                double I = toTransform[2 * i + 1] * toTransform[2 * i * 1];

                magnitude[i] = Math.sqrt(I + R);
            }
            int maxIndex = 0;
            double max = magnitude[0];
            for(int i = 1; i < magnitude.length; i++) {
                if (magnitude[i] > max) {
                    max = magnitude[i];
                    maxIndex = i;
                }
            }

Now i got the index of the max magnitude ... 1 - How can i get the Peak Frequency in details pls ? 2 - Is there any ready function called ComputeFrequency() or getFrequency() ? Thanks in advance :)

Paul R
  • 208,748
  • 37
  • 389
  • 560
Fareed
  • 560
  • 2
  • 7
  • 23

1 Answers1

0

The frequency corresponding to a given FFT bin index is given by:

f = i * Fs / N;

where:

Fs = sample rate (Hz)
N = FFT size
i = bin index

So for your peak index maxIndex and FFT size blockSize the frequency of the peak will be:

f = maxIndex * Fs / blockSize;

See this answer for more details.

Community
  • 1
  • 1
Paul R
  • 208,748
  • 37
  • 389
  • 560
  • Great Thanks for your reply and helping me, Is it right or should i use RealDoubleFFT class to FFT transform ? – Fareed Apr 17 '13 at 15:29
  • this is the link for RealDoubleFFT class [link](http://herschel.esac.esa.int/hcss-doc-10.0/load/hcss_drm/api/herschel/ia/numeric/toolbox/xform/util/RealDoubleFFT.html) – Fareed Apr 17 '13 at 15:43
  • please give me an answer .. this is the last hint in this project i think :) – Fareed Apr 17 '13 at 15:45
  • It doesn't really matter what FFT you use, so long as it works and the performance is acceptable. – Paul R Apr 17 '13 at 16:11
  • Is there any way to test results ? – Fareed Apr 17 '13 at 16:42
  • Sure - create a sine wave at a known frequency, pass it through your FFT and peak finding code, then check that you get the expected frequency. Note also that you should apply a [window function](http://en.wikipedia.org/wiki/Window_function#Hann_.28Hanning.29_window) prior to your FFT. – Paul R Apr 17 '13 at 16:55
  • sorry, can you give me an example about how to apply this sine in java ? – Fareed Apr 17 '13 at 17:01
  • Just create a buffer of the same type and size that you use for input currently, then fill it with samples of a sine wave (e.g. `buff[i] = a * sin(2 * pi * f * i / fs);` where `a` is the amplitude, `f` is the frequency, `fs` is the sample rate) – Paul R Apr 17 '13 at 17:03