Chris@25: require('./test_helper') Chris@25: Chris@25: var ComplexArray = require('../lib/complex_array').ComplexArray Chris@25: Chris@25: describe('fft', function() { Chris@25: describe('#FFT()', function() { Chris@25: describe('on N=4 Arrays', function() { Chris@25: it('should return a single frequency given a constant array', function() { Chris@25: assertFFTMatches([1, 1, 1, 1], new ComplexArray([2, 0, 0, 0])) Chris@25: }) Chris@25: Chris@25: it('should return flat with a delta function input', function() { Chris@25: assertFFTMatches([1, 0, 0, 0], new ComplexArray([0.5, 0.5, 0.5, 0.5])) Chris@25: }) Chris@25: Chris@25: it('should return a single high freq', function() { Chris@25: assertFFTMatches([1, -1, 1, -1], new ComplexArray([0, 0, 2, 0])) Chris@25: }) Chris@25: Chris@25: it('should return a single low freq', function() { Chris@25: assertFFTMatches([1, 0, -1, -0], new ComplexArray([0, 1, 0, 1])) Chris@25: }) Chris@25: Chris@25: it('should return a high freq and DC', function() { Chris@25: assertFFTMatches([1, 0, 1, 0], new ComplexArray([1, 0, 1, 0])) Chris@25: }) Chris@25: }) Chris@25: Chris@25: describe('on N=6 Arrays', function() { Chris@25: it('should return a single frequency given a constant array', function() { Chris@25: assertFFTMatches( Chris@25: [1, 1, 1, 1, 1, 1], Chris@25: new ComplexArray([Math.sqrt(6), 0, 0, 0, 0, 0]) Chris@25: ) Chris@25: }) Chris@25: Chris@25: it('should return flat with a delta function input', function() { Chris@25: var a = 1 / Math.sqrt(6) Chris@25: assertFFTMatches( Chris@25: [1, 0, 0, 0, 0, 0], Chris@25: new ComplexArray([a, a, a, a, a, a]) Chris@25: ) Chris@25: }) Chris@25: }) Chris@25: Chris@25: describe('on N=`prime` Arrays', function() { Chris@25: it('should match the DFT', function() { Chris@25: var a = new ComplexArray(13) Chris@25: Chris@25: a.map(function(value) { Chris@25: value.real = Math.random() Chris@25: value.imag = Math.random() Chris@25: }) Chris@25: Chris@25: assertFFTMatchesDFT(a) Chris@25: }) Chris@25: }) Chris@25: Chris@25: describe('on N=512 Arrays', function() { Chris@25: it('should match the DFT', function() { Chris@25: var a = new ComplexArray(512) Chris@25: Chris@25: a.map(function(value) { Chris@25: value.real = Math.random() Chris@25: value.imag = Math.random() Chris@25: }) Chris@25: Chris@25: assertFFTMatchesDFT(a) Chris@25: }) Chris@25: }) Chris@25: Chris@25: describe('on N=900 Arrays', function() { Chris@25: it('should match the DFT', function() { Chris@25: var a = new ComplexArray(900) Chris@25: Chris@25: a.map(function(value) { Chris@25: value.real = Math.random() Chris@25: value.imag = Math.random() Chris@25: }) Chris@25: Chris@25: assertFFTMatchesDFT(a) Chris@25: }) Chris@25: }) Chris@25: }) Chris@25: Chris@25: describe('#frequencyMap()', function() { Chris@25: it('should not modify the original', function() { Chris@25: var Chris@25: original = new ComplexArray([1, 2, 3, 4]), Chris@25: filtered = original.frequencyMap(function() {}) Chris@25: Chris@25: assertComplexArraysAlmostEqual(original, filtered) Chris@25: }) Chris@25: Chris@25: it('should halve the original', function() { Chris@25: var Chris@25: original = new ComplexArray([1, 2, 3, 4]), Chris@25: filtered = original.frequencyMap(function(value, i) { Chris@25: value.real /= 2 Chris@25: value.imag /= 2 Chris@25: }) Chris@25: Chris@25: assertComplexArraysAlmostEqual( Chris@25: new ComplexArray([0.5, 1, 1.5, 2]), filtered) Chris@25: }) Chris@25: Chris@25: it('should return zeroed ComplexArray', function() { Chris@25: var Chris@25: original = new ComplexArray([1, 2, 3, 4]), Chris@25: filtered = original.frequencyMap(function(value, i) { Chris@25: value.real = value.imag = 0 Chris@25: }) Chris@25: Chris@25: assertComplexArraysAlmostEqual(new ComplexArray([0, 0, 0, 0]), filtered) Chris@25: }) Chris@25: Chris@25: it('should shift the original', function() { Chris@25: var Chris@25: original = new ComplexArray([1, 2, 3, 4]), Chris@25: filtered = original.frequencyMap(function(value, i) { Chris@25: var Chris@25: // Multiply by a phase to shift the original. Chris@25: real_multiplier = i % 2 ? 0 : (1 - i), Chris@25: imag_multiplier = i % 2 ? (2 - i) : 0, Chris@25: swap_real = value.real, Chris@25: swap_imag = value.imag Chris@25: Chris@25: value.real = real_multiplier * swap_real - imag_multiplier * swap_imag Chris@25: value.imag = real_multiplier * swap_imag + imag_multiplier * swap_real Chris@25: }) Chris@25: Chris@25: assertComplexArraysAlmostEqual(new ComplexArray([4, 1, 2, 3]), filtered) Chris@25: }) Chris@25: }) Chris@25: }) Chris@25: