How do I apply FFT onto an audio recording to get a frequency?

503 views Asked by At

This is supposed to be for an android app, so the language in question is obviously Java. I'm trying to record some audio and get the dominant frequency. This is for a very specific purpose, and the frequencies I need to be detected are pure sounds made by another device. I have the recording part done, so the only thing that I need to do is calculate the frequency from the buffer it generates.

I know I'm supposed to use something called FFT, so I put these into my project: http://introcs.cs.princeton.edu/java/97data/FFT.java, and http://introcs.cs.princeton.edu/java/97data/Complex.java.html

I know there are many questions about this, but none of them give an answer that I can understand. Others have broken links.

Anyone know how to do this, and explain in a relatively simple manner?

2

There are 2 answers

10
John Thoits On BEST ANSWER

Generally a DFT (FFT included) implementation will take N time-domain samples (your recording) and produce N/2 complex values in the frequency domain. The angle of the complex value represents the phase and the absolute value of it represents the amplitude. Usually the values output will be ordered from lowest frequency to highest frequency.

Some implementations may output N complex values, but the extra values are redundant unless your input contains complex values. It should not in your case. This is why many implementations input real values and output N/2 complex values, as this is the most common use of FFT.

So, you will want to calculate the absolute value of the output since the amplitude is what you are interested in. The absolute value of a complex number is the square root of the sum of the square of it's real and the square of it's complex component.

The exact frequencies of each value will depend on the number of samples of input and the interval between the samples. The frequency of value at position i (assuming i goes from 0 to N/2 - 1) will be i * (sampling frequency) / N.

This is assuming your N is even, rather than trying to explain the case of N being odd I'll recommend you keep N even for simplicity. For the case of FFT N will always be a power of two so N will always be even anyway.

If you're looking for a tone over a minimum time T then I'd also recommend processing the input in blocks of T/2 size.

0
duffymo On

Fourier transforms are a mathematical technique that lets you go back and forth between time and frequency domains for time-dependent signals.

FFT is a computer algorithm for calculating discrete transforms quickly and efficiently.

You'll take a sample of your time signal and apply FFT to it to get the amplitude versus frequency for the sample.

It's not an easy topic if you don't have the mathematical background. It assumes a good knowledge of trigonometry (sines and cosines), functions, and calculus. If you don't have that, it'll be difficult to read and understand any reference you can find.

If you don't have that background, do your best to treat a library FFT function as a black box and use what it gives back.