我正在为某人开发Python软件,他们特别要求我在我的程序中使用他们编写的DFT函数,该函数是用MATLAB编写的。我的转换根本不起作用,使用sin(2 * pi * r)进行测试。
下面是MATLAB函数:
function X=dft(t,x,f)
% Compute DFT (Discrete Fourier Transform) at frequencies given
% in f, given samples x taken at times t:
% X(f) = sum { x(k) * e**(2*pi*j*t(k)*f) }
% k
shape = size(f);
t = t(:); % Format 't' into a column vector
x = x(:); % Format 'x' into a column vector
f = f(:); % Format 'f' into a column vector
W = exp(-2*pi*j * f*t');
X = W * x;
X = reshape(X,shape);
我的 Python 解释:
def dft(t, x, f):
i = 1j #might not have to set it to a variable but better safe than sorry!
w1 = f * t
w2 = -2 * math.pi * i
W = exp(w1 * w2)
newArr = W * x
return newArr
为什么会出现问题?MATLAB代码正常运行,但Python翻译输出的是一个奇怪的增长正弦曲线,而不是傅里叶变换。我感觉Python在处理计算上有些微不同,但我不知道如何解决这个问题。
f*t'
,我认为这意味着t转置。将你此刻拥有的东西相加计算出w1
,看看是否有效。--不,等等。那不可能是这样的。 - chw21f
是1d还是2d?在MATLAB中,所有的东西都是2d或更高维度,在numpy
中数组可以是1d。对于1d数组进行转置不会有任何变化。 - hpaulj