为什么 irfftn(rfftn(x)) 不等于 x?

Why is irfftn(rfftn(x)) not equal to x?

如果数组 x 的尾部维度为奇数,则变换 y = irfftn(rfftn(x)) 与输入数组的形状不同。这是设计使然吗?如果是这样,动机是什么?示例代码如下。

import numpy as np

shapes = [(10, 10), (11, 11), (10, 11), (11, 10)]

for shape in shapes:
    x = np.random.uniform(0, 1, shape)
    y = np.fft.irfftn(np.fft.rfftn(x))
    if x.shape != y.shape:
        print("expected shape %s but got %s" % (shape, y.shape))

# Output
# expected shape (11, 11) but got (11, 10)
# expected shape (10, 11) but got (10, 10)

你需要传递第二个参数x.shape 在您的情况下,代码将如下所示:

import numpy as np

shapes = [(10, 10), (11, 11), (10, 11), (11, 10)]

for shape in shapes:
    x = np.random.uniform(0, 1, shape)
    y = np.fft.irfftn(np.fft.rfftn(x),x.shape)
    if x.shape != y.shape:
        print("expected shape %s but got %s" % (shape, y.shape))

来自文档

This function computes the inverse of the N-dimensional discrete Fourier Transform for real input over any number of axes in an M-dimensional array by means of the Fast Fourier Transform (FFT). In other words, irfftn(rfftn(a), a.shape) == a to within numerical accuracy. (The a.shape is necessary like len(a) is for irfft, and for the same reason.)

x.shape 来自同一文档的描述:

s : sequence of ints, optional Shape (length of each transformed axis) of the output (s[0] refers to axis 0, s[1] to axis 1, etc.). s is also the number of input points used along this axis, except for the last axis, where s[-1]//2+1 points of the input are used. Along any axis, if the shape indicated by s is smaller than that of the input, the input is cropped. If it is larger, the input is padded with zeros. If s is not given, the shape of the input along the axes specified by axes is used.

https://docs.scipy.org/doc/numpy/reference/generated/numpy.fft.irfftn.html