问题描述
我试图通过操纵rfft2的结果来重新创建完整的fft2的结果.文档指出,rfft2仅计算正系数,因为当输入为实数时,负系数与正系数具有对称性.这对于大型阵列将非常有用,因为计算rfft2的速度比完整的fft2快得多.
I am trying to recreate the result of a full fft2 by manipulating the result of an rfft2. The documentation states that rfft2 only computes the positive coefficients since the negative coefficients have a symmetry with the positive ones when the input is real. This would be extremely useful for large arrays since computing the rfft2 is much faster than the full fft2.
所以下面的代码是我试图从rfft2输出重新创建fft2的原因.我已经尝试了对左"箭头的各种操纵.数组,不能完全获得相同"到处都是真实的.有什么想法吗?
So the below code is me trying to recreate the fft2 from the rfft2 output. I have tried all kinds of manipulations of the "left" array and can't quite get "same" to be true everywhere. Any ideas?
import numpy as np
import matplotlib.pyplot as plt
from skimage.data import camera
frame = camera()
full_fft = np.fft.fft2(frame)
real_fft = np.fft.rfft2(frame)
left = real_fft[:, :-1].copy()
right = np.flipud(left[:, ::-1])
sim_fft2 = np.hstack((left, right))
same = np.isclose(full_fft, sim_fft2)
plt.figure()
plt.imshow(same)
plt.figure()
plt.imshow(np.log(np.abs(full_fft)))
plt.figure()
plt.imshow(np.log(np.abs(sim_fft2)))
推荐答案
我通过在6x6数组上执行fft2来找出对称性,然后只需编程一个函数即可将rfft2的输出转换为与fft2.下面是该函数和对称图像.
I figured out the symmetry by doing the fft2 on a 6x6 array which then just required programming up a function to convert the output of a rfft2 to be the same as a fft2. Below is that function and an image of the symmetry.
def _rfft2_to_fft2(im_shape, rfft):
fcols = im_shape[-1]
fft_cols = rfft.shape[-1]
result = numpy.zeros(im_shape, dtype=rfft.dtype)
result[:, :fft_cols] = rfft
top = rfft[0, 1:]
if fcols%2 == 0:
result[0, fft_cols-1:] = top[::-1].conj()
mid = rfft[1:, 1:]
mid = numpy.hstack((mid, mid[::-1, ::-1][:, 1:].conj()))
else:
result[0, fft_cols:] = top[::-1].conj()
mid = rfft[1:, 1:]
mid = numpy.hstack((mid, mid[::-1, ::-1].conj()))
result[1:, 1:] = mid
return result
这篇关于从rfft2数组创建fft2结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!