且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Python中使用DFT查找一阶导数

更新时间:2022-06-26 23:59:14

原来的问题是np.zeros为您提供了一个实零数组,而不是复杂的零数组,因此此后的赋值不会更改任何内容,例如他们是虚构的.

Turns out the problem is that np.zeros gives you an array of real zeroes and not complex ones, hence the assignments after that don't change anything, as they are imaginary.

因此解决方案非常简单

import numpy as np

N=100
xmin=0
xmax=2.0*np.pi
step = (xmax-xmin)/(N)
xdata = np.linspace(step, xmax, N)
v = np.exp(np.sin(xdata))
derv = np.cos(xdata)*v
vhat = np.fft.fft(v)

what = 1j*np.zeros(N)
what[0:N/2.0] = 1j*np.arange(0, N/2.0, 1)
what[N/2+1:] = 1j*np.arange(-N/2.0 + 1, 0, 1)
what = what*vhat
w = np.real(np.fft.ifft(what))

# Then plotting

从而将np.zeros替换为1j*np.zeros