Public
Edited
May 21, 2022
18 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
angleDifference = (a, b) => {
const dot = a.x * a.x + b.y * b.y;
const cross = a.x * b.y - a.y * b.x;
return Math.atan2(cross, dot) / (2 * Math.PI);
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
csv = FileAttachment("sine_cos_encoder_test_4236rows.csv").csv({typed: true})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
sum = (arr) => arr.reduce((sum, v) => sum + v, 0);
Insert cell
average = (arr) => sum(arr) / arr.length
Insert cell
max = (arr) => arr.reduce((max, v) => v > max ? v : max, -Infinity)
Insert cell
min = (arr) => arr.reduce((min, v) => v < min ? v : min, Infinity)
Insert cell
middle = (arr) => (max(arr) + min(arr)) / 2
Insert cell
height = (arr) => max(arr) - min(arr)
Insert cell
center = (arr) => {
const mid = middle(arr);
const scale = 2 / height(arr);
return arr.map((v) => (v - mid) * scale);
}
Insert cell
zipXY = (left, right) => {
const len = Math.min(left.length, right.length);
const out = Array(len);
for (let i = 0; i < out.length; i++) {
out[i] = {x: left[i], y: right[i]};
}
return out;
}
Insert cell
angularVelocity = (xy) => {
const arr = Array(xy.length - 1);
for (let i = 0; i < arr.length; i++) {
arr[i] = angleDifference(xy[i], xy[i+1]);
}
return arr;
}
Insert cell
integrate = (arr) => {
const out = Array(arr.length);
let sum = 0;
for (let i = 0; i < out.length; i++) {
sum += arr[i];
out[i] = sum;
}
return out;
}
Insert cell
digitize = (arr) => {
const out = new Array(arr.length);
out[0] = arr[0] > 0 ? 1 : 0;
for (let i = 1; i < out.length; i++) {
const threshold = out[i-1] == 1 ? -0.1 : 0.1
out[i] = (arr[i] > threshold) ? 1 : 0;
}
return out;
}
Insert cell
centerXY = (xy) => {
const xs = center(xy.map(({x, y}) => x));
const ys = center(xy.map(({x, y}) => y));
return zipXY(xs, ys);
}
Insert cell
normalizeRadius = (xy) => xy.map(({x, y}) => {
const r = Math.sqrt(x*x + y*y);
return {x: x/r, y: y/r};
});
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