Example: Working with WAV Files and Creating Spectrograms
Use the READWAV and GETWAVINFO functions to read and get format information from WAV files.
Getting WAV Information
1. Use GETWAVINFO to get information from a sound file. This sound file is a sample of the Stenella whale vocalization. You can use any music player to play it.
The vector contains the number of channels, the sample rate, the resolution (number of bits per sample), and the average number of bytes per second that an audio player device needs to process in order to play the audio in real time.
2. Evaluate the vector variables.
Reading in a Sound File
1. Use the READWAV function to read the sound file and save it into a vector.
If READWAV returns a matrix, successive columns represent the separate channels of data.
2. Use the length function to calculate the total number of samples.
3. Plot the signal.
4. Use the match, max, and min functions to find the samples with the maximum and minimum magnitudes, and then find the corresponding sample time.
5. Plot the first 25000 samples and use markers to show the sample with the maximum magnitude.
Create a Spectrogram
Analyze sound data by dividing the data into small time slices and viewing the frequency content of each slice. This example uses a slice of 128 samples.
1. Define a vector of slice sizes in powers of 2.
Where ss is the number of rows in the slice matrix.
2. Define a vector of overlap factors between 0-90%, in increments of 10.
The overlap can not be 100%.
3. Set the overlap factor and use the floor and ceil functions to define the size of the matrix.
If overlap is set to zero then the Data vector is divided into a number of ss-long chunks. As the overlap increases, the number of ss-long chunks also increases, as shown by the following equation:
Each combination of slice size and overlap factor results in different slice matrix dimensions:
Using ss=128 and overlap=40% results in a 128x2602 matrix that could take a very long time to build and plot.
4. Create vector TI to facilitate the setting of ti to one of ten values that are equal to or less than the value of ti.
5. Set ti to the one of the middle elements of TI and observe the contour plot at the bottom of the worksheet.
The new slice matrix dimensions are now:
6. Window each slice with a Hamming window.
7. Use the log function to represent the frequencies in decibels.
8. Use the dft (or the deprecated fft) function to find the Fourier transform of the slice.
To use the deprecated fft function, disable the above region and enable the below region.
9. Use a contour plot to plot spectra.
10. Experiment with different values of slice size (ss), overlap, and ti. However, to observe the effect on the calculation and plotting times, you are advised to change one parameter at a time while leaving the other two set to their default values [128 0.4 82].
The current settings of ss, overlap, and ti are shown below: