Experimenting audioContext for Apple (Safari)
[sgc3.git] / fft.js / test.html
blobfc79b0f48250932066c94db3596562f0f0690692
1 <!DOCTYPE html>
3 <head>
4 <title>fft.js</title>
5 <script src='lib/real.js'></script>
6 <script src='lib/complex.js'></script>
7 <script>
9 function kahanDiff(x, xOffset, xStride, y, yOffset, yStride, n, run) {
10 var sum = 0.0, compensation = 0.0
12 for (var i = 0; i < x.length / run / xStride; i++) {
13 for (var j = 0; j < run; j++) {
14 var v = Math.abs(x[xOffset + run * xStride * i + j] - y[yOffset + run * yStride * i + j] / n) - compensation
16 var t = sum + v
18 compensation = (t - sum) - v
20 sum = t
22 if (isNaN(sum)) {
23 debugger
28 return sum
31 function kahanStrideDiff(x, xOffset, xStride, run) {
32 var sum = 0.0, compensation = 0.0
34 for (var i = 0; i < x.length / run / xStride; i++) {
35 for (var j = run; j < run * xStride; j++) {
36 var v = Math.abs(x[xOffset + run * xStride * i + j]) - compensation
38 var t = sum + v
40 compensation = (t - sum) - v
42 sum = t
44 if (isNaN(sum)) {
45 debugger
50 return sum
53 function kahanOffsetDiff(x, xOffset) {
54 var sum = 0.0, compensation = 0.0
56 for (var i = 0; i < xOffset; i++) {
57 var v = Math.abs(x[i]) - compensation
59 var t = sum + v
61 compensation = (t - sum) - v
63 sum = t
65 if (isNaN(sum)) {
66 debugger
70 return sum
73 function testComplex(n) {
74 var i = new Float64Array(2 * n), o1 = new Float64Array(2 * n), o2 = new Float64Array(2 * n)
75 var fft = new FFT.complex(n, false), ifft = new FFT.complex(n, true)
77 for (var j = 0; j < (2 * n); j++) {
78 i[j] = Math.random()
81 fft.process(o1, 0, 1, i, 0, 1)
82 ifft.process(o2, 0, 1, o1, 0, 1)
84 return kahanDiff(i, 0, 1, o2, 0, 1, n, 2)
87 function testComplexStride(n) {
88 var iStride = ~~(12 * Math.random()) + 1, o1Stride = ~~(16 * Math.random()) + 1, o2Stride = ~~(5 * Math.random()) + 1
90 var i = new Float64Array(2 * iStride * n), o1 = new Float64Array(2 * o1Stride * n), o2 = new Float64Array(2 * o2Stride * n)
91 var fft = new FFT.complex(n, false), ifft = new FFT.complex(n, true)
93 for (var j = 0; j < n; j++) {
94 i[2 * j * iStride + 0] = Math.random()
95 i[2 * j * iStride + 1] = Math.random()
98 fft.process(o1, 0, o1Stride, i, 0, iStride)
99 ifft.process(o2, 0, o2Stride, o1, 0, o1Stride)
101 return [kahanDiff(i, 0, iStride, o2, 0, o2Stride, n, 2), kahanStrideDiff(o2, 0, o2Stride, 2)]
104 function testRealToComplex(n) {
105 var i = new Float64Array(n), o1 = new Float64Array(2 * n), o2 = new Float64Array(2 * n)
106 var fft = new FFT.complex(n, false), ifft = new FFT.complex(n, true)
108 for (var j = 0; j < n; j++) {
109 i[j] = Math.random()
112 fft.simple(o1, i, 'real')
113 ifft.simple(o2, o1)
115 return [kahanDiff(i, 0, 1, o2, 0, 2, n, 1), kahanStrideDiff(o2, 2, 1)]
118 function testRealToComplexWithOffset(n) {
119 var iOffset = ~~(12 * Math.random()), o1Offset = ~~(12 * Math.random()), o2Offset = ~~(12 * Math.random())
121 var i = new Float64Array(iOffset + n), o1 = new Float64Array(o1Offset + 2 * n), o2 = new Float64Array(o2Offset + 2 * n)
122 var fft = new FFT.complex(n, false), ifft = new FFT.complex(n, true)
124 for (var j = 0; j < n; j++) {
125 i[iOffset + j] = Math.random()
128 fft.process(o1, o1Offset, 1, i, iOffset, 1, 'real')
129 ifft.process(o2, o2Offset, 1, o1, o1Offset, 1)
131 return [kahanDiff(i, 1, o2, 2, n, 1), kahanStrideDiff(o2, 2, 1), kahanOffsetDiff(o1, o1Offset), kahanOffsetDiff(o2, o2Offset)]
135 var count = 0
136 for (var i = 2; i < 100; i++) {
137 var v = testComplex(i)
138 if (isNaN(v) || v > 1e-12) {
139 console.log('Complex', i, v), count++
143 for (var i = 2; i < 100; i++) {
144 var v = testComplexStride(i)
145 if (isNaN(v[0]) || v[0] > 1e-12 || v[1] > 0) {
146 console.log('Complex Strided', i, v), count++
150 for (var i = 2; i < 100; i++) {
151 var v = testRealToComplex(i)
152 if (isNaN(v[0]) || isNaN(v[1]) || v[0] > 1e-12 || v[1] > 1e-12) {
153 console.log('Real to Complex', i, v), count++
157 for (var i = 2; i < 100; i++) {
158 var v = testRealToComplexWithOffset(i)
159 if (isNaN(v[0]) || isNaN(v[1]) || isNaN(v[2]) || isNaN(v[3]) || v[0] > 1e-12 || v[1] > 1e-12 || v[2] > 0 || v[3] > 0) {
160 console.log('Real to Complex Offset', i, v), count++
164 var f0 = new Float64Array(32), r0 = new Float64Array(64), fft = new FFT.complex(32, false); f0[0] = 1, f0[31] = 1
165 fft.simple(r0, f0, 'real')
167 console.log(r0)
169 console.log("Failcount", count)
170 </script>
171 </head>