Episode 3: Basic Transforms

Turn one line of arithmetic into a node with Equation, and start a chain that this episode and the next two build into a slowly changing alpha power.

Much of what a pipeline computes is one line of arithmetic: multiply two signals, square one, take a difference. Equation turns that line into a node. Build the chain step by step.

Two sources

Start with the usual MainApp and Pipeline, then two generators: eight channels of noise standing in for EEG, and one slow 0.5 Hz sine that will modulate it. Both are named, so the lanes on the scope can be told apart later.

 7    app = gp.MainApp()
 8    p = gp.Pipeline()
 9
10    # Noise standing in for EEG, and a slow sine that will modulate it
11    noise = gp.Generator(name="noise", sampling_rate=250, channel_count=8,
12                         noise_amplitude=5)
13    modulator = gp.Generator(name="modulator", sampling_rate=250,
14                             channel_count=1, signal_frequency=0.5,
15                             signal_amplitude=1)
Multiply them

gp.Equation("n * (1 + m)") is a node with two input ports, n and m: the names in the expression are the inputs, connected by name. The noise swells and fades every two seconds, a stand-in for alpha activity coming and going.

17    # n * (1 + m): the names in the expression are the input ports
18    multiplier = gp.Equation("n * (1 + m)", name="multiplier")
Keep the alpha band and square it

A Bandpass keeps 8 to 12 Hz. An expression with one input calls it in, the default port, so gp.Equation("in**2") connects like any other node: the power of the alpha band, sample by sample.

20    # Keep the alpha band, then square it: its power, sample by sample
21    alpha = gp.Bandpass(f_lo=8, f_hi=12, name="alpha")
22    power = gp.Equation("in**2", name="power")
One lane per stage

A Router merges the first channel of every stage into one stream, and a TimeSeriesScope shows them top to bottom. The connections follow: the processing chain first, then each stage into its lane.

24    # One lane per stage, top to bottom
25    merger = gp.Router(input_channels={"noise": [0], "modulator": [0],
26                                       "multiplier": [0], "alpha": [0],
27                                       "power": [0]},
28                       output_channels=[gp.Router.ALL])
29    scope = gp.TimeSeriesScope(amplitude_limit=10, time_window=10)
30
31    p.connect(noise, multiplier["n"])
32    p.connect(modulator, multiplier["m"])
33    p.connect(multiplier, alpha)
34    p.connect(alpha, power)
35
36    p.connect(noise, merger["noise"])
37    p.connect(modulator, merger["modulator"])
38    p.connect(multiplier, merger["multiplier"])
39    p.connect(alpha, merger["alpha"])
40    p.connect(power, merger["power"])
41    p.connect(merger, scope)
42    app.add_widget(scope)
Result

Run the script. The noise lane is steady, the modulator swings slowly, the product swells with it, the alpha band is the narrow part of that, and the bottom lane is its power: the right quantity, but one that jumps from sample to sample.

S2E3 example screenshot

Figure 25: Noise, its modulation, the product, the alpha band and its power.

Summary

In this episode, you learned how to:

  • write arithmetic as a node with Equation

  • connect inputs by the names in the expression

  • square a filtered signal into its power

Proceed to Episode 4, where a MovingAverage turns that jumpy power into a level you can read.

Nodes in this episode: Generator, Equation, Bandpass, Router, TimeSeriesScope

File s2e3_equation.py – View file on GitHub

 1"""Season 2, Episode 3: an Equation multiplies two signals, one squares one."""
 2
 3import gpype as gp
 4
 5if __name__ == "__main__":
 6
 7    app = gp.MainApp()
 8    p = gp.Pipeline()
 9
10    # Noise standing in for EEG, and a slow sine that will modulate it
11    noise = gp.Generator(name="noise", sampling_rate=250, channel_count=8,
12                         noise_amplitude=5)
13    modulator = gp.Generator(name="modulator", sampling_rate=250,
14                             channel_count=1, signal_frequency=0.5,
15                             signal_amplitude=1)
16
17    # n * (1 + m): the names in the expression are the input ports
18    multiplier = gp.Equation("n * (1 + m)", name="multiplier")
19
20    # Keep the alpha band, then square it: its power, sample by sample
21    alpha = gp.Bandpass(f_lo=8, f_hi=12, name="alpha")
22    power = gp.Equation("in**2", name="power")
23
24    # One lane per stage, top to bottom
25    merger = gp.Router(input_channels={"noise": [0], "modulator": [0],
26                                       "multiplier": [0], "alpha": [0],
27                                       "power": [0]},
28                       output_channels=[gp.Router.ALL])
29    scope = gp.TimeSeriesScope(amplitude_limit=10, time_window=10)
30
31    p.connect(noise, multiplier["n"])
32    p.connect(modulator, multiplier["m"])
33    p.connect(multiplier, alpha)
34    p.connect(alpha, power)
35
36    p.connect(noise, merger["noise"])
37    p.connect(modulator, merger["modulator"])
38    p.connect(multiplier, merger["multiplier"])
39    p.connect(alpha, merger["alpha"])
40    p.connect(power, merger["power"])
41    p.connect(merger, scope)
42    app.add_widget(scope)
43
44    p.start()
45    app.run()
46    p.stop()
47    p.close()