1
1
import numpy as np
2
2
import matplotlib .pyplot as plt
3
- from scikits .audiolab import wavread
4
3
5
4
6
5
# A class that will downsample the data and recompute when zoomed.
7
6
class DataDisplayDownsampler (object ):
8
7
def __init__ (self , xdata , ydata ):
9
8
self .origYData = ydata
10
9
self .origXData = xdata
11
- self .numpts = 3000
10
+ self .ratio = 5
12
11
self .delta = xdata [- 1 ] - xdata [0 ]
13
12
14
- def resample (self , xstart , xend ):
13
+ def downsample (self , xstart , xend ):
15
14
# Very simple downsampling that takes the points within the range
16
15
# and picks every Nth point
17
16
mask = (self .origXData > xstart ) & (self .origXData < xend )
18
17
xdata = self .origXData [mask ]
19
- ratio = int (xdata .size / self .numpts ) + 1
20
- xdata = xdata [::ratio ]
18
+ xdata = xdata [::self .ratio ]
21
19
22
20
ydata = self .origYData [mask ]
23
- ydata = ydata [::ratio ]
21
+ ydata = ydata [::self . ratio ]
24
22
25
23
return xdata , ydata
26
24
@@ -33,18 +31,16 @@ def update(self, ax):
33
31
self .line .set_data (* self .downsample (xstart , xend ))
34
32
ax .figure .canvas .draw_idle ()
35
33
36
- # Read data
37
- data = wavread ('/usr/share/sounds/purple/receive.wav' )[0 ]
38
- ydata = np .tile (data [:, 0 ], 100 )
39
- xdata = np .arange (ydata .size )
34
+ # Create a signal
35
+ xdata = np .linspace (16 , 365 , 365 - 16 )
36
+ ydata = np .sin (2 * np .pi * xdata / 153 ) + np .cos (2 * np .pi * xdata / 127 )
40
37
41
38
d = DataDisplayDownsampler (xdata , ydata )
42
39
43
40
fig , ax = plt .subplots ()
44
41
45
42
# Hook up the line
46
- xdata , ydata = d .downsample (xdata [0 ], xdata [- 1 ])
47
- d .line , = ax .plot (xdata , ydata )
43
+ d .line , = ax .plot (xdata , ydata , 'o-' )
48
44
ax .set_autoscale_on (False ) # Otherwise, infinite loop
49
45
50
46
# Connect for changing the view limits
0 commit comments