-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmplot.py
More file actions
153 lines (123 loc) · 4.96 KB
/
mplot.py
File metadata and controls
153 lines (123 loc) · 4.96 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import multiprocessing
from multiprocessing import Lock
import audiostreamer as ast
import matplotlib
import matplotlib.pyplot as plt
import threading
import queue
import customthreads as ct
import collections
import numpy as np
class MPlot(ct.StoppableProcess):
def __init__(self, xlim, ylim, channel=1, post_process=None, x_range=None,
*args, **kwargs):
super(MPlot, self).__init__(*args, **kwargs)
self.in_queue = multiprocessing.Queue()
self.xlim = xlim
self.ylim = ylim
self.x_range = x_range
if(self.x_range is None):
self.x_range = np.arange(self.xlim[0], self.xlim[1])
self.lock = Lock()
self.channel = channel
self.post_process = post_process
if(self.post_process is None):
self.post_process = lambda data: data
def init_plot(self):
self.lock.acquire()
self.fig, self.axarr = plt.subplots(self.channel,
sharex=True,
squeeze=False)
for i in range(self.channel):
self.axarr[i, 0].hold(False)
self.fig.show()
self.lock.release()
def run(self):
self.init_plot()
while(self.is_stopped() is False):
try:
data = self.in_queue.get_nowait()
split_data = ast.split_channel(data, self.channel)
p_split_data = [self.post_process(sd) for sd in split_data]
self.lock.acquire()
for i in range(self.channel):
self.axarr[i, 0].plot(self.x_range, p_split_data[i])
self.axarr[i, 0].set_xlim(self.xlim)
self.axarr[i, 0].set_ylim(self.ylim)
self.fig.canvas.draw()
self.lock.release()
except queue.Empty:
pass
class DequeMPlot(MPlot):
def __init__(self, max_size=44100, redraw_interval=None,
*args, **kwargs):
super(DequeMPlot, self).__init__(*args, **kwargs)
self.max_deque_size = max_size
self.deque_list = [collections.deque(maxlen=self.max_deque_size)
for i in range(self.channel)]
for dq in self.deque_list:
dq.extend([0] * max_size)
self.redraw_interval = redraw_interval
def redraw(self):
self.lock.acquire()
for i in range(self.channel):
self.axarr[i, 0].plot(self.x_range, self.deque_list[i])
self.axarr[i, 0].set_xlim(self.xlim)
self.axarr[i, 0].set_ylim(self.ylim)
self.fig.canvas.draw()
self.lock.release()
def redraw_with_timer(self):
self.redraw()
self.lock.acquire()
threading.Timer(self.redraw_interval, self.redraw_with_timer).start()
self.lock.release()
def run(self):
self.init_plot()
if(self.redraw_interval is not None):
self.redraw_with_timer()
while(self.is_stopped() is False):
try:
data = self.in_queue.get_nowait()
split_data = ast.split_channel(data, self.channel)
p_split_data = [self.post_process(sd) for sd in split_data]
for i in range(self.channel):
self.deque_list[i].extend(p_split_data[i])
if(self.redraw_interval is None):
self.redraw()
except queue.Empty:
pass
class SpectroMPlot(MPlot):
def __init__(self, block_size, *args, **kwargs):
super(SpectroMPlot, self).__init__(*args, **kwargs)
self.block_size = block_size
self.arr_list = [np.zeros((len(self.x_range), self.block_size))
for i in range(self.channel)]
self.arr_order = 0
self.x_range_len = len(self.x_range)
def redraw(self):
self.lock.acquire()
for i in range(self.channel):
# self.axarr[i, 0].plot(self.x_range, self.arr_list[i])
self.axarr[i, 0].imshow(np.transpose(self.arr_list[i]),
origin="lower", aspect="auto",
cmap='jet', interpolation="none",
vmin=self.ylim[0], vmax=self.ylim[1])
self.fig.canvas.draw()
# self.fig.canvas.update()
self.lock.release()
def run(self):
self.init_plot()
while(self.is_stopped() is False):
try:
data = self.in_queue.get_nowait()
split_data = ast.split_channel(data, self.channel)
p_split_data = [self.post_process(sd) for sd in split_data]
for i in range(self.channel):
self.arr_list[i][self.arr_order] = p_split_data[i]
self.redraw()
self.arr_order += 1
if(self.arr_order >= self.x_range_len):
self.arr_order = 0
except queue.Empty:
pass
# def redraw(self):