Skip to content

muditbhargava66/FFT-implementation-in-C

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

13 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

FFT Study Repository

A comprehensive collection of Fast Fourier Transform (FFT) algorithms and implementations in C, designed for educational purposes and performance comparison.

πŸ“š Table of Contents

Overview

This repository serves as a comprehensive study resource for understanding FFT algorithms, their implementations, optimizations, and applications. It includes various FFT algorithms from basic to advanced, real-world applications, and performance optimizations.

Key Features

  • Educational Focus: Each implementation includes detailed documentation explaining the algorithm, its complexity, and use cases
  • Production Quality: Professional coding standards with proper error handling, memory management, and testing
  • Performance Analysis: Comprehensive benchmarking suite for comparing different implementations
  • Real Applications: Practical examples showing how FFT is used in audio processing, filtering, and more
  • Multiple Optimizations: SIMD, parallel, and fixed-point implementations for different platforms

Repository Structure

FFT-implementation-in-C/
β”œβ”€β”€ include/               # Header files
β”‚   β”œβ”€β”€ fft_common.h      # Common utilities and definitions
β”‚   └── fft_algorithms.h  # FFT function declarations
β”œβ”€β”€ algorithms/           # Core algorithm implementations
β”‚   β”œβ”€β”€ core/            # FFT algorithms
β”‚   └── dft/             # DFT implementations
β”œβ”€β”€ applications/         # Real-world applications
β”œβ”€β”€ optimizations/        # Performance optimizations
β”œβ”€β”€ benchmarks/          # Performance testing
β”œβ”€β”€ tests/               # Test suites
β”œβ”€β”€ examples/            # Example programs
β”œβ”€β”€ utils/               # Utility functions
└── lib/                 # Compiled library files

Algorithms Implemented

βœ… Core FFT Algorithms

Algorithm Description Complexity Status
Radix-2 DIT Decimation-in-Time FFT O(n log n) βœ…
Radix-2 DIF Decimation-in-Frequency FFT O(n log n) βœ…
Radix-4 Base-4 FFT algorithm O(n log n) βœ…
Split-Radix Optimal operation count O(n log n) βœ…
Bluestein Arbitrary size FFT O(n log n) βœ…
Mixed-Radix Composite size FFT O(n log n) βœ…
Recursive Educational implementation O(n log n) βœ…
Iterative Cache-efficient O(n log n) βœ…

πŸ”§ Optimizations

Optimization Target Platform Speedup Status
SIMD (SSE/AVX) x86/x64 2-4x βœ…
OpenMP Parallel Multi-core ~Nx cores βœ…
Fixed-Point Embedded/DSP Platform-specific βœ…

πŸ“Š Applications

  • Audio Spectrum Analyzer: Real-time frequency analysis
  • Digital Filtering: Low-pass, high-pass, band-pass filters
  • Convolution: Fast convolution using FFT
  • Power Spectrum: Signal power analysis
  • 2D Image FFT: Image processing applications
  • Pitch Detection: Musical note detection

Getting Started

Prerequisites

  • GCC compiler (or compatible C compiler)
  • Make build system
  • POSIX-compliant system (Linux, macOS, WSL)
  • Optional: OpenMP support for parallel implementations

Quick Start

  1. Clone the repository:

    git clone https://github.com/muditbhargava66/FFT-implementation-in-C.git
    cd FFT-implementation-in-C
  2. Run the quick start script:

    chmod +x quickstart.sh
    ./quickstart.sh
  3. Or build manually:

    make all

Building and Running

Build Commands

# Build everything
make all

# Build specific components
make algorithms      # Core FFT algorithms
make applications   # Application examples
make optimizations  # Optimized versions
make benchmarks     # Benchmark suite
make tests          # Test suite

# Build with debug symbols
make debug

# Build with profiling
make profile

# Clean build artifacts
make clean

Running Examples

# Run a specific algorithm
./bin/radix2_dit

