Differences between revisions 15 and 29 (spanning 14 versions)
Revision 15 as of 2010-01-26 00:01:01
Size: 8936
Editor: newacct
Comment:
Revision 29 as of 2012-05-09 04:00:12
Size: 10309
Editor: jason
Comment:
Deletions are marked like this. Additions are marked like this.
Line 8: Line 8:
by Marshall Hampton. When the two frequencies are well seperated, we hear the right hand side of the identity. When they start getting close, we hear the higher-pitched factor in the left-hand side modulated by the lower-pitched envelope.

{{{
by Marshall Hampton. When the two frequencies are well separated, we hear the right hand side of the identity. When they start getting close, we hear the higher-pitched factor in the left-hand side modulated by the lower-pitched envelope.

{{{#!sagecell
Line 28: Line 28:
def sinsound(freq_ratio = slider(0,1,1/144,1/12)): def sinsound(freq_ratio = slider(1/144,1,1/144,1/12)):
Line 41: Line 41:
    html('<embed src="https:./test'+ lab +'.wav" width="200" height="100"></embed>')     html('<embed src="cell://test'+ lab +'.wav" width="200" height="100"></embed>')
Line 46: Line 46:
== Karplus-Strong algorithm for plucked and percussive sound generation ==
by Marshall Hampton

{{{#!sagecell
import wave

class SoundFile:
   def __init__(self, signal,lab=''):
       self.file = wave.open('./test' + lab + '.wav', 'wb')
       self.signal = signal
       self.sr = 44100

   def write(self):
       self.file.setparams((1, 2, self.sr, 44100*4, 'NONE', 'noncompressed'))
       self.file.writeframes(self.signal)
       self.file.close()

mypi = float(pi)
from math import sin

def ks(delay,length,blend = 0,filler=None,stretch=0):
    if filler == None:
        filler = [randint(-16383,16383) for q in range(delay+1)]
    outsig = filler[:]
    index = len(filler)
    while len(outsig) < length:
        s = random()
        if s > stretch:
            b = random()
            if b < 1-blend:
                newvalue = (outsig[index-delay]+outsig[index-delay-1])*.5
            else:
                newvalue = -(outsig[index-delay]+outsig[index-delay-1])*.5
        else:
            newvalue = outsig[index-delay]
        outsig.append(newvalue)
        index += 1
    return [int(round(x)) for x in outsig]

@interact
def sinsound(delay = slider([int(2^i) for i in range(2,10)], default=100, label="initial delay"), blend=slider(srange(0,1,.01,include_endpoint=True),default=0,label="blend factor"), stretch=slider(srange(0,1,.01,include_endpoint=True),default=0,label="stretch factor")):
    s2f = ks(delay,int(44100*(1/2)),blend=blend,stretch=stretch)
    for i in range(12):
        s2f = s2f + ks(int(2^((12+i)/12.0)*delay),int(44100*(1/2)),blend=blend, stretch=stretch)
    html("Karplus-Strong algorithm with blending and delay stretching")
    html("<br>K. Karplus and A. Strong, <em>Digital synthesis of plucked string and drum timbres</em>, \nComputer Music Journal 7 (2) (1983), 43–55.<br>")
    html("Initial waveform:")
    show(list_plot(s2f[0:2000],plotjoined=True), figsize = [7,3.5])
    html("Waveform after stabilization:")
    show(list_plot(s2f[20000:22000],plotjoined=True), figsize = [7,3.5])
    s2str = ''.join(wave.struct.pack('h',x) for x in s2f)
    lab=""
    f = SoundFile(s2str,lab=lab)
    f.write()
    html('<embed src="cell://test'+ lab +'.wav" width="200" height="100"></embed>')
}}}

{{attachment:KarplusStrong.png}}
Line 49: Line 107:
{{{ {{{#!sagecell
Line 105: Line 163:
{{{ {{{#!sagecell
Line 119: Line 177:
{{{ {{{#!sagecell
Line 139: Line 197:
{{{ {{{#!sagecell
Line 147: Line 205:
== A Random Walk ==

by William Stein

{{{
html('<h1>A Random Walk</h1>')
vv = []; nn = 0
@interact
def foo(pts = checkbox(True, "Show points"),
        refresh = checkbox(False, "New random walk every time"),
        steps = (50,(10..500))):
    # We cache the walk in the global variable vv, so that
    # checking or unchecking the points checkbox doesn't change
    # the random walk.
    html("<h2>%s steps</h2>"%steps)
    global vv
    if refresh or len(vv) == 0:
        s = 0; v = [(0,0)]
        for i in range(steps):
             s += random() - 0.5
             v.append((i, s))
        vv = v
    elif len(vv) != steps:
        # Add or subtract some points
        s = vv[-1][1]; j = len(vv)
        for i in range(steps - len(vv)):
            s += random() - 0.5
            vv.append((i+j,s))
        v = vv[:steps]
    else:
        v = vv
    L = line(v, rgbcolor='#4a8de2')
    if pts: L += points(v, pointsize=10, rgbcolor='red')
    show(L, xmin=0, figsize=[8,3])
}}}
{{attachment:randomwalk.png}}

== 3D Random Walk ==
{{{
@interact
def rwalk3d(n=(50,1000), frame=True):
    pnt = [0,0,0]
    v = [copy(pnt)]
    for i in range(n):
        pnt[0] += random()-0.5
        pnt[1] += random()-0.5
        pnt[2] += random()-0.5
        v.append(copy(pnt))
    show(line3d(v,color='black'),aspect_ratio=[1,1,1],frame=frame)
}}}
{{attachment:randomwalk3d.png}}
Line 201: Line 208:
{{{ {{{#!sagecell
Line 247: Line 254:
by Pablo Angulo

{{{
%cython
by Pablo Angulo, Eviatar Bach

{{{#!sagecell
%python
Line 252: Line 260:

def cellular(rule, int N):
from random import randint

def cellular(rule, N, initial='Single-cell'):
Line 258: Line 267:
    initial: starting condition; can be either single-cell or a random binary row
Line 259: Line 269:
    cdef int j,k,l
    M=zeros( (N,2*N+1), dtype=int)
    M[0,N]=1
    M=zeros( (N,2*N+2), dtype=int)
    if initial=='Single-cell':
        M[0,N]=1
    else:
        M[0]=[randint(0,1) for a in range(0,2*N+2)]
Line 264: Line 276:
        for k in range(N-j,N+j+1):         for k in range(0,2*N):
Line 267: Line 279:
    return M
}}}
{{{
    return M[:,:-1]
    
Line 275: Line 286:

@interact
def _( N=input_box(label='Number of iterations',default=100),
}}}
Put in separate cell:
{{{#!sagecell

@interact
def _( initial=selector(['Single-cell', 'Random'], label='Starting condition'), N=input_box(label='Number of iterations',default=100),
Line 279: Line 292:
       size = slider(1, 11, step_size=1, default=6 ) ):        size = slider(1, 11, label= 'Size', step_size=1, default=6 ), auto_update=False):
Line 281: Line 294:
    M = cellular(rule, N)
    plot_M = matrix_plot(M)
    M = cellular(rule, N, initial)
    plot_M = matrix_plot(M, cmap='binary')
Line 285: Line 298:
{{attachment:cellular.png}} {{attachment:cellular2.png}}

Sage Interactions - Miscellaneous

goto interact main page

Hearing a trigonometric identity

by Marshall Hampton. When the two frequencies are well separated, we hear the right hand side of the identity. When they start getting close, we hear the higher-pitched factor in the left-hand side modulated by the lower-pitched envelope.

sinsound.png

Karplus-Strong algorithm for plucked and percussive sound generation

by Marshall Hampton

KarplusStrong.png

An Interactive Venn Diagram

veng.png

Unreadable code

by Igor Tolkov

unreadable.png

Profile a snippet of code

profile.png

Evaluate a bit of code in a given system

by William Stein (there is no way yet to make the text box big):

evalsys.png

Minkowski Sum

by Marshall Hampton

minksum.png

Cellular Automata

by Pablo Angulo, Eviatar Bach

Put in separate cell:

cellular2.png

interact/misc (last edited 2020-06-05 20:32:41 by mathzeta2)