A simple and efficient FFT implementation for Dart

Related tags

Templates fftea
Overview

fftea

pub package Build Status Coverage Status

A simple and efficient Fast Fourier Transform (FFT) library.

FFT converts a time domain signal to the frequency domain, and back again. This is useful for all sorts of applications:

  • Filtering or synthesizing audio
  • Compression algorithms such as JPEG and MP3
  • Computing a spectrogram (most AI applications that analyze audio actually do visual analysis of the spectrogram)
  • Convolutions, such as reverb filters for audio, or blurring filters for images

This library supports FFT of real or complex arrays of any size. It also includes some related utilities, such as windowing functions, STFT, and inverse FFT.

Usage

Running a basic real-valued FFT:

import 'package:fftea/fftea.dart';

List<double> myData = ...;

final fft = FFT(myData.length);
final freq = fft.realFft(myData);

freq is a Float64x2List representing a list of complex numbers. See ComplexArray for helpful extension methods on Float64x2List.

Running an STFT to calculate a spectrogram:

import 'package:fftea/fftea.dart';

List<double> audio = ...;

final chunkSize = 1234;
final stft = STFT(chunkSize, Window.hanning(chunkSize));

final spectrogram = <Float64List>[];
stft.run(audio, (Float64x2List freq) {
  spectrogram.add(freq.discardConjugates().magnitudes());
});

The result of a real valued FFT is about half redundant data, so discardConjugates removes that data from the result (a common practice for spectrograms):

[sum term, ...terms..., nyquist term, ...conjugate terms...]
 ^----- These terms are kept ------^     ^- Discarded -^

Then magnitudes discards the phase data of the complex numbers and just keeps the amplitudes, which is usually what you want for a spectrogram.

If you want to know the frequency of one of the elements of the spectrogram, use stft.frequency(index, samplingFrequency), where samplingFrequency is the frequency that audio was recorded at, eg 44100. The maximum frequency (aka nyquist frequency) of the spectrogram will be stft.frequency(chunkSize / 2, samplingFrequency).

See the example for more detailed usage.

Performance Tips

If you need to squeeze every last CPU cycle out of your FFT, try these tips:

  • Construct your FFT object once, and reuse it, rather than constructing it each time you want to do an FFT.
  • Avoid unnecessary array allocations. If you need to do many FFTs, try reusing the same input array, and just overwrite the data. All the FFT methods have documentation stating whether they allocate any new arrays.
  • If you're doing something like: input data source -> manually convert to a List<double> -> pass to FFT.realFft, this is a bit inefficient. Instead do: input data source -> manually convert to a Float64x2List -> pass to FFT.inPlaceFft. This eliminates an array alocation and conversion.
  • Try to use power of two sized arrays. For example, if you're computing a spectrogram, you can usually just round up the chunk size to the next power of two. Or if you're filtering some audio, try padding your input with silence until it's a power of two, and then trimming the silence afterwards.

Technical Details

Fast Fourier Transform isn't really a single algorithm. It's a family of algorithms that all try to perform the Discreet Fourier Transform (DFT) in O(n*log(n)) time (a naive implementation of DFT is O(n^2)), where n is the size of the input array. These algorithms differ mainly by what kind of array sizes they can handle. For example, some FFT algorithms can only handle power of two sized arrays, while others can only handle prime number sized arrays.

Most FFT libraries only support power of two arrays. The reason for this is that it's relatively easy to implement an efficient FFT algorithm for power of two sizes arrays, using the Cooley-Tukey algorithm. To handle all array sizes, you need a patchwork of different implementations for different kinds of sizes.

In general, Cooley-Tukey algorithm actually works for any non-prime array size, by breaking it down into its prime factors. Powers of two just happen to be particularly fast and easy to implement.

This library handles arbitrary sizes by using Cooley-Tukey to break the size into its prime factors, and then using Rader's algorithm to handle large prime factors, or a naive O(n^2) implementation which is faster for small factors.

There's also a special Radix2FFT implementation for power of two FFTs that is much faster than the other implementations.

Rader's algorithm handles a prime numbered size, n, by transforming it into an FFT of size n - 1, which is non-prime, so can be handled by Cooley-Tukey. Alternatively, the n - 1 FFT can be zero padded up to a power two, which is usually faster because the special Radix2FFT can be used. See the primePaddingHeuristic function.

