v9.1.1
Menu

1D DFT

mediumFREEFourier TransformsSignal ProcessingCV: Image Processing

Implement a 1D Discrete Fourier Transform (DFT) to decompose a signal into its frequency components. This task involves computing the DFT of a given signal, which is a fundamental concept in signal processing and image analysis.

The DFT is a mathematical operation that transforms a discrete-time signal into a discrete-frequency representation, allowing us to analyze the signal in the frequency domain. The DFT is based on the idea of representing a signal as a sum of sinusoids with different frequencies, amplitudes, and phases. The DFT can be used to extract features from signals, such as the magnitude of each frequency component.

To compute the DFT, we can follow these steps:

  1. Define the input signal x[n]x[n] and its length NN.
  2. Iterate over each frequency component kk.
  3. For each kk, compute the sum of the products of the signal x[n]x[n] and the complex exponential e−2πikn/Ne^{-2\pi i kn/N} over all nn.
X[k]=∑n=0N−1x[n]⋅e−2πikn/NX[k] = \sum_{n=0}^{N-1} x[n] \cdot e^{-2\pi i kn/N}

This technique is widely used in image and signal processing applications.

Example

Input
dft_magnitude([1, 0, 1, 0])
Output
[2.0, 0.0, 2.0, 0.0]
Reasoning
  • The input signal is x=[1,0,1,0]x = [1, 0, 1, 0]: N=4N = 4 samples, so four bins k=0,1,2,3k = 0,1,2,3. Only n=0n = 0 and n=2n = 2 contribute, since x[1]=x[3]=0x[1] = x[3] = 0.
  • Each bin is X[k]=x[0] e0+x[2] e−2πik⋅2/4=1+e−πikX[k] = x[0]\,e^{0} + x[2]\,e^{-2\pi i k \cdot 2/4} = 1 + e^{-\pi i k}, and e−πike^{-\pi i k} alternates between +1+1 (even kk) and −1-1 (odd kk).
  • k=0k = 0: 1+1=21 + 1 = 2. k=1k = 1: 1+(−1)=01 + (-1) = 0. k=2k = 2: 1+e−2πi=21 + e^{-2\pi i} = 2. k=3k = 3: 1+e−3πi=1−1=01 + e^{-3\pi i} = 1 - 1 = 0.
  • The output is the magnitude ∣X[k]∣|X[k]| of each complex sum, rounded to 4 decimal places, giving [2.0, 0.0, 2.0, 0.0], one real number per bin.
  • Notice that the signal repeats every 2 samples, so all its energy sits at k=0k = 0 and k=N/2=2k = N/2 = 2 (the fastest possible oscillation). A constant signal like [1,1,1,1][1, 1, 1, 1] would put everything in k=0k = 0: [4,0,0,0][4, 0, 0, 0].

Constraints

  • Return magnitudes rounded to 4 decimal places

Learn this in

More from CV: Image Processing

solution.py

Test results

Sign in to run your code against the test cases.