如何获得非均匀DFFT中的频率?

问题描述

我有如下代码

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

如何转换第二张图上的值来表示频率?

不需要使用nfft模块,将非常感谢使用pynfftscipy的答案。

另请参阅: How do I obtain the frequencies of each value in an FFT?

解决方法

以下似乎有效。注意在绘制傅立叶变换之前插入的线,以生成频率,以及我们绘制数据的 N/2。

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()

输出:

enter image description here