# calibrateMicrophone

Calibration factor for microphone

## Syntax

``calibrationFactor = calibrateMicrophone(micRecording,fs,SPLreading)``
``calibrationFactor = calibrateMicrophone(micRecording,fs,SPLreading,Name,Value)``

## Description

example

````calibrationFactor = calibrateMicrophone(micRecording,fs,SPLreading)` returns the calibration factor for the microphone used to create `micRecording`.```
````calibrationFactor = calibrateMicrophone(micRecording,fs,SPLreading,Name,Value)` specifies options using one or more `Name,Value` pair arguments.Example: ```calibrationFactor = calibrateMicrophone(micRecording,fs,SPLreading,'FrequencyWeighting','Z-weighting')``` returns the calibration factor for an SPL reading that applies Z-weighting.```

## Examples

collapse all

This diagram depicts the setup used in the example:

To run this example, you must connect a microphone and loudspeaker to a full-duplex sound card, and use an SPL meter to determine the true loudness level.

Create an `audioOscillator` object to generate a 1 kHz sine wave at a sample rate of 48 kHz.

```fs = 48e3; osc = audioOscillator("sine",1e3,"SampleRate",fs);```

Create an `audioPlayerRecorder` object to write the sine wave to your loudspeaker and simultaneously read from your microphone.

`playRec = audioPlayerRecorder(fs);`

Create a `dsp.AsyncBuffer` object to store the audio recorded from your microphone. Specify the capacity of the buffer to hold 3 seconds worth of data.

```dur = 3; buff = dsp.AsyncBuffer(dur*fs);```

In a loop, for three seconds:

• Generate a frame of a 1 kHz sinusoid.

• Write the frame to your loudspeaker and simultaneously read a frame from your microphone.

• Write the frame acquired from your microphone to the buffer.

While the loop runs, note the true SPL measurement as reported from your SPL meter. Once complete, read the contents of the buffer object.

```numFrames = dur*(fs/osc.SamplesPerFrame); for ii = numFrames audioOut = osc(); audioIn = playRec(audioOut); write(buff,audioIn); end SPL = 78.2; % read from physical SPL meter micRecording = read(buff);```

Compute the calibration factor for the microphone.

`calibrationFactor = calibrateMicrophone(micRecording,playRec.SampleRate,SPL);`

The diagram depicts the example setup and data flow.

To run this example, you must connect a microphone to your audio card, generate a 1 kHz tone using an external device, and use an SPL meter to determine the true loudness level.

Specify a 48 kHz sample rate for your audio device and a 3-second duration for acquiring audio. Create an `audioDeviceReader` object to read from your audio device.

```fs = 48e3; dur = 3; deviceReader = audioDeviceReader(fs);```

Create a `dsp.AsyncBuffer` object to store the streamed audio.

`buff = dsp.AsyncBuffer(dur*fs);`

Start the 1 kHz test tone using an external loudspeaker. Then, in a loop, read from your audio device and then write the data to the buffer. While the loop runs, note the true SPL measurement as reported from your SPL meter. Once complete, read the contents of the buffer object.

```tic while toc < dur audioIn = deviceReader(); write(buff,audioIn); end SPL = 77.7; % read from physical SPL meter micRecording = read(buff);```

Compute the calibration factor for the microphone.

`calibrationFactor = calibrateMicrophone(micRecording,deviceReader.SampleRate,SPL);`

## Input Arguments

collapse all

Audio signal used to calibrate microphone, specified as a column vector (mono) or matrix of independent channels (stereo). `micRecording` must be acquired from the microphone you want to calibrate. The recording should consist of a 1 kHz test tone.

Data Types: `single` | `double`

Sample rate of microphone recording in Hz, specified as a positive scalar. The recommended sample rate for new recordings is 48 kHz.

Data Types: `single` | `double`

Sound pressure level reported from meter in dB, specified as a scalar or vector. If `SPLreading` is specified as a vector, it must have the same number of elements as columns in `micRecording`.

Data Types: `single` | `double`

### Name-Value Arguments

Specify optional comma-separated pairs of `Name,Value` arguments. `Name` is the argument name and `Value` is the corresponding value. `Name` must appear inside quotes. You can specify several name and value pair arguments in any order as `Name1,Value1,...,NameN,ValueN`.

Example: `calibrateMicrophone(micRecording,fs,SPLReading,'PressureReference',22)`

Reference pressure for dB calculation in pascals, specified as a positive scalar. The default reference pressure (20 micropascals) is the common value for air.

Data Types: `single` | `double`

Frequency weighting used by physical meter, specified as `'A-weighting'`, `'C-weighting'`, or `'Z-weighting'`.

Data Types: `char` | `string`

## Output Arguments

collapse all

Microphone calibration factor, returned as a scalar or row vector with the same number of elements as `SPLreading`.

Data Types: `single` | `double`

## Algorithms

To determine the calibration factor for a microphone, the `calibrateMicrophone` function uses:

• A calibration tone recorded from the microphone you want to calibrate.

• The sample rate used by your sound card for AD conversion.

• The known loudness, usually determined using a physical SPL meter.

• The frequency weighting used by your physical SPL meter.

• The atmospheric pressure at the recording location.

The diagram indicates a typical physical setup and the locations of required information.

The `calibrationFactor` is set according to the equation:

`$\text{CalibrationFactor}=\frac{{10}^{\left(\left(\text{SPLreading}-k\right)/20\right)}}{\mathrm{rms}\left(\text{x}\right)}$`

where `x` is the microphone recording passed through the weighting filter specified in the `FrequencyWeighting` argument. k is 1 pascal relative to the `PressureReference` calculated in dB:

`$k=20{\mathrm{log}}_{10}\left(\frac{1}{\text{PressureReference}}\right).$`