def FFT(img): mt = img.memType dt = img.cmpRepr if cufft is not None: img.MoveToGPU() img.AmPh2ReIm() fft = imsup.Image(img.height, img.width, imsup.Image.cmp['CRI'], imsup.Image.mem['GPU']) cufft.fft(img.reIm, fft.reIm) img.ChangeComplexRepr(dt) img.ChangeMemoryType(mt) else: img.AmPh2ReIm() img.MoveToCPU() fft = imsup.Image(img.height, img.width, imsup.Image.cmp['CRI'], imsup.Image.mem['CPU']) fft.reIm = np.fft.fft2(img.reIm).astype(np.complex64) img.ChangeMemoryType(mt) fft.ChangeMemoryType(mt) img.ChangeComplexRepr(dt) return fft
def test_fft_1d_roundtrip_double(self): from pyculib.fft import fft, ifft N = 32 x = np.asarray(np.arange(N), dtype=np.float64) x0 = x.copy() xf_gpu = np.empty(shape=N//2 + 1, dtype=np.complex128) fft(x, xf_gpu) ifft(xf_gpu, x) self.assertTrue( np.allclose(x / N, x0, atol=1e-6) )
def test_fft_1d_double(self): from pyculib.fft import fft N = 32 x = np.asarray(np.arange(N), dtype=np.float64) xf = np.fft.fft(x) xf_gpu = np.zeros(shape=N//2 + 1, dtype=np.complex128) fft(x, xf_gpu) self.assertTrue( np.allclose(xf[0:N//2+1], xf_gpu, atol=1e-6) )
def test_fft_2d_roundtrip_single(self): from pyculib.fft import fft, ifft N2 = 2 N1 = 32 N = N2 * N1 x = np.asarray(np.arange(N), dtype=np.float32).reshape(N2, N1) x0 = x.copy() xf_gpu = np.empty(shape=(N2, N1//2 + 1), dtype=np.complex64) fft(x, xf_gpu) ifft(xf_gpu, x) self.assertTrue( np.allclose(x / N, x0, atol=1e-6) )
def test_fft_2d_single_col_major(self): from pyculib.fft import fft N2 = 2 N1 = 8 N = N1 * N2 x = np.asarray(np.arange(N), dtype=np.float32).reshape((N2, N1), order='F') xf_ref = np.fft.rfft2(x) xf = np.empty(shape=(N2, N1//2 + 1), dtype=np.complex64, order='F') fft(x, xf) self.assertTrue( np.allclose(xf_ref, xf, atol=1e-6) )
def FFT(img): mt = img.memType dt = img.cmpRepr img.MoveToGPU() img.AmPh2ReIm() fft = imsup.Image(img.height, img.width, imsup.Image.cmp['CRI'], imsup.Image.mem['GPU']) cufft.fft(img.reIm, fft.reIm) img.ChangeComplexRepr(dt) img.ChangeMemoryType(mt) return fft
def test_fft_2d_double(self): from pyculib.fft import fft N2 = 2 N1 = 32 N = N1 * N2 x = np.asarray(np.arange(N), dtype=np.float64).reshape(N2, N1) xf = np.fft.fft2(x) xf_gpu = np.empty(shape=(N2, N1//2 + 1), dtype=np.complex128) fft(x, xf_gpu) self.assertTrue( np.allclose(xf[:, 0:N1//2+1], xf_gpu, atol=1e-6) )
def test_fft_3d_single(self): from pyculib.fft import fft N3 = 2 N2 = 2 N1 = 32 N = N1 * N2 * N3 x = np.asarray(np.arange(N), dtype=np.float32).reshape(N3, N2, N1) xf = np.fft.fftn(x) xf_gpu = np.empty(shape=(N3, N2, N1//2 + 1), dtype=np.complex64) fft(x, xf_gpu) self.assertTrue( np.allclose(xf[:, :, 0:N1//2+1], xf_gpu, atol=1e-6) )
def test_fft_1d_roundtrip_single(self): from pyculib.fft import fft, ifft N = 32 x = np.asarray(np.arange(N), dtype=np.float32) x0 = x.copy() xf_gpu = np.empty(shape=N // 2 + 1, dtype=np.complex64) fft(x, xf_gpu) ifft(xf_gpu, x) #CUDA 10 uses 64bit API internally, so the percision of the numbers is higher than in the CPU version #Round down to match precision x = x.round(4) self.assertTrue(np.allclose(x / N, x0, atol=1e-6))
def compute_psiTpsi(full_size): # V's operator - forward part psiTpsi = np.zeros(full_size) psiTpsi[0, 0] = 4 psiTpsi[0, 1] = psiTpsi[1, 0] = psiTpsi[0, -1] = psiTpsi[-1, 0] = -1 psiTpsi = fft.fft(psiTpsi) return psiTpsi
gauss_fft = np.empty((dim, dim), dtype=complex) img_ifft = np.empty((dim, dim), dtype=complex) # Put the data on the device d_img_data_complex = cuda.to_device(img_data_complex) d_gauss2D_complex = cuda.to_device(gauss2D_complex) # Create device arrays d_img_fft = cuda.device_array((dim, dim), dtype=np.complex) d_gauss_fft = cuda.device_array((dim, dim), dtype=np.complex) d_img_ifft = cuda.device_array((dim, dim), dtype=np.complex) t1 = timer() # FFT the two input arrays cufft.fft(d_img_data_complex, d_img_fft) cufft.fft(d_gauss2D_complex, d_gauss_fft) # Copy data back to host img_fft = d_img_fft.copy_to_host() gauss_fft = d_gauss_fft.copy_to_host() # Multiply each element in fft_img by the corresponding image in fft_gaus img_conv = img_fft * gauss_fft # Copy to the device d_img_conv = cuda.to_device(img_conv) # Inverse Fourier transform cufft.ifft(d_img_conv, d_img_ifft)
def compute_fft(H, full_size, sensor_size): return fft.fft(fft.ifftshift(CT(H, full_size, sensor_size)))
rho = rho + mu3 * (V - W) # print("This is matrix after " + str(i) + " iteration:") # print(C(V, full_size, sensor_size)) return C(V, full_size, sensor_size) ''' Below are the actual test for the program ''' # Define m and n m = 256 n = 256 # Generate psf psf = load_psf(n, m, '/psf/psf_gaussian_256.tif') shifted = circshift(psf, m, n) S = fft.fft(shifted) S_original = fft.fft(psf) # Load image rand_V = Image.open('./image/cameraman.tif') rand_V = np.array(rand_V, dtype='float32') # Calculate noise image # rand_B = np.real(fft.ifft2(S * fft.fft2(rand_V))) rand_B = rand_V #fig1 plt.imshow(rand_B, cmap='gray') plt.title('With Gaussian') plt.show() noise = np.random.normal(scale=10, size=(m, n)) #fig2 plt.imshow(noise, cmap='gray') plt.title('Gaussian')