python-3.x - Python rfft 算法
问题描述
Numpy 具有rfft
计算 FFT 或 Real 输入的功能。我想为此编写自己的代码。这是我的 DFT 和 FFT [ ref ] 的实际代码。
def DFT_slow(x):
"""Compute the discrete Fourier Transform of the 1D array x"""
x = np.asarray(x, dtype=float)
N = x.shape[0]
n = np.arange(N)
k = n.reshape((N, 1))
M = np.exp(-2j * np.pi * k * n / N)
return np.dot(M, x)
def FFT(x):
"""A recursive implementation of the 1D Cooley-Tukey FFT"""
x = np.asarray(x, dtype=float)
N = x.shape[0]
if N % 2 > 0:
raise ValueError("size of x must be a power of 2")
elif N <= 32: # this cutoff should be optimized
return DFT_slow(x)
else:
X_even = FFT(x[::2])
X_odd = FFT(x[1::2])
factor = np.exp(-2j * np.pi * np.arange(N) / N)
return np.concatenate([X_even + factor[:N / 2] * X_odd,
X_even + factor[N / 2:] * X_odd])
我对傅里叶变换不太了解,知道如何rfft
从这些代码中计算?
解决方案
rfft
只是整个输入的正频率分量,fft
因为fft
实值信号的频率是共轭对称的。
def rDFT_slow(x):
"""Compute the discrete Fourier Transform of the 1D array of real data x"""
x = np.real(np.asarray(x, dtype=float))
N = x.shape[0]
n = np.arange(N)
k = np.arange(N//2+1).reshape((N//2+1,1))
M = np.exp(-2j * np.pi * k * n / N)
return np.dot(M, x)
当您只计算一半的系数时,实现递归版本有一些更棘手的因素,但缓慢rDFT
应该让您开始。
推荐阅读
- arrays - 在powershell中转换逗号分隔的数组值
- ruby - 在垃圾收集之前删除 Ruby 中的临时文件
- haskell - 有人可以解释如何统一类型(Haskell)吗?
- spring - 弹簧转换器不能有条件地禁用
- flutter - 在下拉菜单颤动中保持默认选择的值
- aws-lambda - 在 DynamoDB epoch 字段上触发 CloudWatch 事件
- python - 如何将单列数组与另一个数组相加(逐列)?
- python - 我如何知道我的应用程序支持哪些版本的依赖项?
- r - dplyr 的汇总输出是否有确定的输出顺序?
- java - 每次运行的 Google Cloud AppEngine Java 代码重复组件更新