• Stars
    star
    228
  • Rank 168,935 (Top 4 %)
  • Language
    JavaScript
  • Created about 7 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

The fastest JS Radix-4/Radix-2 FFT implementation

FFT.js

Build Status NPM version

Implementation of Radix-4 FFT.

Usage

const FFT = require('fft.js');

const f = new FFT(4096);

const input = new Array(4096);
input.fill(0);

const out = f.createComplexArray();

If data has just real numbers as is the case when toComplexArray is used - real FFT may be run to compute it 25% faster:

const realInput = new Array(f.size);
f.realTransform(out, realInput);

realTransform fills just the left half of the out, so if the full spectrum is needed (which is symmetric), do the following:

f.completeSpectrum(out);

If data on other hand is a complex array:

const data = f.toComplexArray(input);
f.transform(out, data);

Inverse fourier transform:

f.inverseTransform(data, out);

Benchmarks

$ npm run bench
===== table construction =====
    fft.js x 1,426 ops/sec ±0.70% (93 runs sampled)
  Fastest is fft.js
===== transform size=2048 =====
    fft.js x 35,153 ops/sec ±0.83% (94 runs sampled)
    jensnockert x 5,037 ops/sec ±0.98% (91 runs sampled)
    dsp.js x 23,143 ops/sec ±0.64% (96 runs sampled)
    drom x 14,372 ops/sec ±0.76% (92 runs sampled)
  Fastest is fft.js
===== transform size=4096 =====
    fft.js x 15,676 ops/sec ±0.76% (94 runs sampled)
    jensnockert x 3,864 ops/sec ±1.02% (93 runs sampled)
    dsp.js x 7,905 ops/sec ±0.50% (97 runs sampled)
    drom x 6,718 ops/sec ±0.78% (96 runs sampled)
  Fastest is fft.js
===== transform size=8192 =====
    fft.js x 6,896 ops/sec ±0.79% (96 runs sampled)
    jensnockert x 1,193 ops/sec ±0.73% (94 runs sampled)
    dsp.js x 2,300 ops/sec ±0.74% (95 runs sampled)
    drom x 3,164 ops/sec ±0.67% (95 runs sampled)
  Fastest is fft.js
===== transform size=16384 =====
    fft.js x 3,123 ops/sec ±0.84% (95 runs sampled)
    jensnockert x 855 ops/sec ±1.02% (92 runs sampled)
    dsp.js x 948 ops/sec ±0.70% (94 runs sampled)
    drom x 1,428 ops/sec ±0.56% (93 runs sampled)
  Fastest is fft.js
===== realTransform size=2048 =====
    fft.js x 47,511 ops/sec ±0.93% (91 runs sampled)
    fourier-transform x 34,859 ops/sec ±0.77% (93 runs sampled)
  Fastest is fft.js
===== realTransform size=4096 =====
    fft.js x 21,841 ops/sec ±0.70% (94 runs sampled)
    fourier-transform x 16,135 ops/sec ±0.39% (93 runs sampled)
  Fastest is fft.js
===== realTransform size=8192 =====
    fft.js x 9,665 ops/sec ±0.65% (95 runs sampled)
    fourier-transform x 6,456 ops/sec ±0.83% (93 runs sampled)
  Fastest is fft.js
===== realTransform size=16384 =====
    fft.js x 4,399 ops/sec ±0.82% (93 runs sampled)
    fourier-transform x 2,745 ops/sec ±0.68% (94 runs sampled)
  Fastest is fft.js

API Details

Constructor

const FFT = require('fft.js');

const fft = new FFT(size);

NOTE: size MUST be a power of two and MUST be bigger than 1.

If you are looking to find the nearest power of 2 given the size of your dataset, here is a good tutorial

Input/Output formats and helper methods.

fft.createComplexArray()

Create an array of size equal to fft.size * 2. This array contains fft.size complex numbers whose real and imaginary parts are interleaved like this:

const complexArray = [ real0, imaginary0, real1, imaginary1, ... ];

fft.toComplexArray(realInput, /* optional */ storage)

Use provided storage or create a new complex array and fill all real slots with values from realInput array, and all imaginary slots with zeroes.

NOTE: Always provide storage for better performance and to avoid extra time in Garbage Collection.

fft.fromComplexArray(complexInput, /* optional */ storage)

Use provided storage or create an array of size fft.size and fill it with real values from the complexInput.

NOTE: Imaginary values from complexInput are ignored. This is a convenience method.

NOTE: Always provide storage for better performance and to avoid extra time in Garbage Collection.

