Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Discrete Fourier Transform

Today’s exercise focuses on the implementation of the Discrete Fourier Transform (DFT). First, implement the transform directly from its definition. Then, use its separability to reduce the computational complexity. In the next lesson, we will implement the inverse transform.

The Fourier Transform computes the frequency spectrum of a given input image . This spectrum is denoted by ; it is a complex matrix with the same dimensions as the input image. Throughout this exercise, is the number of rows and is the number of columns; the first matrix index denotes a row.

Direct 2D DFT

The basis function is defined as

To compute the basis function, it is useful to use Euler’s formula

For each of the output coefficients, the direct implementation sums over all input pixels. It therefore uses four nested loops and has complexity . For a square image of size , this is .

Separable 2D DFT

The exponential in Equation (2) can be written as a product:

We can therefore compute the same transform in two passes. First, transform each row and store the results in a complex intermediate matrix :

Then, transform each column of :

The row transform does not depend on . Computing it once and reusing it for all avoids repeated work in the direct implementation. The normalization factors multiply to , so both implementations compute the same spectrum.

Implementing the 1D transform

Implement a function that accepts a vector of complex numbers and returns its 1D DFT. For a vector of length , use

The function must accept complex input: the column pass operates on complex values produced by the row pass. Real image pixels can be represented as complex values with a zero imaginary part.

function DFT1D(input):
    L = length(input)
    output = complex vector of length L

    for q = 0 .. L-1:
        sum = 0 + 0i
        for p = 0 .. L-1:
            angle = -2 * pi * p * q / L
            sum += input[p] * (cos(angle) + i * sin(angle))
        output[q] = sum / sqrt(L)

    return output

Use this function to build the 2D transform:

function DFT2D_SEPARABLE(image):
    M = number of rows in image
    N = number of columns in image
    G = complex matrix of size M x N
    F = complex matrix of size M x N

    for m = 0 .. M-1:
        G[m, :] = DFT1D(image[m, :])

    for l = 0 .. N-1:
        F[:, l] = DFT1D(G[:, l])

    return F

Here, A[r, :] denotes an entire row and A[:, c] denotes an entire column. If your matrix representation does not support these operations, copy the row or column into a temporary vector and write the transformed values back.

A 1D DFT of length has complexity . The row pass costs , and the column pass costs . The total complexity is therefore

For a square image, the two passes perform summation terms, giving complexity . This is still a DFT computed from its definition, not an FFT. No power-of-two dimensions are required.

Spectrum Visualization

The result can be split into real and imaginary parts:

The spectrum amplitude is computed as follows:

Compute the phase using atan2 to handle all quadrants and coefficients whose real part is zero:

The phase of a zero coefficient is undefined; use zero for its visualization.

The power spectrum can be computed as

To display the power spectrum, compute and then normalize the resulting values to the interval . If all values are equal, display zeros to avoid division by zero during normalization.

For a conventional visualization, move the zero-frequency coefficient from the top-left corner to the center. For even dimensions, swap the first and third quadrants, and also the second and fourth quadrants. For arbitrary dimensions, use the equivalent circular shift by rows and columns toward increasing indices (fftshift). Apply the same shift to both the real and imaginary parts. Keep the original, unshifted complex spectrum for reconstruction; the shifted spectrum will also be useful later when applying filters.

Hint: Use double for image pixels and scalar calculations, and complex numbers with double real and imaginary components for the spectrum and intermediate results (for example, std::complex<double> in C++).

Tasks and Verification

  1. Implement the direct 2D DFT using Equations (1) and (2).
  2. Implement the complex 1D DFT and use it to transform all rows, followed by all columns. Implement the summation yourself; do not use a library DFT or FFT routine.
  3. Compare the complex spectra of both implementations before shifting or visualization. Report the maximum absolute difference . Small differences caused by floating-point rounding are expected.
  4. Test a small rectangular image (for example, ) to check row and column indexing. Also test a constant image: for pixel value , the only nonzero coefficient should be , up to rounding errors.
  5. Display the amplitude, phase, and logarithmic power spectrum using the separable result.
  6. Measure the execution time of both transforms on square images of sizes , , , and . Exclude loading and visualization from the measurement. Report the times in a table and explain the observed growth. Larger sizes are optional because the direct implementation becomes expensive.

When the side length doubles, the theoretical work grows by a factor of for the direct implementation and for the separable implementation. Actual timings also depend on implementation details and hardware.

Expected Output

Expected output