Scipy 简明教程
SciPy - FFTpack
对时域信号计算 Fourier Transformation ,以检查其在频域中的行为。傅立叶变换在信号和噪声处理、图像处理、音频信号处理等学科中都有应用。SciPy 提供 fftpack 模块,使用户可以计算快速傅立叶变换。
Fourier Transformation is computed on a time domain signal to check its behavior in the frequency domain. Fourier transformation finds its application in disciplines such as signal and noise processing, image processing, audio signal processing, etc. SciPy offers the fftpack module, which lets the user compute fast Fourier transforms.
以下是正弦函数的一个示例,将使用 fftpack 模块计算傅立叶变换。
Following is an example of a sine function, which will be used to calculate Fourier transform using the fftpack module.
Fast Fourier Transform
让我们详细了解一下什么是快速傅立叶变换。
Let us understand what fast Fourier transform is in detail.
One Dimensional Discrete Fourier Transform
长度为 N 的序列 x[n] 的长度为 N 的 FFT y[k] 由 fft() 计算,逆变换由 ifft() 计算。让我们考虑以下示例
The FFT y[k] of length N of the length-N sequence x[n] is calculated by fft() and the inverse transform is calculated using ifft(). Let us consider the following example
#Importing the fft and inverse fft functions from fftpackage
from scipy.fftpack import fft
#create an array with random n numbers
x = np.array([1.0, 2.0, 1.0, -1.0, 1.5])
#Applying the fft function
y = fft(x)
print y
上述程序将生成以下输出。
The above program will generate the following output.
[ 4.50000000+0.j 2.08155948-1.65109876j -1.83155948+1.60822041j
-1.83155948-1.60822041j 2.08155948+1.65109876j ]
我们来看另一个示例
Let us look at another example
#FFT is already in the workspace, using the same workspace to for inverse transform
yinv = ifft(y)
print yinv
上述程序将生成以下输出。
The above program will generate the following output.
[ 1.0+0.j 2.0+0.j 1.0+0.j -1.0+0.j 1.5+0.j ]
scipy.fftpack 模块允许计算快速傅立叶变换。例如,(有噪声的)输入信号可能如下所示 −
The scipy.fftpack module allows computing fast Fourier transforms. As an illustration, a (noisy) input signal may look as follows −
import numpy as np
time_step = 0.02
period = 5.
time_vec = np.arange(0, 20, time_step)
sig = np.sin(2 * np.pi / period * time_vec) + 0.5 *np.random.randn(time_vec.size)
print sig.size
我们正在创建一个时间步长为 0.02 秒的信号。最后一条语句打印信号 sig 的大小。输出如下所示 −
We are creating a signal with a time step of 0.02 seconds. The last statement prints the size of the signal sig. The output would be as follows −
1000
我们不知道信号频率;我们只知道信号 sig 的采样时间步长。该信号应该来自一个真实函数,因此傅立叶变换是对称的。 scipy.fftpack.fftfreq() 函数将生成采样频率, scipy.fftpack.fft() 将计算快速傅立叶变换。
We do not know the signal frequency; we only know the sampling time step of the signal sig. The signal is supposed to come from a real function, so the Fourier transform will be symmetric. The scipy.fftpack.fftfreq() function will generate the sampling frequencies and scipy.fftpack.fft() will compute the fast Fourier transform.
让我们通过一个示例来理解这一点。
Let us understand this with the help of an example.
from scipy import fftpack
sample_freq = fftpack.fftfreq(sig.size, d = time_step)
sig_fft = fftpack.fft(sig)
print sig_fft
上述程序将生成以下输出。
The above program will generate the following output.
array([
25.45122234 +0.00000000e+00j, 6.29800973 +2.20269471e+00j,
11.52137858 -2.00515732e+01j, 1.08111300 +1.35488579e+01j,
…….])
Discrete Cosine Transform
Discrete Cosine Transform (DCT) 用在不同频率下振荡的余弦函数之和表示有限数据点的序列。SciPy 提供了带有函数 dct 的 DCT 和带有函数 idct 的相应的 IDCT。让我们考虑以下示例。
A Discrete Cosine Transform (DCT) expresses a finite sequence of data points in terms of a sum of cosine functions oscillating at different frequencies. SciPy provides a DCT with the function dct and a corresponding IDCT with the function idct. Let us consider the following example.
from scipy.fftpack import dct
print dct(np.array([4., 3., 5., 10., 5., 3.]))
上述程序将生成以下输出。
The above program will generate the following output.
array([ 60., -3.48476592, -13.85640646, 11.3137085, 6., -6.31319305])
离散余弦逆变换从其离散余弦变换 (DCT) 系数重建一个序列。idct 函数是 dct 函数的逆函数。让我们通过以下示例来理解这一点。
The inverse discrete cosine transform reconstructs a sequence from its discrete cosine transform (DCT) coefficients. The idct function is the inverse of the dct function. Let us understand this with the following example.
from scipy.fftpack import dct
print idct(np.array([4., 3., 5., 10., 5., 3.]))
上述程序将生成以下输出。
The above program will generate the following output.
array([ 39.15085889, -20.14213562, -6.45392043, 7.13341236,
8.14213562, -3.83035081])