DSP Notes - 6
Sound I/O and Spectrums
Sound Files on WWW
The sound files located under http://www.ee.calpoly.edu/~fdepiero/curr/ee419/ee419hdr.html were all digitized at a sampling frequency of 22255 Hz. These have a reasonably low noise content and should yield very readable spectrums. Each file is approximately 1000 samples in length. File formats are simple Ascii. These files are readable by both MatLab and DaDisp.
Sound File I/O and Spectral Analysis in DaDisp
To read in an Ascii sound file, use:
w1: reada("C:\dsp4se\b1.dat")
with an appropriate directory and file name. This is equivalent to the menu action:
Data -> Read Ascii -> Series
The proper sample spacing must then be setup in DaDisp. Select window w1, then choose the menu
View -> Data Settings
This will bring up a dialog box. The ‘Horizontal Units’ should have defaulted to ‘Sec’. Set the ‘X Delta’ to the proper sample period. This may be entered as ‘1/22255’ for example.
The magnitude of the spectrum of this signal may then be displayed using
w2: fs(fftp(w1))
as in previous lab experiments.
Attempts were made to perform signal I/O in DaDisp (Student Edition 4.0) using .wav files. While these signals could be read in (see example in lab manual using ‘readb()’ in uINT mode) there are certain problems with this method. Apparently the .wav header is read in as data, snafu’ing the first part of the signal. In particular, note that, the sampling frequency of the signal, which is stored in the .wav header, is NOT read in properly. Ascii signals are recommended.
Sound File I/O and Spectral Analysis in MatLab
To load one of the www signal files into MatLab, use
EDU» load q1.dat
This will initialize the vector q1 with the data in the file q1.dat. This signal may be displayed using
EDU» plot(q1)

The signal q1 was digitized from a guitar. It is the A below middle C, which is the A in the 2nd fret of the G string. Its true analog frequency is 220 Hz (sample rate 22.255 kHz). To compute and display the spectrum of q1, use
EDU» Qmag = abs(fft(q1));
EDU» plot(Qmag)

This really doesn’t look too good. One problem is that the spectrum above is two sided, but is shown wrapping around into positive frequencies (showing 0 to 2*pi, instead of -pi to pi). Another problem is that the horizontal axis is labeled in samples. The plot can also be improved by zooming.
MatLab doesn’t carry along the sample interval associated with signals (nor the specific frequencies associated with any spectrums) - as does DaDisp. The frequencies associated with the FFT of signal q1, sampled at Fs = 22.255 kHz, can be setup in a vector f using
EDU» Fs = 22255;
EDU» f = Fs * (0:length(q1)-1) / length(q1);
The data vectors may then be cropped, to ‘zoom in’ to frequencies of interest. For example, here the vector of frequencies, f, and vector of frequqncy samples, Qmag, are cropped to include the first 50 elements only:
EDU» plot(f(1:50),Qmag(1:50))
Note the vector length of 1 to 50 above was determined by trial and error - experiment to make sure the zooming operation doesn’t clip important portions of the spectrum.

This spectrum is consistent with the frequency of the original analog signal, 220 Hz (with some additional higher harmonics).
Playing Sound Files from MatLab Data
MatLab may be used to convert the available Ascii files into .wav format. This permits the original sounds to be heard using the ‘Sound Recorder’ application, for example. If an Ascii version of a sound file is loaded into MatLab, then a .wav file may be created using the ‘wavwrite()’ command. (See help menu.) The sampling frequency must be specified to use this function, for example:
EDU» wavwrite(q1,22255,'q1.wav')
The ‘wavread()’ function permits .wav files to be input. See the help menu.
Transferring Data from MatLab to DaDisp
It is sometimes useful to transfer signals or sequences from MatLab to DaDisp. For example if a filter was first designed in MatLab it could be brought into DaDisp for simulation via its impulse response, h(n). IIR filters have (theoretically) infinite duration impulse responses. For simulation purposes h(n) may be truncated and accurate results may still be obtained, but the needed length of h(n) prevents manually typing it into DaDisp, as could be done for a short FIR filter.
Hence it is useful to transfer h(n) from MatLab to DaDisp via a file. Ascii files eliminate any problems with machine-to-machine differences in number representations, albeit, they are more bulky.
In order to create an Ascii file that DaDisp can read, data must be stored as a column vector. Given a row vector h, convert and save using:
EDU>> ht = h’;
EDU>> save ht.dat ht -ascii
This will create the file ht.dat in Ascii format. To read this into DaDisp, go to the Data menu, selecting
Data -> Read Ascii -> Series
Recording Sounds and Pulling into MatLab
New sounds may be recorded using the ‘Sound Record’ application. This is available on the PC that is located against the wall, nearest the sink (#EE 4964) which is outfitted with a SoundBlaster card. The output of the SoundBlaster is the center audio jack (RCA) on the front of the machine. The SoundBlaster input is the far right jack (looking from the rear) on the bottom card.
The Sound Record program is under the Accessories group. It is a good idea to record and play back sounds for verification, prior to saving. The default format for saving is .wav, use this and save in the MatLab directory.
A signal in the file aaa.wav may be brought into MatLab using
EDU>> [x,Fs] = wavread(‘aaa.wav);
This will place the signal in the variable x and initialize the variable Fs with the sampling frequency of the signal, in Hz.
The signal itself may be plotted in MatLab with
EDU>> plot(x)
Note that signals digitized using this procedure have a significant dc offset. This must be taken into account when examining the spectrum in MatLab, because of limited plotting resolution. To eliminate the dc offset and to compute and plot the spectrum use:
EDU>> Hmag = abs( fft( x - mean(x) ) );
EDU>> plot(Hmag)
Note that fft() returns a 2-sided spectrum with discrete frequencies ranging from 0 to 2 pi. Hence the first half of the sequence Hmag contains the positive frequency portion (and is the most intuitive portion of the spectrum to examine).