Differences between revisions 16 and 32 (spanning 16 versions)
Revision 16 as of 2010-04-02 15:30:16
Size: 7501
Comment:
Revision 32 as of 2014-06-18 20:08:47
Size: 14786
Editor: janehlong
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 126: Line 184:
    print "<html>" # trick to avoid word wrap
Line 128: Line 185:
        cProfile.run(cmd)         cProfile.runctx(cmd,globals(), locals())
Line 130: Line 187:
        profile.run(cmd)
   print "</html>"
        profile.runctx(cmd,globals(), locals())
Line 139: Line 195:
{{{ {{{#!sagecell
Line 150: Line 206:
{{{ {{{#!sagecell
Line 196: Line 252:
by Pablo Angulo

{{{
%
cython
by Pablo Angulo, Eviatar Bach

{{{#!sagecell
Line 201: Line 256:

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

def cellular(rule, N, initial='Single-cell'):
Line 207: Line 263:
    initial: starting condition; can be either single-cell or a random binary row
Line 208: Line 265:
    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 213: Line 272:
        for k in range(N-j,N+j+1):         for k in range(0,2*N):
Line 216: Line 275:
    return M
}}}
{{{
    return M[:,:-1]
    
Line 226: Line 284:
def _( N=input_box(label='Number of iterations',default=100), def _( initial=selector(['Single-cell', 'Random'], label='Starting condition'), N=input_box(label='Number of iterations',default=100),
Line 228: Line 286:
       size = slider(1, 11, step_size=1, default=6 ) ):        size = slider(1, 11, label= 'Size', step_size=1, default=6 ), auto_update=False):
Line 230: Line 288:
    M = cellular(rule, N)
    plot_M = matrix_plot(M)
    M = cellular(rule, N, initial)
    plot_M = matrix_plot(M, cmap='binary')
Line 234: Line 292:
{{attachment:cellular.png}} {{attachment:cellular2.png}}

== Another Interactive Venn Diagram ==
by Jane Long (adapted from http://wiki.sagemath.org/interact/misc)

This interact models a problem in which a certain number of people are surveyed to see if they participate in three different activities (running, biking, and swimming). Users can indicate the numbers of people in each category, from 0 to 100. Returns a graphic of a labeled Venn diagram with the number of people in each region. Returns an explanatory error message if user input is inconsistent.

{{{#!sagecell
# Adapted from http://wiki.sagemath.org/interact/misc
# An_Interactive_Venn_Diagram

# This interact models problems in which a certain number of people are surveyed to see if they participate in three different activities (running, biking, and swimming). Users can indicate the numbers of people, from 0 to 100. Returns a graphic of a labeled Venn diagram with the number of people in each region. Returns an explanatory error message if user input is inconsistent.

@interact
def _(T=slider([0..100],default=100,label='People surveyed'),X=slider([0..100],default=28,label='Run'), Y=slider([0..100],default=33,label='Bike'), Z=slider([0..100],default=59,label='Swim'),XY=slider([0..100],default=16,label='Run and Bike'),XZ=slider([0..100],default=13,label='Run and Swim'),YZ=slider([0..100],default=12,label='Bike and Swim'),XYZ=slider([0..100],default=7,label='Run, Bike, and Swim')):
    
    centers = [(cos(n*2*pi/3), sin(n*2*pi/3)) for n in [0,1,2]]
    scale = 1.7
    clr = ['yellow', 'blue', 'green']
    G = Graphics()
    for i in range(3):
        G += circle(centers[i], scale, rgbcolor=clr[i],
             fill=True, alpha=0.3)
    for i in range(3):
        G += circle(centers[i], scale, rgbcolor='black')
    
    # Label sets
    G += text('Run',(3,0),rgbcolor='black')
    G += text('Bike',(-1,3),rgbcolor='black')
    G += text('Swim',(-1,-3),rgbcolor='black')
    
    # Plot pairs of intersections
    ZX=XZ-XYZ
    G += text(ZX, (1.3*cos(2*2*pi/3 + pi/3), 1.3*sin(2*2*pi/3 + pi/3)), rgbcolor='black')
    YX=XY-XYZ
    G += text(YX, (1.3*cos(0*2*pi/3 + pi/3), 1.3*sin(0*2*pi/3 + pi/3)), rgbcolor='black')
    ZY=YZ-XYZ
    G += text(ZY, (1.3*cos(1*2*pi/3 + pi/3), 1.3*sin(1*2*pi/3 + pi/3)), rgbcolor='black')
   
    # Plot what is in one but neither other
    XX=X-ZX-YX-XYZ
    G += text(XX, (1.5*centers[0][0],1.7*centers[0][1]), rgbcolor='black')
    YY=Y-ZY-YX-XYZ
    G += text(YY, (1.5*centers[1][0],1.7*centers[1][1]), rgbcolor='black')
    ZZ=Z-ZY-ZX-XYZ
    G += text(ZZ, (1.5*centers[2][0],1.7*centers[2][1]), rgbcolor='black')

    # Plot intersection of all three
    G += text(XYZ, (0,0), rgbcolor='black')
    
    # Indicate number not in X, in Y, or in Z
    C=T-XX-YY-ZZ-ZX-ZY-YX-XYZ
    G += text(C,(3,-3),rgbcolor='black')
    
    # Check reasonableness before displaying result
    if XYZ>XY or XYZ>XZ or XYZ>YZ or XY>X or XY>Y or XZ>X or XZ>Z or YZ>Y or YZ>Z or C<0:
        print 'This situation is impossible!'
        if XYZ > XY:
            print 'The number of people who run, bike, and swim cannot be greater than the number of people who run and bike! (Why?)'
        if XYZ > XZ:
            print 'The number of people who run, bike, and swim cannot be greater than the number of people who run and swim! (Why?)'
        if XYZ > YZ:
            print 'The number of people who run, bike, and swim cannot be greater than the number of people who bike and swim! (Why?)'
        if XY > X:
            print 'The number of people who run and bike cannot be greater than the number of people who run! (Why?)'
        if XY > Y:
            print 'The number of people who run and bike cannot be greater than the number of people who bike! (Why?)'
        if XZ > X:
            print 'The number of people who run and swim cannot be greater than the number of people who run! (Why?)'
        if XZ > Z:
            print 'The number of people who run and swim cannot be greater than the number of people who swim! (Why?)'
        if YZ > Y:
            print 'The number of people who bike and swim cannot be greater than the number of people who bike! (Why?)'
        if YZ > Z:
            print 'The number of people who bike and swim cannot be greater than the number of people who swim! (Why?)'
        if C<0:
            print 'You have indicated too many people! The number of people exceeds the number of people surveyed. (Why?)'
    else:
        G.show(aspect_ratio=1, axes=False)
}}}

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

cellular2.png

Another Interactive Venn Diagram

by Jane Long (adapted from http://wiki.sagemath.org/interact/misc)

This interact models a problem in which a certain number of people are surveyed to see if they participate in three different activities (running, biking, and swimming). Users can indicate the numbers of people in each category, from 0 to 100. Returns a graphic of a labeled Venn diagram with the number of people in each region. Returns an explanatory error message if user input is inconsistent.

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