Skip to content

Instantly share code, notes, and snippets.

@emilisto
Created June 5, 2013 08:54
Show Gist options
  • Save emilisto/5712587 to your computer and use it in GitHub Desktop.
Save emilisto/5712587 to your computer and use it in GitHub Desktop.
$ node scope.js
6Hz: 4048
214Hz: 4048
var ndarray = require('ndarray');
var fft = require('ndarray-fft');
var baseFrequencey = 220;
var floats = new Float64Array(8096);
for (var i = 0; i < floats.length; i++) {
// floats[i] = (i/floats.length) * Math.sin(baseFrequencey * i / floats.length * 2 * Math.PI);
floats[i] = Math.sin(baseFrequencey * i / floats.length * 2 * Math.PI);
}
var reals = ndarray(floats, [ floats.length, 1 ]);
var imns = ndarray.zeros([ floats.length, 1 ]);
fft(1, reals, imns);
var abs = function(re, im) {
return Math.sqrt(Math.pow(re, 2) + Math.pow(im, 2));
};
for (var i = 0; i < reals.data.length; i++) {
var amplitude = abs(reals.data[i], imns.data[i]),
freq = baseFrequencey * i / floats.length;
if(amplitude > 0.0001) {
console.log('%dHz: %d', freq.toFixed(0), amplitude.toFixed(2));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment