Skip to content

Instantly share code, notes, and snippets.

@kaizhu256
Created June 2, 2012 06:55
Show Gist options
  • Save kaizhu256/2857038 to your computer and use it in GitHub Desktop.
Save kaizhu256/2857038 to your computer and use it in GitHub Desktop.
javascript complex fft
my.Complex2.prototype.fft = function(mode) {
var aa, cc, ee, ee0, ii, ii1, ii2, jj, jj2, kk, ll1, ll2, mm, nn, nn1, nn2, offset,
self, ss, stride1, stride2, tt1, tt2, xx, yy;
self = this[0]; ll1 = self.ll1; ll2 = self.ll2; offset = self.offset;
stride1 = self.stride1; stride2 = self.stride2; xx = this[0].arr; yy = this[1].arr;
//// inplace bit-reverse
jj = 0; nn = ll2 * stride2; nn2 = nn >> 1;
for (ii = stride2; ii < nn - stride2; ii += stride2) {
nn1 = nn2; while (jj >= nn1) {jj -= nn1; nn1 >>= 1;} jj += nn1;
if (ii < jj) {
ii2 = ii + offset; jj2 = jj + offset;
for(ii1 = 0; ii1 < ll1; ii1 += 1) {
tt1 = xx[ii2]; xx[ii2] = xx[jj2]; xx[jj2] = tt1;
tt1 = yy[ii2]; yy[ii2] = yy[jj2]; yy[jj2] = tt1;
ii2 += stride1; jj2 += stride1;
}
}
}
//// inplace fft
ee0 = -6.283185307179586; if(mode === 'reverse') {ee0 = -ee0; this.mul(1 / ll2);}
for(mm = 0; (1 << mm) < ll2; mm += 1) {;} nn1 = 0; nn2 = stride2;
for (ii = 0; ii < mm; ii += 1) {
nn1 = nn2; nn2 <<= 1; ee = ee0 * (stride2 / nn2); aa = 0.0;
for (jj = 0; jj < nn1; jj += stride2) {
cc = Math.cos(aa); ss = Math.sin(aa); aa += ee;
for (kk = jj; kk < nn; kk += nn2) {
ii2 = kk + offset; jj2 = kk + nn1 + offset;
for(ii1 = 0; ii1 < ll1; ii1 += 1) {
tt1 = cc * xx[jj2] - ss * yy[jj2]; tt2 = ss * xx[jj2] + cc * yy[jj2];
xx[jj2] = xx[ii2] - tt1; yy[jj2] = yy[ii2] - tt2; xx[ii2] += tt1; yy[ii2] += tt2;
ii2 += stride1; jj2 += stride1;
}
}
}
}
return this;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment