MATLAB Excercise on computing Discrete Fourier Transform of an audio wavefile. 
HCI 575, Spring 2010
Author: Jivko Sinapov, jsinapov@cs.iastate.edu

1. Load the sample wave file:

[W fs] = wavread('popcan_drop.wav');

To play the sound:

playObject = audioplayer(W, fs);
play(playObject)

2. Compute Discrete Fourier Transform spectrogram. The 'nfft' variable should be set to a power of 2 (e.g., 64, 128, etc.). 
The resulting spectrogram will contain the dB intensities for (nfft/2) + 1 frequency bins. 

nfft = 128; 
[B,f,t]=specgram(W,nfft,fs);
size(B)

ans =

          65        2669

The first number is the number of freqeuncy bins: (128/2)+1. The second is the number of time samples in the spectrogram. 

3. Compute and plot the log-normalized dB intensities for each frequency bin:

I = 20*log10(abs(flipud(B)));
imagesc(t,f,I);
xlabel('Time (s)');
ylabel('Frequency (Hz)');


The output should look like the sample DFT spectrogram image 'DFT_default.png'. To do the homework, you will need to use the intensity matrix I. 

4. Advanced: Computing spectrograms using different windowing functions.

This part describes how to pass windowing functions as parameters to the spectrogram function. 
First, we try the gausswin windowing function. Enter:

plot(gausswin(100));

You will see a plot of the guassian function, with 100 different samples. To compute the spectrogram using the gausswin
function, enter:

[B,f,t]=specgram(W,nfft,fs,gausswin(100),50);
I = 20*log10(abs(flipud(B)));
imagesc(t,f,I);
xlabel('Time (s)');
ylabel('Frequency (Hz)');


The output should look like the sample DFT spectrogram image 'DFT_gausswin.png'. 
The last parameter (set to 50) dictates by how much the windows will overlap when each new column of the DFT matrix is computed. 
It has to be less than the number of samples in the windowing function. As a rule of thumb, I usually choose half that number. 
Decreasing the number of samples in the windowing function (100 in this case) leads to a spectrogram with higher temporal resolutions. 
In this example, the resulting spectrogram has 3417 column vectors, each specifying the intensity for each of 65 frequency bins. 
If the number of window samples is decreased to 50 (and the overlap parameter decreased to 25), we get a spectrogram with 6836 samples. 

[B,f,t]=specgram(W,128,fs,gausswin(50),25);
I = 20*log10(abs(flipud(B)));
size(I)

ans =

          65        6836

Now you can try the flattopwin windowing function:

plot(flattopwin(100));

It should look similar to the windowing function shown in class when Discrete Fourier Transform was introduced. 

[B,f,t]=specgram(W,128,fs,flattopwin(100),50);
I = 20*log10(abs(flipud(B)));
imagesc(t,f,I);
xlabel('Time (s)');
ylabel('Frequency (Hz)');

The output should look like the sample DFT spectrogram image 'DFT_tophat.png'. 
By comparing these two windowing functions, you should notice that each choice has a slight influence on the end result.
For a list of other available windowing functions, check out:
http://www.mathworks.com/access/helpdesk/help/toolbox/signal/window.html
