
Measuring dB from digital audio 
Johan Halmén
Member #1,550
September 2001

I thought of making a plotter for dB monitoring. dB seems to be a very complex thing, even if there are some formulas for that. But in the end there are lots of things that can vary. So far I've come up with this: The plotter would constantly take sound samples from the microphone, like 500 ms of sound data. For each chunk of 500 ms, it would add the square of each sample. Take the logarithm of the sum. This value would be linearly relative to the dB value of the sound. Calibrate it using a real dB meter. The used mic has to be calibrated, too. Even if the formula above would be right, the used mic would distort it, if not calibrated. But after testing and calibrating, I should have a rather useable dB plotter. It would continuously sample 500 ms, calculate the dB value, plot it, then sample the next 500 ms. Of course computing time would make the updating time higher than 500 ms, but that's no problem, as long as it plots something. Or does the sampling happen in some interrupt mode while the computer does the calculating? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest. 
Jonatan Hedborg
Member #4,886
July 2004

Im not sure it's as easy as to take the logarithm of the sum; the math looks something like: 20 log (P2/P1) where one of the P's is input power and the other is a reference power (1 × 1012 W/m²).

Johan Halmén
Member #1,550
September 2001

Yes, in my model the sum of the squares of the samples would equal P2/P1. But I'm not sure if it is like that. A microphone has a diaphragm that covers an area, which would be the m² part and the mic chord transfers an AC signal, which would be the W part. So in the end I just want to compute how many effect units a signal is, analysing only the wave form, that is amplitudes and frequencies and such. If it were a simple sine wave, the amplitude would be enough (or would it?). But when it is a very complex wave form, I thought summing the squares of the samples would be a measure of the effect of the wave. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest. 
Tobias Dammers
Member #2,604
August 2002

If you want the power of the signal, you want RMS (Root Mean Square). Take the square of each sample, sum them together, divide by number of samples and squareroot. Transform into dB through logarithms and add or subtract a calibration bias. There.  
Johan Halmén
Member #1,550
September 2001

Great! Thanks, Tobias. I thought it would be something like that. The root thing I wouldn't have come up with. I'm still working on the sound recording thing. So far I have this code:
I guess I do something wrong, it crashes in the middle of the read_sound_input() call. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest. 
Damian Grove
Member #6,758
January 2006

If you wanted to simulate the classic VU meters, you could do 300ms averaging while applying a 10dB lag. I think that would be rather neat. Get into awesome with Saxman!  http://www.youtube.com/saxman727 
Johan Halmén
Member #1,550
September 2001

I just turned to PortAudio and it works better than Allegro's sound recording. Cool. Next week I'll present my dB meter and as an extra bonus a guitar tuning program. Or something. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest. 
