Standing Square Wave on a string?

January 23rd, 2012

I don’t think we succeeded in making a square wave, at least not as well as I’d wanted. (Though we did get closer than some other videos I’ve seen on the internet.)

It’s still pretty cool. Hopefully there will be a better one along shortly. We’ve improved the design considerably since we made this one.

We’re using 2 teensy’s, python running in a virtual machine, an ipad app, a windows freeware tone generator, wikipedia and a bunch of different scopes and meters.

We’re plotting this function into a DAC and piping it into a 100watt amp.

a1*sin( 2*pi*t ) + a3*sin( 3 * 2*pi*t ) + a5*sin( 5 * 2*pi*t ) …. according to wikipedia the constants should be 1/3, 1/5, 1/7… but we’re having some good debate over the physics of a real string vs an ideal medium.

 

The disagreement is cool. It’s lead to lots of different and unexpected waveforms, all superpositions of the harmonics!

String Waves from colin street on Vimeo.

Inverse of Python id function, di — or a[a]=a in Python.

January 23rd, 2012

In the spirit of giving matches to children, here’s a terrible little nugget.

Feel free to try this at home, but you didn’t learn it here and if you try to claim otherwise, we’re denying it.

The “di()” function

def di( id_val ):
    """Given an objects id, returned by the function id, return the original object"""
    for x in globals().values():
        if id(x) == id_val: return x

But why on earth would you do this? Well, this horribleness is motivated by the following:  The set of sets that contain themselves.

In javascript you can execute this:

a = {}
a[a]=a

It’s worthless but it works.

In python you get “TypeError: unhashable type: ‘dict’”

But you can do:

a = {}
a[id(a)]=a

Only this is useless without an inverse function for id. So I give you di(id_number) (which I scraped together from a quick google).

Now the self-referencing sets aren’t useless, they’re dangerous. Wonderful trade!


See Also: BF

Republican 2012 Primary Schedule by States and their Delegate Counts by Type.

January 12th, 2012

I was browsing through Nate Silvers stuff on the 2012 Republic Nomination and noticed the question: Estimate the schedule of the certainty vs the date. In short “When can we expect it to be clear who will win the Republican Nomination?”

After some googling and picking around on Wikipedia I noticed I wasn’t finding a good answer, and I couldn’t even find a CSV of the calender schedule for delegates being voted vs date.

I’m sure it’s out there but until google does it’s job perfectly there’s only so much I can do.

Hence, I’ve compiled another one. It might have errors. It’s based on the data from wikipedia:
Republican_Party_presidential_primaries,2012

Here’s a link: 2012 Republican Primary Schedule/Calendar . CSV

According to Wikipedia there are 3 types of delegates:

“There are 3 kinds of delegates: Parti- or Superdelegates (RNC), Delegates elected in congressional districts (CD) and Delegates elected statewide (AL)”

Taken from http://en.wikipedia.org/wiki/Republican_Party_presidential_primaries, _2012
Date        , State/Territory          , Type                       , RNC , CD  , AL
1/3/2012    , Iowa                     , nonbinding caucus          , 3   , 12  , 13
1/10/2012   , New Hampshire            , semi-closed prmry          , 0   , 0   , 12
1/21/2012   , South Carolina           , open prmry                 , 0   , 14  , 11
1/31/2012   , Florida                  , closed prmry               , 0   , 0   , 50
2/4-11/2012 , Maine                    , nonbinding caucus          , 3   , 6   , 15
2/4/2012    , Nevada                   , binding caucus             , 3   , 12  , 13
2/7/2012    , Minnesota                , caucus                     , 3   , 24  , 13
2/7/2012    , Colorado                 , nonbinding caucus          , 3   , 21  , 12
2/25/2012   , Northern Mariana Islands , nonbinding caucus          , 3   , 0   , 6
2/28/2012   , Arizona                  , semi-closed prmry 29       ,     ,     ,
2/28/2012   , Michigan                 , open prmry 30              ,     ,     ,
3/3/2012    , Washington               , binding caucus             , 3   , 30  , 10
3/6-10/2012 , Wyoming                  , nonbinding caucus          , 3   , 3   , 23
3/6/2012    , (Spr Tues) Massachusetts , semi-closed prmry          , 3   , 27  , 11
3/6/2012    , (Spr Tues) Idaho         , binding caucus             , 3   , 6   , 23
3/6/2012    , (Spr Tues) North Dakota  , nonbinding caucus          , 3   , 3   , 22
3/6/2012    , (Spr Tues) Virginia      , open prmry                 , 3   , 33  , 13
3/6/2012    , (Spr Tues) Tennessee     , open prmry                 , 3   , 27  , 28
3/6/2012    , (Spr Tues) Alaska        , binding caucus             , 3   , 3   , 21
3/6/2012    , (Spr Tues) Georgia       , open prmry                 , 3   , 42  , 31
3/6/2012    , (Spr Tues) Oklahoma      , closed prmry               , 3   , 15  , 25
3/6/2012    , (Spr Tues) Ohio          , open prmry                 , 3   , 48  , 15
3/6/2012    , (Spr Tues) Vermont       , open prmry                 , 3   , 3   , 11
3/10/2012   , Guam                     , nonbinding caucus          , 3   , 0   , 6
3/10/2012   , U.S.                     , Virgin Islands caucus      , 3   , 0   , 6
3/10/2012   , Kansas                   , binding caucus             , 3   , 12  , 25
3/13/2012   , Alaabama                 , semi-closed prmry          , 3   , 21  , 26
3/13/2012   , Mississippi              , open prmry                 , 3   , 12  , 25
3/13/2012   , American                 , Samoa caucus               , 3   , 0   , 6
3/13/2012   , Hawaii                   , binding caucus             , 3   , 6   , 11
3/17/2012   , Missouri                 , binding caucus             , 3   , 24  , 25
3/18/2012   , Puerto                   , Rico binding caucus        , 3   , 0   , 20
3/20/2012   , Illinois                 , open prmry                 , 3   , 54  , 12
3/24/2012   , Louisiana                , open prmry                 , 3   , 18  , 25
4/3/2012    , Wisconsin                , open prmry                 , 3   , 24  , 15
4/3/2012    , Maryland                 , closed prmry               , 3   , 24  , 10
4/3/2012    , Washington D.C.          , closed prmry               , 3   , 0   , 16
4/3/2012    , Texas                    , open prmry                 , 3   , 108 , 44
4/24/2012   , Connecticut              , closed prmry               , 3   , 15  , 10
4/24/2012   , Delaware                 , closed prmry               , 3   , 3   , 11
4/24/2012   , Pennsylvania             , closed prmry               , 3   , 54  , 15
4/24/2012   , Rhode                    , Island semi-closed prmry   , 3   , 6   , 10
4/24/2012   , New                      , York closed prmry          , 3   , 81  , 11
5/8/2012    , North                    , Carolina semi-closed prmry , 3   , 39  , 13
5/8/2012    , Indiana                  , open prmry                 , 3   , 27  , 16
5/8/2012    , West                     , Virginia semi-closed prmry , 3   , 9   , 19
5/15/2012   , Oregon                   , closed prmry               , 3   , 15  , 10
5/15/2012   , Nebraska                 , semi-closed prmry          , 3   , 9   , 23
5/22/2012   , Kentucky                 , closed prmry               , 3   , 18  , 24
5/22/2012   , Arkansas                 , open prmry                 , 3   , 12  , 21
6/5/2012    , California               , semi-closed prmry          , 3   , 159 , 10
6/5/2012    , New Jersey               , closed prmry               , 3   , 36  , 11
6/5/2012    , South Dakota             , closed prmry               , 3   , 3   , 22
6/5/2012    , Montana                  , semi-closed prmry          , 3   , 3   , 20
6/5/2012    , New Mexico               , closed prmry               , 3   , 9   , 11
6/26/2012   , Utah                     , closed prmry               , 3   , 12  , 25

Diffusion-Limited Aggregation Simulator

May 15th, 2011

A recent Computational Physics assignment of mine was to simulate the Diffusion-Limited Aggregation model.

The basic algorithm is:

  1. Start with an initial particle in the lattice  (default to middle).
  2. Place an additional particle at random and walk randomly until that particle is adjacent to another particle.  Then repeat.

Initially, I wrote this in C then plotted the results using gnuplot.  Since then, I’ve written a nice visual demonstration using the Processing JavaScript Library.

It gets laggy after a while but, it produces pretty snowflake-like patterns.

Simplest Fourier Transformation Code Ever.

February 28th, 2011

The simplest Fourier Transform is very naive and very beautiful.

function fourier( in_array ) {
  var len = in_array.length;
  var output = new Array();

  for( var k=0; k < len; k++ ) {
    var real = 0;
    var imag = 0;
    for( var n=0; n < len; n++ ) {
      real += in_array[n]*Math.cos(-2*Math.PI*k*n/len);
      imag += in_array[n]*Math.sin(-2*Math.PI*k*n/len);
    }
    output.push( [ real, imag ] )
  }
  return output;
}

found this here. Obviously, this is going to be a lot slower than taking a proper FFT but I’m using it in some code right now because it was so easy to understand and I liked it. It’s working plenty fast. The first comment I got was “How did you make this animation run so fast?”

Ahh… ye old random walk. I’ll never tire of you…

February 28th, 2011

A random walk / White Noise

I’ve always loved to just watch a good random walk. Today I found myself day dreaming about white noise and decided I needed to watch some go ticking past my screen.

There’s nothing quite as demotivating or fascinating as pondering all the ways our lives are governed by the indomitable, intractable ever unpredictable Random Walk.

It’s important to stare at one every now and then and remind yourself that “No, you can not predict it’s path – no way – no how – never going to happen. Period.” Forgetting this leads to nothing but endless folly and pain.

Strings

February 28th, 2011

A String Simulator

The way that strings behave has always bugged me. This is a simple simulation of masses connected by springs under compression. Each mass has a rest distance and the force on the mass is “(current distance – rest distance) * K_spring_constant.”

I am not certain that this simulation is completely correct. I’m still debating it with some friends but it looks cool and I enjoy playing with it.

I was working on some unrelated math and studying the Navier-Stokes equations when I remembered that I’d years and years ago I’d tried to build a string simulator and I could never get it to demonstrate a standing wave or exhibit any sort of harmonics. I decided to throw this together just to make sure that it really does behave like a guitar string and I’m satisfied that this is very close. Given basically any initial perturbation and reasonable damping the simulator quickly stabilizes to a super position of a few nice harmonics.

I’d like to add a real time visualization of the strings FFT but I’ll probably never get to it. Anyway, I’m putting it up here for now because I probably wont work on it any more.

Monte Carlo Pi

February 14th, 2011

People have been talking to me a lot about Monte Carlo methods lately.

For kicks I made a Pi Calculator.

After about 12 hours it was estimating Pi to be around 3.1428 and closing … very slowly. I seem to recall I wrote one of these back in college that took around a million iterations before getting to 3.14159

This is NerdAction.

January 3rd, 2010

Someday there might actually be something here. But today, there is not.