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:
Where,
: The kth frequency component.
: The nth time domain sample.
: Total number of samples.
: The imaginary unit.
: Complex exponential
Implementation
Step 1: Importing necessary libraries
numpy is used for numerical operations and creating the time domain signal.
matplotlib.pyplot is used for plotting the signal and its frequency spectrum.
scipy.fft provides: fft: Fast Fourier Transform (computes the DFT), ifft: Inverse FFT (reconstructs time domain signal), fft freq: Computes the frequency bins for plotting.
Step 2: Create the Time Domain Signal
fs = 1000: The signal is sampled at 1000 Hz (1000 samples per second).
T = 1.0: Total duration of the signal is 1 second.
t: A time array of 1000 evenly spaced values between 0 and 1 second (not including 1).
Step 3: Apply Discrete Fourier Transform (DFT)
N = len(t): Number of samples (1000 in this case).
fft(signal): Computes the Fast Fourier Transform of the signal converts it from time domain to frequency domain.
fftfreq(N, 1/fs): Generates the corresponding frequency values for plotting.
np.abs(...): Computes magnitude (amplitude) of complex FFT results.
2.0/N: Normalization factor to scale the amplitude correctly.
Step 4: Plot the Frequency Spectrum
plt.figure(figsize=(10, 4)): Sets the size of the plot.
plt.plot(xf, yf): Plots amplitude vs. frequency and shows which frequencies are present in the signal.
Labels and title clarify the graph.
plt.grid(True): Adds grid lines for easier reading.