There are also a few special implementations for handling fixed sized FFTs of very small sizes. There's never really a practical use case for an FFT of size 2 or 3, but these form the base cases of Cooley-Tukey, and make larger FFTs much faster.

Check out the various implementations of FFT in lib/impl.dart for more details.

Benchmarks

This package was built because package:fft is not actively maintained anymore, isn't very efficient, and only supports power of two FFTs. This package aims to support FFTs of any size efficiently, and to make power of two FFTs particularly fast. There are a few improvements that make this Radix2FFT implementation efficient:

  • The main FFT class is constructed with a given size, so that the twiddle factors only have to be calculated once. This is particularly handy for STFT.
  • Doesn't use a wrapper class for complex numbers, just uses a Float64x2List. Every little wrapper class is a seperate allocation and dereference. For inner loop code, like FFT's complex number math, this makes a big difference. Float64x2 can also take advantage of SIMD optimisations.
  • The FFT algorithm is in-place, so no additional arrays are allocated.
  • Loop based FFT, rather than recursive.
  • Using trigonometric tricks to calculate fewer twiddle factors.
  • Bithacks
  • FFT implementations are cached by size.

I found some other promising Dart FFT implementations, so I decided to benchmark them too: scidart, and smart_signal_processing. package:fft and smart_signal_processing only support power of two arrays. Scidart supports arrays of any size, but for other sizes they use naive DFT, which is much slower. So for this first set of benchmarks I'm just testing power of two sizes.

To run the benchmarks:

  1. Go to the bench directory and pub get, cd bench && dart pub get
  2. Run dart run bench.dart

fftea gains some of its speed by caching the twiddle factors between runs, and by doing the FFT in-place. So the benchmarks are broken out into cached and in-place variants. Caching and running in-place is also applied to some of the other libraries, where appropriate.

In the table below, "cached" means the construction of the FFT object is not included in the benchmark time. And "in-place" means using the in-place FFT, and the conversion and copying of the input data into whatever format the FFT wants is not included in the benchmark. Run in Ubuntu on a Dell XPS 13.

Size package:fft smart smart, in-place scidart scidart, in-place* fftea fftea, cached fftea, in-place, cached
16 424.4 us 33.5 us 23.3 us 359.8 us 133.6 us 71.0 us 48.2 us 38.7 us
64 657.7 us 7.6 us 2.9 us 310.2 us 271.8 us 136.8 us 134.6 us 103.5 us
256 614.1 us 15.5 us 11.1 us 984.9 us 1.02 ms 100.3 us 51.9 us 38.3 us
2^10 1.74 ms 50.0 us 46.2 us 9.14 ms 9.17 ms 39.5 us 25.7 us 23.5 us
2^12 8.01 ms 219.7 us 203.1 us 133.10 ms 138.19 ms 119.8 us 109.9 us 104.8 us
2^14 39.69 ms 903.5 us 860.3 us 2.15 s 2.03 s 677.9 us 536.0 us 436.2 us
2^16 225.97 ms 5.36 ms 4.76 ms 42.53 s 42.71 s 3.43 ms 3.14 ms 2.21 ms
2^18 1.21 s 27.89 ms 25.84 ms Skipped Skipped 12.95 ms 12.53 ms 10.99 ms
2^20 7.25 s 164.35 ms 149.33 ms Skipped Skipped 89.84 ms 85.69 ms 74.99 ms

In practice, you usually know how big your FFT is ahead of time, so it's pretty easy to construct your FFT object once, to take advantage of the caching. It's sometimes possible to take advantage of the in-place speed up too, for example if you have to copy your data from another source anyway you may as well construct the flat complex array yourself. Since this isn't always possible, the "fftea, cached" times are probably the most representative. In that case, fftea is about 60-80x faster than package:fft, and about 70% faster than smart_signal_processing. Not sure what's going on with scidart, but it seems to be O(n^2) even for power of two sizes.

* Scidart's FFT doesn't have an in-place mode, but they do use a custom format, so in-place means that the time to convert to that format is not included in the benchmark.

I also benchmarked fftea's various implementations of FFT at different sizes, using bench/impl_bench.dart.

Performance of different fftea implementations

This graph shows how the different implementations perform at different sizes.

Performance of FFT.FFT constructor

This graph shows the performance of the implementation selected by the FFT.FFT constructor, which attempts to automatically pick the right implementation for the given size. Although the overall O(n*log(n)) worst case slope is maintained, there is a lot of variation between specific sizes.

