forked from glumpy/glumpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrealtime-signals.py
executable file
·64 lines (56 loc) · 1.89 KB
/
realtime-signals.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# -----------------------------------------------------------------------------
# Copyright (c) 2009-2016 Nicolas P. Rougier. All rights reserved.
# Distributed under the (new) BSD License.
# -----------------------------------------------------------------------------
# Realtime signals example
#
# Implementation uses a ring buffer such that only new values are uploaded in
# GPU memory. This requires the corresponding numpy array to have a
# Fortran-like layout.
# -----------------------------------------------------------------------------
import numpy as np
from glumpy import app, gloo, gl
vertex = """
uniform int index, size, count;
attribute float x_index, y_index, y_value;
varying float do_discard;
void main (void)
{
float x = 2*(mod(x_index - index, size) / (size)) - 1.0;
if ((x >= +1.0) || (x <= -1.0)) do_discard = 1;
else do_discard = 0;
float y = (2*((y_index+.5)/(count))-1) + y_value;
gl_Position = vec4(x, y, 0, 1);
}
"""
fragment = """
varying float do_discard;
void main(void)
{
if (do_discard > 0) discard;
gl_FragColor = vec4(0,0,0,1);
}
"""
window = app.Window(width=1500, height=1000, color=(1,1,1,1))
@window.event
def on_draw(dt):
global size, count
window.clear()
program.draw(gl.GL_LINES, I)
index = int(program["index"])
y = program["y_value"].reshape(size,count)
yscale = 1.0/count
y[index] = yscale * np.random.uniform(-1,+1,count)
program["index"] = (index + 1) % size
count, size = 64, 1000
program = gloo.Program(vertex, fragment, count=size*count)
program["size"] = size
program["count"] = count
program["x_index"] = np.repeat(np.arange(size),count)
program["y_index"] = np.tile(np.arange(count),size)
program["y_value"] = 0
# Compute indices
I = np.arange(count * size, dtype=np.uint32).reshape(size, -1).T
I = np.roll(np.repeat(I, 2, axis=1), -1, axis=1)
I = I.view(gloo.IndexBuffer)
app.run()