How to get time/freq from FFT in Python
<p>I've got a little problem managing FFT data. I was looking for many examples of how to do FFT, but I couldn't get what I want from any of them. I have a random wave file with 44kHz sample rate and I want to get magnitude of N harmonics each X ms, let's say 100ms should be enough. I tried this code:</p>

<pre><code>import scipy.io.wavfile as wavfile
import numpy as np
import pylab as pl

rate, data = wavfile.read("sound.wav")
t = np.arange(len(data[:,0]))*1.0/rate
p = 20*np.log10(np.abs(np.fft.rfft(data[:2048, 0])))
f = np.linspace(0, rate/2.0, len(p))
pl.plot(f, p)
pl.xlabel("Frequency(Hz)")
pl.ylabel("Power(dB)")
pl.show()
</code></pre>

<p>This was last example I used, I found it somewhere on stackoverflow. The problem is, this gets magnitude which I want, gets frequency, but no time at all. FFT analysis is 3D as far as I know and this is "merged" result of all harmonics. I get this:</p>

<p><a href="http://i.stack.imgur.com/mhPEG.png" rel="nofollow">X-axis = Frequency, Y-axis = Magnitude, Z-axis = Time (invisible)</a></p>

<p>From my understanding of the code, t is time - and it seems like that, but is not needed in the code - We'll maybe need it though. p is array of powers (or magnitude), but it seems like some average of all magnitudes of each frequency f, which is array of frequencies. I don't want average/merged value, I want magnitude for N harmonics each X milliseconds.</p>

<p>Long story short, we can get: 1 magnitude of all frequencies.</p>

<p>We want: All magnitudes of N freqeuencies including time when certain magnitude is present.</p>

<p>Result should look like this array: [time,frequency,amplitude]
So in the end if we want 3 harmonics, it would look like:</p>

<pre><code>[0,100,2.85489] #100Hz harmonic has 2.85489 amplitude on 0ms
[0,200,1.15695] #200Hz ...
[0,300,3.12215]
[100,100,1.22248] #100Hz harmonic has 1.22248 amplitude on 100ms
[100,200,1.58758]
[100,300,2.57578]
[200,100,5.16574]
[200,200,3.15267]
[200,300,0.89987]
</code></pre>

<p>Visualization is not needed, result should be just arrays (or hashes/dictionaries) as listed above.</p>