Rename variables.

This commit is contained in:
Oleksii Trekhleb 2018-08-13 18:09:46 +03:00
parent 8ea35a5549
commit 29fb0c7055

View File

@ -1,29 +1,16 @@
import ComplexNumber from '../complex-number/ComplexNumber'; import ComplexNumber from '../complex-number/ComplexNumber';
import bitLength from '../bits/bitLength';
/**
* Return the no of bits used in the binary representation of input.
*
* @param {Number} [input]
* @return {Number}
*/
function bitLength(input) {
let bitlen = 0;
while ((1 << bitlen) <= input) {
bitlen += 1;
}
return bitlen;
}
/** /**
* Returns the number which is the flipped binary representation of input. * Returns the number which is the flipped binary representation of input.
* *
* @param {Number} [input] * @param {Number} [input]
* @param {Number} [bitlen] * @param {Number} [bitsCount]
* @return {Number} * @return {Number}
*/ */
function reverseBits(input, bitlen) { function reverseBits(input, bitsCount) {
let reversedBits = 0; let reversedBits = 0;
for (let i = 0; i < bitlen; i += 1) { for (let i = 0; i < bitsCount; i += 1) {
reversedBits *= 2; reversedBits *= 2;
if (Math.floor(input / (1 << i)) % 2 === 1) { reversedBits += 1; } if (Math.floor(input / (1 << i)) % 2 === 1) { reversedBits += 1; }
} }
@ -39,8 +26,8 @@ function reverseBits(input, bitlen) {
* @return {ComplexNumber[]} * @return {ComplexNumber[]}
*/ */
export default function fastFourierTransform(inputData, inverse = false) { export default function fastFourierTransform(inputData, inverse = false) {
const bitlen = bitLength(inputData.length - 1); const bitsCount = bitLength(inputData.length - 1);
const N = 1 << bitlen; const N = 1 << bitsCount;
while (inputData.length < N) { while (inputData.length < N) {
inputData.push(new ComplexNumber({ inputData.push(new ComplexNumber({
@ -50,7 +37,7 @@ export default function fastFourierTransform(inputData, inverse = false) {
} }
const output = []; const output = [];
for (let i = 0; i < N; i += 1) { output[i] = inputData[reverseBits(i, bitlen)]; } for (let i = 0; i < N; i += 1) { output[i] = inputData[reverseBits(i, bitsCount)]; }
for (let blockLength = 2; blockLength <= N; blockLength *= 2) { for (let blockLength = 2; blockLength <= N; blockLength *= 2) {
let phaseStep; let phaseStep;