Functions > Reading and Writing Files > Example: Working with WAV Files and Creating Spectrograms
  
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.
Click to copy this expression
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.
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
Reading in a Sound File
1. Use the READWAV function to read the sound file and save it into a vector.
Click to copy this expression
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.
Click to copy this expression
Click to copy this expression
3. Plot the signal.
Click to copy this expression
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.
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
5. Plot the first 25000 samples and use markers to show the sample with the maximum magnitude.
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
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.
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
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.
Click to copy this expression
Click to copy this expression
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.
Click to copy this expression
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:
Click to copy this expression
Click to copy this expression
Each combination of slice size and overlap factor results in different slice matrix dimensions:
Click to copy this expression
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.
Click to copy this expression
Click to copy this expression
5. Set ti to the one of the middle elements of TI and observe the contour plot at the bottom of the worksheet.
Click to copy this expression
Click to copy this expression
Click to copy this expression
The new slice matrix dimensions are now:
Click to copy this expression
6. Window each slice with a Hamming window.
Click to copy this expression
Click to copy this expression
Click to copy this expression
7. Use the log function to represent the frequencies in decibels.
Click to copy this expression
8. Use the dft (or the deprecated fft) function to find the Fourier transform of the slice.
Click to copy this expression
To use the deprecated fft function, disable the above region and enable the below region.
Click to copy this expression
9. Use a contour plot to plot spectra.
Click to copy this expression
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:
Click to copy this expression