Looking at the first graph, you can see that Radix2FFT is consistently the fastest, the PrimeFFT variants are the slowest, and CompositeFFT is in between. Generally, the more composite the size is, the faster the FFT will be. So if you have some flexibility in the FFT size you can choose, try to choose a highly composite size (ie, one where the prime factors are small), or ideally choose a power of two.

Comments
  • Help me with the use of STFT.

    Help me with the use of STFT.

    Hello, I'm trying to implement a function to extract MFCCs and I need to understand its function.

    I implemented this same algorithm in Javascript and it gave great results, but for that I used tensorflow. I used it like this:

    const stfts = tf.signal.stft(input, n_fft, 512);
    

    How could I use your function to do something like this?

    Please understand that I don't have much understanding on the matter.

    opened by certainlyWrong 2
  • Not an issue, just a question

    Not an issue, just a question

    I don't know about audio data / data science, but can this library produce sound from text,

    sorry this question is stupid, I'm really already dizzy looking on google

    opened by azkadev 1
  • The integer literal can't be represented exactly in JavaScript

    The integer literal can't be represented exactly in JavaScript

    Thank you a lot for this great plugin ! It works perfectly on Android, but I get this error on web :

    The integer literal 0xAAAAAAAAAAAAAAAA can't be represented exactly
    in JavaScript
    

    I was planing on replacing all those 64bits integer litterals by something along the line of this. for my app. Do you know if there would be a better way ?

    opened by alemoreau 1
  • Investigate supporting non-power-of-two FFT sizes

    Investigate supporting non-power-of-two FFT sizes

    How hard would this be? Is it worth the effort? Last time I implemented non-power-of-two sizes it was really complicated and had serious numerical precision issues. Maybe could do one of the factorization based algorithms with an O(n^2) fallback for prime sizes?

    opened by liamappelbe 1
  • Switch to faster bit reversal algorithm

    Switch to faster bit reversal algorithm

    There's a faster 64-bit reversal algorithm than what we're doing. It's O(log(bits)) instead of O(bits), using clever bit hacks:

    x = ((x >> 32) & 0x00000000ffffffff) | (x << 32);
    x = ((x >> 16) & 0x0000ffff0000ffff) | ((x & 0x0000ffff0000ffff) << 16);
    x = ((x >> 8) & 0x00ff00ff00ff00ff) | ((x & 0x00ff00ff00ff00ff) << 8);
    x = ((x >> 4) & 0x0f0f0f0f0f0f0f0f) | ((x & 0x0f0f0f0f0f0f0f0f) << 4);
    x = ((x >> 2) & 0x3333333333333333) | ((x & 0x3333333333333333) << 2);
    x = ((x >> 1) & 0x5555555555555555) | ((x & 0x5555555555555555) << 1);
    

    This is slightly different to our case because we want to reverse the bit string based on the size of the FFT. So for a 256 element FFT we want to turn 0x1 into 0x80, not 2^63. So we'll need to shift the result based on the log2 of the FFT size.

    Also rerun the benchmarks with this improvement.

    opened by liamappelbe 1
  • Propose to merge with SciDart

    Propose to merge with SciDart

    Hello! Congratulations for this great project. I'm glad that you mentioned SciDart inside of your benchmarks. I quit the Radix implementation because of complexity and the shortly time. I'm just wondering here: What you think to bring this implementation to be part of SciDart? For me make sense because SciDart was born exactly with the propose to merge all the Dart Scientific stuff.

    opened by polotto 4
  • how to convert to decibel

    how to convert to decibel

    Hi I have used FFT(); like this ... final fftx = FFT(audio.length); final freqlist = fftx.realFft(audio); ... that result is real and imagine number How i convert to Decibel ?

    opened by tawat25 8
Owner
Liam Appelbe
Liam Appelbe
🦜 Parrot - A progressive Dart framework for building efficient, reliable and scalable server-side applications.

?? Parrot A progressive Dart framework for building efficient, reliable and scalable server-side applications. What is Parrot? Parrot is a Dart framew

Odroe 8 Nov 11, 2022
Data Migrator - provide a universal translator for data by being portable, diverse, and efficient in migrating and converting data across discrete schemas

Data Migrator - provide a universal translator for data by being portable, diverse, and efficient in migrating and converting data across discrete schemas

