Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Measuring dB from digital audio

This thread is locked; no one can reply to it. rss feed Print
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 the red "x" that closes a window, really isn't red, but white on red background.

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
avatar

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 × 10-12 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 the red "x" that closes a window, really isn't red, but white on red background.

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
avatar

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 square-root. Transform into dB through logarithms and add or subtract a calibration bias. There.

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

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:

1...
2size = start_sound_input(44100, 16, 0);
3buffer = new unsigned int(size);
4textprintf_ex(screen, font, 10, 10, 0xffffff, 0, "%d %d", size, buffer);
5alert("Start monitoring the mic", "", "", "Ok", NULL, -1, -1);
6 
7do
8{
9 if (read_sound_input(buffer) != 0)
10 {
11 clear_bitmap(screen);
12 p = buffer;
13 for (int i = 0; i < 1024; i++)
14 {
15 putpixel(screen, i, 384 - (*p) / 256, 0xffffff);
16 p++;
17 }
18 }
19}
20while (!key[KEY_ESC]);

I guess I do something wrong, it crashes in the middle of the read_sound_input() call.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Years of thorough research have revealed that the red "x" that closes a window, really isn't red, but white on red background.

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!

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 the red "x" that closes a window, really isn't red, but white on red background.

Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest.

Go to: