August 31, 2011

Logarithmic scaling in acoustics

I want to say first that I really like what Vi Hart does with her videos.

Last week a few people in my twitter stream linked to a pair of videos that Vi did with Sal Khan of the Khan Academy.  Here's one of them:

The point of this video (as I see it) is to discuss the difference between linear and logarithmic scales. It's a great concept that deserves discussion, and they make a decent effort to get their point across, I think.

It's just that the examples from the musical scales are filled with little inaccuracies that could easily have been corrected before they recorded the video.

Vi says at one point (somewhere around the 3:50 mark and after) "C is more like ...I don't know...let's say 300 all right so if this is 300 or 300x or just x...then this frequency would be 600..." Sal was trying to help her out since she didn't know the note frequencies that she was trying to use in her example, so he suggested just calling it "x". I think there can be more confusion introduced by trying to use the "x", but maybe that's just me.   I can get past that, I guess.

There is a whole lot wrong with this small section, I barely know where to start. First, there are no units included at all. Actually, Sal is trying to help Vi by suggesting that they use 440 as a note frequency, but he actually starts to say 440 kHz.  The highest frequency the human ear can hear is around the 20 kHz range for healthy (and young) people. So hundreds of kHz are way out of the range of hearing.  But, Vi wants to use C and picks the number 300 to work with, no units included. This is something I don't let freshman get away with on their work, and I work hard to not let myself forget units, either.

Okay, though, we'll assume the units they are using are Hz. Why pick 300 Hz for a C? (Clearly, she forgot to look up note frequencies. I get it.) Next time Sal and Vi talk about musical notes, I'm sure they will have the note frequencies handy. Why is this important? There is an international standard for note frequencies. Without making this all about tuning and temperament and the origin of harmony, let me say that the standard says that the pitch we call A above middle C is associated with 440 Hz. Based on that standard, if we are looking at an equal temperament scale (the most commonly used scale in music), the frequency of middle C is then 261.63 Hz.  Vi even comments that they have chosen a really "weird" musical scale. Yeah, no kidding.

I know that her point was that the difference in frequencies between notes separated by an octave increases as you go up the keyboard, but is it so hard to use the right frequencies? If the point of posting videos is to be educational, then why have wrong information in them? The details matter. It reminds me of the tuning fork set which have C labeled as having a frequency in multiples of 256 Hz. There have been proposed scales which are based around middle C having a frequency of 256 Hz, but no musician uses this scale today.  Yet, we have countless physics and math teachers who believe that middle C on a piano has a fundamental frequency of 256 Hz. It's unfortunate, because the teachers and their students are missing an opportunity to learn more about music when they use this artificial scientific scale.

Here's a graph of the note frequencies for the equal tempered scale which I took from the page linked above. The horizontal axis is arbitrarily labeled "note number". The point of the graph is to show how the frequency difference between each successive note changes over a wide range of octaves. Each successive point on the graph represents a semitone higher in pitch than the previous note. Your ear perceives each tone as being the same "distance" in pitch as the previous tone, even though the change in frequency is not the same over the whole range.

Let's take the same graph, and make a semi-log plot.  Here, I'm making the vertical axis (frequency) logarithmic.

Notice how now it is a straight line? This is what it means for something to scale logarithmically. The wikipedia page for logarithmic scale has some more examples.

Also, it was too bad that they didn't have any of the actual tones in the examples they were using.  Here's a piano keyboard you can use to hear for yourself how the intervals sound similar across octaves:

Toward the end of the video (around the 6:00 minute mark) Sal makes a comment that the logarithmic scale of frequencies is not the only logarithmic aspect to sound. He refers to the "magnitude of frequencies" and then quickly amends his statement to refer to the decibel scale. Vi chimes in with an example of talking loudly and softly, then starts to say something about the "distances between loudnesses" before Sal interrupts and the video wraps up.  I have no idea what "magnitude of frequencies" and "distances between loudnesses" actually mean.  From the context, it's clear that they are talking about the sound pressure, or sound intensity, or even sound power. All of those are ways to quantify the amplitude of a sound wave. They all mean different things, but the relevant point is that they all scale logarithmically, in a way similar to the pitch scale discussed above.

