How to compute SPL from sound inputed from a microphone.

General discussion about writing or understanding source code in audio software that does not fit in the other specific Programming and Development boards.

How to compute SPL from sound inputed from a microphone.

Postby lance » Mon Oct 26, 2009 1:43 pm

Dear all
I am new to audacity.
I want to develop an ap with function of measure sound level. I have not enough knowlegde about that. May I have your suggestion? Is there some code in audacity I can reference?
My programme language is C# and MFC.
Any suggestion will be greatly appreciated.

Email:lanxiaolin007@sina.com
Xiaolin.lan
lance
 
Posts: 3
Joined: Mon Oct 26, 2009 1:02 pm

Re: How to compute SPL from sound inputed from a microphone.

Postby Irish » Mon Oct 26, 2009 8:11 pm

It is not as simple as that.

To measure sound level, you have to know the following:

With [this microphone], a sound pressure level (SPL) of [this number of decibels] at [this frequency] at a distance of 1 metre from the mic gives an output of [this number of millivolts].
With [this sound card] on [this computer] using [this operating system], and with [these input settings in Control Panel] and [these input settings in Audacity], an input of [this number of millivolts] gives a level of [this number of decibels] on the recording level meters in Audacity.

That's eight variables between the sound and the display you will get in Audacity. Change any one of them and your answer will be wrong.

Now, having completely discouraged you, there is one way you could get an appoximate value for SPL using Audacity;
Beg, borrow or rent a Sound Pressure Level meter. Put its microphone and your microphone (connected to Audacity) side by side and record a range of sounds.
Note the reading on the SPL meter and Audacity's level meter for each sound.

Now you know that with that microphone, that computer and that setup, a level of "x" dB on the Audacity meter means that the sound level is "y" dB.

That's about as close as you will get.

PO'L
Irish
 
Posts: 391
Joined: Sat Sep 05, 2009 9:25 pm
Location: Dublin, Ireland

Re: How to compute SPL from sound inputed from a microphone.

Postby lance » Mon Oct 26, 2009 10:53 pm

Dear PO'L
Thanks for your reply.
I will make the solution according your suggestion.
Could you tell me which file the code is used to compute sound level locate at?
I have read the code for a day, but I am so dizzy because of too many source code files.
lance
 
Posts: 3
Joined: Mon Oct 26, 2009 1:02 pm

Re: How to compute SPL from sound inputed from a microphone.

Postby Irish » Mon Oct 26, 2009 11:17 pm

I don't think the source code will tell you a lot.

Audacity doesn't calculate SPL values; it just converts the analogue input to digital and assigns 0dB on the recording level meters to a certain input level, which corresponds to the maximum value of the digital signal. I'm not familiar with the source code, but I don't think you will find a part of it that you can take out and use as a separate application.

Maybe someone else can point you to the part of the code that generates the meter display.

PO'L
Irish
 
Posts: 391
Joined: Sat Sep 05, 2009 9:25 pm
Location: Dublin, Ireland

Re: How to compute SPL from sound inputed from a microphone.

Postby lance » Tue Oct 27, 2009 10:35 pm

OK!
I should try to look up some other solution.
Thanks!
Lance
lance
 
Posts: 3
Joined: Mon Oct 26, 2009 1:02 pm

Re: How to compute SPL from sound inputed from a microphone.

Postby kozikowski » Sat Nov 14, 2009 7:34 am

<<<That's about as close as you will get.>>>

Real SPL meters also have curves. Workplace rules for dangerous noises are written in "A" curve which gives less importance to very high and very low pitch sounds. The "C" curve isn't a curve at all, but gives equal importance to all pitches of sound. So even if you got the code to work, you'd still only have one of the two meters.

dBSPL meters are not all that big a deal to walk out and buy one.

http://www.radioshack.com/search/index. ... meter&sr=1

I have an analog one and the company owns a digital one. They still make both because you can't measure music with the digital one.

Koz
kozikowski
 
