Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
daisyy-hardware
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
sdiy
daisyy
daisyy-hardware
Commits
42653af1
Commit
42653af1
authored
1 year ago
by
David Huss
Browse files
Options
Downloads
Patches
Plain Diff
Add output to jupyter script
parent
3b28d50a
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
circuitsim/lookup-tables.ipynb
+36
-5
36 additions, 5 deletions
circuitsim/lookup-tables.ipynb
with
36 additions
and
5 deletions
circuitsim/lookup-tables.ipynb
+
36
−
5
View file @
42653af1
...
@@ -480,14 +480,14 @@
...
@@ -480,14 +480,14 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
1
,
"execution_count":
4
,
"id": "e51416f3-f34d-4513-9f0c-fa52c468274e",
"id": "e51416f3-f34d-4513-9f0c-fa52c468274e",
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [
{
{
"data": {
"data": {
"application/vnd.jupyter.widget-view+json": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "
e9fff586d6da4132a47d3bb55ba0a36f
",
"model_id": "
1ca5e203dc524b9d939312138ecffcea
",
"version_major": 2,
"version_major": 2,
"version_minor": 0
"version_minor": 0
},
},
...
@@ -500,11 +500,25 @@
...
@@ -500,11 +500,25 @@
},
},
{
{
"data": {
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "6418d780e5664aa6b160a0322d143213",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"text/plain": [
"
<function __main__.draw(f)>
"
"
Button(button_style='success', description='copy C++ to clipboard', style=ButtonStyle())
"
]
]
},
},
"execution_count": 1,
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"42"
]
},
"execution_count": 4,
"metadata": {},
"metadata": {},
"output_type": "execute_result"
"output_type": "execute_result"
}
}
...
@@ -515,6 +529,7 @@
...
@@ -515,6 +529,7 @@
"from ipywidgets import interact, FloatSlider\n",
"from ipywidgets import interact, FloatSlider\n",
"import matplotlib.transforms as transforms\n",
"import matplotlib.transforms as transforms\n",
"import math\n",
"import math\n",
"import clipboard\n",
"\n",
"\n",
"def draw(f):\n",
"def draw(f):\n",
" fig = plt.figure(figsize=(8, 4))\n",
" fig = plt.figure(figsize=(8, 4))\n",
...
@@ -630,8 +645,24 @@
...
@@ -630,8 +645,24 @@
"# draw(x, y, 0.45/2)\n",
"# draw(x, y, 0.45/2)\n",
"\n",
"\n",
"interact(draw, f=FloatSlider(min=min(x), max=max(x), step=0.001, value=0.2))\n",
"interact(draw, f=FloatSlider(min=min(x), max=max(x), step=0.001, value=0.2))\n",
"text = \"\"\n",
"text += \"// Lookup Table for Pitch Knob\\n\"\n",
"text += f\"float pitch_knob_x[] = {{{', '.join(str(xv) for xv in x)}}};\\n\\n\"\n",
"text += f\"float pitch_knob_y[] = {{{', '.join(str(yv) for yv in y)}}};\\n\\n\"\n",
"\n",
"import ipywidgets as widgets\n",
"from IPython.display import display, HTML, Javascript\n",
"mybtn = widgets.Button(description='copy C++ to clipboard', button_style='success')\n",
"\n",
"def mybtn_event_handler(b):\n",
" print(\"copied\")\n",
" clipboard.copy(text)\n",
"\n",
"mybtn.on_click(mybtn_event_handler)\n",
"\n",
"display(mybtn)\n",
"\n",
"\n",
"
"
"
len(x)
"
]
]
},
},
{
{
...
...
%% Cell type:code id:52c8bec3-db2d-4522-b692-b035a71410de tags:
%% Cell type:code id:52c8bec3-db2d-4522-b692-b035a71410de tags:
```
python
```
python
from
matplotlib
import
pyplot
as
plt
from
matplotlib
import
pyplot
as
plt
%
matplotlib
inline
%
matplotlib
inline
import
math
import
math
!
{
sys
.
executable
}
-
m
pip
install
clipboard
!
{
sys
.
executable
}
-
m
pip
install
clipboard
%
matplotlib
inline
%
matplotlib
inline
import
clipboard
import
clipboard
def
draw
(
r
,
g
,
b
):
def
draw
(
r
,
g
,
b
):
x
=
[
x
for
x
in
range
(
256
)]
x
=
[
x
for
x
in
range
(
256
)]
fig
=
plt
.
figure
(
figsize
=
(
8
,
4
))
fig
=
plt
.
figure
(
figsize
=
(
8
,
4
))
ax
=
fig
.
add_axes
([
0
,
0
,
1
,
1
])
ax
=
fig
.
add_axes
([
0
,
0
,
1
,
1
])
ax
.
axhline
(
y
=
0.5
,
color
=
'
black
'
,
linestyle
=
'
--
'
)
ax
.
axhline
(
y
=
0.5
,
color
=
'
black
'
,
linestyle
=
'
--
'
)
ax
.
set_xticks
(
range
(
0
,
256
,
64
))
ax
.
set_xticks
(
range
(
0
,
256
,
64
))
ax
.
set_yticks
(
range
(
-
256
*
2
,
256
*
2
+
1
,
128
))
ax
.
set_yticks
(
range
(
-
256
*
2
,
256
*
2
+
1
,
128
))
ax
.
grid
()
ax
.
grid
()
ax
.
plot
(
x
,
r
,
'
r
'
)
ax
.
plot
(
x
,
r
,
'
r
'
)
ax
.
plot
(
x
,
g
,
'
g
'
)
ax
.
plot
(
x
,
g
,
'
g
'
)
ax
.
plot
(
x
,
b
,
'
b
'
)
ax
.
plot
(
x
,
b
,
'
b
'
)
def
r
():
def
r
():
start
=
100
start
=
100
start2
=
220
start2
=
220
last_a
=
0
last_a
=
0
for
i
in
range
(
256
):
for
i
in
range
(
256
):
if
i
<
start
:
if
i
<
start
:
yield
0
yield
0
elif
i
<
start2
:
elif
i
<
start2
:
span
=
255
-
start
span
=
255
-
start
d
=
(
i
-
start
)
/
span
d
=
(
i
-
start
)
/
span
last_a
=
int
(
d
*
30.0
)
last_a
=
int
(
d
*
30.0
)
yield
min
(
255
,
last_a
)
yield
min
(
255
,
last_a
)
else
:
else
:
span
=
255
-
start2
span
=
255
-
start2
d
=
(
i
-
start2
)
/
span
d
=
(
i
-
start2
)
/
span
d
=
d
*
d
*
d
d
=
d
*
d
*
d
yield
min
(
255
,
last_a
+
int
(
d
*
350.0
))
yield
min
(
255
,
last_a
+
int
(
d
*
350.0
))
def
g
():
def
g
():
start
=
0
start
=
0
end
=
180
-
80
end
=
180
-
80
scale
=
0.25
scale
=
0.25
for
i
in
range
(
256
):
for
i
in
range
(
256
):
if
i
<
start
:
if
i
<
start
:
yield
0
yield
0
elif
i
>
end
:
elif
i
>
end
:
d
=
1.0
-
((
i
-
end
)
/
(
295
-
end
))
d
=
1.0
-
((
i
-
end
)
/
(
295
-
end
))
# d = (d*d)/4 + d/2
# d = (d*d)/4 + d/2
yield
max
(
0
,
min
(
255
,
int
(
d
*
175
*
scale
)))
yield
max
(
0
,
min
(
255
,
int
(
d
*
175
*
scale
)))
else
:
else
:
d
=
((
i
-
start
)
/
(
255
-
start
))
d
=
((
i
-
start
)
/
(
255
-
start
))
d
=
(
d
*
d
*
d
)
d
=
(
d
*
d
*
d
)
yield
min
(
255
,
int
(
d
*
2800
*
scale
))
yield
min
(
255
,
int
(
d
*
2800
*
scale
))
def
b
():
def
b
():
start
=
4
start
=
4
end
=
40
end
=
40
scale
=
0.2
scale
=
0.2
for
i
in
range
(
256
):
for
i
in
range
(
256
):
if
i
<
start
:
if
i
<
start
:
yield
0
yield
0
elif
i
>
end
:
elif
i
>
end
:
d
=
(
i
-
end
)
/
(
60
)
d
=
(
i
-
end
)
/
(
60
)
d
=
d
*
d
d
=
d
*
d
d
=
1.0
-
d
d
=
1.0
-
d
# d = math.sqrt(d)
# d = math.sqrt(d)
yield
max
(
0
,
min
(
255
,
int
(
d
*
32
*
scale
)))
yield
max
(
0
,
min
(
255
,
int
(
d
*
32
*
scale
)))
else
:
else
:
d
=
(
i
-
start
)
/
(
255
-
start
)
d
=
(
i
-
start
)
/
(
255
-
start
)
d
=
math
.
sqrt
(
d
)
/
2
+
d
/
2
d
=
math
.
sqrt
(
d
)
/
2
+
d
/
2
yield
max
(
0
,
min
(
255
,
int
(
d
*
107
*
scale
)))
yield
max
(
0
,
min
(
255
,
int
(
d
*
107
*
scale
)))
r
,
g
,
b
=
list
(
r
()),
list
(
g
()),
list
(
b
())
r
,
g
,
b
=
list
(
r
()),
list
(
g
()),
list
(
b
())
draw
(
r
,
g
,
b
)
draw
(
r
,
g
,
b
)
text
=
""
text
=
""
text
+=
"
// Lookup Table for Red LED Channel
\n
"
text
+=
"
// Lookup Table for Red LED Channel
\n
"
text
+=
f
"
int red_lookup[] = {{
{
'
,
'
.
join
(
str
(
v
)
for
v
in
r
)
}
}};
\n\n
"
text
+=
f
"
int red_lookup[] = {{
{
'
,
'
.
join
(
str
(
v
)
for
v
in
r
)
}
}};
\n\n
"
text
+=
"
// Lookup Table for Green LED Channel
\n
"
text
+=
"
// Lookup Table for Green LED Channel
\n
"
text
+=
f
"
int green_lookup[] = {{
{
'
,
'
.
join
(
str
(
v
)
for
v
in
g
)
}
}};
\n\n
"
text
+=
f
"
int green_lookup[] = {{
{
'
,
'
.
join
(
str
(
v
)
for
v
in
g
)
}
}};
\n\n
"
text
+=
"
// Lookup Table for Blue LED Channel
\n
"
text
+=
"
// Lookup Table for Blue LED Channel
\n
"
text
+=
f
"
int blue_lookup[] = {{
{
'
,
'
.
join
(
str
(
v
)
for
v
in
b
)
}
}};
\n\n
"
text
+=
f
"
int blue_lookup[] = {{
{
'
,
'
.
join
(
str
(
v
)
for
v
in
b
)
}
}};
\n\n
"
import
ipywidgets
as
widgets
import
ipywidgets
as
widgets
from
IPython.display
import
display
,
HTML
,
Javascript
from
IPython.display
import
display
,
HTML
,
Javascript
mybtn
=
widgets
.
Button
(
description
=
'
copy C++ to clipboard
'
,
button_style
=
'
success
'
)
mybtn
=
widgets
.
Button
(
description
=
'
copy C++ to clipboard
'
,
button_style
=
'
success
'
)
def
mybtn_event_handler
(
b
):
def
mybtn_event_handler
(
b
):
print
(
"
copied
"
)
print
(
"
copied
"
)
clipboard
.
copy
(
text
)
clipboard
.
copy
(
text
)
mybtn
.
on_click
(
mybtn_event_handler
)
mybtn
.
on_click
(
mybtn_event_handler
)
display
(
mybtn
)
display
(
mybtn
)
```
```
%% Output
%% Output
zsh:1: parse error near `-m'
zsh:1: parse error near `-m'
%% Cell type:code id:41562cc6-9911-4fb1-87ec-f2b3c8bfb3c2 tags:
%% Cell type:code id:41562cc6-9911-4fb1-87ec-f2b3c8bfb3c2 tags:
```
python
```
python
from
matplotlib
import
pyplot
as
plt
from
matplotlib
import
pyplot
as
plt
%
matplotlib
inline
%
matplotlib
inline
import
math
import
math
def
draw
(
r
):
def
draw
(
r
):
l
=
len
(
r
)
l
=
len
(
r
)
x
=
[
x
for
x
in
range
(
l
)]
x
=
[
x
for
x
in
range
(
l
)]
fig
=
plt
.
figure
(
figsize
=
(
8
,
4
))
fig
=
plt
.
figure
(
figsize
=
(
8
,
4
))
ax
=
fig
.
add_axes
([
0
,
0
,
1
,
1
])
ax
=
fig
.
add_axes
([
0
,
0
,
1
,
1
])
ax
.
axhline
(
y
=
0.5
,
color
=
'
black
'
,
linestyle
=
'
--
'
)
ax
.
axhline
(
y
=
0.5
,
color
=
'
black
'
,
linestyle
=
'
--
'
)
ax
.
axvline
(
x
=
l
/
2
,
color
=
'
black
'
,
linestyle
=
'
--
'
)
ax
.
axvline
(
x
=
l
/
2
,
color
=
'
black
'
,
linestyle
=
'
--
'
)
ax
.
set_xticks
(
range
(
0
,
l
,
64
))
ax
.
set_xticks
(
range
(
0
,
l
,
64
))
ax
.
set_yticks
(
range
(
-
l
*
2
,
l
*
2
+
1
,
128
))
ax
.
set_yticks
(
range
(
-
l
*
2
,
l
*
2
+
1
,
128
))
ax
.
grid
()
ax
.
grid
()
ax
.
plot
(
x
,
r
)
ax
.
plot
(
x
,
r
)
def
deadband
(
length
,
deadband
=
0.04
):
def
deadband
(
length
,
deadband
=
0.04
):
readings
=
[]
readings
=
[]
for
i
in
range
(
length
):
for
i
in
range
(
length
):
current_reading
=
i
/
(
length
-
1
)
current_reading
=
i
/
(
length
-
1
)
scaler
=
(
1.0
)
/
(
1.0
-
deadband
)
scaler
=
(
1.0
)
/
(
1.0
-
deadband
)
scaler
+=
0.1
scaler
+=
0.1
if
current_reading
<
0.5
:
if
current_reading
<
0.5
:
current_reading
+=
deadband
current_reading
+=
deadband
current_reading
=
min
(
0.5
,
current_reading
)
current_reading
=
min
(
0.5
,
current_reading
)
current_reading
=
0.5
-
current_reading
current_reading
=
0.5
-
current_reading
current_reading
*=
scaler
current_reading
*=
scaler
current_reading
=
0.5
-
current_reading
current_reading
=
0.5
-
current_reading
# current_reading =
# current_reading =
else
:
else
:
current_reading
-=
deadband
current_reading
-=
deadband
current_reading
=
max
(
0.5
,
current_reading
)
current_reading
=
max
(
0.5
,
current_reading
)
current_reading
=
0.5
-
current_reading
current_reading
=
0.5
-
current_reading
current_reading
*=
scaler
current_reading
*=
scaler
current_reading
=
0.5
-
current_reading
current_reading
=
0.5
-
current_reading
val
=
min
(
length
,
max
(
0
,
current_reading
))
val
=
min
(
length
,
max
(
0
,
current_reading
))
readings
.
append
(
val
)
readings
.
append
(
val
)
return
readings
return
readings
bip
=
deadband
(
16
,
deadband
=
0.08
)
bip
=
deadband
(
16
,
deadband
=
0.08
)
draw
(
bip
)
draw
(
bip
)
text
=
""
text
=
""
text
+=
"
// Lookup Table for Bipolar Curve with deadband
\n
"
text
+=
"
// Lookup Table for Bipolar Curve with deadband
\n
"
text
+=
f
"
float bip_lookup[] = {{
{
'
,
'
.
join
(
str
(
v
)
for
v
in
bip
)
}
}};
\n\n
"
text
+=
f
"
float bip_lookup[] = {{
{
'
,
'
.
join
(
str
(
v
)
for
v
in
bip
)
}
}};
\n\n
"
import
ipywidgets
as
widgets
import
ipywidgets
as
widgets
from
IPython.display
import
display
,
HTML
,
Javascript
from
IPython.display
import
display
,
HTML
,
Javascript
mybtn
=
widgets
.
Button
(
description
=
'
copy C++ to clipboard
'
,
button_style
=
'
success
'
)
mybtn
=
widgets
.
Button
(
description
=
'
copy C++ to clipboard
'
,
button_style
=
'
success
'
)
def
mybtn_event_handler
(
b
):
def
mybtn_event_handler
(
b
):
print
(
"
copied
"
)
print
(
"
copied
"
)
clipboard
.
copy
(
text
)
clipboard
.
copy
(
text
)
mybtn
.
on_click
(
mybtn_event_handler
)
mybtn
.
on_click
(
mybtn_event_handler
)
display
(
mybtn
)
display
(
mybtn
)
len
(
bip
)
len
(
bip
)
```
```
%% Output
%% Output
16
16
%% Cell type:code id:ecc666b0-8195-4276-a576-39d41753b540 tags:
%% Cell type:code id:ecc666b0-8195-4276-a576-39d41753b540 tags:
```
python
```
python
from
matplotlib
import
pyplot
as
plt
from
matplotlib
import
pyplot
as
plt
%
matplotlib
inline
%
matplotlib
inline
import
math
import
math
import
sys
import
sys
def
draw
(
r
):
def
draw
(
r
):
l
=
len
(
r
)
l
=
len
(
r
)
x
=
[
x
for
x
in
range
(
l
)]
x
=
[
x
for
x
in
range
(
l
)]
fig
=
plt
.
figure
(
figsize
=
(
8
,
4
))
fig
=
plt
.
figure
(
figsize
=
(
8
,
4
))
ax
=
fig
.
add_axes
([
0
,
0
,
1
,
1
])
ax
=
fig
.
add_axes
([
0
,
0
,
1
,
1
])
ax
.
axhline
(
y
=
0.5
,
color
=
'
black
'
,
linestyle
=
'
--
'
)
ax
.
axhline
(
y
=
0.5
,
color
=
'
black
'
,
linestyle
=
'
--
'
)
ax
.
set_xticks
(
range
(
0
,
l
,
64
))
ax
.
set_xticks
(
range
(
0
,
l
,
64
))
ax
.
set_yticks
(
range
(
-
l
*
2
,
l
*
2
+
1
,
128
))
ax
.
set_yticks
(
range
(
-
l
*
2
,
l
*
2
+
1
,
128
))
ax
.
grid
()
ax
.
grid
()
ax
.
plot
(
x
,
r
)
ax
.
plot
(
x
,
r
)
def
lin_to_log
(
length
,
strength
=
1.0
):
def
lin_to_log
(
length
,
strength
=
1.0
):
# Limit to range 0.0 and 1.0
# Limit to range 0.0 and 1.0
strength
=
min
(
1.0
,
max
(
0.0
,
strength
))
strength
=
min
(
1.0
,
max
(
0.0
,
strength
))
readings
=
[]
readings
=
[]
linear_readings
=
[]
linear_readings
=
[]
for
i
in
range
(
length
):
for
i
in
range
(
length
):
current_reading
=
i
/
length
current_reading
=
i
/
length
linear_readings
.
append
(
current_reading
)
linear_readings
.
append
(
current_reading
)
# Log of 0 is error, so handle it explicitly
# Log of 0 is error, so handle it explicitly
if
i
==
0
:
if
i
==
0
:
current_reading
=
0.0
current_reading
=
0.0
else
:
else
:
current_reading
=
math
.
log10
(
i
)
current_reading
=
math
.
log10
(
i
)
readings
.
append
(
current_reading
)
readings
.
append
(
current_reading
)
# Normalize to scale 0.1 to one
# Normalize to scale 0.1 to one
maxima
=
max
(
readings
)
maxima
=
max
(
readings
)
scaler
=
1.0
/
maxima
scaler
=
1.0
/
maxima
readings
=
[
r
*
scaler
for
r
in
readings
]
readings
=
[
r
*
scaler
for
r
in
readings
]
output
=
[]
output
=
[]
for
i
,
r
in
enumerate
(
readings
):
for
i
,
r
in
enumerate
(
readings
):
val
=
r
*
strength
+
linear_readings
[
i
]
*
(
1.0
-
strength
)
val
=
r
*
strength
+
linear_readings
[
i
]
*
(
1.0
-
strength
)
output
.
append
(
val
)
output
.
append
(
val
)
# Convert to integer value range
# Convert to integer value range
output
=
[
o
for
o
in
output
]
output
=
[
o
for
o
in
output
]
return
output
return
output
# lilo = lin_to_log(4096, strength=1.0)
# lilo = lin_to_log(4096, strength=1.0)
lilo
=
lin_to_log
(
32
,
strength
=
1.0
)
lilo
=
lin_to_log
(
32
,
strength
=
1.0
)
# lilo = [l/256.0 for l in lilo]
# lilo = [l/256.0 for l in lilo]
draw
(
lilo
)
draw
(
lilo
)
text
=
""
text
=
""
text
+=
"
// Lookup Table for Logarithmic Curve
\n
"
text
+=
"
// Lookup Table for Logarithmic Curve
\n
"
text
+=
f
"
float log_lookup[] = {{
{
'
,
'
.
join
(
str
(
v
)
for
v
in
lilo
)
}
}};
\n\n
"
text
+=
f
"
float log_lookup[] = {{
{
'
,
'
.
join
(
str
(
v
)
for
v
in
lilo
)
}
}};
\n\n
"
# print(text)
# print(text)
import
ipywidgets
as
widgets
import
ipywidgets
as
widgets
from
IPython.display
import
display
,
HTML
,
Javascript
from
IPython.display
import
display
,
HTML
,
Javascript
mybtn
=
widgets
.
Button
(
description
=
'
copy C++ to clipboard
'
,
button_style
=
'
success
'
)
mybtn
=
widgets
.
Button
(
description
=
'
copy C++ to clipboard
'
,
button_style
=
'
success
'
)
def
mybtn_event_handler
(
b
):
def
mybtn_event_handler
(
b
):
print
(
"
copied
"
)
print
(
"
copied
"
)
clipboard
.
copy
(
text
)
clipboard
.
copy
(
text
)
mybtn
.
on_click
(
mybtn_event_handler
)
mybtn
.
on_click
(
mybtn_event_handler
)
display
(
mybtn
)
display
(
mybtn
)
min
(
lilo
)
min
(
lilo
)
```
```
%% Output
%% Output
0.0
0.0
%% Cell type:code id:2ecfda42-4a3c-489a-bc90-8576648c339c tags:
%% Cell type:code id:2ecfda42-4a3c-489a-bc90-8576648c339c tags:
```
python
```
python
from
matplotlib
import
pyplot
as
plt
from
matplotlib
import
pyplot
as
plt
%
matplotlib
inline
%
matplotlib
inline
import
math
import
math
import
sys
import
sys
def
draw
(
r
):
def
draw
(
r
):
l
=
len
(
r
)
l
=
len
(
r
)
x
=
[
x
for
x
in
range
(
l
)]
x
=
[
x
for
x
in
range
(
l
)]
fig
=
plt
.
figure
(
figsize
=
(
8
,
4
))
fig
=
plt
.
figure
(
figsize
=
(
8
,
4
))
ax
=
fig
.
add_axes
([
0
,
0
,
1
,
1
])
ax
=
fig
.
add_axes
([
0
,
0
,
1
,
1
])
ax
.
axhline
(
y
=
0.5
,
color
=
'
black
'
,
linestyle
=
'
--
'
)
ax
.
axhline
(
y
=
0.5
,
color
=
'
black
'
,
linestyle
=
'
--
'
)
ax
.
set_xticks
(
range
(
0
,
l
,
64
))
ax
.
set_xticks
(
range
(
0
,
l
,
64
))
ax
.
set_yticks
(
range
(
-
l
*
2
,
l
*
2
+
1
,
128
))
ax
.
set_yticks
(
range
(
-
l
*
2
,
l
*
2
+
1
,
128
))
ax
.
grid
()
ax
.
grid
()
ax
.
plot
(
x
,
r
)
ax
.
plot
(
x
,
r
)
def
exp_lookup
(
length
,
strength
=
1.0
):
def
exp_lookup
(
length
,
strength
=
1.0
):
# Limit to range 0.0 and 1.0
# Limit to range 0.0 and 1.0
strength
=
min
(
1.0
,
max
(
0.0
,
strength
))
strength
=
min
(
1.0
,
max
(
0.0
,
strength
))
readings
=
[]
readings
=
[]
linear_readings
=
[]
linear_readings
=
[]
for
i
in
range
(
length
):
for
i
in
range
(
length
):
current_reading
=
i
/
length
current_reading
=
i
/
length
linear_readings
.
append
(
current_reading
)
linear_readings
.
append
(
current_reading
)
# Log of 0 is error, so handle it explicitly
# Log of 0 is error, so handle it explicitly
if
i
==
0
:
if
i
==
0
:
current_reading
=
0.0
current_reading
=
0.0
else
:
else
:
current_reading
=
i
*
i
current_reading
=
i
*
i
readings
.
append
(
current_reading
)
readings
.
append
(
current_reading
)
# Normalize to scale 0.1 to one
# Normalize to scale 0.1 to one
maxima
=
max
(
readings
)
maxima
=
max
(
readings
)
scaler
=
1.0
/
maxima
scaler
=
1.0
/
maxima
readings
=
[
r
*
scaler
for
r
in
readings
]
readings
=
[
r
*
scaler
for
r
in
readings
]
output
=
[]
output
=
[]
for
i
,
r
in
enumerate
(
readings
):
for
i
,
r
in
enumerate
(
readings
):
val
=
r
*
strength
+
linear_readings
[
i
]
*
(
1.0
-
strength
)
val
=
r
*
strength
+
linear_readings
[
i
]
*
(
1.0
-
strength
)
output
.
append
(
val
)
output
.
append
(
val
)
# Convert to integer value range
# Convert to integer value range
output
=
[
o
for
o
in
output
]
output
=
[
o
for
o
in
output
]
return
output
return
output
# lilo = lin_to_log(4096, strength=1.0)
# lilo = lin_to_log(4096, strength=1.0)
lilo
=
exp_lookup
(
8
,
strength
=
1.0
)
lilo
=
exp_lookup
(
8
,
strength
=
1.0
)
draw
(
lilo
)
draw
(
lilo
)
text
=
""
text
=
""
text
+=
"
// Lookup Table for Exponential Curve
\n
"
text
+=
"
// Lookup Table for Exponential Curve
\n
"
text
+=
f
"
float exp_lookup[] = {{
{
'
,
'
.
join
(
str
(
v
)
for
v
in
lilo
)
}
}};
\n\n
"
text
+=
f
"
float exp_lookup[] = {{
{
'
,
'
.
join
(
str
(
v
)
for
v
in
lilo
)
}
}};
\n\n
"
# print(text)
# print(text)
import
ipywidgets
as
widgets
import
ipywidgets
as
widgets
from
IPython.display
import
display
,
HTML
,
Javascript
from
IPython.display
import
display
,
HTML
,
Javascript
mybtn
=
widgets
.
Button
(
description
=
'
copy C++ to clipboard
'
,
button_style
=
'
success
'
)
mybtn
=
widgets
.
Button
(
description
=
'
copy C++ to clipboard
'
,
button_style
=
'
success
'
)
def
mybtn_event_handler
(
b
):
def
mybtn_event_handler
(
b
):
print
(
"
copied
"
)
print
(
"
copied
"
)
clipboard
.
copy
(
text
)
clipboard
.
copy
(
text
)
mybtn
.
on_click
(
mybtn_event_handler
)
mybtn
.
on_click
(
mybtn_event_handler
)
display
(
mybtn
)
display
(
mybtn
)
max
(
lilo
)
max
(
lilo
)
```
```
%% Output
%% Output
0.9999999999999999
0.9999999999999999
%% Cell type:code id:e51416f3-f34d-4513-9f0c-fa52c468274e tags:
%% Cell type:code id:e51416f3-f34d-4513-9f0c-fa52c468274e tags:
```
python
```
python
from
matplotlib
import
pyplot
as
plt
from
matplotlib
import
pyplot
as
plt
%
matplotlib
inline
%
matplotlib
inline
from
ipywidgets
import
interact
,
FloatSlider
from
ipywidgets
import
interact
,
FloatSlider
import
matplotlib.transforms
as
transforms
import
matplotlib.transforms
as
transforms
import
math
import
math
import
clipboard
def
draw
(
f
):
def
draw
(
f
):
fig
=
plt
.
figure
(
figsize
=
(
8
,
4
))
fig
=
plt
.
figure
(
figsize
=
(
8
,
4
))
ax
=
fig
.
add_axes
([
0
,
0
,
1
,
1
])
ax
=
fig
.
add_axes
([
0
,
0
,
1
,
1
])
b
=
scan2d
(
x
,
y
,
f
)
b
=
scan2d
(
x
,
y
,
f
)
ax
.
axhline
(
y
=
b
,
color
=
'
red
'
,
linestyle
=
'
--
'
)
ax
.
axhline
(
y
=
b
,
color
=
'
red
'
,
linestyle
=
'
--
'
)
ax
.
axvline
(
x
=
f
,
color
=
'
red
'
,
linestyle
=
'
--
'
)
ax
.
axvline
(
x
=
f
,
color
=
'
red
'
,
linestyle
=
'
--
'
)
trans
=
transforms
.
blended_transform_factory
(
trans
=
transforms
.
blended_transform_factory
(
ax
.
get_yticklabels
()[
0
].
get_transform
(),
ax
.
transData
)
ax
.
get_yticklabels
()[
0
].
get_transform
(),
ax
.
transData
)
ax
.
text
(
0.95
,
b
,
"
{:.02f}
"
.
format
(
b
),
color
=
"
red
"
,
transform
=
trans
,
ha
=
"
right
"
,
va
=
"
bottom
"
)
ax
.
text
(
0.95
,
b
,
"
{:.02f}
"
.
format
(
b
),
color
=
"
red
"
,
transform
=
trans
,
ha
=
"
right
"
,
va
=
"
bottom
"
)
ax
.
grid
()
ax
.
grid
()
ax
.
plot
(
x
,
y
)
ax
.
plot
(
x
,
y
)
def
lerp
(
a
,
b
,
f
=
0.5
)
->
float
:
def
lerp
(
a
,
b
,
f
=
0.5
)
->
float
:
f
=
min
(
1.0
,
max
(
0.0
,
f
))
f
=
min
(
1.0
,
max
(
0.0
,
f
))
if
f
==
0.0
:
if
f
==
0.0
:
return
a
return
a
elif
f
==
1.0
:
elif
f
==
1.0
:
return
b
return
b
else
:
else
:
return
a
*
(
1.0
-
f
)
+
b
*
f
return
a
*
(
1.0
-
f
)
+
b
*
f
def
lerp2d
(
x1
,
y1
,
x2
,
y2
,
f
=
0.5
):
def
lerp2d
(
x1
,
y1
,
x2
,
y2
,
f
=
0.5
):
if
f
==
0.0
:
if
f
==
0.0
:
return
[
x1
,
x2
]
return
[
x1
,
x2
]
elif
f
==
1.0
:
elif
f
==
1.0
:
return
[
x1
,
x2
]
return
[
x1
,
x2
]
else
:
else
:
x
=
lerp
(
x1
,
x2
,
f
)
x
=
lerp
(
x1
,
x2
,
f
)
y
=
lerp
(
y1
,
y2
,
f
)
y
=
lerp
(
y1
,
y2
,
f
)
return
[
x
,
y
]
return
[
x
,
y
]
# A function that scans through two lists representing x/y values using a
# A function that scans through two lists representing x/y values using a
# third value called f and returns the linear interpolation between those points
# third value called f and returns the linear interpolation between those points
def
scan2d
(
x
,
y
,
f
):
def
scan2d
(
x
,
y
,
f
):
# f = min(1.0, max(0.0, f))
# f = min(1.0, max(0.0, f))
assert
len
(
x
)
==
len
(
y
)
assert
len
(
x
)
==
len
(
y
)
# Find ax and bx for given factor
# Find ax and bx for given factor
xa
=
None
xa
=
None
last_value
=
None
last_value
=
None
idx
=
None
idx
=
None
for
i
,
v
in
enumerate
(
x
):
for
i
,
v
in
enumerate
(
x
):
# this = abs(f-v)
# this = abs(f-v)
this
=
f
-
v
this
=
f
-
v
if
xa
is
None
or
this
>
0
:
if
xa
is
None
or
this
>
0
:
xa
=
this
xa
=
this
idx
=
i
idx
=
i
idx2
=
min
(
idx
+
1
,
len
(
x
)
-
1
)
idx2
=
min
(
idx
+
1
,
len
(
x
)
-
1
)
if
idx
==
idx2
:
if
idx
==
idx2
:
return
y
[
idx
]
return
y
[
idx
]
xa
=
x
[
idx
]
xa
=
x
[
idx
]
xb
=
x
[
idx2
]
xb
=
x
[
idx2
]
ya
=
y
[
idx
]
ya
=
y
[
idx
]
yb
=
y
[
idx2
]
yb
=
y
[
idx2
]
xspan
=
xb
-
xa
xspan
=
xb
-
xa
xscaler
=
1
/
xspan
xscaler
=
1
/
xspan
new_f
=
(
f
-
xa
)
*
xscaler
new_f
=
(
f
-
xa
)
*
xscaler
return
lerp
(
ya
,
yb
,
new_f
)
return
lerp
(
ya
,
yb
,
new_f
)
lines_orig
=
[
lines_orig
=
[
[
0.0
,
0.0
,
-
0.5
],
[
0.0
,
0.0
,
-
0.5
],
[
0.45
,
0.5
,
0.0
],
[
0.45
,
0.5
,
0.0
],
[
0.55
,
0.5
,
1.0
],
[
0.55
,
0.5
,
1.0
],
[
1.0
,
1.0
,
0.0
],
[
1.0
,
1.0
,
0.0
],
]
]
half_deadband
=
0.1
half_deadband
=
0.1
lines_orig
=
[
lines_orig
=
[
[
-
10.0
,
-
10.0
,
0.0
],
[
-
10.0
,
-
10.0
,
0.0
],
[
-
10.0
+
half_deadband
,
-
10.0
,
0.0
],
[
-
10.0
+
half_deadband
,
-
10.0
,
0.0
],
]
]
steps
=
list
(
range
(
-
9
,
11
))
steps
=
list
(
range
(
-
9
,
11
))
for
i
in
steps
:
for
i
in
steps
:
f
=
float
(
i
)
f
=
float
(
i
)
lines_orig
.
append
([
f
-
half_deadband
,
f
,
0.0
])
lines_orig
.
append
([
f
-
half_deadband
,
f
,
0.0
])
lines_orig
.
append
([
f
+
half_deadband
,
f
,
0.0
])
lines_orig
.
append
([
f
+
half_deadband
,
f
,
0.0
])
# Calculate curves for points of curvature
# Calculate curves for points of curvature
lines
=
[]
lines
=
[]
for
i
,
l
in
enumerate
(
lines_orig
):
for
i
,
l
in
enumerate
(
lines_orig
):
i2
=
min
(
len
(
lines_orig
)
-
1
,
i
+
1
)
i2
=
min
(
len
(
lines_orig
)
-
1
,
i
+
1
)
if
l
[
2
]
==
0.0
:
if
l
[
2
]
==
0.0
:
lines
.
append
(
l
)
lines
.
append
(
l
)
else
:
else
:
xa
=
lines_orig
[
i
][
0
]
xa
=
lines_orig
[
i
][
0
]
xb
=
lines_orig
[
i2
][
0
]
xb
=
lines_orig
[
i2
][
0
]
ya
=
lines_orig
[
i
][
1
]
ya
=
lines_orig
[
i
][
1
]
yb
=
lines_orig
[
i2
][
1
]
yb
=
lines_orig
[
i2
][
1
]
x_span
=
xb
-
xa
x_span
=
xb
-
xa
y_span
=
yb
-
ya
y_span
=
yb
-
ya
x_step
=
1
/
20
x_step
=
1
/
20
y_step
=
1
/
20
y_step
=
1
/
20
for
j
in
range
(
20
):
for
j
in
range
(
20
):
x
=
x_step
*
j
x
=
x_step
*
j
y
=
y_step
*
j
y
=
y_step
*
j
y_curve
=
0
y_curve
=
0
if
l
[
2
]
>
0.0
:
if
l
[
2
]
>
0.0
:
y_curve
=
y
*
y
*
y
y_curve
=
y
*
y
*
y
else
:
else
:
y_curve
=
y
*
y
y_curve
=
y
*
y
y
=
(
1.0
-
l
[
2
])
*
y
+
l
[
2
]
*
y_curve
y
=
(
1.0
-
l
[
2
])
*
y
+
l
[
2
]
*
y_curve
lines
.
append
([
xa
+
x
*
x_span
,
ya
+
y
*
y_span
,
0.0
])
lines
.
append
([
xa
+
x
*
x_span
,
ya
+
y
*
y_span
,
0.0
])
x
=
[
a
[
0
]
for
a
in
lines
]
x
=
[
a
[
0
]
for
a
in
lines
]
y
=
[
a
[
1
]
for
a
in
lines
]
y
=
[
a
[
1
]
for
a
in
lines
]
c
=
[
a
[
2
]
for
a
in
lines
]
c
=
[
a
[
2
]
for
a
in
lines
]
# draw(x, y, 0.45/2)
# draw(x, y, 0.45/2)
interact
(
draw
,
f
=
FloatSlider
(
min
=
min
(
x
),
max
=
max
(
x
),
step
=
0.001
,
value
=
0.2
))
interact
(
draw
,
f
=
FloatSlider
(
min
=
min
(
x
),
max
=
max
(
x
),
step
=
0.001
,
value
=
0.2
))
text
=
""
text
+=
"
// Lookup Table for Pitch Knob
\n
"
text
+=
f
"
float pitch_knob_x[] = {{
{
'
,
'
.
join
(
str
(
xv
)
for
xv
in
x
)
}
}};
\n\n
"
text
+=
f
"
float pitch_knob_y[] = {{
{
'
,
'
.
join
(
str
(
yv
)
for
yv
in
y
)
}
}};
\n\n
"
import
ipywidgets
as
widgets
from
IPython.display
import
display
,
HTML
,
Javascript
mybtn
=
widgets
.
Button
(
description
=
'
copy C++ to clipboard
'
,
button_style
=
'
success
'
)
def
mybtn_event_handler
(
b
):
print
(
"
copied
"
)
clipboard
.
copy
(
text
)
mybtn
.
on_click
(
mybtn_event_handler
)
display
(
mybtn
)
len
(
x
)
```
```
%% Output
%% Output
<function __main__.draw(f)>
42
%% Cell type:code id:f35f1609-3a10-4dce-b7dd-201d79f2c39c tags:
%% Cell type:code id:f35f1609-3a10-4dce-b7dd-201d79f2c39c tags:
```
python
```
python
# Saturation curve
# Saturation curve
# X / Y / Curvature
# X / Y / Curvature
lines_orig
=
[
lines_orig
=
[
[
-
1.5
,
-
1.0
,
1.0
],
[
-
1.5
,
-
1.0
,
1.0
],
[
-
0.7
,
-
0.7
,
0.0
],
[
-
0.7
,
-
0.7
,
0.0
],
[
0.0
,
0.0
,
0.0
],
[
0.0
,
0.0
,
0.0
],
[
0.7
,
0.7
,
-
1.4
],
[
0.7
,
0.7
,
-
1.4
],
[
1.5
,
1.0
,
0.0
],
[
1.5
,
1.0
,
0.0
],
]
]
lines
=
[]
lines
=
[]
for
i
,
l
in
enumerate
(
lines_orig
):
for
i
,
l
in
enumerate
(
lines_orig
):
i2
=
min
(
len
(
lines_orig
)
-
1
,
i
+
1
)
i2
=
min
(
len
(
lines_orig
)
-
1
,
i
+
1
)
if
l
[
2
]
==
0.0
:
if
l
[
2
]
==
0.0
:
lines
.
append
(
l
)
lines
.
append
(
l
)
else
:
else
:
xa
=
lines_orig
[
i
][
0
]
xa
=
lines_orig
[
i
][
0
]
xb
=
lines_orig
[
i2
][
0
]
xb
=
lines_orig
[
i2
][
0
]
ya
=
lines_orig
[
i
][
1
]
ya
=
lines_orig
[
i
][
1
]
yb
=
lines_orig
[
i2
][
1
]
yb
=
lines_orig
[
i2
][
1
]
x_span
=
xb
-
xa
x_span
=
xb
-
xa
y_span
=
yb
-
ya
y_span
=
yb
-
ya
x_step
=
1
/
20
x_step
=
1
/
20
y_step
=
1
/
20
y_step
=
1
/
20
for
j
in
range
(
20
):
for
j
in
range
(
20
):
x
=
x_step
*
j
x
=
x_step
*
j
y
=
y_step
*
j
y
=
y_step
*
j
y_curve
=
0
y_curve
=
0
if
l
[
2
]
>
0.0
:
if
l
[
2
]
>
0.0
:
y_curve
=
y
*
y
*
y
y_curve
=
y
*
y
*
y
else
:
else
:
y_curve
=
y
*
y
y_curve
=
y
*
y
y
=
(
1.0
-
l
[
2
])
*
y
+
l
[
2
]
*
y_curve
y
=
(
1.0
-
l
[
2
])
*
y
+
l
[
2
]
*
y_curve
lines
.
append
([
xa
+
x
*
x_span
,
ya
+
y
*
y_span
,
0.0
])
lines
.
append
([
xa
+
x
*
x_span
,
ya
+
y
*
y_span
,
0.0
])
x
=
[
a
[
0
]
for
a
in
lines
]
x
=
[
a
[
0
]
for
a
in
lines
]
y
=
[
a
[
1
]
for
a
in
lines
]
y
=
[
a
[
1
]
for
a
in
lines
]
c
=
[
a
[
2
]
for
a
in
lines
]
c
=
[
a
[
2
]
for
a
in
lines
]
# draw(x, y, 0.45/2)
# draw(x, y, 0.45/2)
interact
(
draw
,
f
=
FloatSlider
(
min
=
min
(
x
),
max
=
max
(
x
),
step
=
0.001
,
value
=
0.2
))
interact
(
draw
,
f
=
FloatSlider
(
min
=
min
(
x
),
max
=
max
(
x
),
step
=
0.001
,
value
=
0.2
))
```
```
%% Output
%% Output
<function __main__.draw(f)>
<function __main__.draw(f)>
%% Cell type:code id:f0cd9d06-a15a-46b4-a3ef-4aee3d4f7cd0 tags:
%% Cell type:code id:f0cd9d06-a15a-46b4-a3ef-4aee3d4f7cd0 tags:
```
python
```
python
# Midi to pitch
# Midi to pitch
notes
=
[
36
,
48
,
60
]
notes
=
[
36
,
48
,
60
]
for
note
in
notes
:
for
note
in
notes
:
print
((
note
-
36
)
/
12.0
)
print
((
note
-
36
)
/
12.0
)
```
```
%% Output
%% Output
0.0
0.0
1.0
1.0
2.0
2.0
%% Cell type:code id:59b56dbc-6852-4989-bc19-3525ee7caf8b tags:
%% Cell type:code id:59b56dbc-6852-4989-bc19-3525ee7caf8b tags:
```
python
```
python
```
```
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment