0
0
RosHow-ToBeginner · 4 min read

How to Compute Fourier Transform: Simple Guide and Example

To compute the Fourier transform of a signal, you apply the integral formula that converts the signal from time domain to frequency domain. In practice, for digital signals, you use the Fast Fourier Transform (FFT) algorithm, which efficiently computes the discrete Fourier transform.
📐

Syntax

The Fourier transform of a continuous signal x(t) is defined as:

X(f) = ∫ x(t) · e-j2πft dt

where:

  • X(f) is the frequency domain representation
  • x(t) is the time domain signal
  • f is frequency
  • j is the imaginary unit

For digital signals, the Discrete Fourier Transform (DFT) is used, computed efficiently by FFT algorithms.

python
import numpy as np

def compute_fft(signal, sampling_rate):
    n = len(signal)
    freq = np.fft.fftfreq(n, d=1/sampling_rate)
    fft_values = np.fft.fft(signal)
    return freq, fft_values
💻

Example

This example shows how to compute the Fourier transform of a simple sine wave using Python's numpy library and plot its frequency spectrum.

python
import numpy as np
import matplotlib.pyplot as plt

# Parameters
sampling_rate = 1000  # samples per second
T = 1.0  # seconds
f_sin = 5  # frequency of sine wave in Hz

# Time array
x = np.linspace(0, T, int(sampling_rate * T), endpoint=False)

# Create sine wave signal
signal = np.sin(2 * np.pi * f_sin * x)

# Compute FFT
freq, fft_values = compute_fft(signal, sampling_rate)

# Take only positive frequencies
pos_mask = freq >= 0
freq = freq[pos_mask]
fft_values = fft_values[pos_mask]

# Plot magnitude spectrum
plt.plot(freq, np.abs(fft_values))
plt.title('Frequency Spectrum of Sine Wave')
plt.xlabel('Frequency (Hz)')
plt.ylabel('Magnitude')
plt.grid(True)
plt.show()
Output
A plot window showing a peak at 5 Hz in the frequency spectrum.
⚠️

Common Pitfalls

  • Not sampling enough: If the sampling rate is too low, frequencies above half the sampling rate (Nyquist frequency) will be misrepresented (aliasing).
  • Ignoring signal length: FFT works best with signals whose length is a power of two; otherwise, performance may degrade.
  • Not using absolute values: The FFT output is complex; to get magnitude, use the absolute value.
  • Misinterpreting frequency bins: Frequency values from FFT depend on sampling rate and signal length; always compute frequency axis correctly.
python
import numpy as np

# Wrong: Not computing frequency axis
signal = np.sin(2 * np.pi * 5 * np.linspace(0, 1, 1000))
fft_vals = np.fft.fft(signal)
# This alone does not tell frequency

# Right: Compute frequency axis
freqs = np.fft.fftfreq(len(signal), d=1/1000)

# Use absolute value for magnitude
magnitude = np.abs(fft_vals)
📊

Quick Reference

Fourier Transform Tips:

  • Use np.fft.fft() for fast discrete Fourier transform.
  • Calculate frequency bins with np.fft.fftfreq().
  • Use np.abs() to get magnitude spectrum.
  • Sample at least twice the highest frequency in your signal (Nyquist rate).
  • Window your signal if needed to reduce spectral leakage.

Key Takeaways

Use FFT to efficiently compute the Fourier transform of digital signals.
Always calculate the frequency axis to interpret FFT results correctly.
Sample your signal at a rate at least twice the highest frequency to avoid aliasing.
FFT output is complex; use absolute values to get the magnitude spectrum.
Signal length and windowing affect the accuracy of the Fourier transform.