fft.completeSpectrum(spectrum)

Fill the right half of spectrum complex array (See: fft.createComplexArray()) with the complex conjugates of the left half:

for (every every `i` between 1 and (N / 2 - 1))
  spectrum[N - i] = spectrum*[i]

NOTE: This method may be used with fft.realTransform() if full output is desired.

FFT Methods

fft.realTransform(output, input)

Take array of real numbers input and perform FFT transformation on it, filling the left half of the output with the real part of the Fourier Transform's complex output (See: fft.completeSpectrum()).

NOTE: Always use this method if the input for FFT transformation is real (has no imaginary parts). It is about 40% faster to do such transformation this way.

NOTE: input - real array of size fft.size, output - complex array (See: fft.createComplexArray()).

fft.transform(output, input)

Perform transformation on complex input array and store results in the complex output array.

NOTE: input and output are complex arrays (See: fft.createComplexArray()).

fft.inverseTransform(output, input)

Perform inverse Fourier transform on complex input array and store results in the complex output array.

NOTE: input and output are complex arrays (See: fft.createComplexArray()).

LICENSE

This software is licensed under the MIT License.

Copyright Fedor Indutny, 2017.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

elliptic

Fast Elliptic Curve Cryptography in plain javascript
JavaScript
1,583
star
2

node-ip

IP address tools for node.js
JavaScript
1,457
star
3

bn.js

BigNum in pure javascript
JavaScript
1,171
star
4

sticky-session

Sticky session balancer based on a `cluster` module
JavaScript
965
star
5

webpack-common-shake

CommonJS Tree Shaker plugin for WebPack
JavaScript
914
star
6

bud

**NOT MAINTAINED** Bud - The TLS Terminator
C
454
star
7

heartbleed

Extracting server private key using Heartbleed OpenSSL vulnerability.
C++
391
star
8

hash.js

Hash functions in pure javascript
JavaScript
312
star
9

dht.js

True bittorrent DHT in javascript
JavaScript
215
star
10

vock

VoIP on node.js
C++
182
star
11

candor

Experimental VM for a `Candor` language
C++
176
star
12

asn1.js

ASN.1 Decoder/Encoder/DSL
JavaScript
174
star
13

gyp.js

Feature-reduced port of GYP to JavaScript
JavaScript
160
star
14

common-shake

CommonJS Tree Shaker API
JavaScript
149
star
15

node-nat-upnp

NAT port mapping via UPnP
JavaScript
147
star
16

caine

Friendly butler
JavaScript
139
star
17

proof-of-work

Proof of Work with SHA256 and Bloom filter
JavaScript
112
star
18

node-index

Append-only B+ Tree index for node.js
CoffeeScript
85
star
19

wasm-jit

WebAssembly JIT
JavaScript
84
star
20

ocsp

OCSP Stapling/Checking for node.js
JavaScript
83
star
21

gradtype

WIP
Python
67
star
22

keychair

Chair and the Key
JavaScript
65
star
23

uv_link_t

Chainable libuv streams
C
63
star
24

vock-server

Server for Vock VoIP
JavaScript
58
star
25

tlsnappy

TLS, but faster!
C++
57
star
26

uv_ssl_t

Chainable SSL implementation for libuv
C
57
star
27

mmap.js

Working mmap bindings for node.js
C++
56
star
28

bitcode

Generate binary LLVM-compatible bitcode from JS
TypeScript
56
star
29

vote.wdgt

Free Proof-of-Work API for fancy Vote Counting widgets
JavaScript
54
star
30

deadbolt

Autoreleasing locks for node.js
JavaScript
54
star
31

node-netroute

Route table bindings for node.js
C++
48
star
32

wasm-ast

WebAssembly AST Parser
JavaScript
47
star
33

git-secure-tag

Secure git tag signing
JavaScript
46
star
34

heatline

Source-annotating profiler for node.js
JavaScript
46
star
35

home

My homepage
JavaScript
46
star
36

disasm

JS Disassembler
JavaScript
45
star
37

json-depth-stream

Streaming JSON parser with depth-limited auxiliary data
JavaScript
43
star
38

self-signed

Generate Self-Signed certificates in browser
JavaScript
41
star
39

hpack.js

HPACK implementation in pure JavaScript
JavaScript
39
star
40

tls.js

TLS Protocol implementation for node.js
JavaScript
39
star
41

spoon

CPS Style for JavaScript, JS->CFG->JS Transpiler
JavaScript
37
star
42

entropoetry

