如何使用pycuda进行一维fft?

时间:2019-06-04 16:37:49

标签: python gpu fft pycuda

嗨,我正在尝试对信号进行简单的1D-FFT转换。这是我尝试过的:

import numpy as np
from scipy.signal import hilbert, chirp

duration = 1.0
fs = 400.0
samples = int(fs*duration)
t = np.arange(samples) / fs
signal = chirp(t, 20.0, t[-1], 100.0)

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

def fft_gpu1(signal):
  x_gpu = gpuarray.to_gpu(signal)
  x_hat = gpuarray.empty_like(x_gpu, dtype=np.complex64)
  plan = fft.Plan(x_gpu.shape,np.float32,np.complex64)
  fft.fft(x_gpu, x_hat, plan)
  return x_hat.get()

print(fft_gpu1(signal))

但是,我得到的结果是:

[nan+nanj nan+nanj nan+nanj nan+nanj nan+nanj nan+nanj nan+nanj nan+nanj
 nan+nanj nan+nanj nan+nanj nan+nanj nan+nanj nan+nanj nan+nanj nan+nanj
 nan+nanj nan+nanj nan+nanj nan+nanj nan+nanj nan+nanj nan+nanj nan+nanj
 nan+nanj nan+nanj nan+nanj nan+nanj nan+nanj nan+nanj nan+nanj nan+nanj
...
  0. +0.j  0. +0.j  0. +0.j  0. +0.j  0. +0.j  0. +0.j  0. +0.j  0. +0.j
  0. +0.j  0. +0.j  0. +0.j  0. +0.j  0. +0.j  0. +0.j  0. +0.j  0. +0.j
  0. +0.j  0. +0.j  0. +0.j  0. +0.j  0. +0.j  0. +0.j  0. +0.j  0. +0.j]

任何对为什么收到此问题的见解将不胜感激

1 个答案:

答案 0 :(得分:0)

信号应该是 float32 类型,而不是 float64:

x_gpu = gpuarray.to_gpu(signal.astype(np.float32))