-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvelocityoverall
executable file
·64 lines (50 loc) · 2.27 KB
/
velocityoverall
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
#!/usr/bin/env node
const dsp = require('../lib/dsp.js');
// Edit the following parameters as required
const SAMPLING_RATE = 2560;
const NUMBER_OF_SAMPLES = 4096;
const MIN_FILTER_FREQUENCY = 10;
const MAX_FILTER_FREQUENCY = 1000;
// Adapt this function to generate acceleration sample values (in m/s2)
function generateAccelerationSample(element, index) {
return 9.8 / 500 * Math.sin(63 * index * 2 * Math.PI / SAMPLING_RATE); // 63Hz
//return 9.8 * 2 * (Math.random() - 0.5); // Noise
}
// The samples will be generated using the above function
const samples = Array(NUMBER_OF_SAMPLES).fill().map(generateAccelerationSample);
// Start timing the operation
let startTime = Date.now();
// Remove any DC offset from the samples
let offsetSamples = dsp.dcOffset(samples);
console.log(offsetSamples);
// Split the time series samples into up to four sets of at least 256 samples
let sampleSets = dsp.createPowerOfTwoLengthSubSamples(offsetSamples, 256, 4);
// Calculate and average the velocityOverall subsamples
let velocityOverallSum = 0;
sampleSets.forEach((sampleSet, index) => {
let velocityOverall = calculateVelocityOverall(sampleSet);
console.log('velocityOverall(Set ' + index + '): ' + velocityOverall);
velocityOverallSum += velocityOverall;
});
// Output the results
console.log('velocityOverall(*Avg*):', velocityOverallSum / sampleSets.length);
console.log('Completed in', Date.now() - startTime, 'ms');
// Calculate the velocity overall of the given time series samples
function calculateVelocityOverall(samples) {
// Apply a Hann window to the time series
let windowedSamples = dsp.hannWindow(samples);
// Perform a FFT on the windowed samples
let fft = dsp.fft(windowedSamples, SAMPLING_RATE);
// Integrate over frequency (within filter window) to get velocities
let filteredVelocities = [];
for(let bin = 0; bin < fft.numberOfBins; bin++) {
if((fft.frequencies[bin] >= MIN_FILTER_FREQUENCY) &&
(fft.frequencies[bin] <= MAX_FILTER_FREQUENCY)) {
let velocity = fft.magnitudes[bin] / fft.frequencies[bin];
filteredVelocities.push(velocity);
}
}
// Take the RMS of the velocities divided by the square root of the noise
// bandwidth of the Hann window (1.5) to obtain velocity overall
return dsp.rms(filteredVelocities) / Math.sqrt(1.5);
}