audioresample

Sample-rate conversion of audio signals

Since R2023b

Syntax

``audioOut = audioresample(audioIn,InputRate=inputFs,OutputRate=outputFs)``
``audioOut = audioresample(audioIn,InputRate=inputFs,OutputRate=outputFs,Quality=q)``
``audioOut = audioresample(audioIn,ResamplerSource="custom",Resampler=customResampler)``
``[audioOut,resampler] = audioresample(___)``

Description

example

````audioOut = audioresample(audioIn,InputRate=inputFs,OutputRate=outputFs)` filters and resamples the audio signal to the desired sample rate.```
````audioOut = audioresample(audioIn,InputRate=inputFs,OutputRate=outputFs,Quality=q)` specifies the resampling quality.```

example

````audioOut = audioresample(audioIn,ResamplerSource="custom",Resampler=customResampler)` resamples the audio signal with the provided custom resampler.```

example

````[audioOut,resampler] = audioresample(___)` also returns the resampler used by the function.```

Examples

collapse all

Read in an audio signal containing speech. See the sample rate is 22,050 Hz.

```[x,fs] = audioread("speech_dft.wav"); fs```
```fs = 22050 ```

Listen to the audio.

`sound(x,fs)`

Use `audioresample` to resample the audio to have a sample rate of 8000 Hz.

```outputFs = 8000; y = audioresample(x,InputRate=fs,OutputRate=outputFs);```

Listen to the resampled audio.

`sound(y,outputFs)`

Read in an audio signal sampled at 96 kHz and convert it to single precision.

```[x,fs] = audioread("RandomOscThree-24-96-stereo-13secs.aif"); x = single(x);```

Use `designAudioResampler` to design a multistage resampler for single-precision signals. The resampler converts audio from 96 kHz to 44.1 kHz.

```outputFs = 44100; resampler = designAudioResampler(InputRate=fs,OutputRate=outputFs, ... DesignMethod="multistage",DataType="single");```

Resample the audio with the custom resampler.

`y = audioresample(x,ResamplerSource="custom",Resampler=resampler);`

Create a list of audio files to resample. Each of the files has a sample rate of 44.1 kHz.

```audioFiles = ["Ambiance-16-44p1-mono-12secs.wav", ... "Counting-16-44p1-mono-15secs.wav", ... "TrainWhistle-16-44p1-mono-9secs.wav"];```

Design a resampler to resample the audio signals to 48 kHz. This saves computation by not redesigning the resampler for each signal.

```inputFs = 44100; outputFs = 48000; resampler = designAudioResampler(InputRate=inputFs,OutputRate=outputFs);```

In a loop, read in the audio file, resample the signal, and store it in a cell array.

```resampledSignals = cell(size(audioFiles)); for ii = 1:numel(audioFiles) [x,fs] = audioread(audioFiles(ii)); resampledSignals{ii} = audioresample(x,ResamplerSource="custom",Resampler=resampler); reset(resampler) end```

Read in an audio signal with a 96 kHz sample rate.

`[x,fs] = audioread("RandomOscThree-24-96-stereo-13secs.aif");`

Use `audioresample` to resample the audio to 44.1 kHz. Specify an additional output argument to get the resampler object that `audioresample` used.

```[y,resampler] = audioresample(x,InputRate=fs,OutputRate=44100); resampler```
```resampler = dsp.FIRRateConverter with properties: Main InterpolationFactor: 147 DecimationFactor: 320 NumeratorSource: 'Property' Numerator: [4.2508e-08 4.0303e-08 3.8039e-08 3.5716e-08 3.3333e-08 3.0891e-08 2.8388e-08 2.5825e-08 2.3202e-08 2.0517e-08 1.7772e-08 1.4965e-08 1.2096e-08 9.1655e-09 6.1727e-09 3.1176e-09 0 -3.1804e-09 -6.4237e-09 ... ] (1x14113 double) Use get to show all properties ```

Design a resampler to resample audio from 44.1 kHz to 16 kHz.

```inputFs = 44100; outputFs = 16000; resampler = designAudioResampler(InputRate=inputFs,OutputRate=outputFs);```

Create `dsp.AudioFileReader` and `audioDeviceWriter` objects to read in the audio and write the resampled signal to your audio device to listen to it.

```samplesPerFrame = 1024; reader = dsp.AudioFileReader( ... Filename="Counting-16-44p1-mono-15secs.wav", ... SamplesPerFrame=samplesPerFrame); deviceWriter = audioDeviceWriter(SampleRate=outputFs,SupportVariableSizeInput=true);```

In a streaming loop, read in an audio frame, resample the signal, and play the resampled signal on your device.

```while ~isDone(reader) audioIn = reader(); audioOut = audioresample(audioIn,ResamplerSource="custom",Resampler=resampler); deviceWriter(audioOut); end```

Input Arguments

collapse all

Audio input, specified as a column vector or matrix. If the input is a matrix, `audioresample` treats the columns as independent channels.

Data Types: `single` | `double`

Sample rate of the input signal in Hz, specified as one of the following values.

• `8000`

• `16000`

• `32000`

• `11025`

• `22050`

• `44100`

• `88200`

• `176400`

• `352800`

• `705600`

• `12000`

• `24000`

• `48000`

• `96000`

• `192000`

• `384000`

• `768000`

The `audioresample` function does not use this value if you specify `customResampler`.

Data Types: `single` | `double` | `int8` | `int16` | `int32` | `int64` | `uint8` | `uint16` | `uint32` | `uint64`

Sample rate of the output signal in Hz, specified as one of the following values.

• `8000`

• `16000`

• `32000`

• `11025`

• `22050`

• `44100`

• `88200`

• `176400`

• `352800`

• `705600`

• `12000`

• `24000`

• `48000`

• `96000`

• `192000`

• `384000`

• `768000`

The `audioresample` function does not use this value if you specify `customResampler`.

Data Types: `single` | `double` | `int8` | `int16` | `int32` | `int64` | `uint8` | `uint16` | `uint32` | `uint64`

Resampling quality, specified as `"medium"`, `"high"`, or `"ultra"`. Higher quality resampling requires more computation.

• `"medium"``audioresample` attenuates spectral aliases and spectral images by 96 dB and preserves about 90% of the bandwidth of interest.

• `"high"``audioresample` attenuates spectral aliases and spectral images by 120 dB and preserves about 94% of the bandwidth of interest.

• `"ultra"``audioresample` attenuates spectral aliases and spectral images by 144 dB and preserves about 98% of the bandwidth of interest.

The `audioresample` function does not use this value if you specify `customResampler`.

Data Types: `char` | `string`

Output Arguments

collapse all

Resampled audio signal, returned as a column vector or matrix with the same number of channels as the input `audioIn`. The length of the input signal and the output sample rate determine the length of the output signal.

Version History

Introduced in R2023b