Posts: 10920
Joined: Thu Aug 02, 2007 5:57 pm
Location: Los Angeles

Re: How to compute SPL from sound inputed from a microphone.

Postby mr.ogren » Thu Nov 26, 2009 8:45 am

I posted a Nyquist plugin to the audacity-nyquist list a long time ago that can calculate the equivalent and maximum A-weighted level of a selection in an audio track (only mono, and must be 44.1 kHz or higher sampling rate). I have verified it against professional software with deviations less than 0.1 dB.

However, the absolute level must be calibrated which can be a snag for the non professional user. Right now I have set it so that full scale in audacity gives 94 dB(A) SPL at 1000 Hz (94 dB SPL corresponds to a pressure of 1 Pa rms). To use it for more than simple tests you must also ensure that your microphone and soundcard are "good enough".

Thanks to Edgar for developing the x-lisp code for the A-weighting filter!

Code: Select all
;nyquist plug-in
;version 1
;type analyze
;name "Equivalent and maximum dB(A)..."
;action "Calc. A-weighted equivalent level (LAeq) and maximum level with time weighting FAST (LAFmax)..."

; Mikael Ogren, mr.ogren@gmail.com
; 2007-01-12
; Licensed under GPL, no warranty, use at your own risk...

; Calibration so that a 1000 Hz tone with amplitude 1.0 gives 94 dB
(setq calibration (+ 94 28.2))

; A-weighting by Edgar (thanks!)
(setq sa (lp (lp (hp (hp (hp (hp s 20.6) 20.6) 107.7) 737.9) 12200) 12200) )

; Exponential time-weighting filter FAST (125 ms)
; snd-avg is used to downsample to 100 Hz (by averaging over 441 samples)
; This only works for 44.1 kHz sampling frequency, perhaps someone can help out here
; by making a more general approach that works for all sampl. frq?
; The filtering part is OK for all frequencies, but the "441" constant is not.
; -
; The constant 0.000001 is to avoid clipping at filtered squared pressure > 1.0
(setq saf2
(mult 0.000001
(snd-avg (snd-biquad (mult sa sa ) 1 0 0 (exp (/ 1 (mult (snd-srate sa) -0.125))) 0 0 0) 441 441 OP-AVERAGE)
)
)

; Length of the downsampled pressure squared signal
(setq mlength
(snd-length saf2 99999999999)
)

; Calc. the equivalent level
(setq leq
(+ calibration (* 0.5 (linear-to-db (snd-maxsamp (snd-avg saf2 mlength mlength OP-AVERAGE) ))))
);

; Calc. the maximum level
(setq lmax
(+ calibration (* 0.5 (linear-to-db (snd-maxsamp saf2))))
);

; Set the output format to 3 digits (example: 53.3 dB)
(setq *float-format* "%#3.3g");

; Output result as a label track (or append into existing label track)
(setq u (format NIL "LAeq= ~A  LAFmax= ~A" leq lmax))
(list (list 0.0 u))
mr.ogren
 
Posts: 2
Joined: Thu Nov 26, 2009 8:03 am

Re: How to compute SPL from sound inputed from a microphone.

Postby endolith » Fri Jan 15, 2010 10:48 pm

mr.ogren wrote:I have verified it against professional software with deviations less than 0.1 dB.


Can you give more details about what tests you did? Does it work accurately at low levels, for measuring noise floors of audio converters? It looks like the same filter coefficients as the MATLAB thing I translated to make this: http://gist.github.com/148112
endolith
 
Posts: 27
Joined: Fri Jan 15, 2010 10:31 pm

Re: How to compute SPL from sound inputed from a microphone.

Postby mr.ogren » Fri Mar 26, 2010 7:28 am

I made a few example noise and click like waveforms and analyzed with Audacity and two other systems, PULSE by B&K and Artemis by Head Acoustics.

