Missing library from test link list.
[dcpomatic.git] / hacks / fft.py
1 #!/usr/bin/python
2
3 import wave
4 import sys
5 import struct
6 from pylab import *
7 import numpy
8
9 f = wave.open(sys.argv[1], 'rb')
10 width = f.getsampwidth()
11 peak = pow(2, width * 8) / 2
12 channels = f.getnchannels()
13 frames = f.getnframes()
14 print '%d bytes per sample' % width
15 print '%d channels' % channels
16 print '%d frames' % frames
17 data = []
18 for i in range(0, channels):
19     data.append([])
20
21 for i in range(0, frames):
22     frame = f.readframes(1)
23     for j in range(0, channels):
24         v = 0
25         for k in range(0, width):
26             v |= ord(frame[j * width + k]) << (k * 8)
27         if v >= peak:
28             v = v - 2 * peak
29         data[j].append(v)
30
31 f.close()
32
33 names = ['L', 'R', 'C', 'Lfe', 'Ls', 'Rs']
34 dyn_range = 20 * log10(pow(2, 23))
35
36 for i in range(0, channels):
37     s = numpy.fft.fft(data[i])
38 #    subplot(channels, 1, i + 1)
39     # 138.5 is a fudge factor:
40     semilogx(arange(0, frames / 2, 0.5), 20 * log10(abs(s) + 0.1) - dyn_range, label = names[i])
41     xlim(100, frames / 4)
42 plt.gca().xaxis.grid(True, which='major')
43 plt.gca().xaxis.grid(True, which='minor')
44 plt.gca().yaxis.grid(True)
45 legend(loc = 'lower right')
46 show()
47
48 #plot(abs(numpy.fft.fft(data[0])))
49 #plot(data[0])
50 #show()