我试图再次翻译一些 matlab 代码,但我遇到了另一个问题。代码本身非常简单,只是 4 节点旋转因子的演示。这是我的尝试:
from numpy import *
from matplotlib import pyplot as plt
x = zeros(4)
x[-1+1] = 0
x[0+1] = 1
x[1+1] = 1
x[2+1] = 0
z = 0 - 1j
W4 = exp(z*2*pi/4)
W0 = W4 ** 0
W1 = W4 ** 1
W2 = W4 ** 2
W3 = W4 ** 3
X = zeros(4)
X[-1+1] = (x[-1+1] + x[1+1]*W0) + W0*(x[0+1] + x[2+1]*W0)
X[0+1] = (x[-1+1] + x[1+1]*W2) + W1*(x[0+1] + x[2+1]*W2)
X[1+1] = (x[-1+1] + x[1+1]*W0) + W2*(x[0+1] + x[2+1]*W0)
X[2+1] = (x[-1+1] + x[1+1]*W2) + W3*(x[0+1] + x[2+1]*W2)
fx = fft.fft(x)
plt.plot(X)
plt.plot(fx, 'ro')
plt.title("Results 4-point hand programmed FFT (blue) and the PYTHON routine (red o)")
plt.show()
Here are the output images. The first one is run with (almost) identical matlab code, the second one is the image from the python code above.
For lines 24 to 27 it gives me the error "ComplexWarning: Casting complex values to real discards the imaginary part". Now I'm not used to working with complex numbers in python. I tried adding a complex component to all the variables, but it gave me a graph that's way off from the matlab one. Thoughts? If you would like me to post the matlab code as well, let me know.
当您指定数组时x
and X
您需要确保它是复杂的数据类型,即:
x = zeros((4),dtype=complex)
EDIT:
要修复绘图,您需要绘制实部和虚部:
plt.plot(X.real,X.imag)
plt.plot(fx.real,fx.imag, 'ro')
这给了我:
....看起来像你的 Matlab 图表。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)