[SciPy-User] how to interpret numpy.fft.fftfreq output?

Fabricio Silva silva@lma.cnrs-mrs...
Mon Oct 12 14:38:55 CDT 2009


Le lundi 12 octobre 2009 à 19:59 +0200, Francesco Stablum a écrit :
> Hello,
> 
> I am doing the fft of a wave file (44100 Hz) with some kind of success.
> I have a problem: I have to display the frequencies amounts and I have
> to calculate which frequency corresponds to the indexes of the result
> of the fft. I am actually using the fftfreq function in this way:
> 
> fft.fftfreq(5120,d=1.0/22050.0)
> 
> (5120 is number of frames).
> 
> The result is that the second half of the array is negative. I have
> seen in the documentation that it is normal... why?
> I would expect to have an array like this: [0,5, .. , 22045,2050].
> 
> How should interpret the output of fftfreq?

As you may know, Fourier transform of digital signals is aliased, i.e.
fft is Fe-periodic. Due to this fact, you may need to filter your signal
so that its frequential bandwidth is limited within (-Fe/2, Fe/2): it is
what Shannon theorem claims. It also means that all the frequential
information about a digital signal is contained within that interval, or
if you prefer (0,Fe) as the part in (Fe/2, Fe) is the same as (-Fe/2, 0)
because of the periodicity.
To get the frequencies vector associated to the output of np.fft.fft,
two solutions:
>>> s_f = np.fft.fft(s)
>>> freq = np.linspace(0, Fe, len(s_f)) # frequencies in (0, Fe)
or
>>> freq = np.fft.fftfreq(len(s_f), 1./Fe) # frequencies in (0 .. Fe/2, -Fe/2 .. 0)

I suppose you are only interested in the (0, Fe/2) interval because your
signals are real (and the fft is then Hermite-symmetric). You may then
use np.fft.rfft.

-- 
Fabrice Silva
Laboratory of Mechanics and Acoustics (CNRS, UPR 7051)




More information about the SciPy-User mailing list