Checkerboard pattern after FFT

I am trying to create a Gaussian random field by creating a mesh in Fourier space and then the inverse Fourier, iterating over it to get a random field. For this, the inverse Fourier transform image must be real. It seems that I get the residuals in the imaginary part of the grid of the order of 10 ^ -18 - -22, so I expected that these would be numerical errors in the FFT. The real part of the image displays a strange checkerboard pattern in pixels, although the pixels go from positive to negative. To check if the FFT is functioning properly, I tried to convert the Gaussian language, which should return another Gaussian, and again the image of the chessboard is present in the image. When you take the absolute value of the image, it looks great, but I also needso that it takes into account negative values ​​for my Gaussian random field.

To Fourier transform the Gaussian, I use the following code:

#! /usr/bin/env python

import numpy as n
import math as m
import pyfits


def fourierplane(a):
  deltakx = 2*a.kxmax/a.dimkx #stepsize in k_x
  deltaky = 2*a.kymax/a.dimky #stepsize in k_y

  plane = n.zeros([a.dimkx,a.dimky]) #empty matrix to be filled in for the Fourier grid

  for y in range(n.shape(plane)[0]):
    for x in range(n.shape(plane)[1]):
      #Defining coordinates centred at x = N/2, y = N/2
      i1 = x - a.dimkx/2 
      j1 = y - a.dimky/2

      #creating values to fill in in the grid:    
      kx = deltakx*i1  #determining value of k_x at gridpoint
      ky = deltaky*j1  #determining value of k_y at gridpoint
      k = m.sqrt(kx**2 + ky**2) #magnitude of k-vector


      plane[y][x] = m.e**(-(k**2)/(2*a.sigma_k**2)) #gaussian
  return plane

def substruct():

  class fougrid:
    pass

  grid = fougrid()

  grid.kxmax = 2.00 #maximum value k_x
  grid.kymax = 2.00 #maximum value k_y

  grid.sigma_k = (1./20.)*grid.kxmax #width of gaussian

  grid.dimkx = 1024
  grid.dimky= 1024

  fplane = fourierplane(grid) #creating the Fourier grid

  implane = n.fft.ifftshift(n.fft.ifft2(fplane)) #inverse Fourier transformation of the grid to get final image

  ##################################################################
  #seperating real and imaginary part of the Fourier transformed grid
  ##################################################################

  realimplane = implane.real
  imagimplane = implane.imag

  #taking the absolute value:
  absimplane = n.zeros(n.shape(implane))
  for a in range(n.shape(implane)[0]):
    for b in range(n.shape(implane)[1]):
      absimplane[a][b] = m.sqrt(implane[a][b].real**2 + implane[a][b].imag**2)

  #saving images to files:
  pyfits.writeto('randomfield.fits',realimplane) #real part of the image grid
  pyfits.writeto('fplane.fits',fplane) #grid in fourier space
  pyfits.writeto('imranfield.fits',imagimplane) #imaginary part of the image grid
  pyfits.writeto('absranfield.fits',absimplane) #real part of the image grid

substruct() #running the script

Does anyone know how to create this template and how to solve this problem?

+3
source share
1 answer

Whenever you see unexpected variable characters in one DFT domain, this may mean that the data in the other DFT domain has been rotated half through the array (similar to fftshift). If you have a symmetrical "hump" of real values ​​in one domain, then centering this hump on an element of array 0 (instead of an element of array n / 2) will be a state that, most likely, will not lead to the appearance of alternating characters in the transformation domain.

+1
source

All Articles