Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
My greatest pleasures in mathematics come from observing–and here, listening to–the interplay of simple and complex. With a few axioms and definitions you can create surprising worlds, and in what seems like a mess you can find beautiful regularities. It’s damn sexy, frankly.
Here, I use a simple recursive equation to directly generate my sounds in R:
The basic idea is that to get the next value in the sequence, you add
No need for such jargon right now. With
< embed width="450" height="81" src="http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F11733976" allowscriptaccess="always" type="application/x-shockwave-flash">
The soundcloud widget shows you the waveform, which is a (very smooshed-together) graph of the function. I generated 100,000 values and then wrangled in R to set the scale and write the sound file. Sound files, after all, are just a bunch of numbers that your speakers turn into pulsations of sound; here I’m generating those numbers directly using an equation I learned about in time series class. (This is why Peaches instructs us, “Stay in school, cuz it’s da best.”) You’re listening to the data at a rate of 10,000 numbers per second–much lower the CD-quality rate of 44,100 but a convenient round number for us 10-fingered folk–which means that since the whole sequence is 100,000 long that it lasts 10 seconds.
Through the awesome powers of math, I picked values of the parameters that generated a recognizable tone–the A below middle C, or 220 Hertz (cycles per second). For certain parameters, this equation can generate periodic behavior that we can hear as a tone. It’s close to a pure tone–but not quite, there’s a wavering quality to it. You can hear and see that when I did this, there’s a moment in the middle where the sound gets much fainter. This is caused by the element of randomness–the
Here’s another couple of possible values plugged into the equation:
This generated this tone: < embed width="450" height="81" src="http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F11736326" allowscriptaccess="always" type="application/x-shockwave-flash">
This tone, which has random perturbations on a 2000 Hz tone, has a substantially fuzzier sound, less pure than the above. You can see why this might be the case by comparing the above equation with the values I used to generate the first tone–both of the coefficients in this newer equation are closer to zero. This means that less of what you’re hearing is a periodic function that makes a distinct tone, and you’re hearing more of the randomness–i.e. white noise like you’d hear on a TV when you’re between channels.
It turns out that
What if we try 0.2 for
And the result: < embed width="450" height="81" src="http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F11737113" allowscriptaccess="always" type="application/x-shockwave-flash">
And…the noise clearly wins! The periodic aspect of this is too subtle to be audible.
To drive home the point about how
I’m looking forward to making some music with these tasty sounds, so stay tuned! The code is available on my github. Much gratitude, again, belongs to Uwe Ligges and the other contributers to the tuneR package for R.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.