In this post, we discuss why we represent SPL on a logarithmic scale, why you should care, and how we implement that efficiently in small CPUs.
This is part of a series of articles on the general subject of audio signal processing from air to information.
Previous installments include:
- Introduction to MEMS Microphones
- Using a PDM Microphone
- PDM in a Tiny CPU
- Resolution of audio recovered from a PDM data stream
- PDM in ATTiny85 Source Code
SPL is Logarithmic
Sound Pressure Level (SPL) is almost always expressed on a logarithmic scale and written with the unit dB SPL.
But why use a log scale?
To be useful, a scale for SPL must describe anything from a whisper^{1} to a scream^{2} (really, anything heard by “typical” healthy humans^{3}) with convenient values. It should also extend to cover sound outside the safe auditory range in a natural way. Finally, it should have a firm mathematical basis on physical measurements.
So the reference point is a sound quieter^{4} than a whisper, approximately the sound of a mosquito 3 meters away, which is arbitrarily called 0 db SPL. A sound loud enough that most literature agrees is painful is 130 dB SPL. That scale spans the whole perceptual range from just perceptibly quiet to painfully loud with numeric values under a hundred or so, and naturally extends to levels too quiet to perceive as well as levels grossly unsafe to perceive.
But that is a log scale, and we haven’t justified why.
The scale from 0 dB to 130 dB by definition spans a range of 6.5 orders of magnitude of pressure. The quietest perceivable sound is taken to be 1 kHz at 20 µPa RMS (0 dB SPL), and the threshold of pain to be 63.2 Pa RMS (130 dB SPL).
Six and a half orders of magnitude means that the loudest sounds you should be hearing are about three million^{5} times the RMS pressure of the quietest sound you can hear.
So a log is convenient
Since hearing responds to pressure in a generally logarithmic way, using a logarithmic scale for SPL mimics that and makes a number of comparisons of sounds easy.
In a lot of cases it doesn’t really matter how loud an alarm is, as long as it is louder (and more annoying) than the background noise. Annoying is hard to characterize, but we can measure loudness as SPL. Using the logarithmic scale, identifying something as alarming that is 20 dB louder than the ambient level really means 10 times the measured pressure, and is meaningful at all ambient levels. It also means that making the music louder by 3 dB has the same perceptual effect (it got louder by about the same amount).
Naturally, some absolute sound levels have a natural (and important) uses. Long exposures to levels over 85 dB SPL are thought to cause hearing loss, for instance. There are standards that describe alarms that define the SPL at the sounder itself as an absolute level. Environmental standards that refer to or regulate background noise are usually written to absolute levels as well.
As a point of reference, at sea level the loudest sound that can be made without distortion peaks at 1 atm pressure, which is 191 dB SPL, and really not a sound you want to hear. Distorted “sounds” that are louder are possible, and can be produced by explosions.
This is the real value of using a log scale for signals with low precision and a large dynamic range. Small values are visible, and large values are practical. At that point, arranging the output to be 26 dB/V still has a peak value at 130 dB, and can still represent a quiet room (about a volt), a conversation (about 2 volts) and an alarmingly loud sound (3.5 volts or so), and still have room to represent dangerously loud sound.
What if it were linear?
Six and a half orders of magnitude is either a long ruler or a lot of resolution.
If SPL were measured by a linear RMS pressure scale on a ruler with the mosquito at the 1 mm mark, loud conversation lands at 1 m, a really annoying alarm siren would be out at 50 m, riskily loud noises at 1 km, and the painful end of the ruler would be at 3.162 km.
Not that easy to fit in a pocket.
If you want to have the whole thing be portable if not exactly pocket-able, consider a 1 m ruler covering the whole scale from silent to 130 dB. On that scale, the just audible 20 µPa mosquito hum is at the 316 nm mark (about one wavelength of UV light from the end!). The annoyingly loud 1 Pa alarm is marked at 15.8 mm , and risky 20 Pa is marked at 31.6 cm.
This is not a good use of the material we made the stick from. Most of the stick is given over to the loudest sounds, with the interesting levels ranging from quiet room to conversation, party, loud traffic, through possible hearing damage all packed in the bottom two cm.
For our analog output ranging from 0V to 5V, the analogous thing happens. With a linear scale where 0 V corresponds to -∞ dB and 5 V to 130 dB, some familiar sound levels translated to output voltage might be^{6}:
Sound dB SPL Pressure 5V Analog
RMS pressure of -20 µPa 0.00 dB 20 µPa 1.58 µV
flying mosquito at 3 m ~0 dB 20 µPa 1.58 µV
pin drop from 1 cm 15 dB 112 µPa 8.9 µV
quiet room ambient level 30 dB 632 µPa 50 µV
normal conversation at 1 m 40-60 dB ~6.32 mPa 158 µV to 1.58 mV
passenger car at 10 m 60-80 dB ~63.2 mPa 1.58 mV to 15.8 mV
busy traffic at 10 m 80-90 dB ~356 mPa 15.8 mV to 50 mV
possible hearing damage 85 dB 356 mPa 28.1 mV
RMS pressure of 1 Pa 94.00 dB 1.00 Pa 79.3 mV
risk of permanent damage 120 dB 20 Pa 1.58 V
loudest recorded human scream 128 dB 50 Pa 3.97 V
threshold of pain 130 dB 63.2 Pa 5 V
Since we want to use our sensor in a practical way, it should be able to provide an output that usefully distinguishes a quiet room from an occupied room and include headroom to tell an alarm ringing from just a noisy TV.
For the linear output scale in the table, everything quieter than a nearby ringing alarm is in the 0.1 V decade or lower. If that 5V signal is measured by a 12 bit ADC (a very plausible add-on for an Arduino for better resolution than the built-in 10 bit ADC) then it has a 1.22 mV quantization (4095 steps spanning 5V) so too much of the useful range is coded in the first dozen values of the ADC. The built in 10 bit ADC of the ATMega8, by the way, would hide every sound up to 70 dB in the first count of the ADC reading, which is even worse.
But, you are thinking, making the top of the scale be “to the pain”^{7} was just silly. Why not scale it so that 5V is near that handy benchmark of 1 Pa, or 94 dB SPL? That only moves the goal post by a factor of 63 or so. With 94 dB set at 5V, then 50 dB would be 31.6 mV. The first non-zero 12-bit ADC count (1.22 mV) would be 22 dB, and the first 10-bit ADC count (4.89 mV) would be at 34 dB.
This is the real value of using a log scale for signals with low precision and a large dynamic range. Small values are visible, and large values are practical. At that point, arranging the output to be 26 dB/V still has a peak value at 130 dB, and can still represent a quiet room (about a volt), a conversation (about 2 volts) and an alarmingly loud sound (3.5 volts or so), and still have room to represent dangerously loud sound.
Room for Argument
The common use of db SPL It has its advantages and disadvantages, naturally.
A big advantage is that multiplication (the effect of an amplifier or filter) is transformed to addition. Since the effect of changing distance from the sound source is just a multiplication by the ratio of the two distances, that can be expressed by addition of the log of the ratio.
A big disadvantage is that addition (e.g. the effect of turning on a second engine at a different distance and noise output) is more complicated.
Microphones are Linear, SPL is Logarithmic
Measuring small variations in air pressure that happen over very short time scales is easy with the right sensor, widely known as a microphone. Every practical microphone implementation produces a signal that is linear in pressure.
If the microphone is calibrated, then a 1 kHz tone at a given SPL will produce a predictable magnitude of signal, which can be RMS averaged to discover a signal proportional to the RMS pressure at the microphone. This signal is then converted to SPL by scaling it to represent pressure in Pa divided by the reference pressure of 20 µPa, then computing the logarithm and scaling by 20 for display as dB SPL, or merely as required to construct an output signal.
For the ATTiny85 SPL Demonstration, we used a microphone with a digital output specified to capture a 94 dB reference tone as -26 dB FS, with a typically -6 %FS DC offset.
As detailed in the posts about it, converting the 1-bit 1 MHz sample rate from the microphone into nearly 8 kHz 15 bit PCM samples then computing a mean absolute value required only addition and subtraction. The final accumulator needs to be divided by the SPL box size and have its logarithm taken. But those operations can be rearranged, with the log taken first, and division by the box size accounted for by subtracting a constant.
That leaves the only “hard” math operation being the logarithm. Even that can be simplified substantially by noticing that taking the log base 2 is only different by a scale factor from log base 10, and that for fixed point positive binary values, log base 2 is easily approximated by finding the leftmost set bit in the value, and using the remaining bits as a linear interpolation to the next power of 2.
Since our sum over the complete SPL window fits in a 32-bit integer, we can find the leftmost set bit with a loop over at most 32 iterations, and fewer if we can further constrain the magnitude of the sum and if we want some bits left for interpolation. It is then easy to compute 8*log2(v)
for any 32-bit unsigned value and know that the result always fits in an unsigned 8-bit value.
Conversion from 8*log2(v)
to db SPL is then requires scaling to change to change the base of the log to 10, and offsetting to place -26 dB FS at 94 dB SPL.
The Bottom Line
SPL is a logarithmic scale because it makes sense.
If you are measuring loudness, you want your sensor to produce a logarithmic signal so that you can distinguish events from the background at all levels.
Getting SPL from a linear microphone alone requires calculation, so having a sensor that does that for you simplifies your application.
Our next implementation of the SPL meter will document the relationship between sensor output voltage and dB SPL at the microphone.
(Written with StackEdit.)
- approximately 40 dB SPL per HAMBY 2004, which actually makes a whisper loud compared to a quiet room as anyone caught whispering in a library or during an can attest. ↩
- Loudest human scream observed at 8 feet 2 inches was recorded at 128 dB SPL per HAMBY 2004. That would be more like 136 dB at one meter, if you want to compare it to the whisper (40 dB), or loud conversation (60 dB). The best screamers are really loud. ↩
- Habitually listening to sound at the loud end of that range, or being subjected to sounds even louder than 130 dB SPL is likely to not only damage hearing, but can in extreme cases be lethal. Wear ear protection, kids. And pay attention to how loud you’ve turned up your earphones too. And seek professional advice about how loud is too loud, I’m not giving that kind of advice here. ↩
- It is more complicated than that, naturally. The Wikipedia article on equal loudness contours does a pretty good job of explaining the complexity. But the mosquito isn’t a completely arbitrary standard. 0 dB is actually defined for a 1 kHz pure tone, which is the frequency at which all studies agree human hearing is most sensitive. ↩
- “about three million” is exactly . ↩
- Table of values cherry picked from Wikipedia and HAMBY 2004. ↩
- It was. ↩