fft-napi
v1.1.0
Published
Fast Fourier Transform for Float32Array using a native Node-API addon
Readme
fft-napi
Native FFT/IFFT for Node.js using Node-API (N-API).
Install
npm installUsage
var fft = require('fft-napi').fft;
var ifft = require('fft-napi').ifft;
// Input is 1+0i 2+0i 3+0i 4+0i
var input = new Float32Array([1, 0, 2, 0, 3, 0, 4, 0]);
var spectrum = new Float32Array(input.length);
var restored = new Float32Array(input.length);
fft(input, spectrum, function (err, result) {
if (err) throw err;
// result === spectrum
ifft(spectrum, restored, function (err2, restoredResult) {
if (err2) throw err2;
// restoredResult === restored
});
});API
fft(input, output, callback)
Forward complex FFT.
ifft(input, output, callback)
Inverse complex FFT (normalized by 1/N).
ifft applies the inverse transform and scales by 1/N internally, so if your
input to ifft came from fft, then ifft(fft(x)) reconstructs x (within
floating-point tolerance).
Example:
var fft = require('fft-napi').fft;
var ifft = require('fft-napi').ifft;
var time = new Float32Array([1, 0, 2, 0, 3, 0, 4, 0]);
var freq = new Float32Array(time.length);
var restored = new Float32Array(time.length);
fft(time, freq, function (err) {
if (err) throw err;
ifft(freq, restored, function (err2, out) {
if (err2) throw err2;
// out === restored
// restored is approximately equal to time
});
});For both functions:
input:Float32Arraycontaining interleaved complex values[re0, im0, re1, im1, ...]output:Float32Arrayto receive output values in the same layoutcallback:(err, result)called asynchronously when done
Rules:
inputandoutputmust beFloat32Arrayinput.length === output.length- length must be even (pairs of real/imaginary values)
- number of complex samples (
length / 2) must be a power of two
Test
npm test