skcuda.fft与numpy.fft.rfft不同吗?

时间:2019-06-28 18:11:28

标签: python numpy fft pycuda cufft

我试图将ftf的输出与numpy的ftf进行单元测试,我意识到在失败之后不久,这并不是因为我做错了什么,但是skcuda并不能产生相同的答案。我知道它们会有所不同,但是至少其中一个数字与numpy产生的结果相差几个数量级,并且allclosealmost_equal都返回大量错误(33%和rtol=1e-6为25%,atol=1e-6为16%)。我在这里做错了什么?我可以解决这个问题吗?

测试文件:

import pycuda.autoinit
from skcuda import fft
from pycuda import gpuarray
import numpy as np

def test_skcuda():
    array_0 = np.array([[1, 2, 3, 4, 5, 4, 3, 2, 1, 0]], dtype=np.float32)
    array_1 = array_0 * 10
    time_domain_signal = np.array([array_0[0], array_1[0]], dtype=np.float32)
    fft_point_count = 10
    fft_plan = fft.Plan(fft_point_count, np.float32, np.complex64,
                        batch=2)
    fft_reserved = gpuarray.empty((2, fft_point_count // 2 + 1), dtype=np.complex64)
    fft.fft(gpuarray.to_gpu(time_domain_signal), fft_reserved, fft_plan)

    np.testing.assert_array_almost_equal(
        np.fft.rfft(time_domain_signal, fft_point_count), fft_reserved.get())

test_skcuda()

断言失败:

AssertionError: 
Arrays are not almost equal to 6 decimals

(mismatch 25.0%)
 x: array([[ 2.500000e+01+0.000000e+00j, -8.472136e+00-6.155367e+00j,
        -1.193490e-15+2.331468e-15j,  4.721360e-01-1.453085e+00j,
         2.664535e-15+0.000000e+00j,  1.000000e+00+0.000000e+00j],...
 y: array([[ 2.500000e+01+0.000000e+00j, -8.472136e+00-6.155367e+00j,
         8.940697e-08+5.960464e-08j,  4.721359e-01-1.453085e+00j,
         0.000000e+00+0.000000e+00j,  1.000000e+00+0.000000e+00j],...

打印输出:

#numpy
[[ 2.50000000e+01+0.00000000e+00j -8.47213595e+00-6.15536707e+00j
  -1.19348975e-15+2.33146835e-15j  4.72135955e-01-1.45308506e+00j
   2.66453526e-15+0.00000000e+00j  1.00000000e+00+0.00000000e+00j]
 [ 2.50000000e+02+0.00000000e+00j -8.47213595e+01-6.15536707e+01j
  -1.11022302e-14+2.39808173e-14j  4.72135955e+00-1.45308506e+01j
   3.55271368e-14+7.10542736e-15j  1.00000000e+01+0.00000000e+00j]]

#skcuda
[[ 2.5000000e+01+0.0000000e+00j -8.4721355e+00-6.1553669e+00j
   8.9406967e-08+5.9604645e-08j  4.7213593e-01-1.4530852e+00j
   0.0000000e+00+0.0000000e+00j  1.0000000e+00+0.0000000e+00j]
 [ 2.5000000e+02+0.0000000e+00j -8.4721359e+01-6.1553673e+01j
   1.4305115e-06-4.7683716e-07j  4.7213597e+00-1.4530851e+01j
   0.0000000e+00+1.9073486e-06j  1.0000000e+01+0.0000000e+00j]]

3 个答案:

答案 0 :(得分:1)

FFT的输出具有与输入值的大小有关的误差。每个输出元素都是通过组合所有输入元素来计算的,因此,其大小决定了结果的精度。

您正在同一阵列中计算两个1D FFT。它们各自具有不同的幅度输入,因此应具有不同的幅度容差。

以下快速代码演示了如何实现此目标。我不知道如何调整numpy.testing中的任何功能。

import numpy as np

array_0 = np.array([[1, 2, 3, 4, 5, 4, 3, 2, 1, 0]], dtype=np.float32)
array_1 = array_0 * 10
time_domain_signal = np.array([array_0[0], array_1[0]], dtype=np.float32)

# numpy result
a=np.array([[ 2.50000000e+01+0.00000000e+00j, -8.47213595e+00-6.15536707e+00j,
  -1.19348975e-15+2.33146835e-15j,  4.72135955e-01-1.45308506e+00j,
   2.66453526e-15+0.00000000e+00j,  1.00000000e+00+0.00000000e+00j],
 [ 2.50000000e+02+0.00000000e+00j, -8.47213595e+01-6.15536707e+01j,
  -1.11022302e-14+2.39808173e-14j,  4.72135955e+00-1.45308506e+01j,
   3.55271368e-14+7.10542736e-15j,  1.00000000e+01+0.00000000e+00j]])

# skcuda result
b=np.array([[ 2.5000000e+01+0.0000000e+00j, -8.4721355e+00-6.1553669e+00j,
   8.9406967e-08+5.9604645e-08j,  4.7213593e-01-1.4530852e+00j,
   0.0000000e+00+0.0000000e+00j,  1.0000000e+00+0.0000000e+00j],
 [ 2.5000000e+02+0.0000000e+00j, -8.4721359e+01-6.1553673e+01j,
   1.4305115e-06-4.7683716e-07j,  4.7213597e+00-1.4530851e+01j,
   0.0000000e+00+1.9073486e-06j,  1.0000000e+01+0.0000000e+00j]])

# Tolerance for result array row relative to the mean absolute input values
# 1e-6 because we're using single-precision floats
tol = np.mean(np.abs(time_domain_signal), axis=1) * 1e-6

# Compute absolute difference and compare that to our tolearances
diff = np.abs(a-b)
if np.any(diff > tol[:,None]):
   print('ERROR!!!')

答案 1 :(得分:1)

这看起来很像舍入错误,单精度浮点数的精度约为8位小数(双精度数约为16)

代替使用numpy.fft的替代方法是使用fftpack from scipy,它直接支持单精度浮点数,例如:

from scipy import fftpack

x = np.array([1, 2, 3, 4, 5, 4, 3, 2, 1, 0])

y = fftpack.fft(
    np.array([x, x * 10], dtype=np.float32)
)
print(y[:,:6])

输出:

[[ 2.5000000e+01+0.0000000e+00j -8.4721355e+00-6.1553669e+00j
   8.9406967e-08+5.9604645e-08j  4.7213593e-01-1.4530852e+00j
   0.0000000e+00+0.0000000e+00j  1.0000000e+00+0.0000000e+00j]
 [ 2.5000000e+02+0.0000000e+00j -8.4721359e+01-6.1553673e+01j
   1.1920929e-06+1.9073486e-06j  4.7213583e+00-1.4530851e+01j
   0.0000000e+00+1.9073486e-06j  1.0000000e+01+0.0000000e+00j]]

看起来更近

答案 2 :(得分:0)

来自FFT的e-8(浮点型)和e-15(双精度)的微小结果值(给定的满量程输入或输出在1.0附近)完全等于零(加上数字舍入噪声)。

零+噪音==零+噪音

所以您的结果可能实际上是相同的。