Measure sound pressure level of audio signal

The `splMeter`

System
object™ computes sound pressure level measurements. The object returns measurements for:

frequency-weighted sound levels

fast or slow time-weighted sound levels

equivalent-continuous sound levels

peak sound levels

maximum sound levels

To implement SPL metering:

Create the

`splMeter`

object and set its properties.Call the object with arguments, as if it were a function.

To learn more about how System objects work, see What Are System Objects? (MATLAB).

`SPL = splMeter`

creates a System
object, `SPL`

, that performs SPL metering.

`SPL = splMeter(`

sets each
property `Name,Value`

)`Name`

to the specified `Value`

. Unspecified
properties have default values.

```
SPL =
splMeter('FrequencyWeighting','C-weighting','SampleRate',12000)
```

creates a System
object, `SPL`

, that performs C-weighting and operates at 12
kHz.Unless otherwise indicated, properties are *nontunable*, which means you cannot change their
values after calling the object. Objects lock when you call them, and the
`release`

function unlocks them.

If a property is *tunable*, you can change its value at
any time.

For more information on changing property values, see System Design in MATLAB Using System Objects (MATLAB).

`Bandwidth`

— Width of analysis bands`'Full band'`

(default) | `'1 octave'`

| `'2/3 octave'`

| `'1/3 octave'`

Width of analysis bands, specified as `'Full band'`

, ```
'1
octave'
```

, `'2/3 octave'`

, or ```
'1/3
octave'
```

. If `Bandwidth`

is specified as ```
'Full
band'
```

, the SPL meter returns one set of measurements for the whole frequency
band. If `Bandwidth`

is specified as `'1 octave'`

,
`'2/3 octave'`

, or `'1/3 octave'`

, the SPL meter
returns one set of measurements per octave or fractional-octave band.

**Tunable: **No

**Data Types: **`char`

| `string`

`OctaveFilterOrder`

— Order of octave filter`2`

(default) | even integerOrder of the octave filter, specified as an even integer.

**Tunable: **No

To enable this property, set Bandwidth to `'1 octave'`

,
`'2/3 octave'`

, or `'1/3 octave'`

.

**Data Types: **`single`

| `double`

`FrequencyWeighting`

— Frequency weighting applied to input`'A-weighting'`

(default) | `'C-weighting'`

| `'Z-weighting'`

(no weighting)Frequency weighting applied to input, specified as `'A-weighting'`

,
`'C-weighting'`

, or `'Z-weighting'`

, where
Z-weighting corresponds to no weighting. The frequency weighting is designed and
implemented using the `weightingFilter`

System
object.

**Tunable: **No

**Data Types: **`char`

| `string`

`TimeWeighting`

— Time weighting (s)`'Fast'`

(default) | `'Slow'`

Time weighting, in seconds, for calculation of time-weighted sound level and maximum
time-weighted sound level, specified as `'Fast'`

or
`'Slow'`

. The `TimeWeighting`

property is used to
specify the coefficient of a lowpass filter.

`'Fast'`

– 1/8`'Slow'`

– 1

**Tunable: **Yes

**Data Types: **`char`

| `string`

`PressureReference`

— Reference pressure for dB calculations (Pa)`2e-5`

(default) | positive scalarReference pressure for dB calculations in Pa, specified as a positive scalar.

**Tunable: **Yes

**Data Types: **`single`

| `double`

`TimeInterval`

— Time interval for reporting level measurements (s)`1`

(default) | positive scalarTime interval, in seconds, to report equivalent-continuous, peak, and maximum time-weighted sound levels, specified as a positive scalar integer.

**Tunable: **No

**Data Types: **`single`

| `double`

`CalibrationFactor`

— Scalar calibration factor multiplied by input1 (default) | positive finite scalar

Scalar calibration factor multiplied by input.

To set the calibration factor using a reference tone, use `calibrate`

.

**Tunable: **No

**Data Types: **`single`

| `double`

`SampleRate`

— Input sample rate (Hz)`44100`

(default) | positive scalarInput sample rate in Hz, specified as a positive scalar.

**Tunable: **No

**Data Types: **`single`

| `double`

`audioIn`

— Audio input to SPL metercolumn vector | matrix

