Episode 2: Routing Signals

In this episode, you learn how to route signals between nodes in a g.Pype pipeline.

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_router.pyView file on GitHub

  1"""
  2Basic Router Example - Channel Selection and Data Stream Routing
  3
  4This example demonstrates how to use the Router node to selectively combine
  5channels from multiple data sources into a single output stream. The Router
  6is essential for flexible data management in complex BCI pipelines where
  7different signal sources need to be combined or specific channels selected.
  8
  9What this example shows:
 10- Creating two different signal generators (sine and rectangular waves)
 11- Using Router to select specific channels from each source
 12- Combining selected channels into a unified output stream
 13- Flexible channel mapping and data stream management
 14
 15Expected output:
 16When you run this example, you'll see:
 17- 5 total channels in the scope (2 from sine + 3 from rectangular)
 18- Channels 1-2: 10 Hz sine waves from first generator
 19- Channels 3-5: 2 Hz rectangular waves from second generator
 20- Clear visual distinction between the two signal types
 21- Demonstrates how Router preserves signal characteristics while combining
 22
 23Router configuration:
 24- Input 1: Channels 0,1 from sine wave generator (-> output channels 1-2)
 25- Input 2: Channels 3,4,5 from square-wave generator (-> output channels 3-5)
 26- Total output: 5 channels with mixed signal types
 27
 28Real-world applications:
 29- Multi-device integration (combining EEG + EMG + EOG signals)
 30- Channel subset selection (choosing specific electrode locations)
 31- Signal source switching (alternating between different input sources)
 32- Data stream merging (combining multiple acquisition systems)
 33- Feature channel selection (using only relevant channels for analysis)
 34
 35Advanced routing scenarios:
 36- Spatial filtering (selecting channels by brain region)
 37- Artifact channel isolation (separating signal from noise channels)
 38- Multi-modal BCI (combining different signal modalities)
 39- Dynamic channel reconfiguration during experiments
 40- Cross-validation with different channel subsets
 41
 42Technical details:
 43- Router automatically handles different sampling rates (if matched)
 44- Channel indexing starts from 0 in input, 1 in display
 45- Input channels can be reordered or duplicated in output
 46- Router preserves data types and timing information
 47- Multiple Router nodes can be chained for complex routing
 48
 49Usage:
 50    python example_basic_router.py
 51
 52Note:
 53    The Router is one of the most versatile nodes in g.Pype, enabling
 54    sophisticated data flow management essential for complex BCI systems.
 55"""
 56import gpype as gp
 57
 58fs = 250  # Sampling frequency in Hz
 59
 60if __name__ == "__main__":
 61    # Create the main application window
 62    app = gp.MainApp()
 63
 64    # Create processing pipeline
 65    p = gp.Pipeline()
 66
 67    # Generate first signal source: 10 Hz sine waves
 68    source_sine = gp.Generator(
 69        sampling_rate=fs,
 70        channel_count=8,  # 8 channels
 71        signal_frequency=10,  # 10 Hz frequency
 72        signal_amplitude=10,  # High amplitude
 73        signal_shape="sine",
 74    )  # Smooth sine waves
 75
 76    # Generate second signal source: 2 Hz rectangular waves
 77    source_square = gp.Generator(
 78        sampling_rate=fs,
 79        channel_count=8,  # 8 channels
 80        signal_frequency=2,  # 2 Hz frequency
 81        signal_amplitude=5,  # Lower amplitude
 82        signal_shape="rect",
 83    )  # Rectangular signal
 84
 85    # Router for selective channel combination
 86    # Input 1: Take channels 0,1 from sine generator
 87    # Input 2: Take channels 3,4,5 from rectangular generator
 88    # Output: 5 channels total (2 sine + 3 rectangular)
 89    router = gp.Router(input_selector=[[0, 1], [3, 4, 5]])
 90
 91    # Real-time visualization scope
 92    scope = gp.TimeSeriesScope(
 93        amplitude_limit=30, time_window=10  # Y-axis range
 94    )  # 10 seconds display
 95
 96    # Connect the multi-source pipeline
 97    p.connect(source_sine, router["in1"])  # Sine waves -> Router input 1
 98    p.connect(source_square, router["in2"])  # Rectangular -> Router input 2
 99    p.connect(router, scope)  # Combined signals -> Display
100
101    # Add scope to application window
102    app.add_widget(scope)
103
104    # Start multi-source signal processing
105    p.start()  # Begin processing (observe mixed signal types)
106    app.run()  # Show GUI and start main loop
107    p.stop()  # Clean shutdown when window closes