To input an audio file (guitar.wav) to a MATLAB script file, plot a graph of the file, and listen to the file, we can use the following:
[wave,fs]=wavread('guitar.wav'); x = 1 : length(wave); plot(x, wave); xlabel('Number of Samples'); ylabel('Amplitude'); title('guitar.wav'); sound(wave,fs);
The function wavread is used to actually read or input the audio file. The vector wave contains the actual samples of the .wav file, and fs is the sampling rate (samples per second) that was used to digitize the audio wave.
The sound function is used to listen to audio file.
Create a MATLAB script file that contains the above code and see what happens when you run the code. The plot will be very dense because a lot of samples were used to “record” the guitar.wavfile. By the way, what is the sample rate for this audio file?
Determining The Frequency Of An Audio Wave
The frequency of a sound wave is measure in cycles per second, better known as Hertz or Hz. Remember out discussion of the number of roots versus frequency that we had at the end of Part 1. We should be able to use that information to create a MATLAB function that will determine the frequency of an audio tone, given the wave vector and the sample rate, fs. Let’s define this function as follows:
function [frequency, time] = FrequencyAnalyzer(wave, fs)
We will pass the wave vector and the sample rate, fs, to the function and it will return the frequency and time (duration) of the audio tone. Use your knowledge from Part 1 to create this function and test your function with the audio tone file Tone1.wav. The frequency of this tone should be 440 Hz and the duration should be 2 seconds.
Determining Musical Notes
In addition to Tone1.wav, you have been supplied with two additional “unknowns” tones (Tone2.wav, Tone3.wav). These three tones all represent musical notes in the range from A above middle C (sometimes call A4) to G# below high C. The following table shows the correlations between the notes and their frequencies.
Note Frequency (Hz)
Using the information in this table, create a MATLAB function that will determine the corresponding musical note for a given frequency. The function header should be:
function [note] = MusicalNote(frequency)
You pass in a frequency value and the function returns the corresponding musical note. Due to rounding and truncation errors in the data and the computations; you will not be able to determine each frequency exactly. In some cases the frequency may be off by as much as ± 2.0 Hz. Therefore you will have to devise a look-up strategy that will determine the musical note closest to the frequency value supplied to the function. You will also need to be careful when you set up the matrices containing the “musical notes” and the corresponding frequencies. The strings representing the “musical notes” will all have to be the same length (2 characters). Just a little MATLAB “glitch” to deal will.
Once you have completed your MusicalNote function, create a MATLAB script file that loops through the three “unknown” tones and determines the frequency, duration, and corresponding musical note for the three tones. If you so desire, and the computer you are using has a sound card, please play the audio tones as well. If you are musically inclined and have perfect pitch, you should be able to tell immediately what note each of the tones represents.
What are the frequency, duration, and musical note of the guitar.wav sound file? Create a MATLAB script file to determine this information as well.
Playing “Jingle Bells”
Now for our last bit of fun! It is getting to be that time of year. You will notice that there is a subfolder called JingleBells. This folder contains seven .wav files (JB1.wav, JB2.wav, . . ., JB7.wav) representing the seven musical notes (A, B, C, D, E, F, and G) that you need to play a very simple version of the song Jingle Bells. Well, actually you only need five of these notes, but I have given you seven just to make it interesting. First you will need to determine which .wav file represents which note. Write a MATLAB script file to loop through the seven .wav files and make the note determinations. Then you should listen to the JingleBells.wav file that I have also include and decide which notes would have to go where to create a vector that will play Jingle Bells. The note pattern of the first four lines of Jingle Bells is shown below. Hint: Line 1 and Line 3 are the same.
N N N – N N N – N N N N N – – – N N N N N N N N N N N N N – N – N N N – N N N – N N N N N – – – N N N N N N N N N N N N N
All you have to do is replace each N with the corresponding correct note. I have also included two other files SP.wav and LP.wav which represent 25 ms and 400 ms pauses, respectively.
Create a MATLAB script file to generate and play the song Jingle Bells. All adjacent notes should have a 25 ms pause between them, and the dashes shown above, represent the longer 400 ms pauses. By the way, all notes are also 400 ms in duration. You can create vectors for each line of the song and then put them all together in a composite vector to create the song.
Note: Since the .wav files are actually input as column vectors, you will need to create column vectors for each Line of your song and for your composite Song vector.