2

I would like to get a plot of how much each spatial frequency is present in a grayscale image.

I have been told to try np.fft.fft2 but apparently this is not what I need (according to this question). I was then told to look into np.fft.fftfreq - and while that sounds like what I need it will only take an integer as input, so

np.fft.fftfreq(np.fft.fft2(image)) 

won't work. Nor does:

np.fft.fftfreq(np.abs(np.fft.fft2(image)))

How else could I try to do this? it seems like a rather trivial task for a fourier transform. It's actually the task of the fourier transform. I don't understand why np.fft.fft2 doesn't have a flag to make the frequency analysis orientation-agnostic.

Community
  • 1
  • 1
TheChymera
  • 17,004
  • 14
  • 56
  • 86

1 Answers1

3

Maybe you should reread the comments in the linked question, as well as the documentation also posted in the last comment. You are supposed to pass the image shape to np.fft.fftfreq:

freqx = np.fft.fftfreq(image.shape[0])

for x-direction and

freqy = np.fft.fftfreq(image.shape[1])

for y-direction.

The results will be the centers of the frequency bins returned by fft2, for example:

image_fft = np.fft.fft2(image)

Then the frequency corresponding to the amplitude image_fft[i,j] is freqx[i] in x-direction and freqy[i] in y-direction.

Your last sentence indicates that you want to do something completely different, though. The Fourier transform of a two-dimensional input is by common definition also two-dimensional. What deviating definition do you want to use?

  • So to get the orientation agnostic frequency for `image_fft[i,j]` I would do `np.sqrt(freqx[i]^2+freqy[j]^2)`? how can I get an array with all the amplitudes of all frequencies without having to iterate over tens of millions of values? (this is what my last sentence was referring to). – TheChymera Feb 11 '14 at 14:09
  • 1. `^` should be `**`. In python `^` is bitwise XOR. 2. I'm not sure, whether this makes any sense in any context. 3. Iteration in python is pretty simple and if you want to reduce dimensions you need to iterate anyway to perform the reduction for example by averaging. I am still not sure what exactly your desired output is. It would help if you could provide the mathematical formula. –  Feb 11 '14 at 18:12
  • my desired output is a 2xN array where N is the number of discrete frequencies. [0,N] should correspond to the frequencies and [1,N] to the amplitudes of the respective frequencies. I want to plot a graphic which looks like [this](http://www.mathworks.com/help/releases/R2013b/matlab/math/periodogram2.gif). This should be the most basic and commonplace thing fft is used for. Why does nobody seem to understand this? – TheChymera Feb 12 '14 at 16:16