Episode 3: Basic Transforms
In this episode, you learn how to apply basic mathematical transforms to your EEG data using 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_composite_alpha_power.py – View file on GitHub
1"""
2Composite Alpha Power Analysis Example - Real-time EEG Alpha Band Processing
3
4This example demonstrates a complete alpha power analysis pipeline that
5simulates ps EEG data with modulated alpha activity and processes it
6through multiple signal processing stages. It showcases g.Pype features
7including signal generation, filtering, power computation, and multi-channel
8visualization.
9
10What this example shows:
11- Pseudo EEG signal simulation with modulated alpha activity
12- Alpha band filtering (8-12 Hz) for brain rhythm analysis
13- Power extraction using signal squaring
14- Temporal smoothing with moving averages
15- Data decimation for computational efficiency
16- Multi-stage pipeline visualization with Router node
17- Real-time processing of neurophysiological signals
18
19Pipeline architecture:
201. Signal Generation: Creates 8-channel noise + alpha modulation
212. Alpha Filtering: Extracts 8-12 Hz frequency band
223. Power Analysis: Computes instantaneous power (signal squared)
234. Temporal Smoothing: Moving average for stable power estimates
245. Data Reduction: Decimation for efficient downstream processing
256. Multi-channel Display: Router combines all processing stages
26
27Expected behavior:
28When you run this example:
29- Opens time-series visualization showing 7 processing stages
30- Channel 1: Raw noisy signal with modulated alpha
31- Channel 2: 0.5 Hz modulation signal (low-frequency envelope)
32- Channel 3: Modulated signal (noise × (1 + modulation))
33- Channel 4: Alpha-filtered signal (8-12 Hz band)
34- Channel 5: Instantaneous alpha power (squared signal)
35- Channel 6: Smoothed alpha power (moving average)
36- Channel 7: Decimated power (reduced sampling rate)
37
38Real-world applications:
39- EEG alpha rhythm analysis for neurofeedback systems
40- Brain state monitoring and cognitive load assessment
41- Sleep stage detection using alpha power dynamics
42- Attention and relaxation state classification
43- Real-time BCI applications using alpha modulation
44- Clinical EEG analysis for neurological assessment
45
46Scientific background:
47Alpha waves (8-12 Hz) are prominent EEG rhythms associated with:
48- Relaxed wakefulness and eyes-closed states
49- Attention regulation and cognitive processing
50- Sensorimotor idle states and cortical inhibition
51- Individual differences in cognitive performance
52- Pathological changes in neurological disorders
53
54Technical features:
55- Realistic signal modeling with controlled alpha modulation
56- Multi-stage filtering and power analysis pipeline
57- Efficient data processing with decimation
58- Real-time parameter monitoring across processing stages
59
60Pipeline components explained:
61- Generator: Creates controllable synthetic EEG-like signals
62- Equation: Performs mathematical operations (modulation, power)
63- Bandpass: Implements digital filtering for frequency selection
64- MovingAverage: Temporal smoothing for stable estimates
65- Decimator: Reduces data rate while preserving information
66- Router: Combines multiple signals for comparative visualization
67- TimeSeriesScope: Real-time multi-channel display
68
69Usage:
70 python example_composite_alpha_power.py
71
72Prerequisites:
73 - g.Pype framework with signal processing modules
74 - Real-time visualization capabilities
75"""
76import gpype as gp
77
78# Sampling rate configuration for realistic EEG simulation
79fs = 250 # 250 Hz - standard EEG sampling rate
80
81if __name__ == "__main__":
82
83 # Initialize main application for GUI event handling
84 app = gp.MainApp()
85
86 # Create processing pipeline for alpha power analysis
87 p = gp.Pipeline()
88
89 # === SIGNAL GENERATION STAGE ===
90 # Generate 8-channel background noise simulating baseline EEG activity
91 noise = gp.Generator(
92 sampling_rate=fs, channel_count=8, noise_amplitude=5
93 ) # 5 µV RMS noise level
94
95 # Generate low-frequency modulation signal (0.5 Hz sine wave)
96 # This simulates natural alpha power fluctuations
97 modulator = gp.Generator(
98 sampling_rate=fs,
99 channel_count=1,
100 signal_frequency=0.5, # 0.5 Hz modulation
101 signal_amplitude=1, # Modulation depth
102 signal_shape="sine",
103 )
104
105 # === SIGNAL MODULATION STAGE ===
106 # Apply amplitude modulation: noise × (1 + modulation)
107 # Creates realistic EEG with time-varying alpha power
108 multiplier = gp.Equation("n * (1 + m)")
109
110 # === ALPHA BAND FILTERING STAGE ===
111 # Extract alpha frequency band (8-12 Hz) from modulated signal
112 # Standard alpha band definition for EEG analysis
113 alpha_filter = gp.Bandpass(f_lo=8, f_hi=12) # Upper alpha
114
115 # === POWER ANALYSIS STAGE ===
116 # Compute instantaneous power using signal squaring
117 # Power = signal² provides envelope of alpha activity
118 power = gp.Equation("in**2")
119
120 # === TEMPORAL SMOOTHING STAGE ===
121 # Apply moving average to stabilize power estimates
122 # 125 samples = 0.5 seconds at 250 Hz sampling rate
123 moving_average = gp.MovingAverage(window_size=125)
124
125 # === DATA REDUCTION STAGE ===
126 # Reduce data rate for efficient visualization and storage
127 # Factor 50: 250 Hz → 5 Hz (adequate for alpha power tracking)
128 decimator = gp.Decimator(decimation_factor=50)
129
130 # Hold last value for stable display between updates
131 hold = gp.Hold()
132
133 # === VISUALIZATION ROUTING STAGE ===
134 # Router combines all processing stages for comparative analysis
135 # Allows simultaneous viewing of raw, filtered, and processed signals
136 merger = gp.Router(
137 input_selector={
138 "noise": [0], # Raw noise
139 "modulator": [0], # Modulation
140 "multiplier": [0], # Modulated
141 "alpha_filter": [0], # Alpha-filtered
142 "power": [0], # Power signal
143 "moving_average": [0], # Smoothed power
144 "hold": [0],
145 }, # Final output
146 output_selector=[gp.Router.ALL],
147 )
148
149 # === REAL-TIME VISUALIZATION ===
150 # Multi-channel scope for real-time signal monitoring
151 # 10-second time window with ±10 µV amplitude range
152 scope = gp.TimeSeriesScope(
153 amplitude_limit=10, time_window=10 # ±10 µV display range
154 ) # 10-second time window
155
156 # === PIPELINE CONNECTIONS ===
157 # Connect main processing chain: noise → modulation → filter → power
158 p.connect(noise, multiplier["n"]) # Noise input to multiplier
159 p.connect(modulator, multiplier["m"]) # Modulation input to multiplier
160 p.connect(multiplier, alpha_filter) # Modulated signal to alpha filter
161 p.connect(alpha_filter, power) # Filtered signal to power analysis
162 p.connect(power, moving_average) # Power to temporal smoothing
163 p.connect(moving_average, decimator) # Smoothed power to decimation
164 p.connect(decimator, hold) # Decimated signal to hold buffer
165
166 # Connect all processing stages to router for visualization
167 p.connect(noise, merger["noise"]) # Stage 1: Raw noise
168 p.connect(modulator, merger["modulator"]) # Stage 2: Modulation
169 p.connect(multiplier, merger["multiplier"]) # Stage 3: Modulated
170 p.connect(alpha_filter, merger["alpha_filter"]) # Stage 4: Filtered
171 p.connect(power, merger["power"]) # Stage 5: Power
172 p.connect(moving_average, merger["moving_average"]) # Stage 6: Smoothed
173 p.connect(hold, merger["hold"]) # Stage 7: Final
174
175 # Connect router output to visualization scope
176 p.connect(merger, scope)
177
178 # === APPLICATION SETUP ===
179 # Add visualization widget to main application window
180 app.add_widget(scope)
181
182 # === EXECUTION ===
183 # Start pipeline processing and run application event loop
184 p.start() # Begin real-time signal processing
185 app.run() # Start GUI and block until window closes
186 p.stop() # Clean shutdown of processing pipeline