# Is there a built-in equivalent to numpy's fftfreq?

I recently learned that python's numpy package contains a function called fftfreq (documented at numpy.fft.fftfreq) which is enormously powerful: you give it the number of points in a finite sample and the sampling rate (i.e. the two parameters used to generate the time axis that corresponds to a time series which you are Fourier transforming), and it returns a list with the frequency axis that corresponds to the signal's Fourier transform.

Is there a built-in equivalent for Mathematica's Fourier? If not, are there well-established options?

(Also, if there isn't one: Wolfram insiders, nudge nudge hint hint!).

Edit: and, while we're here $$-$$ is there anything like numpy.fft.fftshift ?

• Related, but without any satisfactory solutions: mathematica.stackexchange.com/q/253778/1000, mathematica.stackexchange.com/q/85139/1000 Commented Feb 14, 2023 at 18:37
• Also related, and somehow not turned up by this search: mathematica.stackexchange.com/q/69377/1000 Commented Feb 14, 2023 at 18:38
• "while we're here" - have you seen this already? Commented Feb 14, 2023 at 19:15
• Wait, do you mean you're only interested in built-in solution? Commented Feb 15, 2023 at 5:02
• @xzczd I am primarily interested in built-in functions. But robust and working code is a suitable alternative. Commented Feb 15, 2023 at 11:05

Look at "Periodogram" in the help. It plots the squared magnitude of the power spectrum.

But it is not too difficult to get the frequencies berlonging to the output of "Fourier":

Assuming we have n sample data and a sample rate: sr in units 1/sec:

The highest frequency in the data is: hf=sr/2.

And the lowest frequency, if we sample for sec seconds is lf= 1/sec.

The function "Fourier" returns the coefficients: cof, belonging to the frequencies, in a special order:

1. cof[[1]] contains the DC part

2. cof[[2]] and cof[[n]] contain +/- the lowest frequency of lf

3. cof[[3]] and coff[[n-1]] contain +/- the next higher frequency of 2lf

....

Here is an example:

Assuming sample rate sr=2/sec, sample time sec= 3 sec:

Highest frequency: hf= 1 Hz

Lowest frequency: lf= 1/3 Hz

Next frequency: 2/3 Hz

It's not obvious, but a concise implementation for the functionality can actually be found in

What's wrong with this FFT-based Von Kármán vortex street simulation?

Taking the relevant part out, we have:

fftfreq[n_, d_ : 1] := Mod[Range@n - 1, n, -n/2]/(d n)


Usage (Mimicking the example in document of numpy.fft.fftfreq):

signal = {-2, 8, 6, 4, 1, 0, 3, 5};
fourier = Fourier[signal];
n = signal // Length;
d = 0.1;
freq = fftfreq[n, d];
freq
(* {0., 1.25, 2.5, 3.75, -5., -3.75, -2.5, -1.25} *)


Check:

npfftfreq = ExternalFunction["Python", "import numpy; numpy.fft.fftfreq"];

fftfreq[##] == npfftfreq[##] & @@@ {{7}, {8}, {9, 0.13}, {10, 2.1}}
(* {True, True, True, True} *)