f = 10 # Hz
time = 1 #seconds
sr = 44100
phs = linspace(0, 2* pi* f* time, time*sr)
plot(sin(phs))
[<matplotlib.lines.Line2D at 0x2be5310>]
It's simpler to do phase modulation (in ipython...):
fm = 2
mod = sin(linspace(0, 2 * pi * fm * time, time*sr))
mod_phs = phs + mod
plot(mod_phs)
[<matplotlib.lines.Line2D at 0x310b9d0>]
mod_phs = phs + (2*pi*mod)
plot(mod_phs)
[<matplotlib.lines.Line2D at 0x34982d0>]
plot(sin(mod_phs))
[<matplotlib.lines.Line2D at 0x34c6a10>]
plot(abs(rfft(sin(mod_phs))))
[<matplotlib.lines.Line2D at 0x39f6fd0>]
plot(abs(rfft(sin(mod_phs))))
xlim((0, 30))
(0, 30)
f = 4410 # Hz
time = 1 #seconds
sr = 44100
phs = linspace(0, 2* pi* f * time, time*sr)
fm = 2205
mod = sin(linspace(0, 2 * pi * fm * time, time*sr))
mod_phs = phs + mod
plot(abs(rfft(sin(mod_phs))))
[<matplotlib.lines.Line2D at 0x416e050>]
plot(abs(rfft(sin(mod_phs))))
xlim((2000, 8000))
grid()
from JSAnimation import IPython_display
from matplotlib import animation
fig = plt.figure()
ax = plt.axes(xlim=(0, 22050), ylim=(0,17000))
ax.grid()
line, = ax.plot([], [])
amp_text = ax.text(10000, 15000, "")
x = np.linspace(0, 22050, 22051)
phs = linspace(0, 2* pi* f, time*sr)
mod_amp = linspace(0, 4*pi, 100)
def init():
line.set_data([], [])
return line,
def animate(i):
mod_phs = phs + (mod_amp[i] * mod)
line.set_data(x, abs(rfft(sin(mod_phs))))
# text.text("mod_amp = %f"%mod_amp[i])
return line,
animation.FuncAnimation(fig, animate, init_func=init,
frames=len(mod_amp), interval=300, blit=True)
The ratio of the carrier and modulator frequencies is called the C:M ratio
import csnd6
cs = csnd6.Csound()
csPerf = csnd6.CsoundPerformanceThread(cs)
cs.SetOption('-odac')
cs.CompileOrc(
'''sr = 44100
ksmps = 128
nchnls = 2
0dbfs = 1.0
''')
cs.Start()
csPerf.Play()
cs.CompileOrc('''
girgnp ftgen 0, 0, 0, -23, "../shared/rgnp.txt"
giunrate ftgen 0, 0, 0, -23, "../shared/unrate.txt"
gigs10 ftgen 0, 0, 0, -23, "../shared/gs10.txt"
gisine ftgen 0, 0, 4096, 10, 1
''')
0
cs.CompileOrc('''
instr fm
icarfreq = p4
icmratio = p5
imoddepth = p6
aphs phasor icarfreq
aphsmod oscili imoddepth, icarfreq*icmratio
asig table3 aphs + aphsmod, gisine, 1, 0, 1
outs asig*0.2, asig*0.2
endin
''')
0
cur_time = 0
time_inc = 0.2
score = ''
for moddepth in linspace(0, 1, 20):
score += 'i "fm" %f %f 440 2 %f\n'%(cur_time, time_inc, moddepth)
cur_time += time_inc
cs.ReadScore(score)
0
cur_time = 0
time_inc = 0.2
score = ''
for moddepth in linspace(0, 1, 20):
score += 'i "fm" %f %f 440 3 %f\n'%(cur_time, time_inc, moddepth)
cur_time += time_inc
cs.ReadScore(score)
0
print score
i "fm" 0.000000 0.200000 440 3 0.000000 i "fm" 0.200000 0.200000 440 3 0.052632 i "fm" 0.400000 0.200000 440 3 0.105263 i "fm" 0.600000 0.200000 440 3 0.157895 i "fm" 0.800000 0.200000 440 3 0.210526 i "fm" 1.000000 0.200000 440 3 0.263158 i "fm" 1.200000 0.200000 440 3 0.315789 i "fm" 1.400000 0.200000 440 3 0.368421 i "fm" 1.600000 0.200000 440 3 0.421053 i "fm" 1.800000 0.200000 440 3 0.473684 i "fm" 2.000000 0.200000 440 3 0.526316 i "fm" 2.200000 0.200000 440 3 0.578947 i "fm" 2.400000 0.200000 440 3 0.631579 i "fm" 2.600000 0.200000 440 3 0.684211 i "fm" 2.800000 0.200000 440 3 0.736842 i "fm" 3.000000 0.200000 440 3 0.789474 i "fm" 3.200000 0.200000 440 3 0.842105 i "fm" 3.400000 0.200000 440 3 0.894737 i "fm" 3.600000 0.200000 440 3 0.947368 i "fm" 3.800000 0.200000 440 3 1.000000
cur_time = 0
time_inc = 0.2
score = ''
for moddepth in linspace(0, 1, 20):
score += 'i "fm" %f %f 440 0.5 %f\n'%(cur_time, time_inc, moddepth)
cur_time += time_inc
cs.ReadScore(score)
0
cur_time = 0
time_inc = 0.2
score = ''
for moddepth in linspace(0, 1, 20):
score += 'i "fm" %f %f 440 1.414 %f\n'%(cur_time, time_inc, moddepth)
cur_time += time_inc
cs.ReadScore(score)
0
cs.CompileOrc('''
instr fm
icarfreq = p4
icmratio = p5
imoddepth = p6
aphs phasor icarfreq
aphsmod oscili imoddepth, icarfreq*icmratio
asig table3 aphs + aphsmod, gisine, 1, 0, 1
aenv linen 0.2, 0.01, p3, p3-0.01
outs asig*aenv, asig*aenv
endin
''')
0
cur_time = 0
time_inc = 0.2
score = ''
for moddepth in linspace(0, 1, 20):
score += 'i "fm" %f %f 440 1.414 %f\n'%(cur_time, time_inc * 5, moddepth)
cur_time += time_inc
cs.ReadScore(score)
0
cs.CompileOrc('''
instr fmdepth
icarfreq = p4
icmratio = p5
;imoddepth = p6
amoddepth poscil 1, 1/p3, giunrate
aphs phasor icarfreq
aphsmod oscili amoddepth, icarfreq*icmratio
asig table3 aphs + aphsmod, gisine, 1, 0, 1
aenv linen 0.2, 0.01, p3, 0.01
outs asig*aenv, asig*aenv
endin
''')
0
cs.ReadScore('i "fmdepth" 0 10 220 0.5')
0
cs.ReadScore('i "fmdepth" 0 10 55 0.5')
0
cs.ReadScore('i "fmdepth" 0 10 440 0.717')
0
cs.CompileOrc('''
instr fmdepth
icarfreq = p4
icmratio = p5
;imoddepth = p6
amoddepth poscil 1, 1/p3, giunrate
aphs phasor icarfreq
aphsmod oscili amoddepth/2, icarfreq*icmratio
asig table3 aphs + aphsmod, gisine, 1, 0, 1
aenv linen 0.2, 0.01, p3, 0.01
outs asig*aenv, asig*aenv
endin
''')
0
cs.ReadScore('i "fmdepth" 0 10 440 0.717')
0
cs.CompileOrc('''
instr fmdepth
;icarfreq = p4
icmratio = p4
;imoddepth = p6
amoddepth poscil 1, 1/p3, giunrate
acarfreq poscil 1, 1/p3, girgnp
aphs phasor acarfreq
aphsmod oscili amoddepth/2, acarfreq*icmratio/8
asig table3 aphs + aphsmod, gisine, 1, 0, 1
aenv linen 0.2, 0.01, p3, 0.01
outs asig*aenv, asig*aenv
endin
''')
0
cs.ReadScore('i "fmdepth" 0 10 0.717')
0
cs.ReadScore('i "fmdepth" 0 10 0.5')
0
cs.CompileOrc('''
instr fmdepth
;icarfreq = p4
icmratio = p4
;imoddepth = p6
acarfreq poscil 1, 1/p3, giunrate
amoddepth poscil 1, 1/p3, girgnp
aphs phasor acarfreq
aphsmod oscili amoddepth/5000, acarfreq*icmratio*100
asig table3 aphs + aphsmod, gisine, 1, 0, 1
aenv linen 0.2, 0.01, p3, 0.01
outs asig*aenv, asig*aenv
endin
''')
0
cs.ReadScore('i "fmdepth" 0 10 0.5')
0
To do frequency modulation, we need to work with phase increments per sample:
f = 10
phs_inc = ones((time*sr)) * 2*pi*f/(time*sr)
phs = cumsum(phs_inc)
plot(sin(phs))
[<matplotlib.lines.Line2D at 0x52611d0>]
... Left as an exercise to the reader ...
cs.CompileOrc('''
giwfn1 ftgen 0, 0, 512, 20, 2
instr grain
insnd = gisine
kamp = 0.01
kpitch init 440
kdens init 50
kaoff init 0
kpoff init 0
kgdur init 0.01
imaxgdur = .5
asig grain kamp, kpitch, kdens, kaoff, kpoff, kgdur, insnd, giwfn1, imaxgdur, 1
outs asig, asig
endin
''')
0
cs.ReadScore('i "grain" 0 2')
0
cs.CompileOrc('''
giwfn1 ftgen 0, 0, 512, 20, 2
instr grain
insnd = gisine
kamp init 0.01
kpitch init 440
kdens init 100
kaoff init 0
kpoff init 330
kgdur init 0.03
imaxgdur = .5
asig grain kamp, kpitch, kdens, kaoff, kpoff, kgdur, insnd, giwfn1, imaxgdur, 1
outs asig, asig
endin
''')
0
cs.ReadScore('i "grain" 0 1')
0
cs.CompileOrc('''
giwfn1 ftgen 0, 0, 512, 20, 2
instr grain
insnd = gisine
kamp init 0.01
kpitch init 880
kdens init 100
kaoff init 0
;kpoff init 330
kgdur init 0.02
imaxgdur = .5
kpoff poscil 1, 1/p3, giunrate
asig grain kamp, kpitch, kdens, kaoff, (kpoff - 2)*100, kgdur, insnd, giwfn1, imaxgdur, 1
outs asig, asig
endin
''')
0
cs.ReadScore('i "grain" 0 10')
0
cs.CompileOrc('''
giwfn1 ftgen 0, 0, 512, 20, 2
instr grain
insnd = gisine
kamp init 0.01
kpitch init 660
;kdens init 100
kaoff init 0
kpoff init 0
kgdur init 0.01
imaxgdur = .5
kdens poscil 5, 1/p3, giunrate
asig grain kamp, kpitch, kdens, kaoff, kpoff, kgdur, insnd, giwfn1, imaxgdur, 1
outs asig, asig
endin
''')
0
cs.ReadScore('i "grain" 0 10')
0
Extracting statistical information from the data can be useful to sonify relationships and trends in the data.
samples = normal(loc= 5, scale=3.4, size=100)
scatter(samples, zeros_like(samples))
<matplotlib.collections.PathCollection at 0x6f7d450>
hist(samples);
from scipy.stats import norm
hist(samples);
x = linspace(-5,15, 40)
twinx()
plot(x, norm.pdf(x,loc= 5, scale=3.4), lw=5, c='red')
[<matplotlib.lines.Line2D at 0x727bdd0>]
boxplot(samples);
grid()
boxplot(samples);
grid()
hlines((5, 5+3.4, 5-3.4), 0, 2)
<matplotlib.collections.LineCollection at 0x72699d0>
Standard deviation is $\sigma^2$ !
boxplot(samples);
grid()
hlines((5, 5+sqrt(3.4), 5-sqrt(3.4)), 0, 2)
<matplotlib.collections.LineCollection at 0x7bc51d0>
# Great audio player for ipython notebook I found at http://nbviewer.ipython.org/github/Carreau/posts/blob/master/07-the-sound-of-hydrogen.ipynb
# The original source is here: https://github.com/Carreau/posts
import StringIO
import base64
import struct
from IPython.core.display import HTML
def wavPlayer(data, rate):
""" will display html 5 player for compatible browser
The browser need to know how to play wav through html5.
there is no autoplay to prevent file playing when the browser opens
Adapted from SciPy.io.
"""
buffer = StringIO.StringIO()
buffer.write(b'RIFF')
buffer.write(b'\x00\x00\x00\x00')
buffer.write(b'WAVE')
buffer.write(b'fmt ')
if data.ndim == 1:
noc = 1
else:
noc = data.shape[1]
bits = data.dtype.itemsize * 8
sbytes = rate*(bits // 8)*noc
ba = noc * (bits // 8)
buffer.write(struct.pack('<ihHIIHH', 16, 1, noc, rate, sbytes, ba, bits))
# data chunk
buffer.write(b'data')
buffer.write(struct.pack('<i', data.nbytes))
if data.dtype.byteorder == '>' or (data.dtype.byteorder == '=' and sys.byteorder == 'big'):
data = data.byteswap()
buffer.write(data.tostring())
size = buffer.tell()
buffer.seek(4)
buffer.write(struct.pack('<i', size-8))
val = buffer.getvalue()
src = """
<body>
<audio controls="controls" style="width:600px" >
<source controls src="data:audio/wav;base64,{base64}" type="audio/wav" />
Your browser does not support the audio element.
</audio>
</body>
""".format(base64=base64.encodestring(val))
display(HTML(src))
from sklearn import datasets
iris = datasets.load_iris()
Y = iris.target
print iris.feature_names[0]
min(iris.data[:,0]), max(iris.data[:,0])
sepal length (cm)
(4.2999999999999998, 7.9000000000000004)
plot(iris.data[:,0], marker='+', lw=0)
[<matplotlib.lines.Line2D at 0x8732390>]
dur = 3
sr = 44100
signal = zeros(dur*sr + 1)
value_scaling = 1/(max(iris.data[:,0]) - min(iris.data[:,0]))
value_offset = min(iris.data[:,0])
for length in iris.data[:,0]:
norm_index = (length - value_offset)* value_scaling
signal[int(norm_index * sr * dur)] += 0.2
value_scaling
0.27777777777777773
wavPlayer((signal * 20000).astype(int16), sr)
By: Andrés Cabrera mantaraya36@gmail.com
For Course MAT 240F at UCSB
This ipython notebook is licensed under the CC-BY-NC-SA license: http://creativecommons.org/licenses/by-nc-sa/4.0/