JorenSix / pffft.wasm

WebAssembly build of a small, pretty fast FFT library (PFFFT).

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

pffft.wasm: an FFT library for the web

PFFFT is a small, pretty fast FFT library with a BSD-like license. pffft.wasm make that library available in browsers and Node.js by using WebAssembly. Two builds are provided a SIMD build and on without SIMD which is slower but compatible with more WebAssembly runtimes.

This repository contains:

  • build two builds of pffft.wasm one with SIMD one without SIMD instructions. Include these in either node.js or your website.
  • examples show how to use pffft.wasm
    • browser
      • visualize.html Shows how to calculate an STFT and visualize the result on a Canvas element. Try the pffft.wasm visualization
      • benchmark.html Benchmarks pffft.wasm by analysing a 1024 sample window 100k times. Run the pffft.wasm benchmark to see the difference between the SIMD and non SIMD version on your system.
    • node shows how to call pffft.wasm from a Node.js environment.
  • media a couple of screenshots.
  • src The slightly modified source code of the original PFFFT library. The modifications change some ifdefs to enable SIMD compilation with emscripten. The runner automatically windows incoming data and exposes the functionality to the WebAssebmy side.

Why pffft.wasm?

PFFFT on WebAssembly provides a high performance, small FFT library which can be used in browsers or on Node.js. It is ideally suited for frequency analysis of audio in the browser.

This project also serves as way for me to learn about SIMD and WebAssembly. pffft.wasm is one of the more simple projects where SIMD makes an actual difference. See the benchmarks below.

STFT canvas image
Fig: An STFT calculated with pffft.wasm

Benchmark

The benchmark makes the difference between the SIMD version of pffft.wasm and the version withouth SIMD instructions clear. It is obvious that FFT analysis is a good fit for SIMD optimization. The pure Javascript FFT library FFT.js is used as a baseline. Run the pffft.wasm benchmark on your system to see it in action.

PFFFT benchmark
Fig: benchmarks on an Apple M1 Pro Chip.

Limitations

The wrapper limits window sizes powers of two between (inclusive) 32 and 4096 samples and applies a Hamming window automatically. If other window sizes or types are needed a change to the wrapper followed by recompilation is needed. Also, the time series samples are expected to be 32bit floats.

To use WebAssembly modules in browsers CORS needs to be configured correctly on your web server. A working configuration sets these headers: ‘Cross-Origin-Embedder-Policy’ => ‘require-corp’ and ‘Cross-Origin-Opener-Policy’ => ‘same-origin’. This makes it less easy to install than pure Javascript libraries. Pure Javascript FFT libraries are already quite fast and sufficient for many applications, do consider these before resorting to pffft.wasm.

Evidently, a WebAssembly runtime with SIMD instruction support is needed to get the most performant version.

PulseFFT might be of interest as well: it is a (as far as I can tell non-SIMD) WASM version of KissFFT.

Credits

  • PFFFT a pretty fast FFT library. BSD licensed.
  • Emscripten compiles c code to WebAssembly.
  • Chart.js the charting library used on the benchmark page.

pffft.wasm by Joren Six at IPEM, Ghent University.

About

WebAssembly build of a small, pretty fast FFT library (PFFFT).

License:BSD 2-Clause "Simplified" License


Languages

Language:C 99.5%Language:Makefile 0.5%