numpy rfft 维度向量

The numpy rfft dimension vector

我正在尝试理解 numpy 函数 rfft。

当我这样做时:

frame = [5,5,5,5,5,5]
tfdf = numpy.fft.rfft([5,5,5,5,5,5])
print(len(frame))
print(len(tfdf))
print(tfdf)

结果是:

6
4
[30.+0.j  0.+0.j  0.+0.j  0.+0.j]

为什么 tfdf 的长度是 4 而不是 6? 4 来自哪里?

第二个问题,如果我们像这样添加 NFFT :

NFFT = 20
frame = [5,5,5,5,5,5]
tfdf = numpy.fft.rfft([5,5,5,5,5,5], NFFT)
print(len(frame))
print(len(tfdf))
print(tfdf)

结果是:

6
11
[ 3.00000000e+01+0.00000000e+00j  1.82843788e+01-1.82843788e+01j
  0.00000000e+00-1.53884177e+01j -2.40652626e+00-2.40652626e+00j
  5.00000000e+00-8.88178420e-16j  5.00000000e+00-5.00000000e+00j
  8.88178420e-16-3.63271264e+00j  1.22618638e+00+1.22618638e+00j
  5.00000000e+00+0.00000000e+00j  2.89596110e+00-2.89596110e+00j
  0.00000000e+00+0.00000000e+00j]

又是 11 从哪里来的?

回答你的第一个问题:

根据此处的文档:https://numpy.org/doc/stable/reference/generated/numpy.fft.rfft.html

如果没有指定轴和n,rfft方法return是一个长度为(n/2)+1的变换轴,数组是偶数,并且(n+1)/2如果它是奇怪的。

因为你显示数组帧的长度是 6,这是偶数,所以该方法将 return 一个长度为 (6/2)+1 = 4 的数组。

为了回答你的第二个问题,从上面类似的逻辑,你传递的 n 值为 20,即使数组 returned 的大小为 (20/2)+1 = 11

此函数计算实数输入的一维离散傅立叶变换。 documentation 表示如下:

This function does not compute the negative frequency terms, and the length of the transformed axis of the output is therefore n//2 + 1

因为你的原始向量有 6 项,它变成 => 6//2 + 1 = 3 + 1 = 4

第二种情况也是如此,你有 20 个术语 => 20//2 + 1 = 10 + 1 = 11