Audible Illusions with PTC Mathcad Programming

Programming2

**Guest Post by Luke Westbrook**

When I was a kid, I was enthralled by optical illusions. Okay, you caught me; I am still a big fan of optical illusions. Who isn’t? It’s so fascinating to look at a stationary picture that appears to be moving or to think that you’re seeing colors and shapes that aren’t actually in the image. As a kid, it was like magic to me. When I learned that these illusions were in reality tricking my brain by taking advantage of the way it processes visual cues and stimuli, these illusions became even more intriguing to me. We depend on our brains to accurately interpret the data that our sensors send it, and yet sometimes our brains betray that trust and misinterpret the input.

Of course, this is old news. Everyone has seen optical illusions. But what about audible illusions? Can we trick our brains into hearing something that is not reality? Nope. That’s the end of this blog. See you next week.

Kidding, of course. Yes! We can indeed use sound to trick our brains, and, what is more, I am going to show you how to leverage PTC Mathcad’s programming capabilities (one of my favorite features of PTC Mathcad Prime) to do so!

PTC Mathcad’s programming feature is an incredibly useful tool that does not require you to go through the trouble of learning a scripting language. For example, Excel macros, written in VBscript, not only hide the math but put a requirement on the reader to be VBscript literate. This makes it harder to reuse or even verify. Programming in PTC Mathcad is light on syntax and incorporates natural math notation, allowing users to focus on the logic of the algorithm. By building programs in PTC Mathcad, users can perform iterative looping using the “while” and “for” keywords, create conditional statements specifying when to perform a certain task, or construct complex user-defined functions allowing multiple actions to be packaged in a single function. How do those capabilities allow us to create an audible illusion?

Well, one of the ways that your brain determines where a sound is coming from is through interaural time differences (ITDs), which is really just a fancy term that means that if a dog barks to your right, the sound waves will reach your right ear a fraction of a millisecond before they reach your left ear. Consciously, you don’t notice this, but your brain actually uses this time difference to give you an idea of where the sound is coming from.

What if we take a sound recording and manually delay the left-hand signal with respect to the right-hand signal, you ask? First, you ask great questions! Second, that’s a great idea! Take a look at the user-defined function I wrote using programming.Programming in PTC Mathcad

The arguments are an angle for the sound source (0° is directly ahead, +90° is to the right, and -90° to the left), the name of a WAV sound file (just do an online search for free WAV downloads; there are a lot out there), and the name of the new WAV file you are going to create. Without getting into too much depth, the program first gathers information about my WAV file, then puts the actual sound data into matrix M. Since my WAV is only one channel, I have to copy that data into a second column for M to get stereo sound (one channel for each ear). Using another user-defined function that gives the ITD in units of seconds depending on angle, radius of the head r, and speed of sound in air c, I then determine how many zeros need to be added into the channels to delay the signal.

MC_pg2

Now, I want to add conditions. If there is no delay, I don’t want to modify the signals. If the source is to the right (positive angle), I want to delay the left channel, and vice versa for a negative angle. To do this, I insert an “if” statement. This leads me to a very helpful shortcut. There are 3 ways to add keywords (like “if”) to a program in PTC Mathcad: (1) use the mouse to click on the Programming menu in the Math tab, but that takes time, (2) use the hotkey shortcut (which is [}] for “if”), but those are tough to remember, or (3) the magic shortcut: just type the word “if” and press [Ctrl]+j to convert it to a programming keyword!

After adding zeros to the signals to delay the appropriate channel, I create a new WAV file. Now, I can perform this function for a signal to the right and voila!

PTC Mathcad Programming

I can now go to the appropriate directory, play the newly created file and (I suggest using headphones to really hear the effect) the sound seems to be coming from the right, even though both signals are entering my ears at the same decibel level. All I have done is delayed the left signal to make my brain think the sound is coming from the right! I created an audible illusion using PTC Mathcad!

If you want to try it out for yourself (which I highly recommend), click here for your free permanent download of PTC Mathcad Express. Remember to activate your 30-trial of PTC Mathcad Prime,where you can test out all the premium features like programming.

Click here for more information on programming in PTC Mathcad.

This entry was posted in Education, Tips & Tricks and tagged , , , , , . Bookmark the permalink.

2 thoughts on “Audible Illusions with PTC Mathcad Programming”

  1. Quo Vadis says:

    Could we have the Mathcad worksheet for this?

    1. Beverly Spaulding says:

      We have added the worksheet to the community website here: http://communities.ptc.com/docs/DOC-6266
      Please let us know if you need anything else.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s