Creating Audio Files with MATLAB

Let us create some audio files with MATLAB, which we then integrate into multimedia publications. In principle, any audio material can be used, such as recorded nature sounds. Here is a preview of a section of a new chapter about multimedia publications of the upcoming 2nd Edition of MDRES.

MATLAB contains many sound examples, such as a the sound of a Chinese gong, contained in the file gong.mat, and the Halleluja Chorus from Handel’s Messiah, contained in the file handel.mat, that you can use as examples. However, we are creating very simple files with MATLAB, such as sine waves with noise exported as sound files and animated graphics exported as video files. First, we create a time axis t running from 1 to 30,000 in steps of one unit. We then generate a simple periodic signal x1: a sine wave with a period of 100 and an amplitude of 1 by typing

clear, clc
t = 0 : 30000;
x1 = sin(2*pi*t/100);

We can then convert the signal to sound using sound

sound(x1,44100)

where 44100 corresponds to the sample rate of 44,100 Hz, where Hz is cycles per second. Using this sampling rate, each of the data points in t corresponds to 1/44,100 seconds. The period of 100 therefore produces a tone with a frequency of 44,100 Hz / 100 = 441 Hz. Natural data series, however, are more complex than a simple periodic signal. The slightly more complicated signal can be generated by superimposing several periodic components with different periods. As an example we compute such the signal x2 by adding three sine waves with the periods 300, 100 and 45 by typing

x2 = 0.2*sin(2*pi*t/300) + ...
     0.1*sin(2*pi*t/100) + ...
     0.3*sin(2*pi*t/45);

The corresponding amplitudes are 0.2, 0.1 and 0.3. Again, we can calculate the frequencies of the tone produced by

sound(x2,44100)

by dividing the sampling frequency of 44,100 Hz by the periods of the sine waves (300, 100 and 45), which yields 147 Hz, 441 Hz and 980 Hz. In contrast to our synthetic time series, real data also contain various disturbances, such as random noise. In order to reproduce the effects of noise, a random-number generator can be used to compute Gaussian noise with mean of zero and standard deviation of one. The seed of the algorithm should be set to zero using rng(0), before using randn to generate the noise. We add this noise to the original data, i.e., we generate a signal containing additive noise, by typing

x3 = x2 + 0.1*randn(size(x2));

and again convert the signal to sound:

sound(x3,44100)

We can create a Waveform Audio File Format (WAVE) file from the three signals using

audiowrite('aaudio_1.wav',x1,44100)
audiowrite('aaudio_2.wav',x2,44100)
audiowrite('aaudio_3.wav',x3,44100)

where 44100 again corresponds to the sample rate. According to Wikipedia, WAVE (or WAV) is a Microsoft and IBM audio file format standard for storing an audio bitstream on PCs, mostly used on Windows systems for raw and typically uncompressed audio. The WAVs are compatible with other operating systems, such as macOS and Linux, and be easily converted to other file formats. The function audiowrite also supports other file formats, such as the MPEG-4 format. MPEG stands for the ISO/IEC Moving Picture Experts Group, i.e. the developers group’s name is used for the name of the format, similar to JPEG (Joint Photographic Experts Group, see Chapter 8.3). MPEG-4, which replaced the previous MPEG-1 and MPEG-2 standards, is a method to compress audio and video data to be used in many different applications. For audio files, MPEG-4 includes the Advanced Audio Coding (AAC) standard for lossy digital audio compression, typically achieving a better sound quality as the older MP3 standard contained in MPEG-1 and MPEG-2.

There are a number of software solutions for editing audio files. We typically use such a software simply to cut audio files, that is, shorten their length at one or both ends. For this purpose, there are software tools which are supplied with the operating systems, e.g. Apple QuickTime Player, for computers running macOS, or are available for free download, e.g. VLC Media Player, available for macOS, Windows and Linux. An advanced and very popular software for macOS, Windows and Linux for editing audio files is the free Audacity. The commercial alternative to Audacity is Adobe Audition CC, included in the Adobe Creative Cloud software suite and first released in 2003. Here is a movie showing the audio examples included in a PowerPoint presentation: