Fast Fourier Transform in C++ using KFR. Part 1

In this article I’ll show you how to use Fast Fourier Transform in Digital Signal Processing and how to apply forward and inverse FFT on complex and real data using the KFR framework.


Fast Fourier Transform (FFT) can be used to perform:

  • Convolution (including convolution reverberation)
  • Cross-correlation and auto-correlation
  • Applying large FIR filters
  • Sample rate conversion
  • Spectrum visualization
  • Large integer multiplication
  • and many other algorithms

Often FFT is the most efficient way to perform each of these algorithms.

Moreover, KFR has one of the most efficient FFT implementation, so you can get a great performance boost of your DSP applications using KFR’s FFT for all of these algorithms.

Differences from other implementations

KFR implementation of the FFT:

  • is fully optimized for X86, X86-64, ARM and AARCH64 processors
  • uses vector intrinsics (if available for cpu)
  • supports both single- and double precision
  • can cache internal data between calls to speed up plan creation
  • can do forward and inverse FFT without a need to create two plans
  • can be used for complex-to-complex, real-to-complex and complex-to-real transforms
  • doesn’t require computing FFT at runtime and measuring their execution time to find an optimal configuration
  • has special implementations for FFT sizes up to 256
  • has no external dependencies
  • is thread-safe
  • is written in modern C++
  • is header-only
  • is open source (GPL license)

Getting started

For the list of supported compilers and OSes, see the latest README:

  1. Download the latest version of KFR:

  2. Add the include folder to your include path.
  3. Insert the following line at the beginning of your project’s source code:

KFR is header-only, so no need to compile any source files, create static library and pass it to linker. The only thing you must to do is to unsure that kfr folder from include directory is available for inclusion.

Quick example

Complex input

Let’s apply the FFT to the complex input data:

To perform the inverse FFT do the following:

But as a result, we will get an input scaled by size (256 in this example): 0, 256, 512, ...
This is a property of FFT.

To get a desired result we have to divide result by size:

Real input

Frequency data are stored in CCS format as stated in the table below.

Note, that second half of an output is not stored because of symmetry.

index real imaginary
0 frequency[0].real() (DC offset) always 0 because of real input
1 frequency[1].real() frequency[1].imag()
2 frequency[2].real() frequency[2].imag()
N/2-1 frequency[N/2-1].real() frequency[N/2-1].imag()
N/2 frequency[N/2].real() (Nyquist frequency) always 0 because of real input

The size of the output data is equal to size/2+1

For the inverse FFT, you have to prepare frequency data in the CSS format as well.

To be sure that you will get pure real output, you must ensure that freq[0] and freq[N/2] are real numbers.

Creating FFT plan

Implementation of FFT requires twiddle coefficients to be prepared before actual processing occurs. If FFT will be performed more than once, then it makes sense to store the coefficients and reuse it every time.

FFT Plan caching

If you are using dft, idft, realdft or irealdft functions, all plans will be kept in memory, so the next call to these functions will reuse the saved data.

You can manually get the plan from the cache (or create a new if it doesn’t exist in the cache):

All functions related to the FFT caching are protected with mutex and are thread safe

ctype is a special template variable intended just to pass a type to the function.

dft_plan_ptr is an alias for std::shared_ptr<const dft_plan<T>>

To clear all saved plans and free memory, call the clear function:

As of version 1.3.0, functions convolve and correlate use the cache internally.

Examples using dft_plan

You can create a plan manually without using the cache to get more control over it.

dft_plan class is intended to store all data needed for FFT.

dft_plan has constructor that takes FFT size as an argument.

Input and output arguments for calls to the execute function must have types complex<T>* or univector<complex<T>, N>.

You can create a plan and use it as many times as needed. After creating a plan, all access to it is thread-safe, because executing the FFT doesn’t modify internal data,
so you can share this plan across threads in your application without protecting the plan with locks and mutexes.

Real to complex and complex to real transforms using dft_plan_real


As you can see, using the Fast Fourier Transform is not so hard thing with KFR. Forward and inverse, complex and real transforms can be performed in one line of code with great performance and flexibility.

Moreover, the speed of KFR implementation of the FFT is in many cases higher than the speed of the most known and used libraries written in C, C++ or any other language.

In the next part of the article I will show you how to perform various DSP algorithms using the FFT and the KFR framework.

Leave a Reply

Your email address will not be published. Required fields are marked *