将自定义离散傅立叶变换从 MATLAB 转换为 Python 的问题
Issues Translating Custom Discrete Fourier Transform from MATLAB to Python
我正在为某人开发 Python 软件,他们特别要求我在我的程序中使用他们用 MATLAB 编写的 DFT 函数。我的翻译很明显不起作用,用 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 处理计算的方式略有不同,但我不知道为什么或如何解决这个问题。
Numpy 数组与 *
.
进行元素级乘法运算
您需要 np.dot(w1,w2)
使用 numpy 数组进行矩阵乘法(numpy 矩阵不是这种情况)
确保您清楚distinction between Numpy arrays and matrices。有一个很好的帮助页面"Numpy for Matlab Users":
http://wiki.scipy.org/NumPy_for_Matlab_Users
目前似乎没有工作,所以 here is a temporary link。
此外,使用 t.T
转置一个名为 t
的 numpy 数组。
这是您的 MATLAB 代码 -
t = 0:0.005:10-0.005;
x = sin(2*pi*t);
f = 30*(rand(size(t))+0.225);
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*1j * f*t'); %//'
X = W * x;
X = reshape(X,shape);
figure,plot(f,X,'ro')
这是一个版本的 numpy 移植代码可能看起来像 -
import numpy as np
from numpy import math
import matplotlib.pyplot as plt
t = np.arange(0, 10, 0.005)
x = np.sin(2*np.pi*t)
f = 30*(np.random.rand(t.size)+0.225)
N = t.size
i = 1j
W = np.exp((-2 * math.pi * i)*np.dot(f.reshape(N,1),t.reshape(1,N)))
X = np.dot(W,x.reshape(N,1))
out = X.reshape(f.shape).T
plt.plot(f, out, 'ro')
MATLAB 绘图 -
Numpy 图 -
我正在为某人开发 Python 软件,他们特别要求我在我的程序中使用他们用 MATLAB 编写的 DFT 函数。我的翻译很明显不起作用,用 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 处理计算的方式略有不同,但我不知道为什么或如何解决这个问题。
Numpy 数组与 *
.
您需要 np.dot(w1,w2)
使用 numpy 数组进行矩阵乘法(numpy 矩阵不是这种情况)
确保您清楚distinction between Numpy arrays and matrices。有一个很好的帮助页面"Numpy for Matlab Users":
http://wiki.scipy.org/NumPy_for_Matlab_Users
目前似乎没有工作,所以 here is a temporary link。
此外,使用 t.T
转置一个名为 t
的 numpy 数组。
这是您的 MATLAB 代码 -
t = 0:0.005:10-0.005;
x = sin(2*pi*t);
f = 30*(rand(size(t))+0.225);
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*1j * f*t'); %//'
X = W * x;
X = reshape(X,shape);
figure,plot(f,X,'ro')
这是一个版本的 numpy 移植代码可能看起来像 -
import numpy as np
from numpy import math
import matplotlib.pyplot as plt
t = np.arange(0, 10, 0.005)
x = np.sin(2*np.pi*t)
f = 30*(np.random.rand(t.size)+0.225)
N = t.size
i = 1j
W = np.exp((-2 * math.pi * i)*np.dot(f.reshape(N,1),t.reshape(1,N)))
X = np.dot(W,x.reshape(N,1))
out = X.reshape(f.shape).T
plt.plot(f, out, 'ro')
MATLAB 绘图 -
Numpy 图 -