'''Demo of using scipy and matplotlib to plot a 3-d spectogram.

To use this you will need to have installed:

  * numpy/scipy
  * matplotlib/pylab
  * scikits.audiolab

The first 2 are standard libraries but the 3rd is not (yet). It can be obtained
from: <http://www.ar.media.kyoto-u.ac.jp/members/david/softwares/audiolab/>.
'''
import os

import scikits.audiolab as audiolab
import numpy
import pylab
import matplotlib.axes3d as p3

# path to wav file you want to analyse
fn = os.path.expanduser('~/install/audiolab/scikits/audiolab/test_data/test.wav')
(tmp, sampFreq, nBits) = audiolab.wavread(fn)
(spec,freqs,bins,im)=pylab.specgram(tmp)

# chop out a load of the top frequencies where nothing happens
oldsize, time_samples = spec.shape
# print oldsize, time_samples
newsize = 8
freqs = numpy.resize(freqs, newsize)
spec = numpy.resize(spec, (newsize, time_samples))

fig=pylab.figure(1)
ax = p3.Axes3D(fig)
X,Y = pylab.meshgrid(bins, freqs)
ax.plot_wireframe(X,Y,spec, rstride=1, cstride=10)
# unfortunately contour3D is broken :(
# ax.contour3D(X,Y, spec)
# fig.add_axes(ax)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
# ax.set_ylim(0, 0.03)
# ax.axis(xmin=0, xmax=1.0, ymin=0, ymax=0.5)
pylab.show()