# Run benchmarks
./bin/benchmark_all

# Run tests
./bin/test_all

# Run demonstrations
make demo

Library Usage

The project builds a static library libfft.a that can be linked with your programs:

#include "fft_common.h"
#include "fft_algorithms.h"

int main() {
    int n = 1024;
    complex_t* signal = allocate_complex_array(n);
    
    // Generate signal...
    
    // Apply FFT
    radix2_dit_fft(signal, n, FFT_FORWARD);
    
    // Process spectrum...
    
    free_complex_array(signal);
    return 0;
}

Compile with:

gcc -I/path/to/include myprogram.c -L/path/to/lib -lfft -lm

Code Quality

Coding Standards

  • C99 Standard: Modern C features for better code clarity
  • Consistent Style: 4-space indentation, clear naming conventions
  • Comprehensive Documentation: Doxygen-style comments for all functions
  • Error Handling: Proper validation and error reporting
  • Memory Safety: Careful allocation/deallocation, no memory leaks

Algorithm Implementation

Each algorithm implementation includes:

  1. Detailed Header Documentation: Mathematical background, complexity analysis, references
  2. Step-by-Step Comments: Clear explanation of each algorithmic step
  3. Input Validation: Checking for valid sizes, null pointers, etc.
  4. Test Cases: Built-in testing in main() function
  5. Performance Measurement: Timing and complexity verification

Example structure:

/**
 * @brief Algorithm name and brief description
 * 
 * @details
 * Detailed explanation of the algorithm, including:
 * - Mathematical foundation
 * - Step-by-step process
 * - Data structures used
 * 
 * @param x Input/output array
 * @param n Array length
 * @param dir Transform direction
 * 
 * Time Complexity: O(n log n)
 * Space Complexity: O(1)
 */
void algorithm_fft(complex_t* x, int n, fft_direction dir) {
    /* Input validation */
    if (!x || n <= 0) {
        fprintf(stderr, "Error: Invalid input\n");
        return;
    }
    
    /* Algorithm implementation with clear comments */
    // Step 1: ...
    // Step 2: ...
}

Documentation

Algorithm Documentation

Each algorithm includes:

  • Mathematical derivation
  • Complexity analysis
  • Implementation notes
  • Usage examples
  • Performance characteristics

API Documentation

See include/fft_common.h and include/fft_algorithms.h for the complete API reference.

Key functions:

  • radix2_dit_fft() - Standard FFT for power-of-2 sizes
  • bluestein_fft() - FFT for arbitrary sizes
  • allocate_complex_array() - Memory allocation helper
  • compute_magnitude() - Convert to magnitude spectrum

Performance

Benchmark results on typical hardware (Intel i7, 3.6GHz):

Algorithm N=1024 N=4096 N=16384
Radix-2 DIT 0.08ms 0.4ms 1.8ms
Split-Radix 0.06ms 0.3ms 1.4ms
SIMD Optimized 0.03ms 0.15ms 0.7ms
Parallel (4 cores) 0.02ms 0.1ms 0.5ms

Contributing

We welcome contributions! Please see CONTRIBUTING.md for guidelines.

Areas for contribution:

  • Additional algorithms (Prime Factor, Winograd)
  • GPU implementations (CUDA/OpenCL)
  • More applications
  • Performance optimizations
  • Documentation improvements

References

  1. Cooley, J. W., & Tukey, J. W. (1965). "An algorithm for the machine calculation of complex Fourier series"
  2. Duhamel, P., & Vetterli, M. (1990). "Fast Fourier transforms: a tutorial review"
  3. Frigo, M., & Johnson, S. G. (2005). "The design and implementation of FFTW3"
  4. Van Loan, C. (1992). "Computational frameworks for the fast Fourier transform"

License

This project is licensed under the MIT License - see the LICENSE file for details.


Note: This is an educational repository. For production use, consider established libraries like FFTW, Intel MKL, or cuFFT.