Main Content

designAudioResampler

Design sample-rate converter for audio signals

Since R2023b

    Description

    resampler = designAudioResampler(InputRate=inputFs,OutputRate=outputFs) designs a resampler object to resample audio signals to the desired sample rate. You can use the object with audioresample.

    example

    resampler = designAudioResampler(___,Name=Value) specifies options using one or more name-value arguments. For example, designAudioResampler(InputRate=32000,OutputRate=16000,DataType="single") designs a resampler that operates on single-precision floating-point signals.

    example

    Examples

    collapse all

    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

    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

    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

    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")

    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

    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

    Data type of the signal to be resampled by the resampler, specified as "double" or "single".

    Data Types: char | string

    Output Arguments

    collapse all

    Resampler object, returned as one of the following.

    Version History

    Introduced in R2023b