In [1]:
from __future__ import division, print_function
%matplotlib inline
%config InlineBackend.figure_format = "retina"
from matplotlib import rcParams
rcParams["savefig.dpi"] = 100
rcParams["font.size"] = 20
In [85]:
import kplr
import numpy as np
import matplotlib.pyplot as pl
from IPython.display import Audio
In [259]:
def envelope(attack, decay, sustain, release, sustain_level, t):
t1 = decay + attack
t2 = decay + attack + sustain
t3 = decay + attack + sustain + release
return (
(0 <= t) * (t < attack) * t / attack +
(attack <= t) * (t < t1) * (1.0 - (1 - sustain_level) * (t - attack) / decay) +
(t1 <= t) * (t < t2) * sustain_level +
(t2 <= t) * (t < t3) * sustain_level * (1.0 - (t - t2) / release)
)
def render_note(amp, freq, t0, t, attack=0.01, decay=0.1, sustain=0.4, release=0.2, sustain_level=0.3, out=None):
if out is None:
out = np.zeros_like(t)
out[:] += amp * envelope(attack, decay, sustain, release, sustain_level, t - t0) * np.sin(2 * np.pi * freq * t)
return out
In [260]:
client = kplr.API()
kois = client.kois(where="koi_disposition+like+'CONFIRMED'")
In [261]:
radii = [k.koi_prad for k in kois if k.koi_prad is not None and k.koi_prad < 7.0 and k.koi_period < 10.0]
periods = [k.koi_period for k in kois if k.koi_prad is not None and k.koi_prad < 7.0 and k.koi_period < 10.0]
t0s = [k.koi_time0bk % k.koi_period for k in kois if k.koi_prad is not None and k.koi_prad < 7.0 and k.koi_period < 10.0]
In [272]:
notes = [
# 65.406, # C
# # 69.296,
# 73.416, # D
# 77.782,
# # 82.407, # E
# 87.307, # F
# # 92.499,
# 97.999, # G
# # 103.826,
# # 110.000, # A
# 116.541,
# # 123.471, # B
130.813, # C
# 138.591,
# 146.832, # D
# 155.563,
164.814, # E
# 174.614, # F
# 184.997,
195.998, # G
# 207.652,
# 220.000, # A
# 233.082,
246.942, # B
261.626, # C
# 277.183,
# 293.665, # D
# 311.127,
329.628, # E
# 349.228, # F
# 369.994,
391.995, # G
# 415.305,
# 440.000, # A
466.164,
# 493.883, # B
523.251, # C
# 554.365,
# 587.330, # D
# 622.254,
659.255, # E
# 698.456, # F
# # 739.989,
# 783.991, # G
# # 830.609,
# # 880.000, # A
# 932.328,
# # 987.767, # B
]
In [273]:
n, bins, _ = pl.hist(np.log(radii), len(notes) - 1);
In [274]:
radius_inds = np.digitize(radii, bins) - 1
In [275]:
print(max(radius_inds), len(notes))
In [276]:
sample_rate = 22100
In [277]:
t = np.arange(0, 10., 1.0 / sample_rate)
y = None
factor = 10.0
for i in range(len(periods)):
t0 = factor * t0s[i]
freq = notes[np.random.randint(len(notes))]
while t0 < t[-1]:
y = render_note(1. - 0.2 * (radii[i] - np.min(radii)) / (np.max(radii) - np.min(radii)),
freq, t0, t, out=y)
t0 += factor * periods[i]
In [278]:
Audio(data=y, rate=sample_rate)
Out[278]:
In [269]:
pl.plot(t, y)
# for i in range(len(periods)):
# pl.axvline(t0s[i], color="k", alpha=0.1)
Out[269]:
In [132]:
t2 = np.arange(0, 1., 1.0 / sample_rate)
y2 = render_note(1.0, 440., 0.0, t2)
In [140]:
pl.plot(t2, y2)
Out[140]:
In [134]:
Audio(data=y2, rate=sample_rate)
Out[134]:
In [ ]: