README.md
1# KFR - Fast, modern C++ DSP framework
2
3<p align="center">
4 <img width="300" height="auto" src="img/KFR1.png">
5</p>
6
7
8![Build Status](https://img.shields.io/azure-devops/build/dlevin256/dlevin256/1/master.svg?style=flat-square)
9[![Gitter](https://img.shields.io/gitter/room/kfrlib/kfr.svg?maxAge=2592000&style=flat-square)](https://gitter.im/kfrlib/kfr) ![License](https://img.shields.io/github/license/kfrlib/kfr.svg?style=flat-square)
10
11Compiler support:
12
13![Clang 6+](https://img.shields.io/badge/Clang-6%2B-brightgreen.svg?style=flat-square)
14![Xcode 9+](https://img.shields.io/badge/Xcode-9%2B-brightgreen.svg?style=flat-square)
15![GCC 7+](https://img.shields.io/badge/GCC-7%2B-brightgreen.svg?style=flat-square)
16![MSVC 2017](https://img.shields.io/badge/MSVC-2017%2B-brightgreen.svg?style=flat-square)
17
18https://www.kfr.dev
19
20KFR is an open source C++ DSP framework that focuses on high performance (see benchmark results section).
21
22KFR has no external dependencies except C++17-compatible standard C++ library.
23
24Some C++17 library features will be emulated if not present in the standard library.
25
26# Features
27
28## What's new in KFR 4.0
29
30* IIR filter design
31 * Butterworth
32 * Chebyshev type I and II
33 * Bessel
34 * Lowpass, highpass, bandpass and bandstop filters
35 * Conversion of arbitrary filter from Z,P,K to SOS format (suitable for biquad function and filter)
36* Discrete Cosine Transform type II (and its inverse, also called DCT type III)
37* cmake uninstall target (thank to [@acxz](https://github.com/acxz))
38* C API: DFT, real DFT, DCT, FIR and IIR filters and convolution, memory allocation
39 * Built for SSE2, SSE4.1, AVX, AVX2, AVX512, x86 and x86_64, architecture is selected at runtime
40 * Can be used with any compiler and any language with ability to call C functions
41 * Windows binaries will be available soon
42* C++17
43 * Inline variables
44 * Fold expressions
45 * Structured binding
46* New vector based types: color, rectangle, point, size, border, geometric vector, 2D matrix
47* Color space conversion (sRGB, XYZ, Lab, LCH)
48* MP3 file reading (using third party dr_lib library, see source code for details)
49* Various optimizations and fixes (thank to [@bmanga](https://github.com/bmanga), [@ncorgan](https://github.com/ncorgan), [@rotkreis](https://github.com/rotkreis), [@mujjingun](https://github.com/mujjingun) for fixes and bug reports)
50
51### Release notes
52
53* DFT is limited to Clang due to ICE in MSVC and broken AVX optimization in GCC 8 and 9. Once fixed, support will be added
54
55## What's new in KFR 3.0
56
57* Optimized non-power of two DFT implementation
58* GCC 7+ support
59* MSVC 2017 support
60* AVX-512 support (MSVC and Clang, GCC has incomplete support of AVX-512 instrinsics)
61* EBU R128
62* Ability to include KFR as a subdirectory in cmake project
63* Ability to link objects built for multiple architectures into one binary
64* Number of automatic tests has been increased
65* C API for DFT
66* GPL version changed from 3 to 2+
67
68## All features
69
70* All code in the library is optimized for Intel, AMD (SSE2, SSE3, SSE4.x, AVX and AVX2 and AVX512) and ARM (NEON) processors
71* Mathematical and statistical functions
72* Template expressions (See examples)
73* All data types are supported including complex numbers
74* All vector lengths are also supported. `vec<float,1>`, `vec<unsigned,3>`, `vec<complex<float>, 11>` all are valid vector types in KFR
75* Most of the standard library functions are re-implemented to support vector of any length and data type
76* Runtime cpu detection
77
78### Included DSP/audio algorithms:
79
80* FFT
81* Convolution
82* FIR filtering
83* FIR filter design using the window method
84* Resampling with configurable quality (See resampling.cpp from Examples directory)
85* Goertzel algorithm
86* Fractional delay
87* Biquad filtering
88* Biquad design functions
89* Oscillators: Sine, Square, Sawtooth, Triangle
90* Window functions: Triangular, Bartlett, Cosine, Hann, Bartlett-Hann, Hamming, Bohman, Blackman, Blackman-Harris, Kaiser, Flattop, Gaussian, Lanczos, Rectangular
91* Audio file reading/writing
92* Pseudorandom number generator
93* Sorting
94* Ring (Circular) buffer
95* Simple waveshaper
96* Fast incremental sine/cosine generation
97* EBU R128
98
99# Benchmark results
100## DFT
101
102### KFR 3.0.1
103
104Powers of 2, from 16 to 16777216 (*Higher is better*)
105
106![FFT Performance](img/powers2_double_16_16777216.png)
107
108Prime numbers from 17 to 127 (*Higher is better*)
109
110![DFT Performance](img/primes_double_17_127.png)
111
112Small numbers from 18 to 119 (*Higher is better*)
113
114![DFT Performance](img/composite_double_18_119.png)
115
116Random sizes from 120 to 30720000 (*Higher is better*)
117
118![DFT Performance](img/extra_double_120_30720000.png)
119
120See [fft benchmark](https://github.com/kfrlib/fft-benchmark) for details about benchmarking process.
121
122
123## Biquad
124
125 (*Higher is better*)
126
127![Biquad Performance](img/biquad.svg)
128
129# Usage
130
131## Common prerequisites
132
133* CMake 3.0 or newer for building tests and examples
134* Python 2.7 or 3.x for running examples
135* (Optional) Ninja (https://ninja-build.org/)
136
137For running examples and plotting frequency responses of filters the following python packages are required:
138
139```bash
140pip install matplotlib
141pip install numpy
142pip install scipy
143```
144Or download prebuilt python packages for windows
145
146To obtain the full code, including examples and tests, you can clone the git repository:
147
148```
149git clone https://github.com/kfrlib/kfr.git
150```
151
152## Building KFR C API
153
154### Windows
155
156These commands must be executed in MSVC2017 command prompt
157
158```bash
159cd <path_to_kfr_repository>
160mkdir build && cd build
161cmake -GNinja -DENABLE_CAPI_BUILD=ON -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_COMPILER="<PATH_TO_LLVM_DIR>/bin/clang-cl.exe" ..
162ninja kfr_capi
163```
164
165### Linux, macOS, other
166
167```bash
168cd <path_to_kfr_repository>
169mkdir build && cd build
170cmake -GNinja -DENABLE_CAPI_BUILD=ON -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_COMPILER=clang++ ..
171ninja kfr_capi
172```
173
174#### ArchLinux Package
175KFR is available on the [ArchLinux User Repository](https://wiki.archlinux.org/index.php/Arch_User_Repository) (AUR).
176You can install it with an [AUR helper](https://wiki.archlinux.org/index.php/AUR_helpers), like [`yay`](https://aur.archlinux.org/packages/yay/), as follows:
177
178```bash
179yay -S kfr
180```
181To discuss any issues related to this AUR package refer to the comments section of
182[`kfr`](https://aur.archlinux.org/packages/kfr/).
183
184Prebuilt binaries will be available soon.
185
186## Including in CMake project
187
188CMakeLists.txt contains these libraries:
189* `kfr` - header only interface library
190* `kfr_dft` - static library for DFT and related algorithms
191* `kfr_io` - static library for file IO and audio IO
192
193```cmake
194# Include KFR subdirectory
195add_subdirectory(kfr)
196
197# Add header-only KFR to your executable or library, this sets include directories etc
198target_link_libraries(your_executable_or_library kfr)
199
200# Add KFR DFT to your executable or library, (cpp file will be built for this)
201target_link_libraries(your_executable_or_library kfr_dft)
202
203# Add KFR IO to your executable or library, (cpp file will be built for this)
204target_link_libraries(your_executable_or_library kfr_io)
205```
206
207## Makefile, command line etc (Unix-like systems)
208
209```bash
210# Add this to command line
211-Ipath_to_kfr/include
212
213# And this if needed
214-lkfr_dft -lkfr_io
215
216# C++17 mode must be enabled
217-std=c++17
218# or
219-std=gnu++17
220```
221
222## Linux
223
224### Prerequisites
225* GCC 7 or newer
226* Clang 6.0 or newer
227
228### Command line
229```bash
230cd <path_to_kfr>
231mkdir build && cd build
232cmake -DENABLE_TESTS=ON -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_BUILD_TYPE=Release ..
233make -- -j
234```
235Or using Ninja
236```bash
237cd <path_to_kfr>
238mkdir build && cd build
239cmake -GNinja -DENABLE_TESTS=ON -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_BUILD_TYPE=Release ..
240ninja
241```
242
243## macOS
244
245### Prerequisites
246* XCode 9.x, 10.x or 11.x
247
248### Command line
249Using Xcode project:
250```bash
251cd <path_to_kfr>
252mkdir build && cd build
253cmake -GXcode -DENABLE_TESTS=ON -DCMAKE_BUILD_TYPE=Release ..
254cmake --build .
255```
256Using Unix Makefiles:
257```bash
258cd <path_to_kfr>
259mkdir build && cd build
260cmake -G"Unix Makefiles" -DENABLE_TESTS=ON -DCMAKE_BUILD_TYPE=Release ..
261make -- -j
262```
263Or using Ninja:
264```bash
265cd <path_to_kfr>
266mkdir build && cd build
267cmake -GNinja -DENABLE_TESTS=ON -DCMAKE_BUILD_TYPE=Release ..
268ninja
269```
270
271## Visual Studio
272
273### Prerequisites
274* Visual Studio 2017
275* Latest Clang (http://llvm.org/)
276* Ninja is highly recommended because Visual Studio does not support parallel build with Clang at this moment.
277
278### Visual Studio IDE
279
280To work with KFR in Visual Studio you must add the path to the `include` directory inside KFR directory to the list of the project's include directories.<br>
281More details:
282https://docs.microsoft.com/en-us/cpp/ide/vcpp-directories-property-page?view=vs-2017
283
284Make sure that LLVM toolset is set for the project<br>
285
286Download and install official LLVM extension:
287* LLVM toolchain for Visual Studio https://marketplace.visualstudio.com/items?itemName=LLVMExtensions.llvm-toolchain
288
289More details:
290https://docs.microsoft.com/en-us/cpp/ide/general-property-page-project?view=vs-2017
291
292LLVM/Clang has very good compatibility with MSVC ABI and it's widely used for building large projects on Windows (including Chrome), so switching to LLVM/Clang should not cause compatibility problems.
293
294### Command line
295Using Ninja:
296```
297cd <path_to_kfr>
298mkdir build && cd build
299call "C:\<path to your Visual Studio installation>\VC\Auxiliary\Build\vcvars64.bat"
300cmake -GNinja -DENABLE_TESTS=ON -DCMAKE_CXX_COMPILER="C:/Program Files/LLVM/bin/clang-cl.exe" -DCMAKE_CXX_FLAGS=-m64 -DCMAKE_BUILD_TYPE=Release ..
301ninja
302```
303Or generate Visual Studio solution (building will be slower):
304```
305cd <path_to_kfr>
306mkdir build && cd build
307cmake -G"Visual Studio 15 2017 Win64" -DENABLE_TESTS=ON -Tllvm -DCMAKE_BUILD_TYPE=Release ..
308```
309
310## MinGW/MSYS
311
312### Prerequisites
313* Latest MinGW or MSYS2
314* Clang 6.0 or newer
315
316Using Makefiles:
317```
318cd <path_to_kfr>
319mkdir build && cd build
320cmake -DENABLE_TESTS=ON -DCMAKE_BUILD_TYPE=Release ..
321make -- -j
322```
323Using Ninja:
324```
325cd <path_to_kfr>
326mkdir build && cd build
327cmake -GNinja -DENABLE_TESTS=ON -DCMAKE_BUILD_TYPE=Release ..
328ninja
329```
330
331## Documentation
332
333Documentation home:
334
335https://www.kfrlib.com/newdocs/
336
337Built with
338* [cxxdox - generates markdown docs from C++](https://github.com/kfrlib/cxxdox)
339* [mkdocs - static documentation generator](https://www.mkdocs.org/)
340* [mkdocs-material - material theme for mkdocs](https://squidfunk.github.io/mkdocs-material/)
341
342## Branches
343
344`dev` - current development version. Pull requests should go to `dev`.
345
346`master` - current stable version passing all compiler/architecture tests.
347
348
349## Tests
350
351Every commit is tested in various OS, compilers, compiler settings etc.
352
353You can run the tests using these commands:
354
355```bash
356cd <path_to_cmake_build_directory>
357cd tests
358ctest -V
359```
360
361Tested on the following systems:
362
363
364### macOS
365* (**Intel AVX2**) macOS **10.13.6** / Xcode 10 / AppleClang 10.0.0.10001145
366* (**Intel AVX** Azure Pipelines) macOS **10.13.6** / Xcode 10.1 / AppleClang 10.0.0.10001145
367* (**Intel AVX** Azure Pipelines) macOS **10.13.6** / Xcode 10 / AppleClang 10.0.0.10001145
368* (**Intel AVX** Azure Pipelines) macOS **10.13.6** / Xcode 9.4.1 / AppleClang 9.1.0.9020039
369* (**Intel AVX** Azure Pipelines) macOS **10.13.6** / Xcode 9.0.1 / AppleClang 9.0.0.9000038
370* (**Intel AVX** Azure Pipelines) macOS **10.13.6** / Xcode 8.3.3 / AppleClang 8.1.0.8020042
371* (**Intel AVX2**) macOS **10.11.6** / Xcode 7.3 / AppleClang 7.3.0.7030031
372* (**Intel AVX2**) macOS **10.11.4** / Xcode 7.3 / AppleClang 7.3.0.7030031
373* (**ARMv7, ARMv7s, ARM64**) macOS **10.11.6** / Xcode 7.3 / AppleClang 7.3.0.7030031
374* (**Intel AVX**) macOS **10.10.5** / Xcode 7.1 / AppleClang 7.0.0.7000176
375* (**SSE4.2** Travis-CI) macOS **10.11.6** / Xcode 8 (beta4) / AppleClang 8.0.0.8000035
376* (**SSE4.2** Travis-CI) macOS **10.11.5** / Xcode 7.3 / AppleClang 7.3.0.7030031
377* (**SSE4.2** Travis-CI) macOS **10.11.5** / Xcode 7.2 / AppleClang 7.0.2.7000181
378* (**SSE4.2** Travis-CI) macOS **10.10.5** / Xcode 7.1 / AppleClang 7.0.0.7000176
379* (**SSE4.2** Travis-CI) macOS **10.10.5** / Xcode 7 / AppleClang 7.0.0.7000072
380* (**SSE4.2** Travis-CI) macOS **10.10.5** / Xcode 6.4 / AppleClang 6.1.0.6020053
381* (**SSE4.2** Travis-CI) macOS **10.10.3** / Xcode 6.3 / AppleClang 6.1.0.6020049
382
383### Ubuntu
384* (**Intel AVX2**) Ubuntu **18.04** / gcc-7.x / clang version 7.0.0 (tags/RELEASE_700/final)
385* (**Intel AVX2**) Ubuntu **16.04** / gcc-5.4.0 / clang version 3.8.0 (tags/RELEASE_380/final)
386* (**ARMv7 NEON**) Ubuntu **16.04** / gcc-5.4.0 / clang version 3.8.0 (tags/RELEASE_380/final)
387* (**ARMv7 NEON**) Ubuntu **14.04** / gcc-4.8.4 / clang version 3.8.0 (tags/RELEASE_380/final)
388* (**ARMv7 NEON** Travis-CI) Ubuntu **14.04** / gcc-4.8.4 / clang version 3.8.0 (tags/RELEASE_380/final)
389* (**Intel AVX2** Travis-CI) Ubuntu **12.04** / gcc-5.4.0 / clang version 3.8.0 (tags/RELEASE_380/final)
390* (**Intel AVX2** Travis-CI) Ubuntu **14.04** / gcc-5.3.0 (Ubuntu 5.3.0-3ubuntu1~14.04) 5.3.0 20151204 / clang version 3.8.0 (tags/RELEASE_380/final)
391
392### Windows
393* (**Intel AVX512**) Windows **10** / Visual Studio 2017 / Clang 7.0
394* (**Intel AVX512**) Windows **10** / Visual Studio 2017 / Clang 6.0
395* (**Intel AVX2**) Windows **10** / MinGW-W64 5.2 / clang version 3.8.0 (branches/release_38)
396* (**Intel AVX2**) Windows **10** / MinGW-W64 4.8 / clang version 3.8.0 (branches/release_38)
397* (**Intel AVX**) Windows **8.1** / MinGW-W64 5.4 / clang version 3.8.0 (branches/release_38)
398* (**Intel AVX**) Windows **8.1** / Visual Studio 2015 Update 2 / clang version 3.9.0 (SVN r273898 (27 June 2016))
399
400### Linux on Windows 10
401* (**Intel AVX2**) Windows **10.0.17134.407** compatible with Ubuntu **18.04** / gcc-7.x / clang version 7.0.0 (tags/RELEASE_700/final)
402* (**Intel AVX2**) Windows **10.0.14393** compatible with Ubuntu **14.04** / gcc-5.4.0 / clang version 3.8.0 (tags/RELEASE_380/final)
403
404## Planned for future versions
405
406* Parallel execution of algorithms
407* Serialization/Deserialization of any expression
408* More formats for audio file reading/writing
409
410## License
411
412KFR is dual-licensed, available under both commercial and open-source GPL 2+ license.
413
414If you want to use KFR in commercial product or a closed-source project, you need to [purchase a Commercial License](https://kfr.dev/purchase-license)
415