-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdsp.js
253 lines (204 loc) · 7.62 KB
/
dsp.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
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
248
249
250
251
252
253
/**
* Copyright reelyActive 2025
* Original FFT code Copyright Vail Systems 2015 (Joshua Jung & Ben Bryan)
* We believe in an open Internet of Things
*/
/**
* Calculate the root mean square of the given values.
* @param {Array} values The values.
* @return {Number} The root mean square of the values.
*/
function rms(values) {
if(!Array.isArray(values)) {
return null;
}
let sumOfSquares = values.reduce((sum, value) => sum + (value * value));
return Math.sqrt(sumOfSquares / values.length);
}
/**
* Apply a DC offset to the given values (without modifying the originals).
* @param {Array} values The values which can be either Numbers or Arrays.
* @param {Number} offset The optional offset: if unspecified, any DC offset of
* the given values will be removed.
* @return {Array} A new Array of values with the DC offset applied.
*/
function dcOffset(values, offset) {
if(!Array.isArray(values)) {
return null;
}
let isNumericalArray = values.every((value) => Number.isFinite(value));
if(isNumericalArray) {
offset = offset || // If no offset provided, determine & use the DC offset
-values.reduce((sum, value) => sum + value) / values.length;
return values.map((value) => value + offset);
}
let isMultiDimensionalArray = values.every((value) => Array.isArray(value));
if(isMultiDimensionalArray) {
let offsetValues = [];
values.forEach((element) => {
offsetValues.push(dcOffset(element, offset)); // Self-recursion
});
return offsetValues;
}
return null;
}
/**
* Apply a Hann window to the given series of samples (without modifying the
* source samples).
* @param {Array} samples The samples.
* @return {Array} A new Array of samples with the Hann window applied.
*/
function hannWindow(samples) {
return samples.map((sample, index) => sample *
(1 - Math.pow(Math.cos(Math.PI * index / (samples.length - 1)), 2)));
}
/**
* Split the given samples into a set of subsamples, up to the given maximum
* number of subsamples, each of the given minimum length or a larger power of
* two, whichever is greater.
* @param {Array} samples The samples.
* @param {Number} minLength The minimum length of a subsample.
* @param {Number} maxNumberOfSubs The maximum number of subsamples.
* @return {Array} Array of subsample arrays.
*/
function createPowerOfTwoLengthSubSamples(samples, minLength, maxNumberOfSubs) {
if(!isPowerOfTwo(minLength) || (samples.length < minLength)) {
return [];
}
let subSamples = [];
let numberOfSubs = maxNumberOfSubs;
if(samples.length < (minLength * maxNumberOfSubs)) {
numberOfSubs = Math.floor(samples.length / minLength);
}
let subInterval = Math.floor(samples.length / numberOfSubs);
let subLength = 1 << Math.floor(Math.log2(subInterval));
for(let subIndex = 0; subIndex < numberOfSubs; subIndex++) {
let subStart = subIndex * subInterval;
subSamples.push(samples.slice(subStart, subStart + subLength));
}
return subSamples;
}
/**
* Perform a Fast Fourier Transform using the Cooley-Tukey method.
* Adapted from https://github.com/vail-systems/node-fft
* @param {Array} samples The time series of samples.
* @param {Number} samplingRate The sampling rate (in Hz) of the samples.
* @return {Object} Arrays of magnitudes and frequencies (in Hz), or null
* if the FFT cannot be computed from the given parameters.
*/
function fft(samples, samplingRate) {
if(!Array.isArray(samples) || !isPowerOfTwo(samples.length) ||
!Number.isFinite(samplingRate)) {
return null;
}
let phasors = fftPhasors(samples);
let stepFrequency = samplingRate / phasors.length;
let magnitudes = phasors.map(complexMagnitude).slice(0, phasors.length / 2);
let frequencies = magnitudes.map((element, index) => index * stepFrequency);
return { magnitudes: magnitudes,
frequencies: frequencies,
numberOfBins: magnitudes.length };
}
/**
* Calculate the phasors of a Fast Fourier Transform using the Cooley-Tukey
* method with self-recursion.
* Adapted from https://github.com/vail-systems/node-fft
* @param {Array} vectors The time series of samples/vectors.
* @return {Array} The phasors as an array of arrays, where the latter is in
* [ magnitude, phase ] format.
*/
function fftPhasors(vectors) {
let phasors = [];
// For a single-entry vector, return the magnitude and phase.
if(vectors.length === 1) {
let isComplex = Array.isArray(vectors[0]);
if(isComplex) {
return [ [ vectors[0][0], vectors[0][1] ] ];
}
else {
return [ [ vectors[0], 0 ] ];
}
}
// For longer vectors, split into even and odd samples
let evenVectors = vectors.filter((vector, index) => index % 2 === 0);
let oddVectors = vectors.filter((vector, index) => index % 2 === 1);
// Self-recurse on the even and odd samples
let evenPhasors = fftPhasors(evenVectors);
let oddPhasors = fftPhasors(oddVectors);
// The number of operations is now reduced to N/2
for(let k = 0; k < (vectors.length / 2); k++) {
let t = evenPhasors[k];
let e = complexMultiply(exponent(k, vectors.length), oddPhasors[k]);
phasors[k] = complexAdd(t, e);
phasors[k + (vectors.length / 2)] = complexSubtract(t, e);
}
return phasors;
}
/**
* Determine the exponent as a complex number.
* Adapted from https://github.com/vail-systems/node-fft
* @param {Number} k k.
* @param {Number} N N.
* @return {Array} The exponent as a complex number [ real, imaginary ].
*/
function exponent(k, N) {
let mapExponent = {};
let x = -2 * Math.PI * (k / N);
mapExponent[N] = mapExponent[N] || {};
mapExponent[N][k] = mapExponent[N][k] || [ Math.cos(x), Math.sin(x) ];
return mapExponent[N][k];
}
/**
* Multiply two complex numbers.
* Adapted from https://github.com/vail-systems/node-fft
* @param {Array} a The first complex number [ real, imaginary ].
* @param {Array} b The second complex number [ real, imaginary ].
* @return {Array} The product as a complex number [ real, imaginary ].
*/
function complexMultiply(a, b) {
return [ (a[0] * b[0] - a[1] * b[1]),
(a[0] * b[1] + a[1] * b[0]) ];
};
/**
* Add two complex numbers.
* Adapted from https://github.com/vail-systems/node-fft
* @param {Array} a The first complex number [ real, imaginary ].
* @param {Array} b The second complex number [ real, imaginary ].
* @return {Array} The sum as a complex number [ real, imaginary ].
*/
function complexAdd(a, b) {
return [ a[0] + b[0], a[1] + b[1] ];
};
/**
* Subtract two complex numbers.
* Adapted from https://github.com/vail-systems/node-fft
* @param {Array} a The first complex number [ real, imaginary ].
* @param {Array} b The second complex number [ real, imaginary ].
* @return {Array} The difference as a complex number [ real, imaginary ].
*/
function complexSubtract(a, b) {
return [ a[0] - b[0], a[1] - b[1] ];
};
/**
* Determine the magnitude of a complex number.
* Adapted from https://github.com/vail-systems/node-fft
* @param {Array} c The complex number [ real, imaginary ].
* @return {Number} The magnitude as a real number.
*/
function complexMagnitude(c) {
return Math.sqrt(c[0] * c[0] + c[1] * c[1]);
}
/**
* Determine if the given value is a power of two.
* @param {Number} value The value to check.
* @return {Boolean} True if it is a power of two, false otherwise.
*/
function isPowerOfTwo(value) {
return (Math.log2(value) % 1 === 0);
}
module.exports.rms = rms;
module.exports.dcOffset = dcOffset;
module.exports.hannWindow = hannWindow;
module.exports.createPowerOfTwoLengthSubSamples =
createPowerOfTwoLengthSubSamples;
module.exports.fft = fft;