audioresample
Syntax
Description
Examples
Resample Audio Signal
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)
Resample Audio with Custom Resampler
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);
Resample Multiple Audio Signals in a Loop
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
Get Resampler Used by audioresample
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
Resample Streaming Audio
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
audioIn
— Audio input
column vector | matrix
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
inputFs
— Input sample rate (Hz)
8000
| 16000
| 32000
| 11025
| ...
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
outputFs
— Output sample rate (Hz)
8000
| 16000
| 32000
| 11025
| ...
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
customResampler
— Custom resampler
dsp.FIRRateConverter
| dsp.FIRDecimator
| dsp.FIRInterpolator
| dsp.FilterCascade
| ...
Custom resampler to be used by audioresample
, specified as one
of the following objects.
dsp.FilterCascade
containing one or more of the previously listed objects
You can design the custom resampler using designAudioResampler
.
q
— Resampling quality
"high"
(default) | "medium"
| "ultra"
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
audioOut
— Resampled audio
column vector | matrix
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.
resampler
— Resampler object used
dsp.FIRRateConverter
| dsp.FIRDecimator
| dsp.FIRInterpolator
| dsp.FilterCascade
| ...
Resampler object used by audioresample
, returned as one of the
following.
dsp.FilterCascade
containing one or more of the previously listed objects
The returned object is the same as customResampler
if you
specify it.
Version History
Introduced in R2023b
MATLAB Command
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.
Select a Web Site
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: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)