periodic-function
Version:
Collection of periodic functions
49 lines (33 loc) • 3.85 kB
Markdown
# periodic-function [](http://github.com/badges/stability-badges) [](https://travis-ci.org/dfcreative/periodic-function)
Collection of periodic functions with period normalized to turns.
## Usage
[](https://npmjs.org/package/periodic-function/)
```js
const fn = require('periodic-function/<fn>')
//π radians
let halfTurn = fn(.5)
//2π radians
let fullTurn = fn(1)
```
## API
### let fn = require('periodic-function/\<fn\>')
The `fn` takes the amount of turn `t` as the first argument and optional parameters. The list of available functions:
| Signature | Waveform | Meaning |
---|:---:|---|
| <code>sine(t, phase=0)</code> |  | `Math.sin` normalized to `0..1` rather than radians `0..2π`. To turn into cos, set `phase=.25`. |
| <code>triangle(t, ratio=0.5)</code> |  | Triangular waveform with regulated ratio. To turn into sawtooth set `ratio=0` or `ratio=1`. |
| <code>sawtooth(t, inverse=false)</code> |  | Edge case of triangular waveform, whether descending or ascending. |
| <code>square(t, ratio=0.5)</code> |  | Rectangular waveform with regulated ratio. To turn into pulse set `ratio=0`. |
| <code>pulse(t, tlr=0)</code> |  | Delta-pulse, which is `1` at `0` and `0` anywhere else. Pass `tlr` as a precision tolerance, ie. `1e-5`. |
| <code>fourier(t, real, imag?, normalize=false)</code> |  | [Fourier Series](https://en.wikipedia.org/wiki/Fourier_series) coefficients, ie. harmonics. `0` harmonic is static level, `1`st is base frequency, `2`nd is double base frequency, `3`rd is triple etc. Set `normalize=true` to bring max harmonic to `1`. |
| <code>clausen(t, limit=10)</code> |  | [Clausen function](https://en.wikipedia.org/wiki/Clausen_function). Pass `limit` to indicate number of iterations, precision/performance tradeoff. |
| <code>step(t, samples)</code> |  | [Step function](https://en.wikipedia.org/wiki/Step_function), picks closest sample value out of a set. |
| <code>interpolate(t, samples)</code> |  | Interpolates between closest values in a sample set. |
| <code>noise(t)</code> |  | Repeated sample of noise. |
If you feel like it is not complete list of you know example of a good periodic function, suitable for dsp, welcome to [contribute](https://github.com/dfcreative/periodic-function/issues).
## Related
* [audio-oscillator](https://github.com/audiojs/audio-oscillator)
* [createPeriodicWave](https://developer.mozilla.org/en-US/docs/Web/API/AudioContext/createPeriodicWave)
* [List of periodic functions](https://en.wikipedia.org/wiki/List_of_periodic_functions)
## Credits
© 2017 Dima Yv. MIT License