/
foldback.ts
42 lines (35 loc) · 915 Bytes
/
foldback.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { foldback as _foldback } from "@thi.ng/math";
import { AProc } from "./aproc";
/**
* Recursively folds input into `[-thresh .. +thresh]` interval and
* amplifies it with `amp` (default: 1/thresh).
*
* @remarks
* Reference:
* - https://www.desmos.com/calculator/lkyf2ag3ta
*
* @param thresh - fold threshold
* @param amp - post amplifier
*/
export const foldback = (thresh?: number, amp?: number) =>
new Foldback(thresh, amp);
export class Foldback extends AProc<number, number> {
constructor(protected _thresh = 1, protected _amp = 1 / _thresh) {
super(0);
}
next(x: number) {
return (this._val = _foldback(this._thresh, x) * this._amp);
}
threshold() {
return this._thresh;
}
setThreshold(t: number) {
this._thresh = t;
}
amp() {
return this._amp;
}
setAmp(a: number) {
this._amp = a;
}
}