Skip to content

kimpetertanui/fft

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

14 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Fast Fourier Transform (FFT) Demonstration

A Python implementation demonstrating the Fast Fourier Transform and its application in signal analysis.

πŸ“š Introduction to FFT and DFT

What is the Discrete Fourier Transform (DFT)?

The Discrete Fourier Transform (DFT) is a mathematical technique that converts a discrete time-domain signal (a series of measurements taken at regular time intervals, like recording sound every millisecond) into its frequency-domain representation (showing which musical notes or pitches are present). In simple terms, it answers the question: "What frequencies make up this signal?"

Mathematical Definition:

X[k] = Ξ£(n=0 to N-1) x[n] Β· e^(-2Ο€ikn/N)

Where:

  • x[n] is the input signal in the time domain (the original measurements over time)
  • X[k] is the output in the frequency domain (the breakdown by frequency/pitch)
  • N is the number of samples (how many measurements we took)
  • k is the frequency index (which frequency we're looking at)

Time Complexity: O(NΒ²) - Not efficient for large datasets!

What is the Fast Fourier Transform (FFT)?

The Fast Fourier Transform (FFT) is an algorithm that computes the DFT efficiently. It produces the exact same result as DFT but uses a clever "divide-and-conquer" approach to dramatically reduce computation time.

Key Points:

  • FFT is an algorithm, DFT is a transform
  • FFT computes the DFT in O(N log N) time instead of O(NΒ²)
  • Developed by Cooley and Tukey in 1965 (though Gauss discovered it earlier!)
  • Essential for real-time signal processing

Speed Comparison:

  • For N=1024 samples: DFT requires ~1 million operations, FFT requires ~10,000 operations (100x faster!)
  • For N=1,048,576 samples: DFT requires ~1 trillion operations, FFT requires ~20 million operations (50,000x faster!)

Real-World Applications

  1. Audio Processing: MP3 compression, noise reduction, equalizers
  2. Image Processing: JPEG compression, image filtering, pattern recognition
  3. Telecommunications: Signal modulation/demodulation, spectrum analysis
  4. Medical Imaging: MRI and CT scan reconstruction
  5. Astronomy: Radio telescope data analysis
  6. Vibration Analysis: Mechanical fault detection in engines and machinery

πŸ€– FFT Applications in AI & Machine Learning

1. Audio & Speech AI

  • Speech Recognition (Siri, Alexa, Google Assistant)

    • FFT converts speech waveforms into spectrograms (visual representation of frequencies over time)
    • Mel-Frequency Cepstral Coefficients (MFCCs) - feature extraction for speech models
    • Used in: Whisper, Wav2Vec 2.0, DeepSpeech
  • Music Generation & Analysis

    • Genre classification (Spotify, YouTube Music)
    • Audio source separation (isolating vocals from instruments)
    • Beat detection and tempo estimation
    • Used in: Jukebox (OpenAI), MuseNet, Demucs
  • Voice Deepfakes Detection

    • Analyzing frequency patterns that humans can't hear to detect AI-generated voices
    • Critical for cybersecurity and misinformation prevention

2. Computer Vision & Image Processing

  • Convolutional Neural Networks (CNNs)

    • Fast convolution operations using FFT (reduces complexity from O(NΒ²) to O(N log N))
    • Used in image classification, object detection (YOLO, R-CNN)
  • Image Compression & Denoising

    • JPEG/JPEG2000 uses Discrete Cosine Transform (DCT, cousin of FFT)
    • Noise reduction in medical imaging (X-rays, CT scans)
    • Super-resolution models (enhancing image quality)
  • Facial Recognition

    • Fourier descriptors for face shape analysis
    • Frequency-domain features for robust recognition under varying lighting

3. Time Series Forecasting & Anomaly Detection

  • Financial Markets

    • Stock price prediction using frequency components
    • Detecting periodic patterns in trading data
    • High-frequency trading algorithms
  • IoT & Sensor Data

    • Predictive maintenance (detecting vibration anomalies in machinery before failure)
    • Smart home energy consumption forecasting
    • Wearable health monitoring (ECG, EEG analysis)
  • Climate & Weather Prediction

    • Seasonal pattern extraction from temperature/precipitation data
    • Used in LSTM/Transformer models for better forecasting

4. Natural Language Processing (NLP)

  • Transformer Architecture Optimization

    • Fast attention mechanisms using FFT (FNet: Fourier Transform-based attention)
    • Reduces computational cost in BERT, GPT models
  • Sentiment Analysis Across Time

    • Analyzing periodic trends in social media sentiment
    • Election prediction models, brand monitoring

5. Biomedical AI

  • Brain-Computer Interfaces (BCI)

    • EEG signal processing for neural prosthetics
    • Detecting epileptic seizures, sleep stage classification
    • Used in Neuralink-type applications
  • Medical Diagnosis

    • ECG analysis for heart disease detection
    • Respiratory pattern analysis from wearables
    • Cancer detection in mammograms using frequency features

6. Reinforcement Learning & Robotics

  • Robot Motion Planning

    • Analyzing sensor data (LIDAR, radar) in frequency domain
    • Vibration analysis for optimal grip force in robotic hands
  • Autonomous Vehicles

    • Radar signal processing for object detection
    • Road surface analysis through vibration sensors

7. Generative AI

  • GANs (Generative Adversarial Networks)

    • StyleGAN uses frequency-based feature separation
    • FFT for analyzing generated vs. real data distributions
  • Audio Synthesis

    • Neural vocoders (WaveNet, WaveGlow) use frequency-domain representations
    • Text-to-Speech systems (Tacotron 2)

8. Edge AI & Optimization

  • Model Compression

    • Pruning neural networks using frequency analysis
    • Identifying important vs. redundant features in models
  • Efficient Inference

    • FFT-based layers replace standard convolutions on edge devices
    • Faster inference on mobile phones, IoT devices

9. Quantum Machine Learning

  • Quantum Fourier Transform (QFT)
    • Core component of Shor's algorithm, quantum phase estimation
    • Potential speedup for certain ML algorithms on quantum computers

10. Scientific AI Applications

  • Drug Discovery

    • Molecular dynamics simulations using FFT for force calculations
    • Protein structure prediction (AlphaFold uses Fourier-based techniques)
  • Materials Science

    • Crystal structure analysis
    • Defect detection in manufacturing using frequency patterns

πŸ“š Research Papers Using FFT in AI

  1. FNet: Mixing Tokens with Fourier Transforms (2021) - Google Research

    • Replaces attention in Transformers with FFT, 92% faster
  2. Deep Complex Networks (2018) - Trabelsi et al.

    • Using complex-valued neural networks with FFT for audio/signal processing
  3. WaveNet: A Generative Model for Raw Audio (2016) - DeepMind

    • Revolutionary text-to-speech using frequency-domain insights
  4. FFTNet: A Real-Time Speaker-Dependent Neural Vocoder (2018)

    • Fast audio synthesis using FFT-based architecture

πŸŽ“ Essential FFT Concepts for AI Practitioners

  • Deep Learning: Feature engineering, efficient convolutions, audio/image preprocessing
  • Computer Vision: Image filtering, frequency-based augmentation, compression
  • NLP: Efficient attention mechanisms, audio transcription preprocessing
  • Reinforcement Learning: Sensor data processing, state representation
  • ML Optimization: Faster matrix operations, model compression techniques
  • Signal Processing for AI: Foundation for understanding spectrograms, MFCCs, wavelets

πŸš€ Getting Started

Prerequisites

  • Python 3.7 or higher
  • pip (Python package manager)

Installation

  1. Clone this repository:
git clone <your-repo-url>
cd FFT
  1. Create and activate a virtual environment (recommended):
python3 -m venv .venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate
  1. Install required packages:
pip install numpy matplotlib

Running the Demo

python fft.py

πŸ“Š What This Project Does

This demonstration:

  1. Generates multiple sine waves with different frequencies (5 Hz, 10 Hz, 15 Hz, 20 Hz)
  2. Applies FFT to each signal using NumPy's optimized FFT implementation
  3. Visualizes the results showing:
    • Time Domain (left): The original waveform
    • Frequency Domain (right): The frequency spectrum revealing dominant frequencies

Understanding the Output

  • Time Domain Plot: Shows how the signal varies over time

    • Higher frequency = more oscillations per second
    • All signals have the same amplitude (Β±1)
  • Frequency Domain Plot: Shows which frequencies are present

    • Sharp peaks indicate pure sine waves
    • Peak location tells you the frequency
    • Peak height indicates the strength of that frequency

πŸ”¬ Key Concepts for Discussion

1. Nyquist-Shannon Sampling Theorem

  • Sampling frequency (how often we take measurements) must be at least 2Γ— the highest frequency in the signal
  • In this demo: fs=1000 Hz (1000 measurements per second), so we can accurately capture frequencies up to 500 Hz
  • Violating this causes "aliasing" (when high frequencies disguise themselves as low frequencies - like how wagon wheels appear to spin backwards in movies)

2. Frequency Resolution

  • Determined by: Ξ”f = fs / N
  • In this demo: 1000 Hz / 1000 samples = 1 Hz resolution (we can distinguish frequencies that are at least 1 Hz apart)
  • Longer signals (more measurements) β†’ better frequency resolution (can tell similar frequencies apart)
  • Trade-off: time resolution (when something happens) vs. frequency resolution (what pitch/frequency it is)

3. Window Functions

  • Real-world signals aren't infinite (we can only record for a limited time)
  • Truncating signals (cutting them off) creates spectral leakage (frequencies "bleeding" into nearby frequencies, like audio distortion)
  • Window functions (Hamming, Hanning, etc.) are mathematical techniques that smoothly fade the signal at the edges to reduce this artifact

4. Computational Efficiency

# Direct DFT (slow - O(NΒ²))
def dft_slow(x):
    N = len(x)
    X = np.zeros(N, dtype=complex)
    for k in range(N):
        for n in range(N):
            X[k] += x[n] * np.exp(-2j * np.pi * k * n / N)
    return X

# FFT (fast - O(N log N))
X = np.fft.fft(x)  # This is what we use!

🎯 Experiment Ideas

Try modifying fft.py to explore:

  1. Multiple Frequencies Combined:
signal = np.sin(2*np.pi*5*t) + 0.5*np.sin(2*np.pi*12*t)
  1. Add Noise:
signal = np.sin(2*np.pi*10*t) + 0.3*np.random.randn(len(t))
  1. Square Waves or Sawtooth Waves:
from scipy import signal as scipy_signal
signal = scipy_signal.square(2*np.pi*5*t)
  1. Real Audio Files:
from scipy.io import wavfile
fs, signal = wavfile.read('audio.wav')

πŸ“– Further Reading

🀝 Contributing

Feel free to fork this project and experiment! Suggestions:

  • Add phase spectrum analysis
  • Implement inverse FFT (IFFT) demonstration
  • Create 2D FFT for image processing
  • Add interactive frequency filter

πŸ“ License

This project is open source and available for educational purposes.


Questions for Discussion:

  • Why does a pure sine wave show only one frequency peak?
  • What would happen if we used a sampling rate of 8 Hz for a 5 Hz signal?
  • How would the FFT of a square wave differ from a sine wave?
  • Can you recover the original signal from the FFT? (Hint: Inverse FFT!)

About

FFT

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors