首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NFFT的傅里叶系数-非均匀快速傅立叶变换?

NFFT的傅里叶系数-非均匀快速傅立叶变换?
EN

Stack Overflow用户
提问于 2014-09-24 18:18:07
回答 1查看 3.8K关注 0票数 4

我正在尝试使用python2.7中的pynfft包来做非均匀快速傅立叶变换(nfft)。我只学了两个月的python,所以我有一些困难。

这是我的代码:

代码语言:javascript
复制
import numpy as np
from pynfft.nfft import NFFT

#loading data, 104 lines
t_diff, x_diff = np.loadtxt('data/analysis/amplitudes.dat', unpack = True)

N = [13,8]
M = 52

#fourier coefficients
f_hat = np.fft.fft(x_diff)/(2*M)

#instantiation
plan = NFFT(N,M)

#precomputation
x = t_diff
plan.x = x
plan.precompute()

# vector of non uniform samples
f = x_diff[0:M]

#execution
plan.f = f
plan.f_hat = f_hat
f = plan.trafo()

我基本上是在遵循我在pynfft教程(http://pythonhosted.org/pyNFFT/tutorial.html)中找到的说明。

我需要nfft,因为获取数据的时间间隔不是恒定的(我的意思是,第一个度量在t处,第二个度量在dt之后,第三个度量在dt+dt‘with dt’不同于dt之后,依此类推)。

在执行之前,pynfft包需要傅立叶系数的向量("f_hat"),所以我使用numpy.fft计算了它,但我不确定这个过程是否正确。有没有其他方法可以做到这一点(也许使用nfft)?

我还想计算频率;我知道在numpy.fft中有一个命令: pynfft也有这样的东西吗?我在教程中没有找到任何东西。

谢谢你给我的任何建议。

EN

回答 1

Stack Overflow用户

发布于 2018-03-02 15:27:51

下面是一个工作示例,取自here

首先,我们定义要重构的函数,它是四个谐波的总和:

代码语言:javascript
复制
import numpy as np
import matplotlib.pyplot as plt

np.random.seed(12345)

%pylab inline --no-import-all

# function we want to reconstruct
k=[1,5,10,30] # modulating coefficients
def myf(x,k): 
    return sum(np.sin(x*k0*(2*np.pi)) for k0 in k)

x=np.linspace(-0.5,0.5,1000)   # 'continuous' time/spatial domain; -0.5<x<+0.5
y=myf(x,k)                     # 'true' underlying trigonometric function

fig=plt.figure(1,(20,5))
ax =fig.add_subplot(111)

ax.plot(x,y,'red')
ax.plot(x,y,'r.')

                        # we should sample at a rate of >2*~max(k)
M=256                   # number of nodes
N=128                   # number of Fourier coefficients

nodes =np.random.rand(M)-0.5 # non-uniform oversampling
values=myf(nodes,k)     # nodes&values will be used below to reconstruct 
                        # original function using the Solver

ax.plot(nodes,values,'bo')

ax.set_xlim(-0.5,+0.5)

我们初始化并运行求解器:

代码语言:javascript
复制
from pynfft import NFFT, Solver

f     = np.empty(M,     dtype=np.complex128)
f_hat = np.empty([N,N], dtype=np.complex128)

this_nfft = NFFT(N=[N,N], M=M)
this_nfft.x = np.array([[node_i,0.] for node_i in nodes])
this_nfft.precompute()

this_nfft.f = f
ret2=this_nfft.adjoint()

print this_nfft.M  # number of nodes, complex typed
print this_nfft.N  # number of Fourier coefficients, complex typed
#print this_nfft.x # nodes in [-0.5, 0.5), float typed


this_solver = Solver(this_nfft)
this_solver.y = values          # '''right hand side, samples.'''

#this_solver.f_hat_iter = f_hat # assign arbitrary initial solution guess, default is 0

this_solver.before_loop()       # initialize solver internals

while not np.all(this_solver.r_iter < 1e-2):
this_solver.loop_one_step()

最后,我们显示频率:

代码语言:javascript
复制
import matplotlib.pyplot as plt

fig=plt.figure(1,(20,5))
ax =fig.add_subplot(111)


foo=[ np.abs( this_solver.f_hat_iter[i][0])**2 for i in range(len(this_solver.f_hat_iter) ) ]

ax.plot(np.abs(np.arange(-N/2,+N/2,1)),foo)

干杯

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26014375

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档