11import numpy as np
22import matplotlib .pyplot as plt
3- from scikits .audiolab import wavread
43
54
65# A class that will downsample the data and recompute when zoomed.
76class DataDisplayDownsampler (object ):
87 def __init__ (self , xdata , ydata ):
98 self .origYData = ydata
109 self .origXData = xdata
11- self .numpts = 3000
10+ self .ratio = 5
1211 self .delta = xdata [- 1 ] - xdata [0 ]
1312
14- def resample (self , xstart , xend ):
13+ def downsample (self , xstart , xend ):
1514 # Very simple downsampling that takes the points within the range
1615 # and picks every Nth point
1716 mask = (self .origXData > xstart ) & (self .origXData < xend )
1817 xdata = self .origXData [mask ]
19- ratio = int (xdata .size / self .numpts ) + 1
20- xdata = xdata [::ratio ]
18+ xdata = xdata [::self .ratio ]
2119
2220 ydata = self .origYData [mask ]
23- ydata = ydata [::ratio ]
21+ ydata = ydata [::self . ratio ]
2422
2523 return xdata , ydata
2624
@@ -33,18 +31,16 @@ def update(self, ax):
3331 self .line .set_data (* self .downsample (xstart , xend ))
3432 ax .figure .canvas .draw_idle ()
3533
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 )
4037
4138d = DataDisplayDownsampler (xdata , ydata )
4239
4340fig , ax = plt .subplots ()
4441
4542# 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-' )
4844ax .set_autoscale_on (False ) # Otherwise, infinite loop
4945
5046# Connect for changing the view limits
0 commit comments