Tanner Meade 77 Jan 2, 2023
An efficient and easy CA-Client interaction flutter project

Cloud Accounting An efficient and easy CA-Client interaction flutter project This is an application for a CA to interact with its clients. Clients can

null 1 Dec 18, 2022
UIWidget is a Unity Package which helps developers to create, debug and deploy efficient, cross-platform Apps.

⚠️ The main repository of UIWidgets has been moved to https://github.com/Unity-Technologies/com.unity.uiwidgets. Please visit the new site if you have

Unity Technologies 1.9k Dec 27, 2022
Get It - Simple direct Service Locator that allows to decouple the interface from a concrete implementation and to access the concrete implementation from everywhere in your App. Maintainer: @escamoteur

❤️ Sponsor get_it This is a simple Service Locator for Dart and Flutter projects with some additional goodies highly inspired by Splat. It can be used

Flutter Community 1k Jan 1, 2023
AI Library to create efficient Artificial Neural Networks. Computation uses SIMD (Single Instruction Multiple Data) to improve performance.

eneural_net eNeural.net / Dart is an AI Library for efficient Artificial Neural Networks. The library is portable (native, JS/Web, Flutter) and the co

null 21 Dec 29, 2022
A simple dart zeromq implementation/wrapper around the libzmq C++ library

dartzmq A simple dart zeromq implementation/wrapper around the libzmq C++ library Features Currently supported: Creating sockets (pair, pub, sub, req,

Moritz Wirger 18 Dec 29, 2022
A simple Git LFS server implementation in Dart

A simple Git LFS server implementation in Dart Overview Git LFS client (git-lfs or git lfs command) requires a dedicated LFS-content server (not the G

Hoàng Văn Khoa 4 Oct 31, 2022
Socketio dart server and client - Full Socket.io implementation using Dart Lang

Getting Started Step 1: Run dart_server.dart Step 2: Android Emulator has proble

Trần Thiên Trọng 1 Jan 23, 2022
Firebase dart common interface and implementation for Browser, VM, node and flutter

firebase.dart Firebase dart common interface and implementation for Browser, VM, node and flutter Firebase Initialization Usage in browser import 'pac

Tekartik 6 Nov 28, 2021
Simple UI design implementation of two pages.

dating_app_clone A new Flutter project. Getting Started This project is a starting point for a Flutter application. A few resources to get you started

null 4 Sep 22, 2021
Flutter firebase auth - Simple implementation of Firebase Authentication using Flutter

FlutterFire Authentication Sample A simple implementation of Firebase Authentica

Souvik Biswas 4 Apr 2, 2022
Flutter implementation for ExotikArch via a simple todos CRUD application.

ExotikArch - Flutter setState on steriods ⚡ ExotikArch for Flutter. Deliver production apps fast with flutter. Global State Management Navigation Serv

null 0 Jun 2, 2022
Simple login form implementation with dark mode in Flutter.

Login page Simple login form implementation with dark mode in Flutter. Getting Started This project is a starting point for a Flutter application. A f

Ruben Fontes 1 Jul 4, 2022
Flutter plugin that provides a quick&dirty workaround for incompatibility between VMWare Airwatch MDM solution and Dart Sockets implementation

airwatch_socket_workaround This plugin has been created to enable flutter apps to reach endpoints that are only reachable using a VMWare airwatch per

Gonçalo Silva 5 Nov 11, 2022
ESP-Touch Dart API for Flutter. Platform-specific implementation for Android (Java) and iOS (Objective-C).

esptouch_flutter Flutter plugin for ESP-Touch to configure network for ESP-8266 and ESP-32 devices. Runs on iOS and Android. esptouch_flutter is Flutt

SMAHO Engineering OSS 86 Dec 10, 2022
Implementation of data structures and algorithms in Dart programming language.

Algorithms in Dart Implementation of several algorithms with Dart programming language. Use dartdoc to generate documentation. Lists List data structu

Mafinar Khan 197 Dec 24, 2022
An intuitive Token Parser that includes grammar definition, tokenization, parsing, syntax error and debugging. Implementation based on Lexical Analysis for Dart.

Token Parser An intuitive Token Parser that includes syntax/grammar definition, tokenization and parsing. Implementation based on Lexical Analysis. Re

JUST A SNIPER ツ 2 Dec 15, 2022