SciPy FFTpack
SciPy FFTpack
Fourier transform computes a time-domain signal to examine its behavior in the frequency domain. Fourier transforms are used in signal and noise processing, image processing, audio signal processing, and other disciplines. SciPy provides the fftpack module, which allows users to compute fast Fourier transforms.
Below is an example of a sine function, which will be used to compute the Fourier transform using the fftpack module.
Fast Fourier Transform
Let’s take a closer look at what the Fast Fourier Transform is.
One-Dimensional Discrete Fourier Transform
The FFT y[k] of a sequence x[n] of length N is computed using ftt(), and the inverse transform is computed using ifft(). Let’s consider the following example.
#Importing the fft and inverse fft functions from the fft package
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 produce the following output.
[ 4.50000000+0.j 2.08155948-1.65109876j -1.83155948+1.60822041j
-1.83155948-1.60822041j 2.08155948+1.65109876j ]
Let’s look at another example.
#FFT is already in the workspace, using the same workspace for inverse transform
yinv = ifft(y)
print yinv
The above program will produce the following output.
[ 1.0+0.j 2.0+0.j 1.0+0.j -1.0+0.j 1.5+0.j ]
scipy.ftpack The module can compute the Fast Fourier Transform. 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
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 will be as follows –
1000
We don’t know the frequency of the signal; we only know the sampling time step of the signal sig. The signal should be derived from a real function, so the Fourier transform will be symmetric. The scipy.ftpack.ftfreq() function will generate the sampling frequency, and scipy.ftpack.ft() will calculate the Fast Fourier Transform.
Let’s 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 produce 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) represents a finite sequence of data points as the sum of cosine functions oscillating at different frequencies. SciPy provides a DCT function dct and a corresponding IDCT function idct. Let’s consider the following example.
from scipy.fftpack import dct
print dct(np.array([4., 3., 5., 10., 5., 3.]))
The above program will produce the following output.
array([ 60., -3.48476592, -13.85640646, 11.3137085, 6., -6.31319305])
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’s 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 produce the following output.
array([ 39.15085889, -20.14213562, -6.45392043, 7.13341236,
8.14213562, -3.83035081])