Skip to content

Instantly share code, notes, and snippets.

@antimatter15
Last active January 15, 2024 20:53
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antimatter15/0349ca7d479236fdcdbb to your computer and use it in GitHub Desktop.
Save antimatter15/0349ca7d479236fdcdbb to your computer and use it in GitHub Desktop.
Small Cooley-Tukey radix-2 DIT FFT in Javascript

MiniFFT

MiniFFT takes two arrays (real, imag) as arguments and transforms them in-place. It only works for arrays which are powers-of-two, and imag and real must be the same length.

var re = [1, 2, 3, 4],
    im = [0, 0, 0, 0];
miniFFT(re, im);
console.log(re, im); // [ 10, -2, -2, -1.9999999999999998 ] [ 0, 2, 0, -2 ]

The minified version (minifft.min.js) is only 359 bytes in length.

The implementation is based loosely on http://introcs.cs.princeton.edu/java/97data/InplaceFFT.java.html and http://www.nayuki.io/res/free-small-fft-in-multiple-languages/fft.js

Unlike https://gist.github.com/wrayal/995571, it doesn't need a special complex number library (so that one's closer to 542b+139b). It's also using a non-recursive variant of Cooley-Tukey which has to mean something— right?

MiniDCT

This is an implementation of a Type-II DCT using MiniFFT. For details about Makhoul's algorithm, see this post http://dsp.stackexchange.com/a/10606

It's surprisingly hard to find an implementation of the DCT which isn't using the naive O(n^2) approach.

Like MiniFFT, it operates in-place (well, not really, but it writes its output to the source array).

var arr = [3, 4, 1, 7];
miniDCT(arr);
console.log(arr); // [ 30, -5.094935665899755, 7.0710678118654755, -8.604744653988439 ]

It produces the same output as scipy's fftpack, but Matlab's DCT uses orthogonal normalization and produces a different result. However, it's pretty simple to do it the Matlab way— just scale everything by 1/sqrt(2 * N) and divide the first element by sqrt(2).

MiniIDCT

What DCT is complete without a corresponding IDCT? Here's an implementation of the Type-III DCT (aka the venerable IDCT), modeled after http://www.idlcoyote.com/tip_examples/idct.pro. The awkwardness of this name really almost compels me to rename these functions from Mini to Tiny.

Copyright (c) 2015 Kevin Kwok (antimatter15@gmail.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

function miniDCT(s){
var N = s.length,
K = -Math.PI / (2 * N),
re = new Float64Array(N),
im = new Float64Array(N);
for(var i = 0, j = N; j > i; i++){
re[i] = s[i * 2]
re[--j] = s[i * 2 + 1]
}
miniFFT(re, im)
for(var i = 0; i < N; i++)
s[i] = 2*re[i]*Math.cos(K*i)-2*im[i]*Math.sin(K*i);
}
function miniFFT(re, im) {
var N = re.length;
for (var i = 0; i < N; i++) {
for(var j = 0, h = i, k = N; k >>= 1; h >>= 1)
j = (j << 1) | (h & 1);
if (j > i) {
re[j] = [re[i], re[i] = re[j]][0]
im[j] = [im[i], im[i] = im[j]][0]
}
}
for(var hN = 1; hN * 2 <= N; hN *= 2)
for (var i = 0; i < N; i += hN * 2)
for (var j = i; j < i + hN; j++) {
var cos = Math.cos(Math.PI * (j - i) / hN),
sin = Math.sin(Math.PI * (j - i) / hN)
var tre = re[j+hN] * cos + im[j+hN] * sin,
tim = -re[j+hN] * sin + im[j+hN] * cos;
re[j + hN] = re[j] - tre; im[j + hN] = im[j] - tim;
re[j] += tre; im[j] += tim;
}
}
function miniIDCT(s){
var N = s.length,
K = Math.PI / (2 * N),
im = new Float64Array(N),
re = new Float64Array(N);
re[0] = s[0] / N / 2;
for(var i = 1; i < N; i++){
var im2 = Math.sin(i*K), re2 = Math.cos(i*K);
re[i] = (s[N - i] * im2 + s[i] * re2) / N / 2;
im[i] = (im2 * s[i] - s[N - i] * re2) / N / 2;
}
miniFFT(im, re)
for(var i = 0; i < N / 2; i++){
s[2 * i] = re[i]
s[2 * i + 1] = re[N - i - 1]
}
}
@bt1159
Copy link

bt1159 commented Jan 15, 2024

This doesn't seem to work right, or am I messing it up? I added a series for re & im for a sin wave with period of 1/8th the total sample. I used 32 sample points. When I take the magnitude of the outputs (sum of squares of re & im), I should see a big spike at the fourth one (index = 3) and that's it, but instead I see spikes at index = 6 & 26, which makes no sense. Am I doing something wrong?

var re = [0,0.95105652,0.58778525,-0.58778525,-0.95105652,0,0.95105652,0.58778525,-0.58778525,-0.95105652,0,0.95105652,0.58778525,-0.58778525,-0.95105652,0,0.95105652,0.58778525,-0.58778525,-0.95105652,0,0.95105652,0.58778525,-0.58778525,-0.95105652,0,0.95105652,0.58778525,-0.58778525,-0.95105652,0,0.95105652];
var im = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];

function magnitudes(re,im) {
	var output = [];
	var N = re.length;
	for (var i = 0; i < N; i++) {
		var temp = Math.sqrt((re[i])**2 + (im[i])**2);
		output.push(temp);
	}	
	return output;
}

After running, the output is:

[
    0.95105652,
    0.9903507212512948,
    1.1202168363792175,
    1.3882805034550416,
    1.9386307372652554,
    3.3453544010232936,
    11.965427388642583,
    8.242618372034885,
    3.2212799438455266,
    2.0804703594888956,
    1.586868219374433,
    1.3194887491588165,
    1.1586733518854844,
    1.057898071337986,
    0.9958176309116408,
    0.9618743106851068,
    0.95105652,
    0.9618743106851065,
    0.9958176309116397,
    1.0578980713379857,
    1.1586733518854844,
    1.319488749158817,
    1.586868219374434,
    2.080470359488895,
    3.2212799438455266,
    8.242618372034883,
    11.965427388642581,
    3.3453544010232945,
    1.9386307372652554,
    1.3882805034550425,
    1.1202168363792175,
    0.990350721251295
]

Any thoughts?

By the way, hereis what the data points look like when charted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment