I am using the AHRS library to create a sensor fused array of positional data.
From the Go Pro Telemetry data I have accelerometer, gyroscope and magnetometer data arrays, with a timestamp for each sample with the shape:
{
ACCEL: {
samples: [{time, data}, ...]
},
....
}
I want to merge these into an object
{
time: {accel, gyro, magn}
...
}
With all 3 values for each timestamp
I've kind of got it working with reducers
const magn = result[1].streams['MAGN'].samples.reduce((prev, next) => {
return {...prev, [next.cts]: {magn: next.value}}
}, {})
const gyro = result[1].streams['GYRO'].samples.reduce((prev, next) => {
const closest = prev[Object.keys(prev).reverse()?.find(key => key < next.cts) || Object.keys(prev)[0]]
return {...prev, [next.cts]: {...closest, gyro: next.value}}
}, magn)
const merged = result[1].streams['ACCL'].samples.reduce((prev, next) => {
const closest = prev[Object.keys(prev).reverse()?.find(key => key < next.cts) || Object.keys(prev)[0]]
return {...prev, [next.cts]: {...closest, accel: next.value}}
}, gyro)
But this doesn't seem like very elegant code.
Is there a more efficient way to handle it?
typescript playground
compiled to js on typescriptlang.org