I am trying to take the FFT of some simple images generated procedurally, as below:
You'll notice the image on the left can be described by a wave with $frequency = 3$. This is indicated on the FFT by the white dots centrosymmetrically placed vertically about the origin. You'll notice annoying white stripes throughout the entire spectrum... what are these from? Is there something I do not understand about the FFT that is causing these? I have supplied my code below. Similarly, if I do a different wave, with say $frequency = 8$:
Why are there now x-axis points, even though the image is described entirely by a wave with $(h, k)$ indices equal to $(8, 0)$?
Thank you in advance! I love the FFT, I am happy to be schooled on it any day.
import numpy as np
from random import seed
from random import random
from mpl_toolkits import mplot3d
import matplotlib.pyplot as plt
from scipy.fftpack import fft2
from PIL import Image
#Globals
resolution = 50
#Wave generator
def waveGenerator(h, k, a, phi, res):
mesh = np.fromfunction(lambda x, y: a*np.sin(2*np.pi*h*x/res + 2*np.pi*k*y/res + phi), shape = (res,res))
return mesh
#Make a blank image, 1000x1000
img = np.zeros((resolution, resolution))
# A specific wave...
img = np.add(img, waveGenerator(8, 0, 1, 0, resolution))
#Take the fft of the image
img_fft = fft2(img)
fshift = np.fft.fftshift(img_fft)
magnitude_spectrum = np.log(np.abs(fshift))
plt.subplot(2,2,1)
plt.title('Wave Summation')
plt.imshow(img, cmap="gray")
plt.subplot(2,2,2)
plt.title('Magnitude Spectrum')
plt.imshow(magnitude_spectrum, cmap="gray")
plt.show()
EDIT
Changed the code as per the accepted answer, here is the result. Thank you so much!



You're taking the logarithm of the absolute value of the Fourier coefficients, which diverges as the Fourier coefficients go to zero. So your greyscale plots are just showing the coefficients that are closest to zero because they have the lowest rounding errors. Things look much better with
magnitude_spectrum = np.log(np.abs(fshift) + 1).