History

expmap initial implementation

Paul Batchelor committed 6 years ago

Commit 52441bb1 parent 9c80d957 files · +212 −10Browse files

Unified · Split

7 files
  1. _fig/Makefile +0 −1
  2. _fig/expmap_eqn.tex +9 −0
  3. _fig/expmap_s.tex +9 −0
  4. _fig/expmap_x.tex +9 −0
  5. _fig/figs.mk +6 −0
  6. expmap.org +178 −9
  7. mkdb.janet +1 −0
_fig/Makefile+0−1
@@ -6,7 +6,6 @@ include figs.mk · 5 unmodified lines
6default: $(FIGS)6default: $(FIGS)
7 7
8%.png %2x.png: %.tex8%.png %2x.png: %.tex
9 echo $*
10 $(TEX) $<9 $(TEX) $<
11 $(DVIPNG) -D150 -o $@ $*.dvi10 $(DVIPNG) -D150 -o $@ $*.dvi
12 $(DVIPNG) -D300 -o $*2x.png $*.dvi11 $(DVIPNG) -D300 -o $*2x.png $*.dvi
_fig/expmap_eqn.tex+9−0
@@ -0,0 +1,9 @@
1\hsize 0pt
2\vsize 0pt
3\nopagenumbers
4\overfullrule 0pt
5\noindent
6$$
7{1 - e^{xs} \over 1 - e^{s}}
8$$
9\bye
_fig/expmap_s.tex+9−0
@@ -0,0 +1,9 @@
1\hsize 0pt
2\vsize 0pt
3\nopagenumbers
4\overfullrule 0pt
5\noindent
6$
7s
8$
9\bye
_fig/expmap_x.tex+9−0
@@ -0,0 +1,9 @@
1\hsize 0pt
2\vsize 0pt
3\nopagenumbers
4\overfullrule 0pt
5\noindent
6$
7x
8$
9\bye
_fig/figs.mk+6−0
@@ -37,6 +37,12 @@ chaosnoise_eqn.png \ · 36 unmodified lines
37chaosnoise_eqn2x.png \37chaosnoise_eqn2x.png \
38chaosnoise_gamma.png \38chaosnoise_gamma.png \
39chaosnoise_gamma2x.png \39chaosnoise_gamma2x.png \
40expmap_eqn.png \
41expmap_eqn2x.png \
42expmap_s.png \
43expmap_s2x.png \
44expmap_x.png \
45expmap_x2x.png \
40osc_lerp.png \46osc_lerp.png \
41osc_lerp2x.png \47osc_lerp2x.png \
42osc_lerp_simp.png \48osc_lerp_simp.png \
expmap.org+178−9
@@ -13,7 +13,7 @@ This is all there is to it: · 12 unmodified lines
13 13
14@!(fig "expmap_eqn"14@!(fig "expmap_eqn"
15"15"
16{1 - e^{x*s} \\over 1 - e^{s}}16{1 - e^{xs} \\over 1 - e^{s}}
17")!@17")!@
18 18
19Where @!(smallfig "expmap_x" "x")!@ is the normalized19Where @!(smallfig "expmap_x" "x")!@ is the normalized
@@ -26,6 +26,36 @@ staying closer to 1. As @!(smallfig "expmap_s")!@ gets more · 6 unmodified lines
26positive, the exponential curve will get more concave, with26positive, the exponential curve will get more concave, with
27more values staying closer to 0.27more values staying closer to 0.
28* Tangled Files28* Tangled Files
29=expmap.c= and =expmap.h= are the tangled files. If
30=SK_EXPMAP_PRIV= is defined, it exposes the structs.
31
32#+NAME: expmap.c
33#+BEGIN_SRC c :tangle expmap.c
34#include <math.h>
35#define SK_EXPMAP_PRIV
36#include "expmap.h"
37<<static_funcdefs>>
38<<funcs>>
39#+END_SRC
40
41#+NAME: expmap.h
42#+BEGIN_SRC c :tangle expmap.h
43#ifndef SK_EXPMAP_H
44#define SK_EXPMAP_H
45
46#ifndef SKFLT
47#define SKFLT float
48#endif
49
50<<typedefs>>
51<<funcdefs>>
52
53#ifdef SK_EXPMAP_PRIV
54<<structs>>
55#endif
56
57#endif
58#+END_SRC
29* Naive Function (stateless).59* Naive Function (stateless).
30The function =sk_expmap_stateless= will compute60The function =sk_expmap_stateless= will compute
31this exponential function without any internal state.61this exponential function without any internal state.
@@ -59,22 +89,161 @@ struct sk_expmap { · 27 unmodified lines
59 <<sk_expmap>>89 <<sk_expmap>>
60};90};
61#+END_SRC91#+END_SRC
92* Input/Ouput Caching
93To avoid computation, some caching of the input/output
94variables are used.
95
96=pin= is the cached input value, will be used to avoid
97computation. At init, it is set to be an (invalid) negative
98value.
62 99
63#+NAME: sk_expmap100#+NAME: sk_expmap
64#+BEGIN_SRC c101#+BEGIN_SRC c
65SKFLT prev;102SKFLT pin;
66SKFLT slope;
67SKFLT pslope;
68SKFLT scale;
69#+END_SRC103#+END_SRC
70 104
71=prev= is the previous input value, will be used to avoid105#+NAME: init
72computation.106#+BEGIN_SRC c
107em->pin = -1;
108#+END_SRC
109
110=pout= is the cached computed output value. It is used if no
111computation is needed. By default, it is set to be -1. It is
112assumed that this value shouldn't actually ever be returned.
73 113
74=slope= and =pslope= is the slope parameter and previous114#+NAME: sk_expmap
75slope parameter. When they don't match, =scale= is updated.115#+BEGIN_SRC c
116SKFLT pout;
117#+END_SRC
76 118
119#+NAME: init
120#+BEGIN_SRC c
121em->pout = -1;
122#+END_SRC
123* Scaling Constant
77=scale= is the constant =1/(1 - exp(slope)=. A scaler used124=scale= is the constant =1/(1 - exp(slope)=. A scaler used
78to shave off an expensive =exp= operation and =div=125to shave off an expensive =exp= operation and =div=
79operation.126operation.
127
128#+NAME: sk_expmap
129#+BEGIN_SRC c
130SKFLT scale;
131#+END_SRC
132
133The scaling value can be computed with the static function
134=compute_scale=, given an input slope.
135
136#+NAME: static_funcdefs
137#+BEGIN_SRC c
138static SKFLT compute_scale(SKFLT slope);
139#+END_SRC
140
141#+NAME: funcs
142#+BEGIN_SRC c
143static SKFLT compute_scale(SKFLT slope)
144{
145 return 1 / (1 - exp(slope));
146}
147#+END_SRC
148
149After the =slope= is initialized, the scale is initialized
150to be the slope.
151
152#+NAME: init_scale
153#+BEGIN_SRC c
154em->scale = compute_scale(em->slope);
155#+END_SRC
156* Initialization
157Initialization is done with =sk_expmap_init=.
158
159#+NAME: funcdefs
160#+BEGIN_SRC c
161void sk_expmap_init(sk_expmap *em);
162#+END_SRC
163
164#+NAME: funcs
165#+BEGIN_SRC c
166void sk_expmap_init(sk_expmap *em)
167{
168 <<init>>
169}
170#+END_SRC
171* Changing Slope
172The slope of expmap can be changed with =sk_expmap_slope=.
173
174#+NAME: funcdefs
175#+BEGIN_SRC c
176void sk_expmap_slope(sk_expmap *em, SKFLT slope);
177#+END_SRC
178
179#+NAME: funcs
180#+BEGIN_SRC c
181void sk_expmap_slope(sk_expmap *em, SKFLT slope)
182{
183 em->slope = slope;
184}
185#+END_SRC
186
187The slope parameter uses caching to avoid re-computation.
188
189#+NAME: sk_expmap
190#+BEGIN_SRC c
191SKFLT slope;
192SKFLT pslope;
193#+END_SRC
194
195The slope is set to be =1= to begin.
196
197#+NAME: init
198#+BEGIN_SRC c
199sk_expmap_slope(em, 1);
200em->pslope = 1;
201<<init_scale>>
202#+END_SRC
80* Compute203* Compute
204A single sample of audio is computed with =sk_expmap_tick=.
205
206#+NAME: funcdefs
207#+BEGIN_SRC c
208SKFLT sk_expmap_tick(sk_expmap *em, SKFLT in);
209#+END_SRC
210
211#+NAME: funcs
212#+BEGIN_SRC c
213SKFLT sk_expmap_tick(sk_expmap *em, SKFLT in)
214{
215 SKFLT out = 0;
216 <<update_scale_value>>
217 <<compute>>
218 return out;
219}
220#+END_SRC
221
222Before a sample is computed, the slope parameter is checked
223for updates. If it is updated, the scale needs to be
224re-computed.
225
226#+NAME: update_scale_value
227#+BEGIN_SRC c
228if (em->slope != em->pslope) {
229 em->pslope = em->slope;
230 em->scale = compute_scale(em->slope);
231}
232#+END_SRC
233
234Computation is done only if the input value =in= is
235different from the cached input value =pin=. If
236it is, it updates the cached output value =pout=.
237
238
239the output variable =out= is set to be the cached value
240=pout=.
241
242#+NAME: compute
243#+BEGIN_SRC c
244if (in != em->pin) {
245 em->pin = in;
246 em->pout = (1 - exp(in * em->slope)) * em->scale;
247}
248out = em->pout;
249#+END_SRC
mkdb.janet+1−0
@@ -27,6 +27,7 @@ · 26 unmodified lines
27(ww-add-link "chaosnoise" "chaosnoise.org")27(ww-add-link "chaosnoise" "chaosnoise.org")
28(ww-add-link "oscf" "oscf.org")28(ww-add-link "oscf" "oscf.org")
29(ww-add-link "bezier" "bezier.org")29(ww-add-link "bezier" "bezier.org")
30(ww-add-link "expmap" "expmap.org")
30 31
31# sync and close32# sync and close
32 33

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t