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

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more