Lesson 6 of 15
Discrete Fourier Transform
Discrete Fourier Transform
The Discrete Fourier Transform (DFT) transforms a finite sequence of samples from the time domain into the frequency domain. It is the cornerstone of all digital signal processing.
Definition
Each output is a complex number representing the amplitude and phase of the frequency component at:
where is the sampling rate.
Magnitude Spectrum
The magnitude spectrum shows how much energy is at each frequency. The phase spectrum shows the phase offset.
Example
DFT of (a single impulse):
All frequency bins have equal magnitude — an impulse contains all frequencies equally.
DFT of :
A DC signal (constant) has energy only at frequency .
Your Task
Implement:
dft(x)— returns a list of complex numbersdft_magnitude(x)— returns
import math
def dft(x):
N = len(x)
X = []
for k in range(N):
val = 0 + 0j
for n in range(N):
val += x[n] * math.e ** (-2j * math.pi * k * n / N)
X.append(val)
return X
def dft_magnitude(x):
return [abs(v) for v in dft(x)]Python runtime loading...
Loading...
Click "Run" to execute your code.