Entropic Poetry
JavaScript
36
star
43

dns.js

Plain javascript dns
JavaScript
36
star
44

bthread

BThread - Thread messaging and blog posting on bitcoin blockchain
JavaScript
33
star
45

bplus

Append-only B+ tree implemented in C
C
31
star
46

promise-waitlist

Promise-based Wait List for your apps
JavaScript
31
star
47

json-pipeline

JSON pipeline for a hypothetical compiler
JavaScript
30
star
48

satoshi

Satoshi Conspiracy
JavaScript
30
star
49

bar

Node.js framework for building large modular web applications.
JavaScript
29
star
50

breakdown

Trace outgoing http requests for an http server and track the time spent doing CPU intensive workload during each request.
JavaScript
29
star
51

isodrive

Isometric game engine
JavaScript
28
star
52

miller-rabin

JavaScript
28
star
53

des.js

DES algorithm
JavaScript
27
star
54

pripub

RSA encryption for node.js
C++
25
star
55

minimalistic-crypto-utils

Minimalistic utils for JS-only crypto
JavaScript
24
star
56

macho

Mach-O parser for node.js
JavaScript
23
star
57

wasm-cli

CLI for wasm-jit
JavaScript
23
star
58

node-balancer

Node load balancer
JavaScript
22
star
59

nTPL

node.js Templating system
JavaScript
22
star
60

node.gzip

!!!!!! Not-supported !!!!!!!
JavaScript
22
star
61

uv_http_t

Chainable HTTP Server implementation
C
21
star
62

hmac-drbg

JS-only Hmac DRBG implementation
JavaScript
20
star
63

hash-cracker

V8 Hash Seed timing attack
C
20
star
64

json-gpg

JSON GPG sign and verify
JavaScript
20
star
65

dukhttp

C
19
star
66

bulk-gcd

Bulk GCD in Rust
Rust
19
star
67

brorand

JavaScript
19
star
68

ninja.js

Naive `ninja` implementation
JavaScript
18
star
69

escape.js

Using escape analysis to enforce heap value lifetime in JS
18
star
70

bitcode-builder

API for building typed CFG for bitcode module
TypeScript
17
star
71

github-scan

Collecting public SSH keys using GitHub APIs
TypeScript
16
star
72

elfy

Dumb simple ELF parser
JavaScript
16
star
73

llvm-ir

LLVM IR Builder
JavaScript
16
star
74

redns

Fast and configurable DNS lookup cache
JavaScript
15
star
75

awesome64

WIP Awesome 64-bit integer implementation in JS
JavaScript
15
star
76

pyg

Not GYP
C
14
star
77

linearscan.rs

Linear scan register allocator written in Rust
Rust
14
star
78

crypto-deck

Cryptographically secure Mental Card Deck implementation
JavaScript
14
star
79

hypercorn

WIP
JavaScript
14
star
80

node-bplus

Node.js bindings for a bplus library
C++
13
star
81

mini-test.c

Minimalistic portable test runner for C projects
C
13
star
82

node-cf

Run CFRunLoop within libuv's eventloop
C++
13
star
83

assert-text

Assertion for multi-line texts when you need it
JavaScript
13
star
84

tokio-lock

Access an object from a single Tokio task
Rust
13
star
85

sneequals

Sneaky equality check between objects using proxies
TypeScript
13
star
86

huffin

Vanity ed25519 public keys, through Huffman Codes
JavaScript
12
star
87

core2dump

Core to dump
C
12
star
88

latetyper

Typing latency (OS X only)
C
11
star
89

file-shooter

File Shooter (uv_link_t, uv_ssl_t, uv_http_t)
C
11
star
90

spectrum-analyzer

FFT-based windowed spectrum analyzer
JavaScript
11
star
91

mean.engineer

An incomplete ActivityPub implementation in TypeScript
TypeScript
11
star
92

endian-reader

Small node.js helper for reading from Buffer with specified (ahead of time) endian
JavaScript
11
star
93

audio

Some realtime audio playback/recording for node.js
C++
11
star
94

stream-pair

Coupled Streams
JavaScript
11
star
95

json-pipeline-reducer

Reducer for json-pipeline
JavaScript
11
star
96

mitm.js

JavaScript
10
star
97

json-pipeline-scheduler

Scheduler for JSON-Pipeline project
JavaScript
10
star
98

wbuf

JavaScript
10
star
99

offset-buffer

JavaScript
10
star
100

reopen-tty

A small library to open /dev/tty in a cross-platform way
JavaScript
9
star