Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Python Fourier transform with Python

  1. Jan 13, 2016 #1

    My name is Thibaut. I am looking to improve my code in python in order to have a better look a my fourier transform. as you can see on the image, we barely see any detail of the peaks on the image. Also it's not centred. the zero order peak in on the corner, not in the centre.

    Any idea how to fix it?


    image: click here figure_1.png

    Code (Python):

    import numpy as np
    import matplotlib.pyplot as plt

    x = np.linspace(-100, 100.0, 201.0, endpoint = True)
    y = np.linspace(-100, 100.0, 201.0, endpoint = True)
    xx, yy = np.meshgrid(x, y, sparse=True)

    def f1(a,b):
        return np.piecewise(a, [abs(a) < 2, abs(a) >= 2], [1, 0]) * np.piecewise(b, [abs(b) < 2, abs(b) >= 2], [1, 0])
    def f2(r,c):
        for ii in range (r):
            for jj in range(c):
               if (ii%spacing)==0 and (jj%spacing)==0 and abs(ii-100)<55 and abs(jj-100)<55:
        return a

    h = np.fft.fft2(f1(xx,yy))
    g = np.fft.fft2(f2(201,201))


  2. jcsd
  3. Jan 13, 2016 #2
    It is good practice to read the documentation behind some of these "ready-to-use" functions when using them.

    By default, the Discrete Fast Fourier Transform in Numpy returns the components in standard order, which contains the zero-frequency term first, followed by the positive frequency terms (in increasing order) up till the Nyquist frequency and finally the negative frequency terms (in decreasing negative order). The reason for this default behaviour is due to the way the FFT algorithm works, if I'm not mistaken. With this knowledge, you can re-order the array yourself, or use the in-built fftshift function.

    As for your visualisation, you can always change the colour map to one with better contrast for your data range. The details can be easily found on the matplotlib website.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook