MATLAB Audio Functions
audioread
The audioread function can be used to import an audio le into MATLAB. The samples in the audio le are stored in a column vector. Additionally, the sample rate can be read from the audio le and assigned to a scalar variable. The syntax for using the function is the following: [y,Fs] = audioread(filename).
It is necessary for the variable, filename, to be a string with the name of an audio le stored on the computer. Types of supported les for the audioread function are .wav, . ac, .mp3, .ogg, .aiff, and several others. The le extension should be included in the filename.
In Example 3.1, the syntax for the audioread function is demonstrated. The result of the example is an array, sw, containing the samples of the signal and a variable, Fs, with the sampling rate of the le stored on the computer. The variables are printed to the Command Window to view. Throughout the book, sound les referenced in examples are included in the book’s Additional Content (www.hackaudio.com).
Examples: Execute the following commands in MATLAB.
>> fn = 'sw440Hz.wav';
>> [sw,Fs] = audioread(fn)
sound
The sound function can be used to play a MATLAB array of audio samples through the computer’s output device. It is necessary to specify the appropriate sampling rate, Fs, for playback. The syntax for using the function is the following: sound(y,Fs). The commands to import a sound le and play it are demonstrated in Example 3.2.
Examples: Execute the following commands in MATLAB.
>> fn = 'sw440Hz.wav';
>> [sw,Fs] = audioread(fn);
>> sound(sw,Fs);
audiowrite
The audiowrite function can be used to create an audio le from a MATLAB array, which is then stored on the computer. The name of the le is specied by a string, lename. Folder location can also be referenced in the name of the le. The name of the MATLAB array and
the appropriate sampling rate are specied by the next two variables, respectively. The syntax for using the function is the following: audiowrite(filename,y,Fs). Executing the commands in Example 3.3 creates a new sound le saved to the hard drive of the computer.
Examples: Execute the following commands in MATLAB.
>> fnIn = 'sw440Hz.wav';
>> [sw,Fs] = audioread(fnIn);
>> fnOut = 'testSignal.wav';
>> audiowrite(fnOut,sw,Fs);
audioinfo
The audioinfo function can be used to nd out information about the contents of an audio le. This information includes the name of the le, whether the le is mono or stereo, sampling rate, number of bits per sample, as well as other information. The syntax for using the function is the following: info = audioinfo(filename).
Examples: Execute the following commands in MATLAB.
>> fn = 'sw440Hz.wav';
>> info = audioinfo(fn)

No comments:
Post a Comment