@@ -250,14 +250,27 @@ function FourierTransform(bufferSize, sampleRate) {
250
250
return this . bandwidth * index + this . bandwidth / 2 ;
251
251
} ;
252
252
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
+ }
256
272
257
- this . calculatePeak = function ( band , magnitude ) {
258
- if ( magnitude > this . peak ) {
259
- this . peakBand = band ;
260
- this . peak = magnitude ;
273
+ spectrum [ i ] = mag ;
261
274
}
262
275
} ;
263
276
}
@@ -312,15 +325,7 @@ DFT.prototype.forward = function(buffer) {
312
325
imag [ k ] = ival ;
313
326
}
314
327
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 ( ) ;
324
329
} ;
325
330
326
331
@@ -429,16 +434,7 @@ FFT.prototype.forward = function(buffer) {
429
434
halfSize = halfSize << 1 ;
430
435
}
431
436
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 ( ) ;
442
438
} ;
443
439
444
440
FFT . prototype . inverse = function ( real , imag ) {
@@ -508,7 +504,7 @@ FFT.prototype.inverse = function(real, imag) {
508
504
halfSize = halfSize << 1 ;
509
505
}
510
506
511
- var buffer = new Float32Array ( bufferSize ) ;
507
+ var buffer = new Float32Array ( bufferSize ) ; // this should be reused instead
512
508
for ( i = 0 ; i < bufferSize ; i ++ ) {
513
509
buffer [ i ] = real [ i ] / bufferSize ;
514
510
}
@@ -555,6 +551,7 @@ function RFFT(bufferSize, sampleRate) {
555
551
//swap(a[x], a[r]);
556
552
d [ x ] = s [ r ] ;
557
553
d [ r ] = s [ x ] ;
554
+
558
555
x ++ ;
559
556
560
557
h = nh ;
@@ -590,13 +587,25 @@ function RFFT(bufferSize, sampleRate) {
590
587
// trans[n-1] = im[1]
591
588
592
589
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 ;
595
604
596
605
this . reverseBinPermute ( x , buffer ) ;
597
606
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 ) {
600
609
//sumdiff(x[i0], x[i0+1]); // {a, b} <--| {a+b, a-b}
601
610
st1 = x [ i0 ] - x [ i0 + 1 ] ;
602
611
x [ i0 ] += x [ i0 + 1 ] ;
@@ -638,8 +647,8 @@ RFFT.prototype.forward = function(buffer) {
638
647
t1 = x [ i3 ] + x [ i4 ] ;
639
648
t2 = x [ i3 ] - x [ i4 ] ;
640
649
641
- t1 = - t1 * SQRT1_2 ;
642
- t2 *= SQRT1_2 ;
650
+ t1 = - t1 * Math . SQRT1_2 ;
651
+ t2 *= Math . SQRT1_2 ;
643
652
644
653
// sumdiff(t1, x[i2], x[i4], x[i3]); // {s, d} <--| {a+b, a-b}
645
654
st1 = x [ i2 ] ;
@@ -673,7 +682,7 @@ RFFT.prototype.forward = function(buffer) {
673
682
674
683
e = TWO_PI / n2 ;
675
684
676
- for ( j = 1 ; j < n8 ; j ++ ) {
685
+ for ( var j = 1 ; j < n8 ; j ++ ) {
677
686
a = j * e ;
678
687
ss1 = Math . sin ( a ) ;
679
688
cc1 = Math . cos ( a ) ;
@@ -733,25 +742,26 @@ RFFT.prototype.forward = function(buffer) {
733
742
x [ i5 ] -= t4 ;
734
743
}
735
744
736
- ix = ( id << 1 ) - n2 ;
745
+ ix = id << 1 - n2 ;
737
746
id = id << 2 ;
738
747
739
748
} while ( ix < n ) ;
740
749
}
741
750
}
742
751
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 ) ;
746
756
747
- var real , imag ;
757
+ if ( mag > this . peak ) {
758
+ this . peakBand = i ;
759
+ this . peak = mag ;
760
+ }
748
761
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 ;
754
763
}
764
+
755
765
spectrum [ 0 ] = bSi * x [ 0 ] ;
756
766
757
767
return spectrum ;
0 commit comments