8000 More optimizations to FFT and RFT classes. · JavaScriptIOT/dsp.js@864bad1 · GitHub
[go: up one dir, main page]

Skip to content
< 8000 script crossorigin="anonymous" type="application/javascript" src="https://github.githubassets.com/assets/sessions-eed3aa0554dd.js" defer="defer">

Commit 864bad1

Browse files
committed
More optimizations to FFT and RFT classes.
1 parent 50ad101 commit 864bad1

File tree

1 file changed

+54
-44
lines changed

1 file changed

+54
-44
lines changed

dsp.js

Lines changed: 54 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -250,14 +250,27 @@ function FourierTransform(bufferSize, sampleRate) {
250250
return this.bandwidth * index + this.bandwidth / 2;
251251
};
252252

253-
this.calculateMagnitude = function(real, imag) {
254-
return 2 / this.bufferSize * Math.sqrt(real * real + imag * imag);
255-
};
253+
this.calculateSpectrum = function() {
254+
var spectrum = this.spectrum,
255+
real = this.real,
256+
imag = this.imag,
257+
bSi = 2 / this.bufferSize,
258+
sqrt = Math.sqrt,
259+
rval,
260+
ival,
261+
mag;
262+
263+
for (var i = 0, N = bufferSize/2; i < N; i++) {
264+
rval = real[i];
265+
ival = imag[i];
266+
mag = bSi * sqrt(rval * rval + ival * ival);
267+
268+
if (mag > this.peak) {
269+
this.peakBand = i;
270+
this.peak = mag;
271+
}
256272

257-
this.calculatePeak = function(band, magnitude) {
258-
if (magnitude > this.peak) {
259-
this.peakBand = band;
260-
this.peak = magnitude;
273+
spectrum[i] = mag;
261274
}
262275
};
263276
}
@@ -312,15 +325,7 @@ DFT.prototype.forward = function(buffer) {
312325
imag[k] = ival;
313326
}
314327

315-
var spectrum = this.spectrum;
316-
var bSi = 2 / this.bufferSize;
317-
318-
for (var i = 0; i < this.bufferSize/2; i++) {
319-
spectrum[i] = bSi * Math.sqrt(real[i] * real[i] + imag[i] * imag[i]);
320-
this.calculatePeak(i, spectrum[i]);
321-
}
322-
323-
return spectrum;
328+
return this.calculateSpectrum();
324329
};
325330

326331

@@ -429,16 +434,7 @@ FFT.prototype.forward = function(buffer) {
429434
halfSize = halfSize << 1;
430435
}
431436

432-
i = bufferSize/2;
433-
434-
var bSi = 2 / bufferSize;
435-
436-
while(i--) {
437-
spectrum[i] = bSi * Math.sqrt(real[i] * real[i] + imag[i] * imag[i]);
438-
this.calculatePeak(i, spectrum[i]);
439-
}
440-
441-
return spectrum;
437+
return this.calculateSpectrum();
442438
};
443439

444440
FFT.prototype.inverse = function(real, imag) {
@@ -508,7 +504,7 @@ FFT.prototype.inverse = function(real, imag) {
508504
halfSize = halfSize << 1;
509505
}
510506

511-
var buffer = new Float32Array(bufferSize);
507+
var buffer = new Float32Array(bufferSize); // this should be reused instead
512508
for (i = 0; i < bufferSize; i++) {
513509
buffer[i] = real[i] / bufferSize;
514510
}
@@ -555,6 +551,7 @@ function RFFT(bufferSize, sampleRate) {
555551
//swap(a[x], a[r]);
556552
d[x] = s[r];
557553
d[r] = s[x];
554+
558555
x++;
559556

560557
h = nh;
@@ -590,13 +587,25 @@ function RFFT(bufferSize, sampleRate) {
590587
// trans[n-1] = im[1]
591588

592589
RFFT.prototype.forward = function(buffer) {
593-
var n = this.bufferSize, x = this.trans, n2, n4, n8, nn, st1, t1, t2, t3, t4, ix, id, i0, i1, i2, i3, i4, i5, i6, i7, i8, e, a, j, cc1, ss1, cc3, ss3;
594-
var TWO_PI = 2*Math.PI, SQRT1_2 = Math.SQRT1_2;
590+
var n = this.bufferSize,
591+
spectrum = this.spectrum,
592+
x = this.trans,
593+
TWO_PI = 2*Math.PI,
594+
sqrt = Math.sqrt,
595+
i = n >>> 1,
596+
bSi = 2 / n,
597+
n2, n4, n8, nn,
598+
t1, t2, t3, t4,
599+
i1, i2, i3, i4, i5, i6, i7, i8,
600+
st1, cc1, ss1, cc3, ss3,
601+
e,
602+
a,
603+
rval, ival, mag;
595604

596605
this.reverseBinPermute(x, buffer);
597606

598-
for (ix = 0, id = 4; ix < n; id *= 4) {
599-
for (i0 = ix; i0 < n; i0 += id) {
607+
for (var ix = 0, id = 4; ix < n; id *= 4) {
608+
for (var i0 = ix; i0 < n; i0 += id) {
600609
//sumdiff(x[i0], x[i0+1]); // {a, b} <--| {a+b, a-b}
601610
st1 = x[i0] - x[i0+1];
602611
x[i0] += x[i0+1];
@@ -638,8 +647,8 @@ RFFT.prototype.forward = function(buffer) {
638647
t1 = x[i3] + x[i4];
639648
t2 = x[i3] - x[i4];
640649

641-
t1 = -t1 * SQRT1_2;
642-
t2 *= SQRT1_2;
650+
t1 = -t1 * Math.SQRT1_2;
651+
t2 *= Math.SQRT1_2;
643652

644653
// sumdiff(t1, x[i2], x[i4], x[i3]); // {s, d} <--| {a+b, a-b}
645654
st1 = x[i2];
@@ -673,7 +682,7 @@ RFFT.prototype.forward = function(buffer) {
673682

674683
e = TWO_PI / n2;
675684

676-
for (j = 1; j < n8; j++) {
685+
for (var j = 1; j < n8; j++) {
677686
a = j * e;
678687
ss1 = Math.sin(a);
679688
cc1 = Math.cos(a);
@@ -733,25 +742,26 @@ RFFT.prototype.forward = function(buffer) {
733742
x[i5] -= t4;
734743
}
735744

736-
ix = (id<<1) - n2;
745+
ix = id << 1 - n2;
737746
id = id << 2;
738747

739748
} while (ix < n);
740749
}
741750
}
742751

743-
var spectrum = this.spectrum;
744-
var i = (n>>>1);
745-
var bSi = 2.0 / n;
752+
while (--i) {
753+
rval = x[i];
754+
ival = x[n-i-1];
755+
mag = bSi * sqrt(rval * rval + ival * ival);
746756

747-
var real, imag;
757+
if (mag > this.peak) {
758+
this.peakBand = i;
759+
this.peak = mag;
760+
}
748761

749-
while (--i) {
750-
real = x[i];
751-
imag = x[n-i-1];
752-
spectrum[i] = bSi * Math.sqrt(real * real + imag * imag);
753-
this.calculatePeak(i, spectrum[i]);
762+
spectrum[i] = mag;
754763
}
764+
755765
spectrum[0] = bSi * x[0];
756766

757767
return spectrum;

0 commit comments

Comments
 (0)
0