Fourier Transformation in Machine Learning (With Python Examples)

The Fourier transformation is a fundamental feature transformation technique used in signal processing and analysis, as well as in machine learning, image processing, and many other fields.

This mathematical technique enables the conversion of a signal from its time or space domain to its frequency domain, revealing the frequency components that make up the signal. In this article, we will explore the Fourier transformation in detail, including its history, mathematical foundations, and applications.

Mathematical Foundations of Fourier Transformation

The Fourier transform converts a signal from the time or space domain to the frequency domain. It is a complex-valued function that maps a continuous, time-varying signal to a continuous, frequency-varying function. The Fourier transform is defined as follows:


where F(ω) is the Fourier transform of the signal f(t), ω is the frequency variable, i is the imaginary unit, and t is the time variable.

The Fourier transform has many important properties, including linearity, shift invariance, and duality. These properties make it a powerful tool for analyzing signals and systems.

Applications of Fourier Transformation

The Fourier transformation has numerous applications in science, engineering, and technology. Here are a few examples:

  • Signal Processing
  • Image Processing
  • Quantum Mechanics
  • Speech Recognition

Signal Processing

The Fourier transform is used extensively in signal processing to analyze and filter signals. It can be used to extract specific frequency components from a signal, remove noise, and compress data.

Image Processing

The Fourier transform is used in image processing to analyze and enhance images. It can be used to identify features in an image, remove noise, and compress data.

Quantum Mechanics

The Fourier transform plays a crucial role in quantum mechanics, where it is used to describe the wave functions of particles.

Speech Recognition

The Fourier transform is used in speech recognition to convert audio signals into frequency components that can be analyzed and classified.

Fourier Transform with SciPy FFT

The scipy.fft method is a function in the SciPy library that computes the one-dimensional n-point discrete Fourier Transform (DFT) of a real or complex sequence using the Fast Fourier Transform (FFT) algorithm. This method provides a fast and efficient way to perform Fourier transformations in Python, making it a popular tool for signal processing and scientific computing. The scipy.fft method can also handle multi-dimensional arrays and offers a variety of options for handling edge cases and performing various types of Fourier transforms.

Python Code Examples

Fourier transformation with scipy

import numpy as np
from scipy.fft import fft
import matplotlib.pyplot as plt
#create a signal
t = np.linspace(0, 2 * np.pi, 1000, endpoint=False)
signal = np.sin(5 * t) + np.sin(10*t)

#apply Fourier transformation
fourier_transform = fft(signal)

#plot the signal and its Fourier transformation
fig, ax = plt.subplots(2, 1)
ax[0].plot(t, signal)
ax[0].set_xlabel('Time (s)')
ax[0].set_ylabel('Amplitude')
ax[1].plot(np.abs(fourier_transform))
ax[1].set_xlabel('Frequency (Hz)')
ax[1].set_ylabel('Amplitude')
plt.show()

This code generates a signal consisting of two sine waves with frequencies of 5 Hz and 10 Hz. Then, it applies the Fourier transformation to the signal using the fft function from the scipy.fft module. Finally, it plots the original signal and its Fourier transformation using the plot function from the matplotlib.pyplot module.

Other Python Example

from scipy.fft import fft, fftfreq
import numpy as np
# Number of sample points
N = 600
# sample spacing
T = 1.0 / 800.0
x = np.linspace(0.0, N*T, N, endpoint=False)
y = np.sin(50.0 * 2.0*np.pi*x) + 0.5*np.sin(80.0 * 2.0*np.pi*x)
yf = fft(y)
xf = fftfreq(N, T)[:N//2]
import matplotlib.pyplot as plt
plt.plot(xf, 2.0/N * np.abs(yf[0:N//2]))
plt.grid()
plt.show()

Useful Python Libraries for Fourier transformation

  • NumPy: numpy.fft
  • SciPy: scipy.fft
  • PyTorch: torch.fft
  • TensorFlow: tf.signal.fft
  • OpenCV: cv2.dft
  • Matplotlib: matplotlib.pyplot.fft

Datasets useful for Fourier transformation

Scipy’s lena image


from scipy import misc
lena = misc.lena()

Scikit-image’s astronaut image


from skimage import data
astro = data.astronaut()

Audio signal from Scipy’s signal library


from scipy import signal
#generate a chirp signal
t = np.linspace(0, 10, 5000, endpoint=False)
signal = signal.chirp(t, 20, 500, 3000, method='linear')

To Know Before You Learn Fourier transformation?

  • Basic understanding of calculus and complex numbers
  • Understanding of signal processing concepts such as time-domain and frequency-domain signals
  • Familiarity with Python programming language and relevant libraries like NumPy, SciPy, and Matplotlib
  • Understanding of linear algebra concepts like vectors and matrices
  • Familiarity with machine learning concepts and their applications in signal processing.
  • Data transformation basics

Important Concepts in Fourier transformation

  • Frequency domain
  • Time domain
  • Fourier series
  • Fourier transform
  • Inverse Fourier transform
  • Sampling theorem
  • Nyquist frequency
  • Power spectral density
  • Discrete Fourier transform

What’s Next?

  • Feature Transformation
  • Feature engineering
  • Signal Processing and Filtering
  • Fourier Series and Harmonic Analysis
  • Spectral Analysis and Power Spectrum Estimation
  • Wavelets and Wavelet Transforms
  • Image Processing and Computer Vision Techniques.

Relevant entities

EntitiesProperties
Fourier transformationConverts signal from time domain to frequency domain
Discrete Fourier transformationUsed for discrete signals and finite signal lengths
Fast Fourier transformationEfficient algorithm for computing Fourier transformation
Frequency spectrumGraphical representation of signal in frequency domain
Power spectrumGraphical representation of signal power in frequency domain
Window functionFunction used to reduce spectral leakage in Fourier transformation

Sources:

  • https://en.wikipedia.org/wiki/Fourier_transform
  • https://www.mathworks.com/help/matlab/ref/fft.html
  • https://docs.scipy.org/doc/scipy/reference/tutorial/fft.html
  • https://towardsdatascience.com/fast-fourier-transform-937926e591cb
  • https://www.kaggle.com/residentmario/fourier-transforms-with-numpy
  • https://www.analyticsvidhya.com/blog/2021/04/fourier-transform-in-machine-learning-a-primer/
  • https://towardsdatascience.com/exploring-signals-using-fourier-transform-7b5bf5f05d7c
  • https://www.sciencedirect.com/topics/engineering/fourier-transform

Conclusion

The Fourier transformation is a powerful mathematical tool that plays a critical role in signal processing, image processing, and many other fields. Understanding the Fourier transform is essential for anyone working with signals and systems, and it is a fundamental concept in the field of machine learning. Whether you are analyzing sound waves, enhancing images, or exploring the wave functions of particles, the Fourier transform is an indispensable tool for gaining insights into the underlying structure of the data.