Skip to content

BazeJared/DFT-Experiments

Repository files navigation

Discrete Fourier Transform Experiments

A Python exploration of the 1-D and 2-D Discrete Fourier Transform, demonstrating spectrum analysis, centering via the shift property, and the inverse relationship between spatial extent and frequency spread. Includes experiments on simple signals, cosine waves, rectangular pulses, and square images of varying size.


Overview

This project implements and visualizes DFT behavior through two main experiment groups:

Experiment 1 — 1-D DFT Analysis: Computes the forward and inverse DFT on three signal types, plotting real, imaginary, magnitude, and phase components with centered frequency axes.

  • (a) A short discrete signal f = [2, 3, 4, 4] to validate the DFT/IDFT round-trip
  • (b) A cosine wave with 8 cycles over 128 samples, demonstrating symmetric spectral peaks at ±u and near-zero imaginary component
  • (c) A 128-sample rectangular pulse loaded from Rect_128.txt, producing the characteristic sinc-shaped magnitude spectrum

Experiment 2 — 2-D DFT of Square Images: Generates centered white squares (32×32, 64×64, 128×128) inside a 512×512 image and computes the 2-D DFT using a separable row-column approach. Displays both unshifted and centered ((-1)^(x+y) shifted) log-magnitude spectra, showing how larger spatial objects produce narrower frequency spreads.

Key Concepts Demonstrated

Concept Where
DFT / IDFT round-trip reconstruction Experiment 1(a)
Spectral peaks of a pure cosine Experiment 1(b)
Shift property: f(x)(-1)^x ↔ F(u - N/2) Experiments 1(b), 1(c), 2
Rect ↔ Sinc duality Experiment 1(c)
Separable 2-D DFT (row-column decomposition) Experiment 2
Inverse spatial-frequency size relationship Experiment 2(a–c)
Log-magnitude display: `log(1 + F(u,v)

Usage

Requirements

pip install numpy matplotlib

Running

Place Rect_128.txt in the same directory as main.py, then:

python main.py

This opens interactive matplotlib windows for each experiment. The experiments run sequentially: 1(a) → 1(b) → 1(c) → 2(a) → 2(b) → 2(c). Close each plot window to advance to the next.

Test Data

File Description
Rect_128.txt 128-sample rectangular pulse (64 ones padded with zeros on both sides)

Key Functions

experiment1a(f)                                    # 1-D DFT of arbitrary signal with centered spectrum plots
experiment1b(u, N)                                 # Cosine wave generation + DFT with shift property demo
experiment1c()                                     # Rectangular pulse DFT showing sinc-like magnitude
two_D_DFT(N, M, real_Fuv, imag_Fuv, isign)        # Separable row-column 2-D DFT (isign: 1=forward, -1=inverse)
experiment2(N, square_size)                        # 2-D DFT of centered square with shifted/unshifted spectra

Experiment Parameters

Experiment 1(b) — Cosine Wave

Parameter Value
Frequency (u) 8 cycles
Samples (N) 128

Experiment 2 — Square Images

Square Size Image Size Spectrum Behavior
32×32 512×512 Wide cross-shaped sinc with many visible lobes
64×64 512×512 Narrower main lobe, fewer high-frequency ripples
128×128 512×512 Very narrow main lobe, energy concentrated at center

Results Summary

1-D signals: The DFT correctly decomposes simple signals, cosines produce symmetric spectral peaks at ±u with negligible imaginary components, and the rectangular pulse yields the expected sinc envelope. All inverse transforms perfectly reconstruct the originals.

2-D images: The separable row-column DFT produces cross-shaped sinc patterns from square images, with spectrum width inversely proportional to square size. The (-1)^(x+y) centering shift moves the DC component from the corners to the center for intuitive visualization.

Project Structure

DFT-Experiments/
├── main.py          # All experiments (1a, 1b, 1c, 2a, 2b, 2c)
├── Rect_128.txt     # Rectangular pulse test data
└── README.md

License

MIT

About

Python implementation of 1-D and 2-D Discrete Fourier Transform experiments — spectrum analysis, shift-property centering, rect-sinc duality, and inverse spatial-frequency size relationships on signals and images.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages