Skip to content

FFT Frequency Detector

March 16, 2012
tags: ,

While doing some research on making a timestretch/pitchshift effect in Reaktor, I came across a method that can be used to calculate the fundamental frequency of an audio signal using FFT and some simple math. The algorithm is simple – we compute the FFT of a signal, find the bin index with the highest amplitude, do a bit of math, and we’re done!  EzFFT will take care of the FFT for us. Here’s my method for finding the index with the highest amplitude:

Frequency Detector

This macro accepts the three outputs from the ezFFT 512 module, and outputs the bin index with the highest amplitude, and the amp and phase difference of that bin. At Index = 0, it resets the amplitude of bin 0 to be the highest value. Then any incoming amplitude higher than that value replaces it until the last bin, 511, at which point the values are read out.

For our purposes, neither the phase nor the amplitude is of much concern, but the phase difference (meaning the change in phase from the last FFT bin with the same index value) is of utmost importance. A couple of calculations using the phase difference and the bin index will give us the real frequency of the input (or a very good approximation). First, the frequency center of a bin, can be defined as

Fc = Index * (sample rate / FFT size)

or, in Reaktor,

Center frequency of an FFT bin

Next, the real frequency can be calculated as

F = Fc + (phase difference * (sample rate / (2π * FFT window size) ) )

The phase difference, according to the text, must be wrapped between the values -π and π. Unfortunately, I found this method did not work for half of the bin indexes. However, I found a simple fix was to wrap between the values of  -1/2π and 1/2π instead (for this macro, I simply lifted the 2Pi Wrap macro out of the factory cos function, and changed all the constant values from 2π to π). Translating the above function to Reaktor is simple – we’ve already calculated Fc, and the sample rate, window size and 2π are constants that can be plugged in beforehand:

Calculate the real frequency

It make look strange the I mentioned using a ezFFT 512 module above, yet here I’m defining the FFT size as 1024. The reason for this is that the ezFFT 512 is actually composed of two 1024-bin FFT modules. However, the last 512 bins of each module is a mirror image of the first 512, and also defines frequencies above the Nyquist, so we don’t need to worry about them, and ezFFT helpfully trims them for us. The window size is defined as 512, I admit to using trial and error to arrive at this figure (although the only other candidate was 1024, so it wasn’t too tough).

While this method does introduce a few milliseconds of latency, it is quite a bit more accurate than most others I have seen. I hope you find it useful.

Download the Freq Detector here.

2 Comments leave one →
  1. meter maid permalink
    March 18, 2012 3:42 pm

    This is really brilliant. I found a great use for it already. The left input is the carrier and the right input is the modulator (guts taken from Lilthree from the UL) It sounds cool when a voice sample is going into both inputs.


  1. Pitchshifting with FFT « salamanderangram

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: