Discrete Fourier Transform (DFT) using Scipy (original) (raw)

Last Updated : 23 Jul, 2025

The Discrete Fourier Transform (DFT) is a powerful mathematical tool used in signal processing, data analysis, image processing and many other fields. It transforms a sequence of values into components of different frequencies revealing the signal's frequency content. In Python DFT is commonly computed using SciPy which provides a simple interface to fast and efficient Fourier transforms.

The DFT converts a finite sequence of equally spaced time domain samples into a sequence of frequency domain components. Mathematically, for a sequence of N complex numbers x0, x1,..., xN-1 the DFT is defined as:

X_k = \sum_{n=0}^{N-1} x_n \cdot e^{-2\pi i kn / N}, \quad \text{for } k = 0, 1, \ldots, N-1

Where,

Implementation

Step 1: Importing necessary libraries

import numpy as np import matplotlib.pyplot as plt from scipy.fft import fft, ifft, fftfreq

`

Step 2: Create the Time Domain Signal

fs = 1000
T = 1.0
t = np.linspace(0, T, int(fs * T), endpoint=False) signal = np.sin(2 * np.pi * 50 * t) + 0.5 * np.sin(2 * np.pi * 120 * t)

`

Step 3: Apply Discrete Fourier Transform (DFT)

N = len(t) yf = fft(signal) xf = fftfreq(N, 1/fs)
xf = xf[:N//2] yf = 2.0/N * np.abs(yf[:N//2])

`

Step 4: Plot the Frequency Spectrum

plt.figure(figsize=(10, 4)) plt.plot(xf, yf) plt.title("Frequency Spectrum") plt.xlabel("Frequency (Hz)") plt.ylabel("Amplitude") plt.grid(True) plt.show()

`

**Output:

output

Frequency Spectrum