5

Some days ago I came across this answer about the usage of the FFT

In the answer there's a piece of code like this:

w = np.fft.fft(data)
freqs = np.fft.fftfreq(len(w))

I read about the function fftfreq in the numpy documentation (here) and i found that it returns an array with the following content:

f = [0, 1, ...,   n/2-1,     -n/2, ..., -1] / (d*n)   if n is even
f = [0, 1, ..., (n-1)/2, -(n-1)/2, ..., -1] / (d*n)   if n is odd

In my case, the d var is equal to 1 and n is an even number.

So my question is: what is exactly the aim of fftfreq?

I was wondering if it was a sort of a triangular window function.

Yaron
  • 10,166
  • 9
  • 45
  • 65
  • 1
    It's just the set of frequencies that match the output of the FFT, for plotting purposes, or whatever. – Ahmed Fasih Jun 26 '17 at 11:43
  • @AhmedFasih well, I get it. So each array element's should be multiplied by the sampling rate, in order to get the frequency. I still don't understand why :( – Davide Pisanò Jun 26 '17 at 12:00
  • You can pass the sample spacing into `fftfreq` and it'll scale the output for you but yes. I like using `fftfreq` because it's a little tricky to get the relationship between signal length and frequency values depending on odd/even. – Ahmed Fasih Jun 26 '17 at 18:48

2 Answers2

3

The returned float array f contains the frequency bin centers in cycles per unit of the sample spacing (with zero at the start). For instance, if the sample spacing is in seconds, then the frequency unit is cycles/second.

fftfreq gives the range of possible frequencies of Fourier Transform.

1

np.fft.fftfreq try to map the frequency range (y) to a kind of "index" range (x) as the following figure. Note that blue line -> positive frequency, orange line -> negative frequency.

IPhysResearch
  • 119
  • 1
  • 4