| 1 | # KISS FFT [](https://travis-ci.com/mborgerding/kissfft) |
| 2 | |
| 3 | KISS FFT - A mixed-radix Fast Fourier Transform based up on the principle, |
| 4 | "Keep It Simple, Stupid." |
| 5 | |
| 6 | There are many great fft libraries already around. Kiss FFT is not trying |
| 7 | to be better than any of them. It only attempts to be a reasonably efficient, |
| 8 | moderately useful FFT that can use fixed or floating data types and can be |
| 9 | incorporated into someone's C program in a few minutes with trivial licensing. |
| 10 | |
| 11 | ## USAGE: |
| 12 | |
| 13 | The basic usage for 1-d complex FFT is: |
| 14 | |
| 15 | ```c |
| 16 | #include "kiss_fft.h" |
| 17 | kiss_fft_cfg cfg = kiss_fft_alloc( nfft ,is_inverse_fft ,0,0 ); |
| 18 | while ... |
| 19 | |
| 20 | ... // put kth sample in cx_in[k].r and cx_in[k].i |
| 21 | |
| 22 | kiss_fft( cfg , cx_in , cx_out ); |
| 23 | |
| 24 | ... // transformed. DC is in cx_out[0].r and cx_out[0].i |
| 25 | |
| 26 | kiss_fft_free(cfg); |
| 27 | ``` |
| 28 | - **Note**: frequency-domain data is stored from dc up to 2pi. |
| 29 | so cx_out[0] is the dc bin of the FFT |
| 30 | and cx_out[nfft/2] is the Nyquist bin (if exists) |
| 31 | |
| 32 | Declarations are in "kiss_fft.h", along with a brief description of the |
| 33 | functions you'll need to use. |
| 34 | |
| 35 | Code definitions for 1d complex FFTs are in kiss_fft.c. |
| 36 | |
| 37 | You can do other cool stuff with the extras you'll find in tools/ |
| 38 | > - multi-dimensional FFTs |
| 39 | > - real-optimized FFTs (returns the positive half-spectrum: |
| 40 | (nfft/2+1) complex frequency bins) |
| 41 | > - fast convolution FIR filtering (not available for fixed point) |
| 42 | > - spectrum image creation |
| 43 | |
| 44 | The core fft and most tools/ code can be compiled to use float, double, |
| 45 | Q15 short or Q31 samples. The default is float. |
| 46 | |
| 47 | ## BUILDING: |
| 48 | |
| 49 | There are two functionally-equivalent build systems supported by kissfft: |
| 50 | |
| 51 | - Make (traditional Makefiles for Unix / Linux systems) |
| 52 | - CMake (more modern and feature-rich build system developed by Kitware) |
| 53 | |
| 54 | To build kissfft, the following build environment can be used: |
| 55 | |
| 56 | - GNU build environment with GCC, Clang and GNU Make or CMake (>= 3.6) |
| 57 | - Microsoft Visual C++ (MSVC) with CMake (>= 3.6) |
| 58 | |
| 59 | Additional libraries required to build and test kissfft include: |
| 60 | |
| 61 | - libpng for psdpng tool, |
| 62 | - libfftw3 to validate kissfft results against it, |
| 63 | - python 2/3 with Numpy to validate kissfft results against it. |
| 64 | - OpenMP supported by GCC, Clang or MSVC for multi-core FFT transformations |
| 65 | |
| 66 | Environments like Cygwin and MinGW can be highly likely used to build kissfft |
| 67 | targeting Windows platform, but no tests were performed to the date. |
| 68 | |
| 69 | Both Make and CMake builds are easily configurable: |
| 70 | |
| 71 | - `KISSFFT_DATATYPE=<datatype>` (for Make) or `-DKISSFFT_DATATYPE=<datatype>` |
| 72 | (for CMake) denote the principal datatype used by kissfft. It can be one |
| 73 | of the following: |
| 74 | |
| 75 | - float (default) |
| 76 | - double |
| 77 | - int16_t |
| 78 | - int32_t |
| 79 | - SIMD (requires SSE instruction set support on target CPU) |
| 80 | |
| 81 | - `KISSFFT_OPENMP=1` (for Make) or `-DKISSFFT_OPENMP=ON` (for CMake) builds kissfft |
| 82 | with OpenMP support. Please note that a supported compiler is required and this |
| 83 | option is turned off by default. |
| 84 | |
| 85 | - `KISSFFT_STATIC=1` (for Make) or `-DKISSFFT_STATIC=ON` (for CMake) instructs |
| 86 | the builder to create static library ('.lib' for Windows / '.a' for Unix or Linux). |
| 87 | By default, this option is turned off and the shared library is created |
| 88 | ('.dll' for Windows, '.so' for Linux or Unix, '.dylib' for Mac OSX) |
| 89 | |
| 90 | - `-DKISSFFT_TEST=OFF` (for CMake) disables building tests for kissfft. On Make, |
| 91 | building tests is done separately by 'make testall' or 'make testsingle', so |
| 92 | no specific setting is required. |
| 93 | |
| 94 | - `KISSFFT_TOOLS=0` (for Make) or `-DKISSFFT_TOOLS=OFF` (for CMake) builds kissfft |
| 95 | without command-line tools like 'fastconv'. By default the tools are built. |
| 96 | |
| 97 | - `KISSFFT_USE_ALLOCA=1` (for Make) or `-DKISSFFT_USE_ALLOCA=ON` (for CMake) |
| 98 | build kissfft with 'alloca' usage instead of 'malloc' / 'free'. |
| 99 | |
| 100 | - `PREFIX=/full/path/to/installation/prefix/directory` (for Make) or |
| 101 | `-DCMAKE_INSTALL_PREFIX=/full/path/to/installation/prefix/directory` (for CMake) |
| 102 | specifies the prefix directory to install kissfft into. |
| 103 | |
| 104 | For example, to build kissfft as a static library with 'int16_t' datatype and |
| 105 | OpenMP support using Make, run the command from kissfft source tree: |
| 106 | |
| 107 | ``` |
| 108 | make KISSFFT_DATATYPE=int16_t KISSFFT_STATIC=1 KISSFFT_OPENMP=1 all |
| 109 | ``` |
| 110 | |
| 111 | The same configuration for CMake is: |
| 112 | |
| 113 | ``` |
| 114 | mkdir build && cd build |
| 115 | cmake -DKISSFFT_DATATYPE=int16_t -DKISSFFT_STATIC=ON -DKISSFFT_OPENMP=ON .. |
| 116 | make all |
| 117 | ``` |
| 118 | |
| 119 | To specify '/tmp/1234' as installation prefix directory, run: |
| 120 | |
| 121 | |
| 122 | ``` |
| 123 | make PREFIX=/tmp/1234 KISSFFT_DATATYPE=int16_t KISSFFT_STATIC=1 KISSFFT_OPENMP=1 install |
| 124 | ``` |
| 125 | |
| 126 | or |
| 127 | |
| 128 | ``` |
| 129 | mkdir build && cd build |
| 130 | cmake -DCMAKE_INSTALL_PREFIX=/tmp/1234 -DKISSFFT_DATATYPE=int16_t -DKISSFFT_STATIC=ON -DKISSFFT_OPENMP=ON .. |
| 131 | make all |
| 132 | make install |
| 133 | ``` |
| 134 | |
| 135 | ## TESTING: |
| 136 | |
| 137 | To validate the build configured as an example above, run the following command from |
| 138 | kissfft source tree: |
| 139 | |
| 140 | ``` |
| 141 | make KISSFFT_DATATYPE=int16_t KISSFFT_STATIC=1 KISSFFT_OPENMP=1 testsingle |
| 142 | ``` |
| 143 | |
| 144 | if using Make, or: |
| 145 | |
| 146 | ``` |
| 147 | make test |
| 148 | ``` |
| 149 | |
| 150 | if using CMake. |
| 151 | |
| 152 | To test all possible build configurations, please run an extended testsuite from |
| 153 | kissfft source tree: |
| 154 | |
| 155 | ``` |
| 156 | sh test/kissfft-testsuite.sh |
| 157 | ``` |
| 158 | |
| 159 | Please note that the extended testsuite takes around 20-40 minutes depending on device |
| 160 | it runs on. This testsuite is useful for reporting bugs or testing the pull requests. |
| 161 | |
| 162 | ## BACKGROUND |
| 163 | |
| 164 | I started coding this because I couldn't find a fixed point FFT that didn't |
| 165 | use assembly code. I started with floating point numbers so I could get the |
| 166 | theory straight before working on fixed point issues. In the end, I had a |
| 167 | little bit of code that could be recompiled easily to do ffts with short, float |
| 168 | or double (other types should be easy too). |
| 169 | |
| 170 | Once I got my FFT working, I was curious about the speed compared to |
| 171 | a well respected and highly optimized fft library. I don't want to criticize |
| 172 | this great library, so let's call it FFT_BRANDX. |
| 173 | During this process, I learned: |
| 174 | |
| 175 | > 1. FFT_BRANDX has more than 100K lines of code. The core of kiss_fft is about 500 lines (cpx 1-d). |
| 176 | > 2. It took me an embarrassingly long time to get FFT_BRANDX working. |
| 177 | > 3. A simple program using FFT_BRANDX is 522KB. A similar program using kiss_fft is 18KB (without optimizing for size). |
| 178 | > 4. FFT_BRANDX is roughly twice as fast as KISS FFT in default mode. |
| 179 | |
| 180 | It is wonderful that free, highly optimized libraries like FFT_BRANDX exist. |
| 181 | But such libraries carry a huge burden of complexity necessary to extract every |
| 182 | last bit of performance. |
| 183 | |
| 184 | **Sometimes simpler is better, even if it's not better.** |
| 185 | |
| 186 | ## FREQUENTLY ASKED QUESTIONS: |
| 187 | > Q: Can I use kissfft in a project with a ___ license?</br> |
| 188 | > A: Yes. See LICENSE below. |
| 189 | |
| 190 | > Q: Why don't I get the output I expect?</br> |
| 191 | > A: The two most common causes of this are |
| 192 | > 1) scaling : is there a constant multiplier between what you got and what you want? |
| 193 | > 2) mixed build environment -- all code must be compiled with same preprocessor |
| 194 | > definitions for FIXED_POINT and kiss_fft_scalar |
| 195 | |
| 196 | > Q: Will you write/debug my code for me?</br> |
| 197 | > A: Probably not unless you pay me. I am happy to answer pointed and topical questions, but |
| 198 | > I may refer you to a book, a forum, or some other resource. |
| 199 | |
| 200 | |
| 201 | ## PERFORMANCE |
| 202 | (on Athlon XP 2100+, with gcc 2.96, float data type) |
| 203 | |
| 204 | Kiss performed 10000 1024-pt cpx ffts in .63 s of cpu time. |
| 205 | For comparison, it took md5sum twice as long to process the same amount of data. |
| 206 | Transforming 5 minutes of CD quality audio takes less than a second (nfft=1024). |
| 207 | |
| 208 | **DO NOT:** |
| 209 | - use Kiss if you need the Fastest Fourier Transform in the World |
| 210 | - ask me to add features that will bloat the code |
| 211 | |
| 212 | ## UNDER THE HOOD |
| 213 | |
| 214 | Kiss FFT uses a time decimation, mixed-radix, out-of-place FFT. If you give it an input buffer |
| 215 | and output buffer that are the same, a temporary buffer will be created to hold the data. |
| 216 | |
| 217 | No static data is used. The core routines of kiss_fft are thread-safe (but not all of the tools directory).[ |
| 218 | |
| 219 | No scaling is done for the floating point version (for speed). |
| 220 | Scaling is done both ways for the fixed-point version (for overflow prevention). |
| 221 | |
| 222 | Optimized butterflies are used for factors 2,3,4, and 5. |
| 223 | |
| 224 | The real (i.e. not complex) optimization code only works for even length ffts. It does two half-length |
| 225 | FFTs in parallel (packed into real&imag), and then combines them via twiddling. The result is |
| 226 | nfft/2+1 complex frequency bins from DC to Nyquist. If you don't know what this means, search the web. |
| 227 | |
| 228 | The fast convolution filtering uses the overlap-scrap method, slightly |
| 229 | modified to put the scrap at the tail. |
| 230 | |
| 231 | ## LICENSE |
| 232 | Revised BSD License, see COPYING for verbiage. |
| 233 | Basically, "free to use&change, give credit where due, no guarantees" |
| 234 | Note this license is compatible with GPL at one end of the spectrum and closed, commercial software at |
| 235 | the other end. See http://www.fsf.org/licensing/licenses |
| 236 | |
| 237 | ## TODO |
| 238 | - Add real optimization for odd length FFTs |
| 239 | - Document/revisit the input/output fft scaling |
| 240 | - Make doc describing the overlap (tail) scrap fast convolution filtering in kiss_fastfir.c |
| 241 | - Test all the ./tools/ code with fixed point (kiss_fastfir.c doesn't work, maybe others) |
| 242 | |
| 243 | ## AUTHOR |
| 244 | Mark Borgerding |
| 245 | [email protected] |