vail-systems / node-fft

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

Can't get phasors from Buffer #3

Open VolodymyrTymets opened 7 years ago

VolodymyrTymets commented 7 years ago

I need get frequencies of .wav file. I can read file via fs

   fs.readFile('output.wav', (err, buffer) => { 
    var phasors= fft(buffer); // exception here
   })

Exception

/node_modules/fft-js/src/complex.js:24
    return [(a[0] * b[0] - a[1] * b[1]),
TypeError: Cannot read property '0' of undefined

Is this possible to get phasors from Buffer? Or exist another way to get phasors from .wav file?

c0d3runn3r commented 7 years ago

I'm also getting that error:

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

TypeError: Cannot read property '0' of undefined

Source code (works if n==8, but not if n==10):

const fft=require("fft-js");

let input=[];
let n=10;
for(let m=0; m<n; m++) {

    input.push(Math.sin(m*2*Math.PI/n));
}

let phasors=fft.fft(input);
console.log(input);
console.log(phasors);
aprowe commented 7 years ago

I also get this, and I am first converting the Buffer to an Array, via Array.prototype.slice.call(buffer,0) which makes an array of 1.5 million integers.

aprowe commented 7 years ago

oh, fft only takes arrays that are a power of two of length.