Skip to content
Advertisement

What does np.fft.fftfreq actually do?

I have a monthly time series and I am taking the discrete fourier transform of it. However I am confused as to how numpy converts the time domain into frequency domain?

I am using np.fft.fftfreq and my time array is is 708 indices long and each measurement of the data is computed every month.

This is the output frequency using the numpy fftfreq:

frequency = np.fft.fftfreq(len(time_months),d=1)

print(frequency)

output:
[0.         0.00141243 0.00282486 0.00423729 0.00564972 0.00706215
 0.00847458 0.00988701 0.01129944 0.01271186 0.01412429 0.01553672
 0.01694915 0.01836158 0.01977401 0.02118644 0.02259887 0.0240113
 0.02542373 0.02683616 0.02824859 0.02966102 0.03107345 0.03248588
 0.03389831 0.03531073 0.03672316 0.03813559 0.03954802 0.04096045
 0.04237288 0.04378531 0.04519774 0.04661017 0.0480226  0.04943503
 0.05084746 0.05225989 0.05367232 0.05508475 0.05649718 0.0579096
 0.05932203 0.06073446 0.06214689 0.06355932 0.06497175 0.06638418
 0.06779661 0.06920904 0.07062147 0.0720339  0.07344633 0.07485876
 0.07627119 0.07768362 0.07909605 0.08050847 0.0819209  0.08333333
 0.08474576 0.08615819 0.08757062 0.08898305 0.09039548 0.09180791
 0.09322034 0.09463277 0.0960452  0.09745763 0.09887006 0.10028249
 0.10169492 0.10310734 0.10451977 0.1059322  0.10734463 0.10875706... 
0.49152542 0.49293785 0.49435028 0.49576271 0.49717514 0.49858757
 0.5

But when I try to look at the sample rate and nyquist frequency, I cannot recreate the numpys frequency output. What is np.fft.fftfreq really doing to convert the time domain to the frequency domain?

I tried this:

sample_time = np.diff(time_months) #taking difference between each time stamp

sample_time_mean = np.mean(sample_time)

print('sample time (months)', sample_time_mean, 'months')

sample_time_mean_days = sample_time_mean*30.4 #days

print('sample time (days)', sample_time_mean_days, 'days')

sample_rate_days = 1/sample_time_mean_days

print('Sampling rate (per day, 1/sample time) =', sample_rate_days, 'sample/day')

nyq = sample_rate_days/2

print('nyquist frequency (per day)', nyq)

sample time (months) 0.08333333333325754 months
sample time (days) 2.5333333333310293 days
Sampling rate (per day, 1/sample time) = 0.39473684210562215 sample/day
nyquist frequency (per day) 0.19736842105281108

Advertisement

Answer

Exactly what fftfreq is doing can be found here

And more information on the relationship between the input signal and the Fourier transform can be found here

The Nyquist frequency is only the minimum sampling frequency required to get complete information on a signal, and is not necessarily going to limit the FFT results.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement