carlbook
What is this place?

I am Carl and this is carlbook. This is my personal site with various programming bits and other interesting things.

 
Email me!
Yes I'm real!
Fast Number Base Changing in Python - 09/03/10
I wrote a short function in python that should be fairly fast. Given number n and base b, it converts n in just O(log n) rounds. Here is the first version:

from math import log

def chbase(n, b):
    if n < 1:
        return 0
    cols = []
    power = int(log(n, b))
    while power >= 0:
        count = int(n / (b**power))
        n -= count * b**power
        power -= 1    
        cols += [count] 
    return cols

For example, calling chbase(12, 3) would produce [1, 1, 0] since 12 is 1*9 + 1*3 + 0*1. However, this is in big endian representation of the number. This is useful for humans to look at but is not the only way to represent the number. The problem with this representation is that code that tries to use this list of columns has to know the length of the list before it can be used. In the following code, the little endian list is produced. Changes to the code are in bold:

from math import log

def chbase(n, b):
    if n < 1:
        return 0
    cols = []
    power = int(log(n, b))
    while power >= 0:
        count = int(n / (b**power))
        n -= count * b**power
        power -= 1    
        cols = [count] + cols
    return cols

Calling chbase(12, 3) would produce [0, 1, 1] since 12 is0*1 + 1*3 + 1*9.

To convert the number back into its original form you would use the following:

def revbase(cols, b):
    power = 0
    total = 0
    for each in cols:
        total += each * b ** power
        power += 1
    return total

To use it, pass the columns as the first parameter and the base as the second: revbase([0,1,1], 3) and the result is 12. In fact this would even work: revbase( chbase( 12, 3), 3) since it too would return 12.

Files:
A Tiny Equation - 09/02/10
While working on my CS homework I arrived at an interesting equation that I think I have proved correct:



Email me if you can prove it or disprove it.

Files:
A Python Script to Download wimp.com Videos - 09/02/10
Sometimes on wimp.com they have videos that I like to hold on to before they disappear. Since wimp tends to have a lot of good videos, it's hard to download them all. So, I wrote a python script to download a wimp video given the url. It doubles as a tiny library (you can import it) or run it as a shell script. Enjoy:
#!/usr/bin/python
import urllib, sys

def getvid(url):
    f = urllib.urlopen(url)
    html = f.read()
    f.close()
    html = html.split('s1.addVariable("file","', 1)
    html = html[1]
    html = html.split('");', 1)
    html = html[0]
    vidurl = html
    vidname = html.rsplit('/', 1)
    vidname = vidname[1]
    urllib.urlretrieve(vidurl, vidname)
    print "Done!"

if __name__ == "__main__":

    if len(sys.argv) != 2:
        print "Usage: ./wimpget.py \"<url>\""
    else:
        getvid(sys.argv[1])

Files:
Sample Video - A Talented Girl and Others - 08/31/10
Here are some sample videos. These were originally from www.wimp.com but have been re-encoded for demo purposes. Wimp has all there videos in a format that cannot be played on most phones and other mobile devices. I have made these videos for both browser use or mobiles.

Files:
Sound Generation - 08/29/10
Recently I have been playing with a program I am writing in python to generate music and sound. Some of the more interesting sound bytes I was able to generate are provided below. I'll give a brief description of each so you know what you are hearing.

- Envelope Fun
This file shows off a sound envelope I made. An envelope is basically a function that changes the intensity (loudness) of a sound so that it is soft at the beginning and end. Notes that have an envelope applied to them sound more realistic, as if they are being played by a real instrument.

- Green Sleeves
This file is a short melody played with pure sine waves. It has no envelope so the transition between notes is sharp and not very pleasant to hear.

- Green Sleeves Enveloped
Exactly like the file before it but with an envelope applied to each note. Also, each note is still playing even after the next note starts. This also makes it sound more like a real instrument.

- Order 2.5 Wave
This is a note that is being generated from a finite number of polynomials usually in the form (x ** (n - 1) ) * (x - 1/f) / m. The frequency is denoted f, the order is n, and the time is x. This represents a single cycle of the polynomial that begins at time 0 and ends at time 1/f. Thus, f cycles can be played in a single second. Note: for my program the maximum value any note can have is between 1 and -1. m is determined using calculus as a the maximum value and then used to scale down the loudness. For this file, a 440hz 'A' note is played using a 2.5 order polynomial.

- Order 2.5 C-E-G Chord
This is the the same as the previous note except that it is a C-E-G chord. Interestingly, this sounds similar to a piano chord despite having a completely different shape.

- PolyContinuum
Polycontinuum is a sound byte where a single frequency is played but the polynomial is continuously increasing in order. It starts off as a sawtooth wave and slowly becomes higher order.

- PolySinusoid
Polysine is similar to the previous sound byte except the order of the polynomial is determined by an internal sine wave. The frequency of the sound is the same, but the order is changing with time.

I hope that some of these sound bytes are interesting!

Files:

Old Posts:
- Fast Number Base Changing in Python

- A Tiny Equation

- A Python Script to Download wimp.com Videos

- Sample Video - A Talented Girl and Others

- Sound Generation

- Day 0