-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
247 lines (196 loc) · 6.22 KB
/
index.js
File metadata and controls
247 lines (196 loc) · 6.22 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define([], factory);
} else if (typeof exports === 'object') {
module.exports = factory();
} else {
root.fft = factory();
}
}(this, function () {
var exp = {},
TWO_PI = 2 * Math.PI,
sin = Math.sin,
wap,
newSlice,
cloneFrame;
swap = function (data, i, j) {
var tmpi = data[i];
data[i] = data[j];
data[j] = tmpi;
};
newSlice = function (source, size, offset) {
var len = source.length,
dataSlice = new Float32Array(size),
remaining = len - offset;
for (i = 0; i < size; i++) {
dataSlice[i] = (remaining >= size) ? source[i + offset] : 0.0;
}
return dataSlice;
};
cloneFrame = function (frame) {
return newSlice(frame, frame.length, 0);
};
/**
* Compute the N size (I)FFT of a signal in place.
* @function
* @param {Float32Array} data - the complex signal data real and imaginary on consecutive indices
* @param {int} n - FFT size. Should be a power of 2
* @param {int} isign - [1, -1] if 1 compite FFT if -1 compute IFFT
* @return {Float32Array} ref to data
*/
var fft = function (data, n, isign) {
if (n < 2 || n&(n - 1)) {
throw "N must be a power of 2";
}
var TwoN, mmax, m, j, istep, i,
wtemp, wr, wpr, wpi, wi, theta, tempr, tempi;
TwoN = n * 2;
j = 1;
for (i = 1; i < TwoN; i += 2) {
if (j > i) {
swap(data, j - 1, i - 1);
swap(data, j, i);
}
m = n;
while (m >= 2 && j > m) {
j -= m;
m = m >> 1;
}
j += m;
}
mmax = 2;
while (TwoN > mmax) {
istep = mmax << 1;
theta = isign * (TWO_PI / mmax);
wtemp = sin(0.5 * theta);
wpr = -2.0 * wtemp * wtemp;
wpi = sin(theta);
wr = 1.0;
wi = 0.0;
for (m = 1; m < mmax; m += 2) {
for (i=m; i <= TwoN; i += istep) {
j = i + mmax;
tempr = wr * data[j - 1] - wi * data[j];
tempi = wr * data[j] + wi * data[j-1];
data[j-1] = data[i-1] - tempr;
data[j] = data[i] - tempi;
data[i-1] += tempr;
data[i] += tempi;
}
wtemp = wr;
wr = wr * wpr - wi * wpi + wr;
wi = wi * wpr + wtemp * wpi + wi;
}
mmax = istep;
}
return data;
};
/**
* Convert Real valued signal to complex
* @function
* @param {Float32Array} realdata - real data signal
* @return {Float32Array} - the converted signal. Note it has 2 * realdata.length items
*/
var complexFromReal = function (realdata) {
var i,j = 0, complexdata = new Float32Array(realdata.length * 2);
for (i = 0; i < complexdata.length; i = i + 2) {
complexdata[i] = realdata[j];
complexdata[i + 1] = 0;
j++;
}
return complexdata;
};
/**
* Convert Complex valued signal to magnitudes
* @function
* @param {Float32Array} data - complex data signal
* @return {Float32Array} - the magnitudes
*/
var calculateMagnitudes = function (data) {
var len = data.length / 2,
sqrt = Math.sqrt,
magnitudes = new Float32Array(len), i, j, re, im, mag;
j = 0;
for (i = 0; i < len; i = i + 2) {
re = data[i];
im = data[i + 1];
mag = sqrt(re*re + im*im);
magnitudes[j] = mag;
j++;
}
return magnitudes;
};
var calculatePhases = function (data) {
var len = data.length / 2,
atan = Math.atan,
phases = new Float32Array(len), i, j, re, im;
j = 0;
for (i = 0; i < len; i = i + 2) {
re = data[i];
im = data[i + 1];
phases[j] = atan(im / re);
j++;
}
return phases;
};
/**
* Generate a hamming window.
* @function
* @param {Float32Array} data - A real valued signal
* @return {Float32Array} - data multiplied by hamming function
*/
var hamming = function (data) {
var i = 0,
A = 0.54,
B = 0.46,
len = data.length,
N = len - 1,
result = new Float32Array(len),
cos = Math.cos;
for (i = 0; i < len; i++) {
result[i] = data[i] * (A - (B * cos(TWO_PI * i) / N));
}
return result;
};
var toDB = function (magnitudes) {
var len = magnitudes.length,
log10 = function (n) { return Math.log(n) / Math.LN10; },
results = new Float32Array(len),
i;
for (i = 0; i < len; i++) {
results[i] = 20 * log10(magnitudes[i]);
}
return results;
};
var FFT = function (data, n) {
var data = hamming(data);
var complexData = complexFromReal(data);
// return fft(complexData, n, 1);
var fftResults = fft(complexData, n, 1);
return toDB(calculateMagnitudes(fftResults));
};
/**
* Real-value signal FFT with hamming window helper function.
* @function
* @param {Float32Array} data - A real valued signal
* @param {int} size - Should be power of two
* @return {Float32Array} - data multiplied by hamming function
*/
var FFT_HAMMING = function (data, fftSize) {
var data = hamming(data),
complexData = complexFromReal(data);
return calculateMagnitudes(fft(complexData, fftSize, 1));
};
exp.windowing_functions = {hamming: hamming};
exp.util = {
toDB: toDB,
calculatePhases: calculatePhases,
calculateMagnitudes: calculateMagnitudes,
complexFromReal: complexFromReal,
slice: newSlice,
clone: cloneFrame
};
exp._fft = fft;
exp.FFT = FFT_HAMMING;
return exp;
}));