Episode 5: Decimation
Take the smoothed alpha power of Episode 4 from 250 Hz down to 5 Hz with a Decimator, and bring it back up with an Interpolator so the scope can show it beside the rest.
A level that changes a few times a second does not need 250 values a second. A Decimator keeps one sample in N, and everything after it has N times less to do. Continue from Episode 4’s chain.
One change to the sources first: frame_size=1 on both. Left to choose, a source at 250 Hz picks frames of 4, the decimated branch comes back in frames of 1, and the Router refuses to start, naming both: “All ports must have the same frame size; Router got noise=4, modulator=4, …, decimated=1”.
10 # The chain of Episode 4. frame_size=1 on both sources: the decimated
11 # branch comes back in frames of 1, and the Router needs every input
12 # to agree on the frame size as well as the rate
13 noise = gp.Generator(name="noise", sampling_rate=250, channel_count=8,
14 noise_amplitude=5, frame_size=1)
15 modulator = gp.Generator(name="modulator", sampling_rate=250,
16 channel_count=1, signal_frequency=0.5,
17 signal_amplitude=1, frame_size=1)
gp.Decimator(decimation_factor=50) passes on one sample in 50 and records the new rate, 5 Hz, in its output, so the nodes after it know. By default it filters first: anything faster than the new rate can carry would otherwise fold back into it and show up as a slow signal that was never there. anti_alias=False keeps exactly samples 49, 99, 149 and so on; the filter costs a delay that adds to the moving average’s.
23 # One sample in 50: 250 Hz becomes 5 Hz, anti-alias filtered first
24 decimator = gp.Decimator(decimation_factor=50, name="decimator")
To show the 5 Hz feature beside the 250 Hz stages, an Interpolator repeats each value 50 times, and the result takes the last lane.
26 # Back to 250 Hz, each value repeated 50 times, so the Router can
27 # show the 5 Hz feature beside the full-rate stages
28 upsampler = gp.Interpolator(interpolation_factor=50, name="upsampler")
29
30 merger = gp.Router(input_channels={"noise": [0], "modulator": [0],
31 "multiplier": [0], "alpha": [0],
32 "power": [0], "smoothed": [0],
33 "decimated": [0]},
34 output_channels=[gp.Router.ALL])
35 scope = gp.TimeSeriesScope(amplitude_limit=10, time_window=10)
42 p.connect(smoothed, decimator)
43 p.connect(decimator, upsampler)
44
45 p.connect(noise, merger["noise"])
46 p.connect(modulator, merger["modulator"])
47 p.connect(multiplier, merger["multiplier"])
48 p.connect(alpha, merger["alpha"])
49 p.connect(power, merger["power"])
50 p.connect(smoothed, merger["smoothed"])
51 p.connect(upsampler, merger["decimated"])
Run the script. The last lane is the smoothed level again, now as a staircase: one step every fifth of a second, a little later than the lane above it.
Figure 27: Seven stages, the last one decimated to 5 Hz and shown at 250 Hz again.
The scope advances in steps of 200 ms. The Router emits a frame only when every input has one, and the decimated branch delivers one value every 200 ms, which the Interpolator turns into 50 samples at once; the six full-rate lanes wait for it. That is the cost of merging a 5 Hz branch back into a 250 Hz stream, not a slow scope: give the decimated feature a scope of its own, or decimate less, and the full-rate lanes move every frame again.
In this episode, you learned how to:
lower a stream’s rate with Decimator, anti-alias filtered or not
restore a rate with Interpolator where branches must agree
state
frame_sizewhere a merge needs it
Proceed to Episode 6, where an FFT shows which rhythms a signal is made of.
Nodes in this episode: Generator, Equation, Bandpass, MovingAverage, Decimator, Interpolator, Router, TimeSeriesScope
File s2e5_decimator.py – View file on GitHub
1"""Season 2, Episode 5: a Decimator and an Interpolator change the rate."""
2
3import gpype as gp
4
5if __name__ == "__main__":
6
7 app = gp.MainApp()
8 p = gp.Pipeline()
9
10 # The chain of Episode 4. frame_size=1 on both sources: the decimated
11 # branch comes back in frames of 1, and the Router needs every input
12 # to agree on the frame size as well as the rate
13 noise = gp.Generator(name="noise", sampling_rate=250, channel_count=8,
14 noise_amplitude=5, frame_size=1)
15 modulator = gp.Generator(name="modulator", sampling_rate=250,
16 channel_count=1, signal_frequency=0.5,
17 signal_amplitude=1, frame_size=1)
18 multiplier = gp.Equation("n * (1 + m)", name="multiplier")
19 alpha = gp.Bandpass(f_lo=8, f_hi=12, name="alpha")
20 power = gp.Equation("in**2", name="power")
21 smoothed = gp.MovingAverage(window_size=125, name="smoothed")
22
23 # One sample in 50: 250 Hz becomes 5 Hz, anti-alias filtered first
24 decimator = gp.Decimator(decimation_factor=50, name="decimator")
25
26 # Back to 250 Hz, each value repeated 50 times, so the Router can
27 # show the 5 Hz feature beside the full-rate stages
28 upsampler = gp.Interpolator(interpolation_factor=50, name="upsampler")
29
30 merger = gp.Router(input_channels={"noise": [0], "modulator": [0],
31 "multiplier": [0], "alpha": [0],
32 "power": [0], "smoothed": [0],
33 "decimated": [0]},
34 output_channels=[gp.Router.ALL])
35 scope = gp.TimeSeriesScope(amplitude_limit=10, time_window=10)
36
37 p.connect(noise, multiplier["n"])
38 p.connect(modulator, multiplier["m"])
39 p.connect(multiplier, alpha)
40 p.connect(alpha, power)
41 p.connect(power, smoothed)
42 p.connect(smoothed, decimator)
43 p.connect(decimator, upsampler)
44
45 p.connect(noise, merger["noise"])
46 p.connect(modulator, merger["modulator"])
47 p.connect(multiplier, merger["multiplier"])
48 p.connect(alpha, merger["alpha"])
49 p.connect(power, merger["power"])
50 p.connect(smoothed, merger["smoothed"])
51 p.connect(upsampler, merger["decimated"])
52 p.connect(merger, scope)
53 app.add_widget(scope)
54
55 p.start()
56 app.run()
57 p.stop()
58 p.close()