且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

解释numpy.fft.fft2输出

更新时间:2022-03-03 16:33:52

freq具有一些非常大的值,而有许多小的值.您可以通过绘制

freq has a few very large values, and lots of small values. You can see that by plotting

plt.hist(freq.ravel(), bins=100)

(请参见下文.)因此,当您使用

(See below.) So, when you use

ax1.imshow(freq, interpolation="none")

Matplotlib使用freq.min()作为颜色范围内的最小值(默认为蓝色),并使用freq.max()作为颜色范围内的最大值(默认为红色).由于freq中的几乎所有值都接近蓝色末端,因此该图总体上看起来是蓝色的.

Matplotlib uses freq.min() as the lowest value in the color range (which is by default colored blue), and freq.max() as the highest value in the color range (which is by default colored red). Since almost all the values in freq are near the blue end, the plot as a whole looks blue.

通过重新缩放freq中的值,可以获得更有用的图,从而使较低的值在颜色范围内更广泛地分布.

You can get a more informative plot by rescaling the values in freq so that the low values are more widely distributed on the color range.

例如,通过采用freqlog,可以更好地分配值. (您可能不想放弃最高值,因为它们对应于具有最高功率的频率.)

For example, you can get a better distribution of values by taking the log of freq. (You probably don't want to throw away the highest values, since they correspond to frequencies with the highest power.)

import matplotlib as ml
import matplotlib.pyplot as plt
import numpy as np
import Image
file_path = "data"
image = np.asarray(Image.open(file_path).convert('L'))
freq = np.fft.fft2(image)
freq = np.abs(freq)

fig, ax = plt.subplots(nrows=2, ncols=2, figsize=(14, 6))
ax[0,0].hist(freq.ravel(), bins=100)
ax[0,0].set_title('hist(freq)')
ax[0,1].hist(np.log(freq).ravel(), bins=100)
ax[0,1].set_title('hist(log(freq))')
ax[1,0].imshow(np.log(freq), interpolation="none")
ax[1,0].set_title('log(freq)')
ax[1,1].imshow(image, interpolation="none")
plt.show()

来自文档:>

类似于fft的输出包含零频率项 在转换轴的低阶角,

The output, analogously to fft, contains the term for zero frequency in the low-order corner of the transformed axes,

因此,freq[0,0]是零频率"术语.换句话说,它是离散傅里叶变换中的常数项.

Thus, freq[0,0] is the "zero frequency" term. In other words, it is the constant term in the discrete Fourier Transform.