It's too bad they didn't take the time to go into the example of sound intensity and the decibel level. It's a great example of logarithmic scaling.  Here's an example from an awesome website on musical acoustics at the University of New South Wales in Australia:

Credit for the sound files and flash animation: John Tann and George Hatsidimitris

The example has broadband noise decreasing such that the sound power is halved each step. Sound power is proportional to the square of sound pressure, so if the sound power is halved, the sound pressure is reduced by the square root of 2 in each step. The above animation has the sound pressure envelope outlined in red on a linear scale If I put the sound pressure envelope on a logarithmic scale it looks like this:

I'm graphing sound pressure on the vertical axis using arbitrary units. In many cases where we are looking at measuring sound pressure we don't necessarily need the actual pressure measurement. Many times we only care how it compares to another sound pressure measurement. In this case, I know that the sound pressure level (which is different than the sound pressure!) is changing by -3 dB in each case. It doesn't matter what the original sound pressure is, since it is the relative change between the two that we are interested in.

As with the example of pitch, the sound pressure graph appears linear on a semi-log plot. This is what is meant by something scaling logarithmically. Notice that this example covers 4 orders of magnitude! That is a huge range, but it is only a part of the range of hearing for human ear.  A healthy ear is sensitive to roughly 6  orders of magnitude in sound pressure: from the threshold of audibility to the ear's threshold of pain.

Since it is often unwieldy to deal with values covering several orders of magnitude, even on a semi-log plot, we convert the sound pressure measurements to sound pressure levels (SPL) which is sometimes referred to as a sound level for short. A sound level measurement is always with respect to a reference.  Here's the formula for sound pressure level:

$$L_p = 20 log({p \over p_0})$$
where $L_p$ is the sound pressure level, $p$ is the sound pressure measurement and $p_0$ is the reference sound pressure. Note that the reference sound pressure does not have to be the threshold of audibility. We can use any sound pressure measurement as the reference sound pressure, then the sound pressure level is just a comparison between the two sound pressures.  That's why I didn't need to know the units of sound pressure in the previous graph.  I was only interested in the relative change in the sound pressure level between the sound samples.  In this case, they decreased by 3 dB.  I can use the sound pressure level equation to find the sound level and make a graph of that:

Note now that the graph uses a linear scale, and has the same shape as the semi-log plot of the sound pressure. The logarithmic nature of sound pressure scaling is accounted for by the definition of the sound pressure level.

And just to be clear, I really do like Vi's videos.

August 22, 2011

Parsing XML with python

I'm transitioning my classes from the "Desire to Learn" (D2L) CMS to the ANGEL CMS.  Unfortunately, I didn't export my classes before I lost access to D2L.  Fortunately, I know a great person who was able to export the D2L files and send them to me. 

Unfortunately, ANGEL doesn't recognize D2L files.  Bummer.  

All of my "Reading Quiz" questions are stored in a question bank which is an xml file.  If I knew how to parse XML, I could probably cut and paste questions in to ANGEL.

I don't know how to parse XML, but I do know how to use Google. I found a bunch of tutorials, but ultimately settled on the Dive into Python tutorial.  

My question bank had 2345 entries in it.  The entries were made up of three things: a) the actual questions, b) multiple choice answers and distractors, and c) empty entries between questions.   All the entries were identified with the 'mattext' tag, although there didn't seem to be an easy way to separate the various types of entries.

Here is some code I wrote.  I'm putting it here so I don't lose it.

#!/usr/bin/env python

from xml.dom import minidom 

xmldoc = minidom.parse('./questiondb.xml')
mattextlist = xmldoc.getElementsByTagName('mattext')

for i in range(2345):
#print i
print mattextlist[i]
print "

except AttributeError:
print "

An AttributeError was triggered by every one of the empty entries.  Printing the blank lines every time an empty entry was reached made the output of the script easier to interpret visually.

