Chris@25
|
1 require('./test_helper')
|
Chris@25
|
2
|
Chris@25
|
3 var ComplexArray = require('../lib/complex_array').ComplexArray
|
Chris@25
|
4
|
Chris@25
|
5 describe('fft', function() {
|
Chris@25
|
6 describe('#FFT()', function() {
|
Chris@25
|
7 describe('on N=4 Arrays', function() {
|
Chris@25
|
8 it('should return a single frequency given a constant array', function() {
|
Chris@25
|
9 assertFFTMatches([1, 1, 1, 1], new ComplexArray([2, 0, 0, 0]))
|
Chris@25
|
10 })
|
Chris@25
|
11
|
Chris@25
|
12 it('should return flat with a delta function input', function() {
|
Chris@25
|
13 assertFFTMatches([1, 0, 0, 0], new ComplexArray([0.5, 0.5, 0.5, 0.5]))
|
Chris@25
|
14 })
|
Chris@25
|
15
|
Chris@25
|
16 it('should return a single high freq', function() {
|
Chris@25
|
17 assertFFTMatches([1, -1, 1, -1], new ComplexArray([0, 0, 2, 0]))
|
Chris@25
|
18 })
|
Chris@25
|
19
|
Chris@25
|
20 it('should return a single low freq', function() {
|
Chris@25
|
21 assertFFTMatches([1, 0, -1, -0], new ComplexArray([0, 1, 0, 1]))
|
Chris@25
|
22 })
|
Chris@25
|
23
|
Chris@25
|
24 it('should return a high freq and DC', function() {
|
Chris@25
|
25 assertFFTMatches([1, 0, 1, 0], new ComplexArray([1, 0, 1, 0]))
|
Chris@25
|
26 })
|
Chris@25
|
27 })
|
Chris@25
|
28
|
Chris@25
|
29 describe('on N=6 Arrays', function() {
|
Chris@25
|
30 it('should return a single frequency given a constant array', function() {
|
Chris@25
|
31 assertFFTMatches(
|
Chris@25
|
32 [1, 1, 1, 1, 1, 1],
|
Chris@25
|
33 new ComplexArray([Math.sqrt(6), 0, 0, 0, 0, 0])
|
Chris@25
|
34 )
|
Chris@25
|
35 })
|
Chris@25
|
36
|
Chris@25
|
37 it('should return flat with a delta function input', function() {
|
Chris@25
|
38 var a = 1 / Math.sqrt(6)
|
Chris@25
|
39 assertFFTMatches(
|
Chris@25
|
40 [1, 0, 0, 0, 0, 0],
|
Chris@25
|
41 new ComplexArray([a, a, a, a, a, a])
|
Chris@25
|
42 )
|
Chris@25
|
43 })
|
Chris@25
|
44 })
|
Chris@25
|
45
|
Chris@25
|
46 describe('on N=`prime` Arrays', function() {
|
Chris@25
|
47 it('should match the DFT', function() {
|
Chris@25
|
48 var a = new ComplexArray(13)
|
Chris@25
|
49
|
Chris@25
|
50 a.map(function(value) {
|
Chris@25
|
51 value.real = Math.random()
|
Chris@25
|
52 value.imag = Math.random()
|
Chris@25
|
53 })
|
Chris@25
|
54
|
Chris@25
|
55 assertFFTMatchesDFT(a)
|
Chris@25
|
56 })
|
Chris@25
|
57 })
|
Chris@25
|
58
|
Chris@25
|
59 describe('on N=512 Arrays', function() {
|
Chris@25
|
60 it('should match the DFT', function() {
|
Chris@25
|
61 var a = new ComplexArray(512)
|
Chris@25
|
62
|
Chris@25
|
63 a.map(function(value) {
|
Chris@25
|
64 value.real = Math.random()
|
Chris@25
|
65 value.imag = Math.random()
|
Chris@25
|
66 })
|
Chris@25
|
67
|
Chris@25
|
68 assertFFTMatchesDFT(a)
|
Chris@25
|
69 })
|
Chris@25
|
70 })
|
Chris@25
|
71
|
Chris@25
|
72 describe('on N=900 Arrays', function() {
|
Chris@25
|
73 it('should match the DFT', function() {
|
Chris@25
|
74 var a = new ComplexArray(900)
|
Chris@25
|
75
|
Chris@25
|
76 a.map(function(value) {
|
Chris@25
|
77 value.real = Math.random()
|
Chris@25
|
78 value.imag = Math.random()
|
Chris@25
|
79 })
|
Chris@25
|
80
|
Chris@25
|
81 assertFFTMatchesDFT(a)
|
Chris@25
|
82 })
|
Chris@25
|
83 })
|
Chris@25
|
84 })
|
Chris@25
|
85
|
Chris@25
|
86 describe('#frequencyMap()', function() {
|
Chris@25
|
87 it('should not modify the original', function() {
|
Chris@25
|
88 var
|
Chris@25
|
89 original = new ComplexArray([1, 2, 3, 4]),
|
Chris@25
|
90 filtered = original.frequencyMap(function() {})
|
Chris@25
|
91
|
Chris@25
|
92 assertComplexArraysAlmostEqual(original, filtered)
|
Chris@25
|
93 })
|
Chris@25
|
94
|
Chris@25
|
95 it('should halve the original', function() {
|
Chris@25
|
96 var
|
Chris@25
|
97 original = new ComplexArray([1, 2, 3, 4]),
|
Chris@25
|
98 filtered = original.frequencyMap(function(value, i) {
|
Chris@25
|
99 value.real /= 2
|
Chris@25
|
100 value.imag /= 2
|
Chris@25
|
101 })
|
Chris@25
|
102
|
Chris@25
|
103 assertComplexArraysAlmostEqual(
|
Chris@25
|
104 new ComplexArray([0.5, 1, 1.5, 2]), filtered)
|
Chris@25
|
105 })
|
Chris@25
|
106
|
Chris@25
|
107 it('should return zeroed ComplexArray', function() {
|
Chris@25
|
108 var
|
Chris@25
|
109 original = new ComplexArray([1, 2, 3, 4]),
|
Chris@25
|
110 filtered = original.frequencyMap(function(value, i) {
|
Chris@25
|
111 value.real = value.imag = 0
|
Chris@25
|
112 })
|
Chris@25
|
113
|
Chris@25
|
114 assertComplexArraysAlmostEqual(new ComplexArray([0, 0, 0, 0]), filtered)
|
Chris@25
|
115 })
|
Chris@25
|
116
|
Chris@25
|
117 it('should shift the original', function() {
|
Chris@25
|
118 var
|
Chris@25
|
119 original = new ComplexArray([1, 2, 3, 4]),
|
Chris@25
|
120 filtered = original.frequencyMap(function(value, i) {
|
Chris@25
|
121 var
|
Chris@25
|
122 // Multiply by a phase to shift the original.
|
Chris@25
|
123 real_multiplier = i % 2 ? 0 : (1 - i),
|
Chris@25
|
124 imag_multiplier = i % 2 ? (2 - i) : 0,
|
Chris@25
|
125 swap_real = value.real,
|
Chris@25
|
126 swap_imag = value.imag
|
Chris@25
|
127
|
Chris@25
|
128 value.real = real_multiplier * swap_real - imag_multiplier * swap_imag
|
Chris@25
|
129 value.imag = real_multiplier * swap_imag + imag_multiplier * swap_real
|
Chris@25
|
130 })
|
Chris@25
|
131
|
Chris@25
|
132 assertComplexArraysAlmostEqual(new ComplexArray([4, 1, 2, 3]), filtered)
|
Chris@25
|
133 })
|
Chris@25
|
134 })
|
Chris@25
|
135 })
|
Chris@25
|
136
|