0
0
RosConceptBeginner · 4 min read

What is DFT (Discrete Fourier Transform)? Simple Explanation & Example

The Discrete Fourier Transform (DFT) is a mathematical method that converts a sequence of values from the time domain into the frequency domain. It helps to find the different frequencies present in a signal by breaking it down into sine and cosine components.
⚙️

How It Works

The Discrete Fourier Transform (DFT) takes a list of numbers, which represent a signal over time, and changes it into a list of numbers that represent the signal's frequencies. Imagine listening to a song and wanting to know which musical notes are playing; the DFT helps find those notes by analyzing the signal.

It works by multiplying the signal by sine and cosine waves of different frequencies and summing the results. This process shows how much of each frequency is present in the original signal. Think of it like shining different colored lights on an object to see which colors reflect back the most.

💻

Example

This example shows how to compute the DFT of a simple signal using Python's numpy library. The signal is a combination of two sine waves with different frequencies.

python
import numpy as np
import matplotlib.pyplot as plt

# Create a sample signal: sum of two sine waves
fs = 100  # Sampling frequency (samples per second)
t = np.arange(0, 1, 1/fs)  # Time vector for 1 second
freq1 = 5  # Frequency of first sine wave (Hz)
freq2 = 20  # Frequency of second sine wave (Hz)
signal = np.sin(2 * np.pi * freq1 * t) + 0.5 * np.sin(2 * np.pi * freq2 * t)

# Compute the DFT using numpy's FFT function
# FFT is a fast algorithm to compute DFT
dft = np.fft.fft(signal)

# Frequency bins
freqs = np.fft.fftfreq(len(signal), 1/fs)

# Take only the positive half of frequencies and magnitudes
positive_freqs = freqs[:len(freqs)//2]
magnitude = np.abs(dft)[:len(freqs)//2]

# Print first 10 frequency components and their magnitudes
for f, mag in zip(positive_freqs[:10], magnitude[:10]):
    print(f"Frequency: {f:.1f} Hz, Magnitude: {mag:.2f}")

# Plot the magnitude spectrum
plt.stem(positive_freqs, magnitude, use_line_collection=True)
plt.xlabel('Frequency (Hz)')
plt.ylabel('Magnitude')
plt.title('DFT Magnitude Spectrum')
plt.show()
Output
Frequency: 0.0 Hz, Magnitude: 50.00 Frequency: 1.0 Hz, Magnitude: 0.00 Frequency: 2.0 Hz, Magnitude: 0.00 Frequency: 3.0 Hz, Magnitude: 0.00 Frequency: 4.0 Hz, Magnitude: 0.00 Frequency: 5.0 Hz, Magnitude: 50.00 Frequency: 6.0 Hz, Magnitude: 0.00 Frequency: 7.0 Hz, Magnitude: 0.00 Frequency: 8.0 Hz, Magnitude: 0.00 Frequency: 9.0 Hz, Magnitude: 0.00
🎯

When to Use

Use the DFT when you want to understand the frequency content of a signal. It is very useful in audio processing to find musical notes, in communications to analyze signals, and in image processing to detect patterns.

For example, engineers use DFT to check if a machine is vibrating at unusual frequencies, which can indicate a problem. Musicians use it to tune instruments by analyzing sound waves. It is also the foundation for many modern technologies like MP3 compression and wireless communication.

Key Points

  • The DFT converts time-based signals into frequency components.
  • It shows how much of each frequency is present in the signal.
  • It is computed using sums of sine and cosine waves.
  • Fast algorithms like FFT make DFT practical for real-world use.
  • It is widely used in audio, communications, and engineering diagnostics.

Key Takeaways

The DFT breaks down a signal into its frequency parts to analyze what frequencies are present.
It works by multiplying the signal with sine and cosine waves and summing the results.
Fast algorithms like FFT make computing the DFT efficient for practical use.
DFT is essential in fields like audio processing, communications, and machine diagnostics.
Understanding DFT helps in analyzing and processing signals in many real-world applications.