Skip to content

Instantly share code, notes, and snippets.

@kaizhu256
Created June 5, 2012 08:08
Show Gist options
  • Save kaizhu256/2873525 to your computer and use it in GitHub Desktop.
Save kaizhu256/2873525 to your computer and use it in GitHub Desktop.
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;
{{my.Array2.rgxEach1.5}}
{{my.Array2.rgxEach1.6}} return this;
## OPTIMIZATION - cache callback
my.Array2.prototype._itpUp = function(yy) {};:
{{my.Array2.rgxEach1.1}}, dy, ii3, ll3, scale, stride3, yy0, yyNext;
{{my.Array2.rgxEach1.2}}
scale = (yy.ll2 + 0.5) / this.ll2; yyNext = my.Array2.iter0(yy, 1); yy = yy.arr;
{{my.Array2.rgxEach1.3}}
ii3 = 0;
{{my.Array2.rgxEach1.4}}
yy[yyNext()] = yy0 = arr[jj2]; ii3 += 1; if(ii2 + 1 >= ll2) {continue;}
ll3 = Math.floor((ii2 + 1) * scale); dy = (arr[jj2 + stride2] - arr[jj2]) / (ll3 - ii3);
for(0; ii3 < ll3; ii3 += 1) {yy[yyNext()] = yy0 += dy;}
{{my.Array2.rgxEach1.5}}
{{my.Array2.rgxEach1.6}} return this;
my.Array2.prototype.itp = function(yy) {};:
## same dimensions - no need to itp
if(this.ll2 === yy.ll2) {this.set(yy); return this;}
## down sample
if(this.ll2 < yy.ll2) {return this._itpDown(yy);}
## up sample
return this._itpUp(yy);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment