Episode 6: Spectral Analysis via FFT

In this episode, you learn how to perform spectral analysis using the Fast Fourier Transform (FFT) in g.Pype.

Note

This page is still under development. Until we have the step-by-step instructions ready, please refer to the code example below.

File example_basic_fft.pyView file on GitHub

 1"""
 2Basic FFT (Fast Fourier Transform) Example - Frequency Domain Analysis
 3
 4This example demonstrates how to perform frequency domain analysis using FFT
 5to convert time-domain signals into their frequency components. This is
 6fundamental for BCI applications that analyze spectral content of neural
 7signals.
 8
 9What this example shows:
10- Generating a 10 Hz rectangular wave signal with noise
11- Applying FFT with windowing to convert to frequency domain
12- Real-time spectrum visualization showing frequency peaks
13- Pipeline: Generator -> FFT -> Spectrum Scope
14
15Expected output:
16The spectrum scope will display:
17- Strong peak at 10 Hz (fundamental frequency)
18- Additional peaks at 30, 50, 70 Hz... (odd harmonics of rectangular wave)
19- Background noise floor across all frequencies
20
21This demonstrates how rectangular waves contain multiple frequency components
22(harmonics), unlike pure sine waves which show only a single peak.
23
24Real-world applications:
25- EEG rhythm analysis (alpha, beta, gamma bands)
26- Motor imagery classification (mu/beta suppression)
27- SSVEP detection (steady-state visual evoked potentials)
28- Artifact identification in frequency domain
29- Power spectral density analysis
30
31Technical details:
32- Window size: 250 samples (1 second at 250 Hz)
33- Overlap: 50% (smooth spectral updates)
34- Window function: Hamming (reduces spectral leakage)
35- Frequency resolution: 1 Hz (250 Hz / 250 samples)
36
37Usage:
38    python example_basic_fft.py
39"""
40import gpype as gp
41
42fs = 250  # Sampling frequency in Hz
43
44if __name__ == "__main__":
45
46    # Create the main application window
47    app = gp.MainApp()
48
49    # Create processing pipeline
50    p = gp.Pipeline()
51
52    # Generate 10 Hz rectangular wave (rich in harmonics)
53    source = gp.Generator(
54        sampling_rate=fs,
55        channel_count=2,  # Dual channel
56        signal_frequency=10,  # 10 Hz fundamental
57        signal_amplitude=10,  # Signal strength
58        signal_shape="rect",  # Rectangle = harmonics
59        noise_amplitude=1,
60    )  # Background noise
61
62    # FFT analysis with windowing (1 second windows, 50% overlap)
63    fft = gp.FFT(
64        window_size=fs,  # 250 samples = 1 sec window
65        overlap=0.5,  # 50% overlap for smooth updates
66        window_function="hamming",
67    )  # Reduce spectral leakage
68
69    # Frequency domain visualization (spectrum analyzer)
70    scope = gp.SpectrumScope(amplitude_limit=20)  # Y-axis: 0-20 dB
71
72    # Connect processing chain: source -> FFT -> spectrum display
73    p.connect(source, fft)
74    p.connect(fft, scope)
75
76    # Add spectrum scope to application window
77    app.add_widget(scope)
78
79    # Start pipeline and run application
80    p.start()  # Begin signal processing
81    app.run()  # Show GUI and start main loop
82    p.stop()  # Clean shutdown