How to obtain frequencies in Non-Uniform DFFT?

Viewed 532

I have code that looks like this:

import matplotlib.pyplot as plt
import numpy as np
from nfft import nfft


# number of sample points
N = 400

# Simulated non-uniform data
x = np.linspace(0.0, 1 / 2, N) + np.random.random((N)) * 0.001
y = np.sin(50.0 * 2.0 * np.pi * x) + 0.5 * np.sin(80.0 * 2.0 * np.pi * x)
yf = np.abs(nfft(x, y))

fig, axs = plt.subplots(1)
fig_f, axs_f = plt.subplots(1)

axs.plot(x, y, '.', color='red')
axs_f.plot(x, yf, color='red')

graphs produced by code

How do I convert the values on the second graph to represent frequency?

The use of the nfft module is not required, answers using pynfft or scipy will be greatly appreciated.

See also: How do I obtain the frequencies of each value in an FFT?

1 Answers

The following seems to work. Notice the line inserted before graphing the Fourier transform, to generate the frequencies, and that we graph N/2 of the data.

import matplotlib.pyplot as plt
import numpy as np
from nfft import nfft

# number of sample points
N = 400

# Simulated non-uniform data
x = np.linspace(0.0,0.5-0.02, N) + np.random.random((N)) * 0.001
print(x)

print( 'random' )
print( np.random.random((N)) * 0.001 )

y = np.sin(50.0 * 2.0 * np.pi * x) + 0.5 * np.sin(80.0 * 2.0 * np.pi * x)
yf = np.abs(nfft(x, y))

fig, axs = plt.subplots(1)
fig_f, axs_f = plt.subplots(1)

axs.plot(x, y, '.', color='red')

xf = np.fft.fftfreq(N,1./N)

axs_f.plot(xf[:int(N/2)], yf[:int(N/2)], color='red')

plt.show()

Output:

enter image description here

Related