vail-systems / node-fft

Pure Node.js implementation of the Fast Fourier Transform (Cooley-Tukey method).
133 stars 24 forks source link

Input type #7

Open RogerMito opened 6 years ago

RogerMito commented 6 years ago

Hello guys.

I'm tryng use the lib with an array containing vibration values (mm/s or g). For example, I have the following input: [10, 0.8285441, 0.69100095, 1.0226318, 0.7041154, 1.51439095] ... But when I try to do:

let fft2 = require('fft-js').fft;
let y = fft2(signal);
console.log(y);

Nothing happens (there is no output). I guess that the library isn't working for me. I found into README.md file the following: "The following code assumes a complex number is an array: [real, imaginary]", that way, I'll need to get the real and imaginay parts of my original input?

Could you help me?

Thanks and congratulations by the project.

borodadada commented 4 years ago

yes, i try use value from -1 to 1,

let signal = []
for (let i = 1; i < 100; i++) {
    signal.push( Math.sin( i/10 ) )
}

const phasors = fft(signal)

node_modules\fft-js\src\complex.js:24
    return [(a[0] * b[0] - a[1] * b[1]),

TypeError: Cannot read property '0' of undefined
TeHMoroS commented 4 years ago

That's because almost all implementation of FFT in JavaScript I came across have an assumption that everyone is working with integers (DSP anyone?). That very assumption makes people implement bit shifting using bit operators, which clearly don't work well with floating point numbers.

FFT requires that samples be arranged in a specific way (by bit reversal), but reversing bits isn't the only way to accomplish that. A floating-friendly way is bit reversal sorting only through simple index manipulation (it requires that the amount of items to sort is always a power of 2). A BASIC example of FFT with that bit reversal sorting is shown here (I've already reimplemented it in JS and it works nicely with floats): http://www.dspguide.com/graphics/T_12_4.gif