Audio input to the SPL meter, specified as a column vector or matrix. The columns of the matrix are treated as independent audio channels.

**Data Types: **`single`

| `double`

`Lt`

— Time-weighted sound level (dB)column vector | matrix | 3-D array

Time-weighted sound level in dB, returned as a column vector, matrix, or 3-D array
the same type as `audioIn`

.

Size and interpretation of the outputs depend on what the Bandwidth property is set to:

`'Full band'`

(default) ––`Lt`

,`Leq`

,`Lpeak`

, and`Lmax`

are returned as column vectors or matrices the same size as`audioIn`

.`'1 octave'`

,`'2/3 octave'`

, or`'1/3 octave'`

––`Lt`

,`Leq`

,`Lpeak`

, and`Lmax`

are returned as*L*-by-*B*-by-*C*arrays.*L*–– Number of rows in`audioIn`

*B*–– Number of octave bands*C*–– Number of columns in`audioIn`

**Data Types: **`single`

| `double`

`Leq`

— Equivalent-continuous sound level (dB)column vector | matrix | 3-D array

Equivalent-continuous sound level in dB, returned as a column vector, matrix, or
3-D array the same type as `audioIn`

.

Size and interpretation of the outputs depend on what the Bandwidth property is set to:

`'Full band'`

(default) ––`Lt`

,`Leq`

,`Lpeak`

, and`Lmax`

are returned as column vectors or matrices the same size as`audioIn`

.`'1 octave'`

,`'2/3 octave'`

, or`'1/3 octave'`

––`Lt`

,`Leq`

,`Lpeak`

, and`Lmax`

are returned as*L*-by-*B*-by-*C*arrays.*L*–– Number of rows in`audioIn`

*B*–– Number of octave bands*C*–– Number of columns in`audioIn`

**Data Types: **`single`

| `double`

`Lpeak`

— Peak sound level (dB)column vector | matrix | 3-D array

Peak sound level in dB, returned as a column vector, matrix, or 3-D array the same
type as `audioIn`

.

Size and interpretation of the outputs depend on what the Bandwidth property is set to:

`'Full band'`

(default) ––`Lt`

,`Leq`

,`Lpeak`

, and`Lmax`

are returned as column vectors or matrices the same size as`audioIn`

.`'1 octave'`

,`'2/3 octave'`

, or`'1/3 octave'`

––`Lt`

,`Leq`

,`Lpeak`

, and`Lmax`

are returned as*L*-by-*B*-by-*C*arrays.*L*–– Number of rows in`audioIn`

*B*–– Number of octave bands*C*–– Number of columns in`audioIn`

**Data Types: **`single`

| `double`

`Lmax`

— Maximum time-weighted sound level (dB)column vector | matrix | 3-D array

Maximum time-weighted sound level in dB, returned as a column vector, matrix, or
3-D array the same type as `audioIn`

.

Size and interpretation of the outputs depend on what the Bandwidth property is set to:

`'Full band'`

(default) ––`Lt`

,`Leq`

,`Lpeak`

, and`Lmax`

are returned as column vectors or matrices the same size as`audioIn`

.`'1 octave'`

,`'2/3 octave'`

, or`'1/3 octave'`

––`Lt`

,`Leq`

,`Lpeak`

, and`Lmax`

are returned as*L*-by-*B*-by-*C*arrays.*L*–– Number of rows in`audioIn`

*B*–– Number of octave bands*C*–– Number of columns in`audioIn`

**Data Types: **`single`

| `double`

To use an object function, specify the
System
object as the first input argument. For
example, to release system resources of a System
object named `obj`

, use
this syntax:

release(obj)

`calibrate` | Calibrate meter using calibration tone with known level |

`getCenterFrequencies` | Center frequencies of filters |

Use the `splMeter`

System object™ to measure the A-weighted sound pressure level of a streaming audio signal. Specify a two second time-interval for reporting and a fast time-weighting. Visualize the SPL measurements using the `dsp.TimeScope`

System object.

Create a `dsp.AudioFileReader`

object to read in an audio file frame by frame. Create an `audioDeviceWriter`

object to listen to the audio signal. Create a `dsp.TimeScope`

object to visualize SPL measurements. Create an `splMeter`

