Ana Mandic
Ana Mandic

Reputation: 151

FFT on the 8 bit PCM signal

I'm using this fast fourier transform implementation in node-js: https://www.npmjs.com/package/fft-js.

I am using wav reader which reads my wav file which is encoded as 8 bit PCM and outputs data as an array of 8 bit unsigned integers.

I see that fft-js expects signal values from -1 to 1 as seen in this example of it's usage:

var fft = require('fft-js').fft,
    signal = [1,0,1,0];

var phasors = fft(signal);

console.log(phasors);

What should I do? Should I convert my 8 bit pcm representation of the wav file to values between -1 and 1, and if so how?

Upvotes: 0

Views: 1186

Answers (1)

mikefrey
mikefrey

Reputation: 4681

According to this article on Wikipedia, you should be able to take the 8bit uint data and map it to a number between -1 and 1 with something similar to this:

let arrForFFT = uint8Array.map(num => (num - 128) / 128)

If my uint8Array looks like this:

[ 256, 192, 128, 64, 0]

Then arrForFFT would look like this:

[ 1, 0.5, 0, -0.5, -1]

Edit: If you're not using ES2015, the code would look like this:

var arrForFFT = uint8Array.map(function(num) {
  return (num - 128) / 128
})

Upvotes: 2

Related Questions