-
Notifications
You must be signed in to change notification settings - Fork 19
/
test.js
146 lines (111 loc) · 2.67 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
'use strict'
var test = require('tape')
var assert = require('assert')
var almost = require('almost-equal')
var rfft = require('./')
var rfftAsm = require('./asm')
var dsp = require('dsp.js')
var ndfft = require('ndarray-fft')
var ndarray = require('ndarray')
var isBrowser = typeof document !== 'undefined'
var N = 4096/4;
var real = new Float32Array(N);
var im = new Float32Array(N);
for (var i = 0; i < N; i++) {
real[i] = Math.sin(10000 * (i / N) / (Math.PI * 2) );
im[i] = 0;
// real[i] = Math.random() * 2 - 0.5;
// im[i] = Math.random() * 2 - 0.5;
}
var result = [
0.2007274180650711,
0.29625651240348816,
0.6431688070297241,
0.628587007522583,
0.10844749957323074,
0.04224899411201477,
0.016048559918999672,
0.013186296448111534 ];
assert.almost = function (x, y) {
if (x.length && y.length) return x.every(function (x, i) {
return assert.almost(x, y[i]);
});
var EPSILON = 10e-2;
if (!almost(x, y, EPSILON)) assert.fail(x, y,
`${x} ≈ ${y}`, '≈');
return true;
};
test('rfft', function (t) {
//RFFT direct transform
var mag1 = rfftAsm(real);
//FFT transform
var fft = new dsp.FFT(N, 44100);
fft.forward(real);
var mag2 = fft.spectrum;
//ndarray-fft
var x = ndarray(real);
var y = ndarray(im);
ndfft(1, x, y);
var mag3 = new Array();
for (var i = x.shape[0]/2; i < x.shape[0]; i++) {
var rv = x.get(i), iv = y.get(i);
mag3.push(Math.sqrt(rv*rv + iv*iv))
}
normalize(mag3)
draw(mag1)
draw(mag2)
draw(mag3)
for (var i = 0; i < mag1.length; i++) {
var v1 = mag1[i]
var v2 = mag2[i]
var v3 = mag3[i]
// if (Math.abs(v1 - v2) > 1e-2) console.log(v1, v2, i)
}
// assert.almost(mag1, mag2);
t.end()
});
test('performance', function (t) {
assert.almost(rfft(real), rfftAsm(real))
console.time('asm')
for (var i = 0; i < 1e4; i++) {
rfftAsm(real);
}
console.timeEnd('asm')
console.time('regular')
for (var i = 0; i < 1e4; i++) {
rfft(real);
}
console.timeEnd('regular')
t.end()
});
function draw (arr) {
if (!isBrowser) return
let canvas = document.body.appendChild(document.createElement('canvas'));
let ctx = canvas.getContext('2d')
canvas.style.cssText = `
margin: 5px;
display: block;
outline: 1px solid rgba(255,240,230,1);
`
let w = canvas.width;
let h = canvas.height;
ctx.beginPath();
for (let i = 0, len = arr.length; i < len; i++) {
let r = i/len;
ctx.lineTo(r*w, h - h*arr[i]);
}
ctx.stroke();
ctx.closePath();
}
function normalize (arr) {
var max = -999;
var min = 999;
for (var i = 0, l = arr.length; i < l; i++) {
max = Math.max(arr[i], max);
min = Math.min(arr[i], min);
}
for (var i = 0, l = arr.length; i < l; i++) {
arr[i] = (arr[i] - min) / (max - min)
}
return arr;
}