Skip to content

Rfft inverse transform implemented #5

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
186 changes: 186 additions & 0 deletions dsp.js
Original file line number Diff line number Diff line change
@@ -539,6 +539,7 @@ function RFFT(bufferSize, sampleRate) {
FourierTransform.call(this, bufferSize, sampleRate);

this.trans = new Float32Array(bufferSize);
this.itrans = new Float32Array(bufferSize);

// don't use a lookup table to do the permute, use this instead
this.reverseBinPermute = function (dest, source) {
@@ -569,6 +570,37 @@ function RFFT(bufferSize, sampleRate) {
} while (i < halfSize);
dest[nm1] = source[nm1];
};

// don't use a lookup table to do the permute, use this instead
// the inverse transform needs to do this in place so we have this
this.reverseBinPermuteInPlace = function (buf) {
var bufferSize = this.bufferSize,
halfSize = bufferSize >>> 1,
nm1 = bufferSize - 1,
i = 1, r = 0, h, t;

do {
r += halfSize;
t = buf[i];
buf[i] = buf[r];
buf[r] = t;

i++;

h = halfSize << 1;
while (h = h >> 1, !((r ^= h) & h));

if (r >= i) {
t = buf[i];
buf[i] = buf[r];
buf[r] = t;
t = buf[nm1-i];
buf[nm1-i] = buf[nm1-r];
buf[nm1-r] = t;
}
i++;
} while (i < halfSize);
};
}


