Sure, here we go:
Playing a .wav file in MATLAB is a relatively simple task that requires a basic understanding of the software and some fundamental concepts in audio processing. MATLAB, or Matrix Laboratory, is a powerful computational program that allows users to analyze and visualize data in ways that can be critical to the success of their research or project. Specifically, for .wav files, MATLAB provides a built-in function called ‘audioread’. This function reads a wave file and returns the audio data. However, to play this audio we would need another function which is ‘sound’.
[data, fs] = audioread('sample.wav'); // Reads the wave file sound(data, fs); // Plays the sound
The above code reads a file named ‘sample.wav’ and then plays the sound file.
audioread essentially takes the string path of the audio file as an argument and returns two values: ‘data’ and ‘fs’. ‘data’ is the actual audio data and ‘fs’ denotes the frequency at which the audio plays. sound function then uses these two values to play the audio.
Understanding the ‘audioread’ Function
audioread is a MATLAB function that is designed to read audio data directly into the workspace from an audio file. It supports a large variety of file formats including .wav, .ogg, .flac, .au, .mp3, .m4a, etc. It basically extracts raw audio data from these files, which you can then manipulate or analyze using a wide array of MATLAB’s audio processing tools.
- The first parameter for ‘audioread’ is the filename (string format) of the audio file to read. Your filename should include the file extension.
- The function returns two output parameters: ‘y’ and ‘Fs’. ‘y’ is an array holding the audio data, and ‘Fs’ is the sampling frequency of that audio data in Hz.
Working with the ‘sound’ Function
With the raw data and the sampling frequency obtained by the audioread function, we can now move on to the second element of our audio script in MATLAB – the sound function. The function allows us to play the sound represented by a given data sequence at a specified sample rate.
- The ‘sound’ function accepts two parameters – ‘y’ and ‘Fs’. ‘y’ is the audio signal, and ‘Fs’ is the sample rate.
- It uses your system’s default audio output device to play the sound.
In brief, if you want to play a wav file in MATLAB, these two built-in functions allow you to easily accomplish the task. Not only do they simplify the process of reading and playing audio data, but they also provide an introduction to audio processing concepts in a practical and user-friendly way.