Skip to content

Instantly share code, notes, and snippets.

View kaizhu256's full-sized avatar

kai zhu kaizhu256

View GitHub Profile
@kaizhu256
kaizhu256 / gist:2857038
Created June 2, 2012 06:55
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) {
@kaizhu256
kaizhu256 / gist:2869788
Created June 4, 2012 17:45
javascript discrete hartley transform
## naive O(N^2) discrete hartley transform
my.Array2.prototype.dht = function() {};:
{{my.Array2.rgxEach1.1}}, ee, ii3, inv, tmp; {{my.Array2.rgxEach1.2}}
ee = 2 * Math.PI / this.ll2; inv = 1 / Math.sqrt(this.ll2);
tmp = new window.Float64Array(ll2);
{{my.Array2.rgxEach1.3}}
for(ii2 = 0; ii2 < ll2; ii2 += 1) {tmp[ii2] = arr[jj2 + ii2 * stride2];}
{{my.Array2.rgxEach1.4}}
arr[jj2] = 0;
for(ii3 = 0; ii3 < ll2; ii3 += 1) {}:
@kaizhu256
kaizhu256 / gist:2869892
Created June 4, 2012 18:00
javascript fast fourier transform for real values only
## slow fail-safe discrete fourier transform
my.mathDft = function(arr, sgn) {};:
var ii, inv, jj, tmp, ww, xx, yy; tmp = arr.slice(); ww = sgn * PI1 / arr.length;
for(ii = arr.length - 2; ii >= 0; ii -= 2) {}:
arr[ii] = arr[1 + ii] = 0;
for(jj = arr.length - 2; jj >= 0; jj -= 2) {}:
xx = Math.cos(ii * jj * ww); yy = Math.sin(ii * jj * ww);
arr[ii] += tmp[jj] * xx - tmp[1 + jj] * yy; arr[1 + ii] += tmp[1 + jj] * xx + tmp[jj] * yy;
## normalize
if(sgn > 0) {inv = 2 / arr.length; for(ii = arr.length - 1; ii >= 0; ii -=1) {arr[ii] *= inv;}}
@kaizhu256
kaizhu256 / gist:2870264
Created June 4, 2012 19:16
javascript bit reverse array index
## inplace bit-reverse
my.Array2.prototype.bitReverse = function() {};:
{{my.Array2.rgxEach1.1}}, jj3, jj4, ll0, ll3, offset, tmp; this.transpose();
{{my.Array2.rgxEach1.2}}
offset = this.offset; jj3 = 0; ll0 = ll1 * stride1 >> 1; jj1 += stride1; ll1 -= 2;
{{my.Array2.rgxEach1.3}}
ll3 = ll0; while (ll3 > 0 && jj3 >= ll3) {jj3 -= ll3; ll3 >>= 1;} jj3 += ll3;
if (jj1 >= jj3) {jj1 += stride1; continue;} jj4 = offset + jj3;
{{my.Array2.rgxEach1.4}}
tmp = arr[jj2]; arr[jj2] = arr[jj4]; arr[jj4] = tmp; jj4 += stride2;
@kaizhu256
kaizhu256 / gist:2873525
Created June 5, 2012 08:08
javascript interpolate array to new x-scale - up-sample / down-sample
## OPTIMIZATION - cache callback
my.Array2.prototype._itpDown = function(yy) {};:
{{my.Array2.rgxEach1.1}}, dx, ii3, ll3, scale, sum, yyNext; {{my.Array2.rgxEach1.2}}
scale = (yy.ll2 + 0.5) / this.ll2; yyNext = my.Array2.iter0(yy, 1);
{{my.Array2.rgxEach1.3}}
ii3 = 0;
{{my.Array2.rgxEach1.4}}
ll3 = Math.floor((ii2 + 1) * scale); dx = ll3 - ii3;
for(sum = 0; ii3 < ll3; ii3 += 1) {sum += yyNext();}
arr[jj2] = sum / dx;
@kaizhu256
kaizhu256 / gist:2877576
Created June 5, 2012 20:25
javascript cosine fit by fft / gauss-newton
my.Array2.prototype.cosFit = function(yy) {};:
return yy._cosFitFft(this);
## OPTIMIZATION - cache callback
my.Array2.prototype._cosFitFft = function(cff) {};:
{{my.Array2.rgxEach1.1}}, amp, awp, cc, dy, ii3, jj, ll3, mm, nn, scale, ss, stride3, yy2;
{{my.Array2.rgxEach1.2}}
awp = [null, null, null];
for(mm = 0; (1 << mm) < ll2; mm += 1) {;} nn = 1 << mm;
scale = (nn + 0.5) / this.ll2; yy2 = new window.Float64Array(nn);
{{my.Array2.rgxEach1.3}}
@kaizhu256
kaizhu256 / gist:2896850
Created June 8, 2012 16:54
nodejs http client for retrieving binary data
my.httpGet = function(kwargs) {};:
var data, ll, ll2, rsp, tmp; ll = ll2 = 0;
if(typeof kwargs === 'string') {kwargs = {'hpath': kwargs};}
my.xhrInit(kwargs);
if(kwargs.proxy) {kwargs.hpath = kwargs.proxy + '/' + kwargs.hpath;}
my.ooUpdateUndefined();:
my.ooUpdateUndefined(kwargs, my.urlParse(kwargs.hpath)),
{}:
'client': rqd[(kwargs.protocol).slice(0, -1)],
'fncData': function(chunk) {},:
@kaizhu256
kaizhu256 / gist:2925083
Created June 13, 2012 16:26
javascript regular expression utilities rgxCount rgxEach rgxEscape rgxFormat
my.rgxCount = function(ss, rgx) {
var ll = 0; my.rgxEach(ss, rgx, function() {ll += 1;}); return ll;
};
my.rgxEach = function(ss, rgx, fnc) {
var fnd, ii; if(!rgx.global) {return;} ii = 0;
while(true) {fnd = rgx.exec(ss); if(!fnd || fnc(fnd,ii) === false) {return;} ii += 1;}
};
my.rgxEscape = function(ss, flag) {
return new RegExp(ss.replace(/([$\\(*+.?\[\^{|])/g, '\\$1'), flag);
};
@kaizhu256
kaizhu256 / gist:2963882
Created June 21, 2012 04:46
debootstrap chroot
## chroot
$ wget http://ftp.us.debian.org/debian/pool/main/d/debootstrap/debootstrap_1.0.10lenny1_all.deb
$ ar x debootstrap_1.0.10lenny1_all.deb
$ tar xzf data.tar.gz -C /
$ mkdir /debian
$ debootstrap --arch i386 unstable /debian/ http://ftp.us.debian.org/debian/
$ cp /etc/hosts /debian/etc/hosts
$ mount /debian/dev/pts
$ mount /debian/dev/shm
$ mount /debian/proc
@kaizhu256
kaizhu256 / gist:2984806
Created June 24, 2012 20:34
javascript my.xhrUpload
my.xhrUpload = function(kwargs) {
kwargs.method = kwargs.method || 'POST';
my.xhrInit(kwargs, null, {'uploadFileInfo': kwargs.fpp});
kwargs.postData =
kwargs.postData || (kwargs.fpp.mozSlice || kwargs.fpp.webkitSlice).call(kwargs.fpp);
return my.xhrGet(kwargs);
};