View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Sound Pressure Level Calculator

Join the 15-year community celebration.

Play games and win prizes!

» Learn more

3.0
3.0 | 4 ratings Rate this file 29 Downloads (last 30 days) File Size: 121 KB File ID: #35876 Version: 1.2
image thumbnail

Sound Pressure Level Calculator

by

Chad Greene (view profile)

 

28 Mar 2012 (Updated )

Calculates sound pressure level in decibels, given a pressure signal in pascals.

| Watch this File

File Information
Description

Calculate sound pressure level (in dB) of given pressure signal 'p_Pa'.
SYNTAX
spl_dB = spl(p_Pa,ref)
spl_dB = spl(p_Pa,ref,windowSize)
spl_dB = spl(p_Pa,ref,windowSize,Fs)
  
DESCRIPTION
spl_dB = spl(p_Pa,ref) returns sound pressure level in decibels referenced to reference pressure |ref| in pascals. This usage returns a scalar value of spl_dB for the entire p_Pa signal.
spl_dB = spl(p_Pa,ref,windowSize) returns a moving SPL calculation along the window size specified by windowSize, where the units of windowSize are number of time indicies.

spl_dB = spl(p_Pa,ref,windowSize,Fs) returns a moving SPL, where windowSize is not indices of time, but _units_of time equivalent to units of 1/Fs.

INPUTS:
p_Pa = vector of pressure signal in units of pascals. Can be other units if you declare a reference pressure of matching units.

ref = reference pressure in units matching p_Pa or simply 'air' or 'water' if p_Pa is in pascals.

windowSize = window size of moving spl calculation. If no windowSize is declared, the spl of the entire input signal will be returned as a scalar. If windowSize is declared, but Fs is not declared, the units of windowSize are number of elements of the input vector. If windowSize and Fs are declared, the units of windowSize are time given by 1/Fs.

Fs = (optional) sampling frequency. Note! including Fs changes how this function interprets the units of windowSize.

OUTPUT:
SPL = sound pressure level in decibels. If windowSize is declared SPL is a vector of the same length as p_Pa. If windowSize is not declared, SPL is a scalar.

Note that this does account for frequency content. A-weighted decibels (dBA) are frequency-dependent. This function does not compute dBA.

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
EXAMPLES
* * * Example 1: * * *

load train % (let's assume y is has pascals as its units)
spl(y,'air')
ans =
       84.6
   

* * * Example 2: Enter your own custom reference pressure: * * *

load train % (let's assume y is has pascals as its units)
spl(y,20*10^-6)
ans =
       84.6

* * * Example 3: A moving window of 501 elements and plot: * * *

load train
SPL = spl(y,'air',501); % <-- Here's how to use the function.
 
t = cumsum(ones(size(y))/Fs);
figure
subplot(2,1,1)
plot(t,y)
axis tight
ylabel('pressure (Pa)')

subplot(2,1,2)
plot(t,SPL)
axis tight
ylabel('spl (dB)')
xlabel('time (s)')

* * * Example 3: A 10 ms moving window and plot: * * *

load train
SPL = spl(y,'air',0.010,Fs);
 
t = cumsum(ones(size(y))/Fs);
figure
subplot(2,1,1)
plot(t,y)
axis tight
ylabel('pressure (Pa)')

subplot(2,1,2)
plot(t,SPL)
axis tight
ylabel('spl (dB)')
xlabel('time (s)')

Acknowledgements

Slidefun inspired this file.

This file inspired Water Sound Speed Calculator, Seawater Acoustic Absorption Calculator, and Plotpsd.

Required Products MATLAB
MATLAB release MATLAB 9.0 (R2016a)
MATLAB Search Path
/
/__MACOSX
/__MACOSX/spl_demo
/spl_demo
/spl_demo/html
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (11)
09 Mar 2016 Chad Greene

Chad Greene (view profile)

Hi Shruti,

I have not worked with real-time data collection. I do not think the spl function will work for real-time applications as it is currently written.

Comment only
09 Mar 2016 Shruti Modak

Can this be used to plot real time db value of sound from the inbuilt-microphone?

Comment only
05 Mar 2016 Stephen Cobeldick

A very tidy submission. Clearly documented, excellent input/output argument descriptions, does exactly what it says on the box.

I liked the demo file and the clearly presented examples. Top work!

One minor (stylistic) point: it would be nice to have titles in the demo figures ('Handel', etc).

The range of ratings made me curious about this submission, but after checking the code and running some examples it is clear that those first two raters were just being obnoxious.

04 Feb 2016 Christina Vanden Bosch

Hello! I have an RMS vector, how do I convert that into Pascal so that I can use this to convert to dB SPL?

Comment only
16 Sep 2015 Chad Greene

Chad Greene (view profile)

fab--That error message means Matlab can't find the function. Is it still in your downloads folder? Is it unzipped? Is it in your current directory?

Comment only
16 Sep 2015 fab

fab (view profile)

This does not work. I get this same msg everytime: 'Undefined function 'spl' for
input arguments of type
'double'.'

Comment only
22 Sep 2014 Chad Greene

Chad Greene (view profile)

Hi Loannis,

I would suggest thinking about where your data come from. Do you have a sensor that can measure imaginary pascals? What does the imaginary component of your measurement represent?

Comment only
22 Sep 2014 Ioannis

Hi, i would like to ask regarding the given signal. If we have real and imaginary part (in Pa) what will be the function of the SPL?

15 Aug 2014 Chad Greene

Chad Greene (view profile)

The original version of this function was pretty lousy. I made some major changes in 2014 which included significant improvements in efficiency and usability, but without verbal feedback from Piao or André, it is difficult to know whether I have addressed their issues. If you see room for improvement, please describe what you're thinking. I am always happy to hear how my scripts can be improved.

Comment only
11 Jan 2013 André

André (view profile)

 
27 Sep 2012 Piao zhigang  
Updates
21 Apr 2014 1.1

Version 2 now allows for a moving SPL calculation.

06 May 2014 1.2

slidefun now included in the function. Demo included.

04 Mar 2016 1.2

MathWorks update: Added Live Script.

Contact us