Performance at low input levels will be determined by the hardware, not the filter implementation. The difference between your filter and the one I use (which is a reversed engineered A-filter posted by Edgar on the Audacity Nyquist e-mail list) and the link in your post is the use a bilinear transform. Edgars solution instead uses the filter design tools of nyquist (lp and hp) to create a chain of multiple filters.
mr.ogren
 
Posts: 2
Joined: Thu Nov 26, 2009 8:03 am

Re: How to compute SPL from sound inputed from a microphone.

Postby endolith » Sat Jul 10, 2010 4:10 am

Ok, so, I learned a little Lisp :o and tried to figure out how to return the RMS level as a dialog box. It seemed to work with sine waves, but when I tried other sources, it didn't match my other measurements. I'm guessing I'm not using the RMS function correctly. It doesn't output a single number? So then what does it output?

Can anyone explain?

Code: Select all
;nyquist plug-in
;version 1
;type analyze
;name "Amplitude statistics..."
;action "Calculate amplitude statistics..."

; Mikael Ogren, mr.ogren@gmail.com
; 2007-01-12
; Licensed under GPL, no warranty, use at your own risk...

; Set the output format (example: 53.3 dB)
(setq *float-format* "%#3.2f");

(if (arrayp s) ; stereo
    (progn
        ; Calculate the maximum and RMS levels
        ; RMS of full scale square wave is 0 dBFS
        (setq lmax (snd-maxsamp (aref s 0)))
        (setq rmax (snd-maxsamp (aref s 1)))
        (setq lrms (snd-maxsamp (rms (aref s 0))))
        (setq rrms (snd-maxsamp (rms (aref s 1))))

        ; A-weighted version of sound - by Edgar (thanks!)
        (setq sa (lp (lp (hp (hp (hp (hp s 20.6) 20.6) 107.7) 737.9) 12200) 12200) )

        ; Calculate the RMS level of the A-weighted signal
        ; constant is a fudge factor to normalize to 0 dB at 1 kHz
        (setq larms (* 1.34374 (snd-maxsamp (rms (aref sa 0)))))
        (setq rarms (* 1.34374 (snd-maxsamp (rms (aref sa 1)))))

        (format NIL
            "Left:~%Peak level ~a (~a dBFS)~%RMS level ~a (~a dBFS)~%A-weighted: ~a dBFS(A)~%~%Right:~%Peak level ~a (~a dBFS)~%RMS level ~a (~a dBFS)~%A-weighted: ~a dBFS(A)"
            lmax (linear-to-db lmax) lrms (linear-to-db lrms) (linear-to-db larms)
            rmax (linear-to-db rmax) rrms (linear-to-db rrms) (linear-to-db rarms))
    )
   
    ; mono
    (progn
        ; Calculate the maximum and RMS levels
        ; RMS of full scale square wave is 0 dBFS
        (setq lmax (snd-maxsamp s))
        (setq lrms (snd-maxsamp (rms s)))

        ; A-weighted version of sound - by Edgar (thanks!)
        (setq sa (lp (lp (hp (hp (hp (hp s 20.6) 20.6) 107.7) 737.9) 12200) 12200) )

        ; Calculate the RMS level of the A-weighted signal
        ; constant is a fudge factor to normalize to 0 dB at 1 kHz
        (setq larms (* 1.34374 (snd-maxsamp (rms sa))))

        (format NIL
            "Peak level ~a (~a dBFS)~%RMS level ~a (~a dBFS)~%A-weighted: ~a dBFS(A)"
            lmax (linear-to-db lmax) lrms (linear-to-db lrms) (linear-to-db larms))
    )
)
Attachments
not quite right white and pink.PNG
noise does not measure right
not quite right white and pink.PNG (76.97 KiB) Viewed 346 times
sine waves in three programs.PNG
sine waves measure right
sine waves in three programs.PNG (92.11 KiB) Viewed 346 times
endolith
 
Posts: 27
Joined: Fri Jan 15, 2010 10:31 pm

Next

Return to General Audio Programming



Who is online

Users browsing this forum: No registered users and 1 guest