Calculate Frequency from sound input using FFT

杀马特。学长 韩版系。学妹 提交于 2019-12-20 04:55:33

问题


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 :)


回答1:


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.



来源:https://stackoverflow.com/questions/16060134/calculate-frequency-from-sound-input-using-fft

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!