Skip to content

Added cooley tukey to js #520

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions contents/cooley_tukey/code/javascript/fft.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
const complex = require('./complex');

function dft(X) {
const len = X.length;
const fft = []
for (let i = 0; i < len; ++i) {
fft[i] = complex.Complex(0,0);
for (let j = 0; j < len; ++j) {
fft[i]=fft[i].add(complex.Complex(0, -2.0 * Math.PI * i * j / len).exp().mul(X[j]));
}
}
return fft;
}

function cooleyTukey(X) {
const len = X.length;
if (len <= 1) {
return X;
}
const hlen = Math.floor(len/2); //so we dont have to redo this many times

const Xeven = [];
const Xodd = [];
for (let i = 0; i < hlen; ++i) {
Xodd[i] = X[2 * i + 1];
Xeven[i] = X[2 * i];
}
const even = cooleyTukey(Xeven);
const odd = cooleyTukey(Xodd);
const fft = [];
for (let i = 0; i < hlen; ++i) {
fft[i] = complex.Complex(0, -2 * Math.PI * i / len).exp().mul(odd[i]).add(even[i]);
fft[i+hlen] = complex.Complex(0, -2 * Math.PI * i / len).exp().mul(odd[i]).mul(-1).add(even[i]);
}
return fft;
}

function bitReverse(X) {
const len = X.length;
const lglen = Math.floor(Math.log2(len)) - 1;
for (let i = 0; i < len; ++i) {
let n = i;
let a = i;
let count = lglen;
for (n >>= 1; n > 0; n >>= 1) {
a = (a << 1) | (n & 1);
--count;
}
n = (a << count) & ((2 << lglen) - 1);
if (n > i) {
const tmp = X[i];
X[i] = X[n];
X[n] = tmp;
}
}
return X;
}

function iterativeCooleyTukey(X) {
X = bitReverse(X);
const len = X.length;

for (let i = 1; i <= Math.floor(Math.log2(len)); ++i) {
const stride = 1 << i;
const hstride = 1 << (i - 1);
const w = complex.Complex(0, -2 * Math.PI / stride).exp();
for (let j = 0; j < len; j += stride) {
let v = complex.Complex(1,0);
for (let k = 0; k < hstride; ++k) {
X[k + j + hstride] = X[k + j].sub(X[k + j + hstride].mul(v));
X[k + j] = X[k + j].mul(2).sub(X[k + j + hstride]);
v = v.mul(w);
}
}
}
return X;
}

let m=[];
for(let i=0;i<64;++i) {
m[i]=complex.Complex(1/(i+1),0);
}
let C = cooleyTukey(m);
let D = dft(m);
let I = iterativeCooleyTukey(m);
for(let i=0;i<64;++i) {
console.log(C[i]);
console.log(D[i]);
console.log(I[i]);
}
6 changes: 3 additions & 3 deletions contents/cooley_tukey/code/python/fft.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ def iterative_cooley_tukey(X):
for j in range(0, N, stride):
v = 1
for k in range(stride//2):
X[k + j + stride//2] = X[k + j] - v*X[k + j + stride//2];
X[k + j] -= (X[k + j + stride//2] - X[k + j]);
v *= w;
X[k + j + stride//2] = X[k + j] - v*X[k + j + stride//2]
X[k + j] -= (X[k + j + stride//2] - X[k + j])
v *= w
return X

X = []
Expand Down
6 changes: 6 additions & 0 deletions contents/cooley_tukey/cooley_tukey.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ For some reason, though, putting code to this transformation really helped me fi
[import:5-11, lang:"python"](code/python/fft.py)
{% sample lang="scratch" %}
[import:4-13, lang:"julia"](code/julia/fft.jl)
{% sample lang="js" %}
[import:6-19, lang:"javascript"](code/javascript/fft.js)
{% endmethod %}

In this function, we define `n` to be a set of integers from $$0 \rightarrow N-1$$ and arrange them to be a column.
Expand Down Expand Up @@ -130,6 +132,8 @@ In the end, the code looks like:
[import:13-24, lang:"python"](code/python/fft.py)
{% sample lang="scratch" %}
[import:16-32, lang:"julia"](code/julia/fft.jl)
{% sample lang="js" %}
[import:21-45, lang:"javascript"](code/javascript/fft.js)
{% endmethod %}

As a side note, we are enforcing that the array must be a power of 2 for the operation to work.
Expand Down Expand Up @@ -236,6 +240,8 @@ Note: I implemented this in Julia because the code seems more straightforward in
[import, lang:"python"](code/python/fft.py)
{% sample lang="scratch" %}
Some rather impressive scratch code was submitted by Jie and can be found here: https://scratch.mit.edu/projects/37759604/#editor
{% sample lang="js" %}
[import, lang:"javascript"](code/javascript/fft.js)
{% endmethod %}


Expand Down