Chris@0: /* Chris@0: * Free FFT and convolution (JavaScript) Chris@0: * Chris@0: * Copyright (c) 2014 Project Nayuki Chris@0: * http://www.nayuki.io/page/free-small-fft-in-multiple-languages Chris@24: * Chris@0: * (MIT License) Chris@0: * Permission is hereby granted, free of charge, to any person obtaining a copy of Chris@0: * this software and associated documentation files (the "Software"), to deal in Chris@0: * the Software without restriction, including without limitation the rights to Chris@0: * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of Chris@0: * the Software, and to permit persons to whom the Software is furnished to do so, Chris@0: * subject to the following conditions: Chris@0: * - The above copyright notice and this permission notice shall be included in Chris@0: * all copies or substantial portions of the Software. Chris@0: * - The Software is provided "as is", without warranty of any kind, express or Chris@0: * implied, including but not limited to the warranties of merchantability, Chris@0: * fitness for a particular purpose and noninfringement. In no event shall the Chris@0: * authors or copyright holders be liable for any claim, damages or other Chris@0: * liability, whether in an action of contract, tort or otherwise, arising from, Chris@0: * out of or in connection with the Software or the use or other dealings in the Chris@0: * Software. Chris@24: * Chris@24: * Slightly restructured by Chris Cannam, cannam@all-day-breakfast.com Chris@0: */ Chris@0: Chris@0: "use strict"; Chris@0: Chris@24: /* Chris@24: * Construct an object for calculating the discrete Fourier transform (DFT) of size n, where n is a power of 2. Chris@24: */ Chris@17: function FFTNayuki(n) { Chris@19: Chris@17: this.n = n; Chris@17: this.levels = -1; Chris@0: Chris@17: for (var i = 0; i < 32; i++) { Chris@17: if (1 << i == n) { Chris@17: this.levels = i; // Equal to log2(n) Chris@17: } Chris@17: } Chris@17: if (this.levels == -1) { Chris@17: throw "Length is not a power of 2"; Chris@17: } Chris@17: Chris@17: this.cosTable = new Array(n / 2); Chris@17: this.sinTable = new Array(n / 2); Chris@17: for (var i = 0; i < n / 2; i++) { Chris@17: this.cosTable[i] = Math.cos(2 * Math.PI * i / n); Chris@17: this.sinTable[i] = Math.sin(2 * Math.PI * i / n); Chris@17: } Chris@17: Chris@24: /* Chris@24: * Computes the discrete Fourier transform (DFT) of the given complex vector, storing the result back into the vector. Chris@24: * The vector's length must be equal to the size n that was passed to the object constructor, and this must be a power of 2. Uses the Cooley-Tukey decimation-in-time radix-2 algorithm. Chris@24: */ Chris@17: this.forward = function(real, imag) { Chris@22: Chris@22: var n = this.n; Chris@17: Chris@24: // Bit-reversed addressing permutation Chris@17: for (var i = 0; i < n; i++) { Chris@17: var j = reverseBits(i, this.levels); Chris@17: if (j > i) { Chris@17: var temp = real[i]; Chris@17: real[i] = real[j]; Chris@17: real[j] = temp; Chris@17: temp = imag[i]; Chris@17: imag[i] = imag[j]; Chris@17: imag[j] = temp; Chris@17: } Chris@17: } Chris@0: Chris@24: // Cooley-Tukey decimation-in-time radix-2 FFT Chris@17: for (var size = 2; size <= n; size *= 2) { Chris@17: var halfsize = size / 2; Chris@17: var tablestep = n / size; Chris@17: for (var i = 0; i < n; i += size) { Chris@17: for (var j = i, k = 0; j < i + halfsize; j++, k += tablestep) { Chris@17: var tpre = real[j+halfsize] * this.cosTable[k] + Chris@17: imag[j+halfsize] * this.sinTable[k]; Chris@17: var tpim = -real[j+halfsize] * this.sinTable[k] + Chris@17: imag[j+halfsize] * this.cosTable[k]; Chris@17: real[j + halfsize] = real[j] - tpre; Chris@17: imag[j + halfsize] = imag[j] - tpim; Chris@17: real[j] += tpre; Chris@17: imag[j] += tpim; Chris@17: } Chris@17: } Chris@17: } Chris@17: Chris@24: // Returns the integer whose value is the reverse of the lowest 'bits' bits of the integer 'x'. Chris@17: function reverseBits(x, bits) { Chris@17: var y = 0; Chris@17: for (var i = 0; i < bits; i++) { Chris@17: y = (y << 1) | (x & 1); Chris@17: x >>>= 1; Chris@17: } Chris@17: return y; Chris@17: } Chris@17: } Chris@0: Chris@24: /* Chris@24: * Computes the inverse discrete Fourier transform (IDFT) of the given complex vector, storing the result back into the vector. Chris@24: * The vector's length must be equal to the size n that was passed to the object constructor, and this must be a power of 2. This is a wrapper function. This transform does not perform scaling, so the inverse is not a true inverse. Chris@24: */ Chris@17: this.inverse = function(real, imag) { Chris@17: forward(imag, real); Chris@0: } Chris@0: } Chris@0: