[英]Numerical differentiation via numpy FFT
I am learning how to use numpy for Fast Fourier transform differentiation. 我正在学习如何使用numpy进行快速傅里叶变换区分。 In the code below, I create a simple sine function and try to get the cosine. 在下面的代码中,我创建了一个简单的正弦函数并尝试获得余弦。 The result is shown in the image, there seems to be a normalization factor which I do not understand despite reading the documentation and which prevents me from getting the correct results. 结果显示在图像中,似乎有一个归一化因素,尽管阅读文档但我无法理解,这使我无法获得正确的结果。
Can you tell me how to get rid of the normalization factor or if I am failing in a different way? 你能告诉我如何摆脱正常化因素,或者我是否以不同的方式失败? Also please explain why the Nyquist frequency is not present when the array is length is odd. 还请解释为什么当阵列长度为奇数时,奈奎斯特频率不存在。
x = np.arange(start=-300., stop=300.1, step=0.1)
sine = np.sin(x)
Y = np.fft.rfft(a=sine, n=len(x))
L = 2.*np.pi #period
N = size(Y)
for k, y in enumerate(Y):
Y[k] *= 2.*np.pi*1j*k/L
# if N is even, the last entry is the Nyquist frequency.
#if N is odd, there it is not there.
if N%2 == 0:
Y[-1] *= 0.
cosine = np.fft.irfft(a=Y, n=len(x))
Can you tell me how to get rid of the normalization factor or if I am failing in a different way? 你能告诉我如何摆脱正常化因素,或者我是否以不同的方式失败?
Add np.exp()
for the term 2.*np.pi*1j*k/L
. 为术语2.*np.pi*1j*k/L
添加np.exp()
。 This term seems to be the amount of phase rotation, so their norm should be 1. 这个术语似乎是相位旋转的量,所以它们的范数应该是1。
for k in range(N):
Y[k] *= np.exp(2.*np.pi*1j*k/L)
Also please explain why the Nyquist frequency is not present when the array is length is odd. 还请解释为什么当阵列长度为奇数时,奈奎斯特频率不存在。
It's a nature of discrete Fourier transformation. 这是离散傅立叶变换的本质。 Briefly, when the number of sampling points N is odd, there is no integer that equals to N/2 . 简而言之,当采样点的数量N是奇数时,没有等于N / 2的整数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.