Published
Edited
Apr 20, 2022
9 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
sineWave = (period = 1, amplitude = 2) => x => amplitude * Math.sin(x * 2 * Math.PI / period)
Insert cell
squareWave = (period = 1, amplitude = 2) => x => amplitude * Math.sign(Math.sin(x * 2 * Math.PI / period))
Insert cell
triangleWave = (period = 1, amplitude = 2) => (
x => Math.abs(x / period + 0.25 - Math.floor(x / period + .75)) * 4 * amplitude - amplitude
)
Insert cell
sawtoothWave = (period = 1, amplitude = 2) => x => (2 * amplitude / period) * mod(x, period) - amplitude
Insert cell
functions = new Map([
['sine', sineWave(period, amplitude)],
['square', squareWave(period, amplitude)],
['triangle', triangleWave(period, amplitude)],
['sawtooth', sawtoothWave(period, amplitude)],
])
Insert cell
samples = mapMap(functions, fn => build_samples(fn, -4, 4, { N: 500 }))
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
capfirst = str => str.slice(0, 1).toUpperCase() + str.slice(1)
Insert cell
mod = (n, m) => ((n % m) + m) % m
Insert cell
mapMap = (map, fn) => new Map(Array.from(map).map(([key, val]) => [key, fn(val)]))
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