This was a fun little bit of python.  I'm glad I was able to salvage stuff from the D2L class files.

August 11, 2011

After the Global Physics Department chat, I'm still very confused as to how the sig fig haters instruct their students to report numerical values.

I did a quick experiment measurement tonight: I measured the length and width of a sheet of printer paper in centimeters.  I came up with \( l = 27.95\space \text{cm} \) and \( w = 21.60\space \text{cm} \). Each of the measurements I believed to be within \( \pm 0.05 \space \text{cm} \).

If I want to find the area, what value should I report? \( l \times w = 603.72 \space \text{cm}^2 \) without regard to the number of figures being reported.

Now, if I use the "crank three" method to get the range of values for the area, so that I can report the uncertainty in my area calculation, I would have:

$$l_{max} \times w_{max} = a_{max} = 605.1175\space \text{cm}^2 $$
$$l_{min} \times w_{min} = a_{min} = 601.245\space \text{cm}^2 $$

Then the uncertainty in the area calculation should be:

$$\Delta a = {a_{max} - a_{min} \over 2 } = 1.93625 \space \text{cm}^2 $$

Which means, that if I am a student just learning how to do this, I might report my answer as:

$$ 603.72 \space \text{cm}^2 \pm 1.93625 \space \text{cm}^2 $$

If I'm a really good student, I might remember that my instructor mentioned something about the uncertainty indicating how many digits should be reported in the answer.  Maybe I even have in my notes a simple example from class.  Hmmm, now I'm just confused.  It seems like there should be a way to round my answer (both the value and the uncertainty) appropriately.  But, how? (Remember, I'm still a beginning physics student.)

Here's my (the physics instructor, not the student, now) point: if you hate the rules or guidelines surrounding the traditional way of doing sig figs, that's fine with me.  I'll even hop on that bus with you most of the way.  But at some point, there has to be an actual discussion about the significance of the digits in the answers and the uncertainty.  From there on out, we can choose whatever (appropriate) method we want for finding uncertainty, right?

August 10, 2011

Significant figures are significant

Have you heard about the Global Physics Department Meetings? Physics teachers (high school and college) meet online every Wednesday night for about an hour to chat about a topic related to teaching physics.

This week the topic is about uncertainty, error propagation and significant figures. It seems that there is no shortage of physics teachers who are opposed to teaching about and requiring students to use significant figures.

Count me in the camp which thinks significant figures have a place in our classes and labs that we run.

Here's why:

The goal of a student getting a college degree in physics may not be to ultimately become a Ph. D. physicist (although that is a path that some physics majors start to pursue after graduation). But, there is a reasonable expectation that our students, once graduated, will be in a technical field of some sort that will require them to present data, calculations, results, whatever-you-want-to-call-it. In a word: numbers. (Oh, numbers with units, of course.) If a student graduates having never worked with significant figures, then writes a technical report for their employer with nonsensical data reporting it could reflect poorly on my department or the college and potentially negatively affect future applicants from our program.

I want my students to be thinking about the proper reporting of their numbers from the start of their physics career. I find that encouraging first-year student to wrestle with the idea of proper data reporting is easier than forcing juniors and seniors to have to go back to the concept of significant figures while they are undertaking more complicated laboratories and undergraduate research projects. If the concept of significant figures is already habit for them, it saves time and effort in the long run.

I tend to mostly agree with what Rhett said about significant figures in one of his posts, especially this part:

I believe that many people (you know who you are) either treat significant figures as some fundamental truth, or they haven’t thought about them one way or the other. This is a huge problem when you are sig-fig-stickler.
I'm not (I hope!) a sig-fig stickler. I want my students to be able to follow the general guidelines for using significant figures, but also have the critical thinking skills to be able to break the guidelines, where appropriate.

I've updated a handout that I give to some of my classes (usually general education astronomy classes, although sometime introductory physics classes as well) to emphasize that guidelines are only that: a guide.  We should follow them when appropriate, but only when appropriate.

Here's the handout, if you're interested.