5
$\begingroup$

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 ?

$\endgroup$
5

2 Answers 2

3
$\begingroup$

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

$\endgroup$
3
$\begingroup$

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} *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.