to measure the sound pressure level of the audio file. Use the default calibration factor of 1.

source = dsp.AudioFileReader('Ambiance-16-44p1-mono-12secs.wav'); fs = source.SampleRate; player = audioDeviceWriter('SampleRate',fs); scope = dsp.TimeScope('SampleRate',fs, ... 'TimeSpanOverrunAction','Scroll', ... 'TimeSpan',3,'ShowGrid',true, ... 'YLimits',[20 110],'AxesScaling','Auto', ... 'ShowLegend',true,'BufferLength',4*3*fs, ... 'ChannelNames', ... {'Lt_AF','Leq_A','Lpeak_A','Lmax_AF'}, ... 'Name','Sound Pressure Level Meter'); SPL = splMeter('TimeWeighting','Fast', ... 'FrequencyWeighting','A-weighting', ... 'SampleRate',fs, ... 'TimeInterval',2);

In an audio stream loop:

Read in the audio signal frame.

Play the audio signal to your output device.

Call the SPL meter to return the time-weighted, equivalent-continuous, peak, and maximum time-weighted sound levels in dB.

Display the sound levels using the scope.

As a best practice, release your objects once complete.

while ~isDone(source) x = source(); player(x); [Lt,Leq,Lpeak,Lmax] = SPL(x); scope([Lt,Leq,Lpeak,Lmax]) end release(source) release(player) release(SPL) release(scope)

The `splMeter`

enables you to monitor sound pressure level for octave and fractional-octave bands. In this example, you monitor the equivalent-continuous sound pressure level of 1/3-octave bands.

Create a `dsp.AudioFileReader`

object to read in an audio file frame by frame. Create an `audioDeviceWriter`

object so you can listen to the audio signal. Create an `splMeter`

to measure the octave sound pressure level of the audio file. Use the default calibration factor of 1. Create a `dsp.ArrayPlot`

object to visualize the equivalent-continuous SPL for each octave band.

source = dsp.AudioFileReader('JetAirplane-16-11p025-mono-16secs.wav'); fs = source.SampleRate; player = audioDeviceWriter('SampleRate',fs); SPL = splMeter( ... 'Bandwidth','1/3 octave', ... 'SampleRate',fs); centerFrequencies = getCenterFrequencies(SPL); scope = dsp.ArrayPlot(... 'XDataMode','Custom', ... 'CustomXData',centerFrequencies, ... 'XLabel','Octave Band Center Frequencies (Hz)', ... 'YLabel','Equivalent-Continuous Sound Level (dB)', ... 'YLimits',[20 90], ... 'ShowGrid',true, ... 'Name','Sound Pressure Level Meter');

In an audio stream loop:

Read in the audio signal frame.

Play the audio signal to your output device.

Call the SPL meter to return the equivalent-continuous sound pressure level in dB.

Display the sound levels using the scope. Update the scope only when the equivalent-continuous sound pressure level has changed.

As a best practice, release your objects once complete.

