Episode 4: Moving Average

Smooth the jumpy alpha power of Episode 3 into a level with a MovingAverage, and see what the smoothing costs.

Squared, a signal becomes power, but a jumpy one, useless to read at a glance. A moving average turns it into a level. Continue from Episode 3’s chain.

The chain so far

The sources, the multiplier, the alpha band and its power are Episode 3’s, unchanged.

10    # The chain of Episode 3: modulated noise, its alpha band, its power
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    multiplier = gp.Equation("n * (1 + m)", name="multiplier")
17    alpha = gp.Bandpass(f_lo=8, f_hi=12, name="alpha")
18    power = gp.Equation("in**2", name="power")
Add the average

gp.MovingAverage(window_size=125) replaces each sample with the mean of the last 125: half a second at 250 Hz. It takes the power’s output, and gets a lane of its own on the scope.

20    # The mean of the last 125 samples: half a second at 250 Hz
21    smoothed = gp.MovingAverage(window_size=125, name="smoothed")
22
23    merger = gp.Router(input_channels={"noise": [0], "modulator": [0],
24                                       "multiplier": [0], "alpha": [0],
25                                       "power": [0], "smoothed": [0]},
26                       output_channels=[gp.Router.ALL])
27    scope = gp.TimeSeriesScope(amplitude_limit=10, time_window=10)
33    p.connect(power, smoothed)
34
35    p.connect(noise, merger["noise"])
36    p.connect(modulator, merger["modulator"])
37    p.connect(multiplier, merger["multiplier"])
38    p.connect(alpha, merger["alpha"])
39    p.connect(power, merger["power"])
40    p.connect(smoothed, merger["smoothed"])
Result

Run the script and compare the last two lanes. The level follows the slow swelling of the signal that the power only hinted at.

S2E4 example screenshot

Figure 26: The alpha power, and the same power averaged over half a second.

What it costs

A moving average only knows the samples before the current one, so it reports a change only once enough of its window has seen it: its own share of the delay is about half its window, 250 ms here, and the bandpass before it adds its own. A longer window smooths more and lags more; no setting does one without the other. Every real-time feature makes that trade, and choosing the window is choosing where on it to sit.

Summary

In this episode, you learned how to:

  • smooth a power estimate with MovingAverage

  • choose a window in samples for a rate

  • weigh smoothing against the delay it adds

Proceed to Episode 5, where a Decimator drops the rate of a level that changes only a few times a second.

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

File s2e4_moving_average.py – View file on GitHub

 1"""Season 2, Episode 4: a MovingAverage turns a jumpy power into a level."""
 2
 3import gpype as gp
 4
 5if __name__ == "__main__":
 6
 7    app = gp.MainApp()
 8    p = gp.Pipeline()
 9
10    # The chain of Episode 3: modulated noise, its alpha band, its power
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    multiplier = gp.Equation("n * (1 + m)", name="multiplier")
17    alpha = gp.Bandpass(f_lo=8, f_hi=12, name="alpha")
18    power = gp.Equation("in**2", name="power")
19
20    # The mean of the last 125 samples: half a second at 250 Hz
21    smoothed = gp.MovingAverage(window_size=125, name="smoothed")
22
23    merger = gp.Router(input_channels={"noise": [0], "modulator": [0],
24                                       "multiplier": [0], "alpha": [0],
25                                       "power": [0], "smoothed": [0]},
26                       output_channels=[gp.Router.ALL])
27    scope = gp.TimeSeriesScope(amplitude_limit=10, time_window=10)
28
29    p.connect(noise, multiplier["n"])
30    p.connect(modulator, multiplier["m"])
31    p.connect(multiplier, alpha)
32    p.connect(alpha, power)
33    p.connect(power, smoothed)
34
35    p.connect(noise, merger["noise"])
36    p.connect(modulator, merger["modulator"])
37    p.connect(multiplier, merger["multiplier"])
38    p.connect(alpha, merger["alpha"])
39    p.connect(power, merger["power"])
40    p.connect(smoothed, merger["smoothed"])
41    p.connect(merger, scope)
42    app.add_widget(scope)
43
44    p.start()
45    app.run()
46    p.stop()
47    p.close()