-
Notifications
You must be signed in to change notification settings - Fork 0
/
sinfract-cli.py
182 lines (162 loc) · 6.24 KB
/
sinfract-cli.py
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import argparse
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from scipy.io.wavfile import write
import sinfract
SPEED = 1.1
def animate(frame, args, ax):
if frame == 0:
frame = 1
start = args.xstart / (SPEED**frame)
end = args.xend / (SPEED**frame)
x = np.linspace(start, end, args.precision)
data = calc_data(x, args)
#line.set_ydata(data)
ax.clear()
return ax.plot(x, data)
def calc_data(x, args):
if args.method == 'bessel':
return sinfract.bessel(x,
args.depth,
args.frequency,
args.mod_frequency,
args.beta)
elif args.method == 'sinfract':
return sinfract.sinfract(x,
args.depth,
args.a_param,
args.frequency,
args.scale)
else:
return sinfract.weierstrass(x,
args.depth,
args.a_param,
args.b_param,
args.frequency)
def main():
parser = argparse.ArgumentParser(description="sinfract-cli - CLI utility for visualizing and listening to weierstrass functions & friends", formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('-m',
'--method',
type=str,
default="weierstrass",
dest="method",
help='''\
Function to plot. Values:
'weierstrass': classical Weierstrass function
'bessel': modified Weierstrass function,
so that a parameter equals a Bessel function of the first kind with parameter beta,
and b parameter is (frequency+n*fm)^n
'sinfract': fractal sinusoid with amplitude a, frequency f and scale s
''')
parser.add_argument('-d',
'--depth',
help="number of summations to compute",
type=int,
default=100,
dest="depth")
parser.add_argument('-s',
'--scale',
help="scaling factor, use with sinfract method",
type=float,
default=2,
dest="scale")
parser.add_argument('-p',
'--precision',
help="precision of the x axis",
type=int,
default=1000,
dest="precision")
parser.add_argument('-start',
'--start',
help="starting point of plot on x axis",
type=float,
default=-2,
dest="xstart")
parser.add_argument('-end',
'--end',
help="end point of plot on x axis",
type=float,
default=2,
dest="xend")
parser.add_argument('-b',
'--b',
help="b parameter, use with weierstrass method",
type=float,
default=7,
dest="b_param")
parser.add_argument('-a',
'--a',
help="a parameter, use with weierstrass or sinfract method",
type=float,
default=0.2,
dest="a_param")
parser.add_argument('-f',
'--frequency',
help="strarting frequency of function, use with bessel or sinfract method",
type=float,
default=1,
dest="frequency")
parser.add_argument('-fm',
'--mod-freq',
help="frequency of modulator, use with bessel method",
type=float,
default=1,
dest="mod_frequency")
parser.add_argument('-beta',
'--beta',
help="beta parameter of Bessel function, use with bessel method",
type=float,
default=1.0,
dest="beta")
parser.add_argument('-audio',
'--audio',
help="save wave audio file",
action="store_true",
dest="audio")
parser.add_argument('-anim',
'--animate',
help="animate plot",
action="store_true",
dest="anim")
args = parser.parse_args()
x = np.linspace(args.xstart, args.xend, args.precision)
fig, ax = plt.subplots()
ax.spines['left'].set_position('center')
ax.spines['bottom'].set_position('center')
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
data = calc_data(x, args)
line, = ax.plot(x, data)
print(args.depth)
if args.anim:
anim_running = True
anim = animation.FuncAnimation(
fig, animate, fargs=(args,ax), interval=50)
def onClick(event):
nonlocal anim_running
if anim_running:
anim.event_source.stop()
anim.frame_seq = anim.new_frame_seq()
anim_running = False
else:
anim.event_source.start()
anim_running = True
fig.canvas.mpl_connect('button_press_event', onClick)
else:
plt.plot(x, data, 'b-')
mng = plt.get_current_fig_manager()
mng.resize(*mng.window.maxsize())
plt.show()
if args.audio:
if args.precision % 44100 != 0:
print("WARNING: precision not a multiple of 44100")
print("saving function as audio...")
scaled = np.int16(data/np.max(np.abs(data)) * 32767)
write('{}-f{}-s{}-d{}.wav'.format(args.method, args.frequency, args.scale, args.depth), 44100, scaled)
# show the plot
plt.show()
if __name__=='__main__':
main()