New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

periodic-function

Package Overview
Dependencies
Maintainers
2
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

periodic-function

Periodic waveform functions — phase normalized to [0,1]

latest
Source
npmnpm
Version
2.1.0
Version published
Weekly downloads
640
12700%
Maintainers
2
Weekly downloads
 
Created
Source

periodic-function

Periodic waveform functions. Phase t is normalized to [0, 1] — one full turn.

npm install periodic-function
import { sine, square, wavetable } from 'periodic-function'

sine(0.25)          // 1  (peak)
square(0.75)        // -1 (low)
wavetable(null, [0, 1, 0, 0.5])  // Float32Array wavetable from Fourier coefficients

API

All functions take phase t ∈ [0, 1] as first argument. Values outside [0, 1] wrap correctly.

FunctionDescription
Waveforms
sinesine(t, phase=0)Sine wave. phase=0.25 gives cosine.
cosinecosine(t, phase=0)Cosine wave. Equivalent to sine(t, 0.25).
sawtoothsawtooth(t)Descending ramp: 1 at t=0, −1 approaching t=1. For ascending ramp use triangle(t, 0).
squaresquare(t, duty=0.5)Square wave. duty = fraction of period spent high.
triangletriangle(t, ratio=0.5)Triangle wave. ratio = peak position (0 = ascending ramp, 1 = descending ramp).
trapezoidtrapezoid(t, p1=0.25, p2=0.5, p3=0.75)Trapezoid wave. Rise ends at p1, fall starts at p2, fall ends at p3. Generalizes square and triangle.
pulsepulse(t, width=0)Dirac-like pulse: 1 at t=0, 0 elsewhere. width extends the high region.
clausenclausen(t, harmonics=10)Clausen function: Σ sin(kθ)/k².
noisenoise(t)Periodic noise — repeating random buffer.
Fourier / Wavetable
fourierfourier(t, real, imag)Evaluate one sample from Fourier coefficients. real[k] and imag[k] are cosine/sine amplitudes for harmonic k. Index 0 is DC, 1 is fundamental.
wavetablewavetable(real, imag, {size=8192, normalize=true})Build a Float32Array wavetable from Fourier coefficients. Used for AudioContext.createPeriodicWave.
Lookup
interpolateinterpolate(t, samples)Linearly interpolate between samples, treating them as one period.
stepstep(t, samples)Step lookup — nearest sample, no interpolation.

Examples

// Cosine as a phase-shifted sine
sine(0, 0.25)     // 1  (same as cosine(0))

// Square wave with 10% duty cycle
square(0.05, 0.1) // 1
square(0.15, 0.1) // -1

// Triangle with peak at 0.25 (asymmetric)
triangle(0.25, 0.25) // -1  (valley, since peak is at t=0)

// Trapezoid as a square with soft edges
trapezoid(t, 0.05, 0.5, 0.55)

// Fourier series: pure sine
fourier(0.25, null, [0, 1])  // 1

// Wavetable for Web Audio API PeriodicWave
const real = new Float32Array(64)
const imag = new Float32Array(64)
for (let k = 1; k < 64; k += 2) imag[k] = 4 / (Math.PI * k)  // square wave
const table = wavetable(real, imag)  // Float32Array[8192], normalized to ±1

License

MIT © Dmitry Iv

Keywords

periodic

FAQs

Package last updated on 31 Mar 2026

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts