In this article I’m going to explain how to calculate a real-number based FFT using FFTW, providing the source code. First, though, I will introduce some important concepts.
Then I will provide code with comments within it. I will also provide a header containing sample data from a single channel sound file whose frames have been converted into double values. The samples are of a 440 Hz sine tone mixed with noise.
I exported the raw sound data from Audacity, and used the od program (GNU coreutils), to convert the raw data to double precision floating point numbers, and outputted that into a file that I could reformat into the header, “a_with_noise.h”.
Thanks Jake for pointing me in the right direction, providing sample code for computing a complex number FFT.
- Programming explanations and examples are for the C programming language.
- You understand what a FFT is used for.
- You have installed FFTW3 on your system.
- You are using a GNU/Linux distribution.
This value is used to help determine the frequency bin size. For the sample data, the audio was originally sampled at 44100 times per second or 44100 Hz.
In the source code I’ve set ‘N’ to 4096, because it’s evenly divisible by two.
Frequency Bin Size
This is equal to the sampling rate divided by ‘N’, or 44100 divided by 4096.
These numbers consist of two parts: a real and imaginary component. By splitting them into two, it allows us to perform algebraic operations on it that normally couldn’t work with real numbers.
In C, we have a standard header, “complex.h”, which defines a complex type.
Get the rest of the file from here: https://gist.githubusercontent.com/ben-cottrell-nz/d2a55a105fa49842da00291ddfa22625/raw/62e30668928ac5666da0510a67586c8f48d0bd49/a_with_noise.h
Make sure you have built FFTW3 and installed it. When I tested this code, I used the CMake build system generator, but for the sake of brevity you could simply compile and test this code by executing the following text in your terminal emulator:
mkdir build cd build gcc -o fft_test ../fft.c -I.. -lfftw3 -lm
If the compiler finished with no errors, run:
Here’s a CMakeLists.txt file as well:
cmake_minimum_required(VERSION 3.5) project(fft_test LANGUAGES C) set(CMAKE_C_STANDARD 11) set(CMAKE_INCLUDE_CURRENT_DIR ON) add_executable(fft_test fft.c) target_link_libraries(fft_test m fftw3)
I hope this gets you closer to being able to compute real-number FFT’s with FFTW3. If you have any questions about this, please leave them below.