Update FFT tests.

This commit is contained in:
Oleksii Trekhleb 2018-08-15 17:47:32 +03:00
parent c2f7e49f07
commit 13ed5061a3
3 changed files with 20 additions and 19 deletions

View File

@ -120,8 +120,9 @@ Stuart Riffle has a great interpretation of the Fourier Transform:
## References
- [An Interactive Guide To The Fourier Transform](https://betterexplained.com/articles/an-interactive-guide-to-the-fourier-transform/)
- [YouTube by Better Explained](https://www.youtube.com/watch?v=iN0VG9N2q0U&t=0s&index=77&list=PLLXdhg_r2hKA7DPDsunoDZ-Z769jWn4R8)
- [YouTube by 3Blue1Brown](https://www.youtube.com/watch?v=spUNpyF58BY&t=0s&index=76&list=PLLXdhg_r2hKA7DPDsunoDZ-Z769jWn4R8)
- [DFT on YouTube by Better Explained](https://www.youtube.com/watch?v=iN0VG9N2q0U&t=0s&index=77&list=PLLXdhg_r2hKA7DPDsunoDZ-Z769jWn4R8)
- [FT on YouTube by 3Blue1Brown](https://www.youtube.com/watch?v=spUNpyF58BY&t=0s&index=76&list=PLLXdhg_r2hKA7DPDsunoDZ-Z769jWn4R8)
- [FFT on YouTube by Simon Xu](https://www.youtube.com/watch?v=htCj9exbGo0&index=78&list=PLLXdhg_r2hKA7DPDsunoDZ-Z769jWn4R8&t=0s)
- Wikipedia
- [FT](https://en.wikipedia.org/wiki/Fourier_transform)
- [DFT](https://www.wikiwand.com/en/Discrete_Fourier_transform)

View File

@ -53,10 +53,10 @@ describe('fastFourierTransform', () => {
];
const output = fastFourierTransform(input);
const invertedOut = fastFourierTransform(output, true);
const invertedOutput = fastFourierTransform(output, true);
expect(sequencesApproximatelyEqual(expectedOutput, output, delta)).toBe(true);
expect(sequencesApproximatelyEqual(input, invertedOut, delta)).toBe(true);
expect(sequencesApproximatelyEqual(input, invertedOutput, delta)).toBe(true);
});
it('should calculate the radix-2 discrete fourier transform #3', () => {

View File

@ -4,17 +4,17 @@ import bitLength from '../bits/bitLength';
/**
* Returns the number which is the flipped binary representation of input.
*
* @param {Number} [input]
* @param {Number} [bitsCount]
* @return {Number}
* @param {number} input
* @param {number} bitsCount
* @return {number}
*/
function reverseBits(input, bitsCount) {
let reversedBits = 0;
for (let i = 0; i < bitsCount; i += 1) {
for (let bitIndex = 0; bitIndex < bitsCount; bitIndex += 1) {
reversedBits *= 2;
if (Math.floor(input / (1 << i)) % 2 === 1) {
if (Math.floor(input / (1 << bitIndex)) % 2 === 1) {
reversedBits += 1;
}
}
@ -39,8 +39,8 @@ export default function fastFourierTransform(inputData, inverse = false) {
}
const output = [];
for (let i = 0; i < N; i += 1) {
output[i] = inputData[reverseBits(i, bitsCount)];
for (let dataSampleIndex = 0; dataSampleIndex < N; dataSampleIndex += 1) {
output[dataSampleIndex] = inputData[reverseBits(dataSampleIndex, bitsCount)];
}
for (let blockLength = 2; blockLength <= N; blockLength *= 2) {
@ -53,14 +53,14 @@ export default function fastFourierTransform(inputData, inverse = false) {
for (let blockStart = 0; blockStart < N; blockStart += blockLength) {
let phase = new ComplexNumber({ re: 1, im: 0 });
for (let idx = blockStart; idx < blockStart + blockLength / 2; idx += 1) {
const component = output[idx + blockLength / 2].multiply(phase);
for (let signalId = blockStart; signalId < (blockStart + blockLength / 2); signalId += 1) {
const component = output[signalId + blockLength / 2].multiply(phase);
const upd1 = output[idx].add(component);
const upd2 = output[idx].subtract(component);
const upd1 = output[signalId].add(component);
const upd2 = output[signalId].subtract(component);
output[idx] = upd1;
output[idx + blockLength / 2] = upd2;
output[signalId] = upd1;
output[signalId + blockLength / 2] = upd2;
phase = phase.multiply(phaseStep);
}
@ -68,8 +68,8 @@ export default function fastFourierTransform(inputData, inverse = false) {
}
if (inverse) {
for (let idx = 0; idx < N; idx += 1) {
output[idx] /= N;
for (let signalId = 0; signalId < N; signalId += 1) {
output[signalId] /= N;
}
}