Sampling Error in Mathematica

Happy Halloween!

In the spirit of Halloween, it seems that my Mathematica stopped functioning for the past week (too spooky for me :O). Manipulate[], arguably the most useful function in the program, refused to work. It got to the point where something as simple as this

Manipulate[ToString[a], {a, 0, 1}]

would not work at all!

Thankfully, we’re back up and running. Off to sampling errors!

Sampling Sound Waves

Although we often like to think that sampling will always yield accurate results, this often is not the case. Consider the images below:


The red points represent how often a point from the function is taken. The first example shows that the predicted red function matches the actual wave. However, the next one shows how it seems as if the amplitude of the function is changing. And the last one shows that by taking samples 2Pi increments away from each other, you get a straight line. It should be noted that the sampling itself is a flaw caused by human error; this error is very much different from the fact that there is “infinite precision” for vector graphs.

Such is the danger of interpolating data without sufficient sampling. An interactive version of the above and the code for the images themselves can be found here.

Sampling errors are present in our daily lives as well! The most intuitive example is with sound. Hypothetically, a higher frequency means a higher pitched sound.

(Note: I actually wasn’t aware that you could play sounds in Mathematica until a little less than a year after I started using it. That just goes to show that there are so many unexplored features of this program… I wonder how many people specialize in fields that I’m not even aware of? I feel as if I am pulling so many layers off this onion that the pile of onion shavings is hundreds of times larger than what would seem possible for its size. /rant)


And for a while, the sounds actually do get higher pitched. If you look at the wave with a frequency of 20,000, you can see that Mathematica is struggling.

sampling3But if you suddenly increase the frequency to 256,000, the pitch dramatically decreases. So what gives?


This is actually a problem concerning hardware. By trying to play this sound, you’re literally forcing your headphones (or speakers) to vibrate so quickly that there is now way for it to keep up. By trying to oscillate quickly, it downgrades the frequency of its pitch to a harmonic of 256,000; if you want to see a really good example of this, set the interactive code’s sampling value at 5.5.


Everything suddenly seems lower pitched. If there’s a lesson here, I guess it’s that you should always be careful how you sample… You may even end up with a flat line.

Probably not how this works, but still.

P.S.: Many of the ideas for this post came from my research mentor Dr. James Choi. I am very grateful for his help!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s