Skip to content

Instantly share code, notes, and snippets.

/fourier.js Secret

Last active August 28, 2023 20:54
  • Star 44 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save anonymous/129d477ddb1c8025c9ac to your computer and use it in GitHub Desktop.
Fourier.Transform = function(data) {
var N = data.length;
var frequencies = [];
// for every frequency...
for (var freq = 0; freq < N; freq++) {
var re = 0;
var im = 0;
// for every point in time...
for (var t = 0; t < N; t++) {
// Spin the signal _backwards_ at each frequency (as radians/s, not Hertz)
var rate = -1 * (2 * Math.PI) * freq;
// How far around the circle have we gone at time=t?
var time = t / N;
var distance = rate * time;
// datapoint * e^(-i*2*pi*f) is complex, store each part
var re_part = data[t] * Math.cos(distance);
var im_part = data[t] * Math.sin(distance);
// add this data point's contribution
re += re_part;
im += im_part;
}
// Close to zero? You're zero.
if (Math.abs(re) < 1e-10) { re = 0; }
if (Math.abs(im) < 1e-10) { im = 0; }
// Average contribution at this frequency
re = re / N;
im = im / N;
frequencies[freq] = {
re: re,
im: im,
freq: freq,
amp: Math.sqrt(re*re + im*im),
phase: Math.atan2(im, re) * 180 / Math.PI // in degrees
};
}
return frequencies;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment