Created
June 5, 2013 08:54
-
-
Save emilisto/5712587 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ node scope.js | |
6Hz: 4048 | |
214Hz: 4048 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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