Published
Edited
Dec 9, 2020
Importers
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
trajectory
Insert cell
sample(trajectory, { fs })
Insert cell
function sample(
trajectory,
{ includeNext = false, fs = 50, interpolation = 'basis' } = {}
) {
if (trajectory.length < 2) {
return trajectory;
}
const fn =
interpolation === 'basis'
? values => d3.interpolateBasis(values)
: values => d3.piecewise(d3.interpolateNumber, values);
const x = fn(trajectory.map(d => d.x));
const y = fn(trajectory.map(d => d.y));
const t = trajectory.map(d => d.t);
const limits = d3.extent(t);
const numSamples = Math.floor((limits[1] - limits[0]) * fs);
const sampleTimes = d3
.range(numSamples + 1 + (includeNext === true ? 1 : 0))
.map(function(d) {
return d / fs + limits[0];
});

const timeToUnitRange = d3
.scaleLinear()
.domain(t)
.range(d3.range(t.length).map(d => d / (t.length - 1)));

return sampleTimes.map(sampleTime => {
const u = timeToUnitRange(sampleTime);
return {
x: x(u),
y: y(u),
t: sampleTime
};
});
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more