如何从无冗余傅立叶变换(例如 PyTorch)到冗余(完整)傅立叶变换?
How do I go from redundancy-free Fourier Transform (e.g. PyTorch) to redundant (full) Fourier Transform?
在图像上使用 np.fft.fft2
时,结果与输入的大小相同。对于真实图像,真实到复杂的 FT 具有对称性,其中 ft[i,j] == ft[-i,-j].conj()
,如 中所述。
为此,一些框架如 PyTorch 或 scikit-cuda,return 形状为 (height // 2 +1, width // 2 + 1)
的 FT。
现在,给定一个 redundancy-free/one-sided FT,我如何使用 numpy index magic 将它映射到 numpy 的完整 FT 输出?
背景:我需要这个来翻译一些 numpy 代码。
如果您正在使用 torch.rfft
,那么您可以设置 onesided=False
以获得完整的转换。
该文档没有说明输出的格式,最好的猜测是假设它 returns 元素的前半部分沿着最后一个维度,这意味着 ft[i,j]
, i
在半开范围 [0
,in.shape[0]
), j
在半开范围 [0
,in.shape[1]
), 和in
输入图片,可以这样读:
cutoff = in.shape[1] // 2 + 1
if j >= cutoff:
return ft[-i, in.shape[1] - j].conj()
else:
return ft[i, j]
如果您使用 skcuda.fft.fft
,文档同样明确,因此我会做出与上述相同的猜测。
要从这些函数返回的半平面 DFT 中获得完整的 DFT,请使用以下代码:
import numpy as np
size = 6
halfsize = size // 2 + 1
half_ft = np.random.randn(size, halfsize) # example half-plane DFT
if size % 2:
# odd size input
other_half = half_ft[:, -1:0:-1]
else:
# even size input
other_half = half_ft[:, -2:0:-1]
other_half = np.conj(other_half[np.hstack((0, np.arange(size-1, 0, -1))), :])
full_ft = np.hstack((half_ft, other_half))
也就是说,我们沿两个维度翻转数组(这是二维情况,根据需要调整其他维度),但第一行和第一列(直流分量)不重复,并且对于偶数大小的输入,最后一行和最后一列也不重复。
终于成功使用np.meshgrid
正确填写了相关数据。
我们可以对整个行范围和列范围的缺失部分使用范围,以便只用适当的数据填充这些索引。
import numpy as np
np.random.seed(0)
N = 10
image = np.random.rand(N, N)
h, w = image.shape
ft = np.fft.rfft2(image)
ft_reference = np.fft.fft2(image)
ft_full = np.zeros_like(image, dtype=np.complex128)
ft_full[:ft.shape[0], :ft.shape[1]] = ft
X, Y = np.meshgrid(range(h), range(w // 2 + 1, w), indexing='ij')
ft_full[X, Y] = ft_full[-X, -Y].conj()
print(np.allclose(ft_full, ft_reference))
在图像上使用 np.fft.fft2
时,结果与输入的大小相同。对于真实图像,真实到复杂的 FT 具有对称性,其中 ft[i,j] == ft[-i,-j].conj()
,如 (height // 2 +1, width // 2 + 1)
的 FT。
现在,给定一个 redundancy-free/one-sided FT,我如何使用 numpy index magic 将它映射到 numpy 的完整 FT 输出?
背景:我需要这个来翻译一些 numpy 代码。
如果您正在使用 torch.rfft
,那么您可以设置 onesided=False
以获得完整的转换。
该文档没有说明输出的格式,最好的猜测是假设它 returns 元素的前半部分沿着最后一个维度,这意味着 ft[i,j]
, i
在半开范围 [0
,in.shape[0]
), j
在半开范围 [0
,in.shape[1]
), 和in
输入图片,可以这样读:
cutoff = in.shape[1] // 2 + 1
if j >= cutoff:
return ft[-i, in.shape[1] - j].conj()
else:
return ft[i, j]
如果您使用 skcuda.fft.fft
,文档同样明确,因此我会做出与上述相同的猜测。
要从这些函数返回的半平面 DFT 中获得完整的 DFT,请使用以下代码:
import numpy as np
size = 6
halfsize = size // 2 + 1
half_ft = np.random.randn(size, halfsize) # example half-plane DFT
if size % 2:
# odd size input
other_half = half_ft[:, -1:0:-1]
else:
# even size input
other_half = half_ft[:, -2:0:-1]
other_half = np.conj(other_half[np.hstack((0, np.arange(size-1, 0, -1))), :])
full_ft = np.hstack((half_ft, other_half))
也就是说,我们沿两个维度翻转数组(这是二维情况,根据需要调整其他维度),但第一行和第一列(直流分量)不重复,并且对于偶数大小的输入,最后一行和最后一列也不重复。
终于成功使用np.meshgrid
正确填写了相关数据。
我们可以对整个行范围和列范围的缺失部分使用范围,以便只用适当的数据填充这些索引。
import numpy as np
np.random.seed(0)
N = 10
image = np.random.rand(N, N)
h, w = image.shape
ft = np.fft.rfft2(image)
ft_reference = np.fft.fft2(image)
ft_full = np.zeros_like(image, dtype=np.complex128)
ft_full[:ft.shape[0], :ft.shape[1]] = ft
X, Y = np.meshgrid(range(h), range(w // 2 + 1, w), indexing='ij')
ft_full[X, Y] = ft_full[-X, -Y].conj()
print(np.allclose(ft_full, ft_reference))