designAudioResampler
Syntax
Description
designs a resampler object to resample audio signals to the desired sample rate. You can use
the object with resampler
= designAudioResampler(InputRate=inputFs
,OutputRate=outputFs
)audioresample
.
specifies options using one or more name-value arguments. For example,
resampler
= designAudioResampler(___,Name=Value
)designAudioResampler(InputRate=32000,OutputRate=16000,DataType="single")
designs a resampler that operates on single-precision floating-point signals.
Examples
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
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
inputFs
— Input sample rate (Hz)
8000
| 16000
| 32000
| 11025
| ...
Sample rate in Hz of the signal to resample, specified as one of the following values.
8000
16000
32000
11025
22050
44100
88200
176400
352800
705600
12000
24000
48000
96000
192000
384000
768000
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
outputFs
— Output sample rate (Hz)
8000
| 16000
| 32000
| 11025
| ...
Sample rate in Hz of the resampled signal, specified as one of the following values.
8000
16000
32000
11025
22050
44100
88200
176400
352800
705600
12000
24000
48000
96000
192000
384000
768000
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
Name-Value Arguments
Specify optional pairs of arguments as
Name1=Value1,...,NameN=ValueN
, where Name
is
the argument name and Value
is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.
Before R2021a, use commas to separate each name and value, and enclose
Name
in quotes.
Example: designAudioResampler(InputRate=32000,OutputRate=16000,Quality="ultra")
Quality
— Resampling quality
"high"
(default) | "medium"
| "ultra"
Resampling quality, specified as "medium"
,
"high"
, or "ultra"
. Higher quality resampling
requires more computation.
"medium"
— The resampler attenuates spectral aliases and spectral images by 96 dB and preserves about 90% of the bandwidth of interest."high"
— The resampler attenuates spectral aliases and spectral images by 120 dB and preserves about 94% of the bandwidth of interest."ultra"
— The resampler attenuates spectral aliases and spectral images by 144 dB and preserves about 98% of the bandwidth of interest.
Data Types: char
| string
DesignMethod
— Design method
"auto"
(default) | "singlestage"
| "multistage"
Design method for the audio resampler, specified as
"singlestage"
, "multistage"
, or
"auto"
.
"singlestage"
— Designs the resampler as a one-stage filter."multistage"
— Breaks down the design into multiple stages for efficiency."auto"
— Chooses the design, single stage or multistage, that yields fewer multiplications per input sample.
Data Types: char
| string
DataType
— Data type
"double"
(default) | "single"
Data type of the signal to be resampled by the resampler, specified as
"double"
or "single"
.
Data Types: char
| string
Output Arguments
resampler
— Resampler object
dsp.FIRRateConverter
| dsp.FIRDecimator
| dsp.FIRInterpolator
| dsp.FilterCascade
| ...
Resampler object, returned as one of the following.
dsp.FilterCascade
containing one or more of the previously listed objects
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 (한국어)