LeqPrevious = zeros(size(centerFrequencies)); while ~isDone(source) x = source(); player(x); [~,Leq] = SPL(x); for i = 1:size(Leq,1) if LeqPrevious ~= Leq(i,:) scope(Leq(i,:)') LeqPrevious = Leq(i,:); end end end release(source) release(player) release(SPL) release(scope)

Sound pressure level calculations follow the algorithms described in [1]. You can specify property values to conform to standards [2] and [3].

To account for environmental and input device effects in SPL measurements, the audio input is multiplied by a calibration factor:

$$x=\text{audioIn}\times \text{CalibrationFactor}$$

The `CalibrationFactor`

property can be set directly, or by using the
`calibrate`

function, which compares a known level with acquired data. The known level is determined
using a physical calibrator.

A-, C-, or Z-frequency weighting is applied. The frequency weighting is implemented
using the `weightingFilter`

System
object.

If you specify the `Bandwidth`

property as ```
'1
octave'
```

, `'2/3 octave'`

or `'1/3 octave'`

,
then the SPL calculations are applied to each octave or fractional-octave band. These
analysis bands are determined after frequency weighting.

Time-weighted sound level is defined as the ratio of the time-weighted root mean squared sound pressure to the reference sound pressure, converted to dB. That is,

$$\begin{array}{l}Lt=10{\mathrm{log}}_{10}\left\{\frac{\left(\raisebox{1ex}{$1$}\!\left/ \!\raisebox{-1ex}{$\tau $}\right.\right){\displaystyle {\int}_{{t}_{s}}^{t}y{(\xi )}^{2}{e}^{-\left(t-\xi \right)/\tau}d\xi}}{{p}_{o}^{2}}\right\}\\ \text{\hspace{0.17em}}\text{\hspace{0.17em}}\text{\hspace{0.17em}}=10{\mathrm{log}}_{10}\left\{\frac{h({y}^{2})}{{p}_{o}^{2}}\right\}\end{array}$$

*h*(*y*^{2}) can be
interpreted as the convolution of *y*^{2} with a
filter with impulse response $$\left(\raisebox{1ex}{$1$}\!\left/ \!\raisebox{-1ex}{$\tau $}\right.\right){e}^{-\raisebox{1ex}{$t$}\!\left/ \!\raisebox{-1ex}{$\tau $}\right.}$$. *y* is the output of the frequency-weighting filter. The
impulse response corresponds to a lowpass filter of the form $$H\left(s\right)=\frac{\raisebox{1ex}{$1$}\!\left/ \!\raisebox{-1ex}{$\tau $}\right.}{s+\raisebox{1ex}{$1$}\!\left/ \!\raisebox{-1ex}{$\tau $}\right.}$$. Using impulse invariance, the discrete filter can be interpreted
as,

$$H\left(z\right)=\frac{\raisebox{1ex}{$1$}\!\left/ \!\raisebox{-1ex}{$\left(\tau \times fs\right)$}\right.}{1-{e}^{-\raisebox{1ex}{$1$}\!\left/ \!\raisebox{-1ex}{$\left(\tau \times fs\right)$}\right.}{z}^{-1}}\text{\hspace{0.17em}}.$$

*τ*is specified by the time-weighting coefficient as 0.125 (if`TimeWeighting`

is set to`'Fast'`

) or 1 (if`TimeWeighting`

is set to`'Slow'`

).*fs*is the sample rate specified by the`SampleRate`

property.

Equivalent-continuous sound level is also called time-average sound level. It is defined as the ratio of root mean squared sound pressure to the reference sound pressure, converted to dB. That is,

$$\begin{array}{l}Leq=10{\mathrm{log}}_{10}\left\{\frac{\left(\raisebox{1ex}{$1$}\!\left/ \!\raisebox{-1ex}{$T$}\right.\right){\displaystyle {\int}_{{t}_{1}}^{{t}_{2}}{y}^{2}dt}}{{p}_{o}^{2}}\right\}\\ \text{\hspace{1em}}=20{\mathrm{log}}_{10}\left(\text{rms}\left(y\right)/{p}_{\text{o}}\right)\end{array}$$

where

*y*is the output of the frequency-weighting filter.*p*_{o}is the reference sound pressure, specified by the`PressureReference`

property.

Peak sound level is defined as the ratio of peak sound pressure to the reference sound pressure, converted to dB. That is,

$$Lpeak=20{\mathrm{log}}_{10}\left(\mathrm{max}\left(\left|y\right|\right)/{p}_{o}\right)$$

where

*y*is the output of the frequency-weighting filter.*p*_{o}is the reference sound pressure, specified by the`PressureReference`

property.

Maximum time-weighted sound level is defined as the greatest time-weighted sound level within a stated time interval.

[1] Harris, Cyril M. *Handbook of
Acoustical Measurements and Noise Control*. 3rd ed. American Institute of
Physics, 1998.

[2] International Electrotechnical Commission. Electroacoustics - Sound level meters - Part 1: Specifications. IEC 61672-1:2013.

[3] American National Standards Institute. ANSI S1.4: Specification for Sound Level Meters. 1983.

Generate C and C++ code using MATLAB® Coder™.

Usage notes and limitations:

System Objects in MATLAB Code Generation (MATLAB Coder)

A modified version of this example exists on your system. Do you want to open this version instead?

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

Select web siteYou can also select a web site from the following list:

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

- América Latina (Español)
- Canada (English)
- United States (English)

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)