Main Content

sonify

Convert numeric data to sound

Since R2024b

    Description

    sonify(yData) converts the numeric data specified in yData into sound and provides immediate playback capabilities.

    example

    sonify(xData,yData) also uses xData, which corresponds to time, sequence, or spatial positioning in yData, to convert the numeric data.

    example

    sonify(___,Name=Value) specifies additional options using one or more name-value arguments.

    example

    [sonifiedData,Fs] = sonify(___) returns the sonified audio samples sonifiedData and the sample rate Fs.

    Examples

    collapse all

    Load the co2emission.mat file. This file contains the data set for carbon dioxide emission data from 1949 to 2022. Download the file attached with this example.

    load co2emission.mat

    Sonify the carbon dioxide emission data. The sound plays once you run this command.

    sonify(co2);

    Load the co2emission.mat file. This file contains the data set for carbon dioxide emission data from 1949 to 2022. Download the file attached with this example.

    load co2emission.mat

    Sonify carbon dioxide data in the frequency range of 1000 to 3000 Hz. Play the generated sound for 5 seconds and return the sonified data and the sampling frequency.

    [sonifiedData,Fs] = sonify(co2,'FrequencyRange',[1000,3000],'Playback',true,'Duration',5);

    Write the sonified data and the sampling rate to an audio file by using the audiowrite function. The function saves the file in the current folder.

    audiowrite("sonifiedOutput.wav",sonifiedData,Fs);

    Load the sample data set population.mat. The data set contains nonuniform population data for years 1949 to 2024 where the data for some of the years is missing. Download the file attached with this example.

    load population.mat

    Sonify the year and population values. The sound plays once you run this command.

    sonify(year,population);

    Load the sample data set population.mat. The data set contains nonuniform population data for years 1949 to 2024 where the data for some of the years is missing. Download the file attached with this example

    load population.mat

    Sonify the year and population values in the frequency range of 1000 Hz and 2000 Hz. Disable automatic playback and set the duration of the generated sound to 10 seconds. Return the sonified data and the sampling frequency.

    [sonifiedData,Fs] = sonify(year,population,'FrequencyRange',[1000,2000],'Playback',false,'Duration',10);

    Create an audioplayer object with the sonified data and the sampling frequency.

    playerObj = audioplayer(sonifiedData,Fs);

    Play audio from the audioplayer object for 2 seconds.

    play(playerObj);
    pause(2);

    Pause playback of the audioplayer object for 2 seconds.

    pause(playerObj);
    pause(2);

    Resume playback of the audioplayer object and play for 2 seconds.

    resume(playerObj);
    pause(2);

    Stop playback of the audioplayer object.

    stop(playerObj);

    Input Arguments

    collapse all

    Data for sonification, specified as a scalar or a vector. You can sonify the data based on parameters such as FrequencyRange, Playback, and Duration.

    Example: sonify(yData)

    Data Types: double

    Data corresponding to time, sequence, or spatial positioning of yData, specified as a scalar or a vector. The values in xData must be real and monotonically increasing and they can increase uniformly or nonuniformly.

    Example: sonify(xData, yData);

    Data Types: double

    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.

    Example: sonify(xData, yData,'FrequencyRange', [600,1800])

    Frequency range to which you want to map your input data, specified as a two-element vector. You can set the frequency range between 16 Hz to 4095 Hz.

    Example: sonify(xData, yData,'FrequencyRange', [600,1800])

    Data Types: double

    Stop or play sonified data, specified as a numeric or logical 1 (true) or 0 (false).

    Example: sonify(xData, yData,'Playback', true)

    Data Types: logical

    Duration of playback in seconds, specified as a positive integer.

    Example: sonify(xData, yData,'Duration', 10)

    Data Types: double

    Output Arguments

    collapse all

    Sonified audio samples, returned as a numeric vector.

    Sample rate, returned as 8192 Hertz (Hz).

    Note

    You can use the sonifiedData and Fs outputs with the audioplayer object to play, pause, resume, and stop the sound. You can write the sonified data to a .wav file by using the audiowrite function.

    Version History

    Introduced in R2024b