@@ -584,6 +616,11 @@ function RFFT(bufferSize, sampleRate) {
// trans[n/2+2] = im[n/2-2]
// ...
// trans[n-1] = im[1]
//
// note before using RFFT.trans you need to scale it, however since [0-1]
// is frequently not the range you want, or you often want to work with it in
// some other way we leave it unscaled for speed (this way we make one fewer
// pass, if you're willing to remeber to scale it yourself.

RFFT.prototype.forward = function(buffer) {
var n = this.bufferSize,
@@ -748,6 +785,8 @@ RFFT.prototype.forward = function(buffer) {
}
}

// NOTE: for extra speed we could split this step out since 'trans' is available
// however I did not since that wouldn't match the API of the others
while (--i) {
rval = x[i];
ival = x[n-i-1];
@@ -766,6 +805,153 @@ RFFT.prototype.forward = function(buffer) {
return spectrum;
};

RFFT.prototype.scale_trans() = function() {
var i=0,bSi = 1.0/n, x = this.trans;
while(i < x.length) { x[i] *= bSi; i++; }
}

// input must have ordering as in output of the forward version,
// you can just pass in this.trans and it should work (if you've scaled it)
RFFT.prototype.inverse = function(buffer) {
var n = this.bufferSize,
x = this.itrans,
TWO_PI = 2*Math.PI,
n2, n4, n8, nn,
t1, t2, t3, t4, t5,
j, i0, i1, i2, i3, i4, i5, i6, i7, i8, ud, ix, id,
st1, cc1, ss1, cc3, ss3,
e,
a;

x.set(buffer);

nn = n>>>1;
n2 = n<<1;

while ( nn >>>= 1 )
{
ix = 0;
id = n2;
n2 >>>= 1;
n4 = n2>>>2;
n8 = n4>>>1;

do // ix
{
for (i0=ix; i0<n; i0+=id)
{
i1 = i0;
i2 = i1 + n4;
i3 = i2 + n4;
i4 = i3 + n4;

//sumdiff3(x[i1], x[i3], t1);// {a, b, d} <--| {a+b, b, a-b}
t1 = x[i1] - x[i3]; x[i1] += x[i3];

x[i2] += x[i2];
x[i4] += x[i4];

//sumdiff3_r(x[i4], t1, x[i3]);// {a,b,d} <--| {a+b, b, b-a}
x[i3] = t1 - x[i4]; x[i4] += t1;

if ( n4!=1 ) // note: optimise (Note this comment from original C++)
{
i1 += n8;
i2 += n8;
i3 += n8;
i4 += n8;

//sumdiff3(x[i1], x[i2], t1); // {a, b, d} <--| {a+b, b, a-b}
t1 = x[i1] - x[i2]; x[i1] += x[i2];

//sumdiff(a, b, &s, &d) {s, d} <--| {a+b, a-b}
//sumdiff(x[i4], x[i3], t2, x[i2]);
t2 = x[i4] + x[i3]; x[i2] = x[i4] - x[i3];

t2 = -t2 * Math.SQRT2;
t1 *= Math.SQRT2;
//sumdiff(a, b, &s, &d) {s, d} <--| {a+b, a-b}
//sumdiff(t2, t1, x[i3], x[i4]);
x[i3] = t2 + t1; x[i4] = t2 - t1;
}
}

ix = (id<<1) - n2;
id <<= 2;
} while ( ix<n );

e = TWO_PI/n2;
for (j=1; j<n8; j++)
{
a = j*e;

ss1 = Math.sin(a);
cc1 = Math.cos(a);

ss3 = sin(3*a); cc3 = cos(3*a);
cc3 = 4*cc1*(cc1*cc1-0.75);
ss3 = 4*ss1*(0.75-ss1*ss1);

ix = 0;
id = n2<<1;
do // ix-loop
{
for (i0=ix; i0<n; i0+=id)
{
i1 = i0 + j;
i2 = i1 + n4;
i3 = i2 + n4;
i4 = i3 + n4;

i5 = i0 + n4 - j;
i6 = i5 + n4;
i7 = i6 + n4;
i8 = i7 + n4;

//sumdiff3(x[i1], x[i6], t1); // {a, b, d} <--| {a+b, b, a-b}
t1 = x[i1] - x[i6]; x[i1] += x[i6];
//sumdiff3(x[i5], x[i2], t2); // {a, b, d} <--| {a+b, b, a-b}
t2 = x[i5] - x[i2]; x[i5] += x[i2];
//t2 = x[i5] + x[i2]; x[i5] = x[i5] - x[i2];

//sumdiff(a, b, &s, &d) {s, d} <--| {a+b, a-b}
//sumdiff(x[i8], x[i3], t3, x[i6]);
//sumdiff(x[i4], x[i7], t4, x[i2]);
t3 = x[i8] + x[i3]; x[i6] = x[i8] - x[i3];
t4 = x[i4] + x[i7]; x[i2] = x[i4] - x[i7];

//sumdiff3(t1, t4, t5); // {a, b, d} <--| {a+b, b, a-b}
t5 = t1 - t4; t1 += t4;
//sumdiff3(t2, t3, t4); // {a, b, d} <--| {a+b, b, a-b}
t4 = t2 - t3; t2 += t3;

//cmult(c, s, x, y, &u, &v) {u,v} <--| {x*c-y*s, x*s+y*c}
//cmult(ss1, cc1, t5, t4, x[i7], x[i3]);
//cmult(cc3, ss3, t1, t2, x[i4], x[i8]);
x[i7] = t5*ss1 - t4*cc1; x[i3] = t5*cc1 + t4*ss1;
x[i4] = t1*cc3 - t2*ss3; x[i8] = t1*ss3 + t2*cc3;
}

ix = (id<<1) - n2;
id <<= 2;
} while ( ix < n );
}
}

for (ix=0, id=4; ix<n; id*=4)
{
for (i0=ix; i0<n; i0+=id) {
// sumdiff(&a, &b) {a, b} <--| {a+b, a-b}
//sumdiff(x[i0], x[i0+1]);
st1 = x[i0] - x[i0+1]; x[i0] += x[i0+1]; x[i0+1] = st1;
}
ix = 2*(id-1);
}

this.reverseBinPermuteInPlace(x);
return x;
};

function Sampler(file, bufferSize, sampleRate, playStart, playEnd, loopStart, loopEnd, loopMode) {
this.file = file;
this.bufferSize = bufferSize;