Imported Upstream version 3.2.2
[debian/gnuradio] / gr-utils / src / python / plot_data.py
1 #
2 # Copyright 2007,2008 Free Software Foundation, Inc.
3
4 # This file is part of GNU Radio
5
6 # GNU Radio is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3, or (at your option)
9 # any later version.
10
11 # GNU Radio is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with GNU Radio; see the file COPYING.  If not, write to
18 # the Free Software Foundation, Inc., 51 Franklin Street,
19 # Boston, MA 02110-1301, USA.
20
21
22 try:
23     import scipy
24 except ImportError:
25     print "Please install SciPy to run this script (http://www.scipy.org/)"
26     raise SystemExit, 1
27
28 try:
29     from pylab import *
30 except ImportError:
31     print "Please install Matplotlib to run this script (http://matplotlib.sourceforge.net/)"
32     raise SystemExit, 1
33
34 from optparse import OptionParser
35
36 matplotlib.interactive(True)
37 matplotlib.use('TkAgg')
38
39 class plot_data:
40     def __init__(self, datatype, filenames, options):
41         self.hfile = list()
42         self.legend_text = list()
43         for f in filenames:
44             self.hfile.append(open(f, "r"))
45             self.legend_text.append(f)
46
47         self.block_length = options.block
48         self.start = options.start
49         self.sample_rate = options.sample_rate
50
51         self.datatype = datatype
52         self.sizeof_data = datatype().nbytes    # number of bytes per sample in file
53
54         self.axis_font_size = 16
55         self.label_font_size = 18
56         self.title_font_size = 20
57         self.text_size = 22
58
59         # Setup PLOT
60         self.fig = figure(1, figsize=(16, 9), facecolor='w')
61         rcParams['xtick.labelsize'] = self.axis_font_size
62         rcParams['ytick.labelsize'] = self.axis_font_size
63         
64         self.text_file_pos = figtext(0.10, 0.88, "File Position: ", weight="heavy", size=self.text_size)
65         self.text_block    = figtext(0.40, 0.88, ("Block Size: %d" % self.block_length),
66                                      weight="heavy", size=self.text_size)
67         self.text_sr       = figtext(0.60, 0.88, ("Sample Rate: %.2f" % self.sample_rate),
68                                      weight="heavy", size=self.text_size)
69         self.make_plots()
70
71         self.button_left_axes = self.fig.add_axes([0.45, 0.01, 0.05, 0.05], frameon=True)
72         self.button_left = Button(self.button_left_axes, "<")
73         self.button_left_callback = self.button_left.on_clicked(self.button_left_click)
74
75         self.button_right_axes = self.fig.add_axes([0.50, 0.01, 0.05, 0.05], frameon=True)
76         self.button_right = Button(self.button_right_axes, ">")
77         self.button_right_callback = self.button_right.on_clicked(self.button_right_click)
78
79         self.xlim = self.sp_f.get_xlim()
80
81         self.manager = get_current_fig_manager()
82         connect('key_press_event', self.click)
83         show()
84         
85     def get_data(self, hfile):
86         self.text_file_pos.set_text("File Position: %d" % (hfile.tell()//self.sizeof_data))
87         f = scipy.fromfile(hfile, dtype=self.datatype, count=self.block_length)
88         #print "Read in %d items" % len(self.f)
89         if(len(f) == 0):
90             print "End of File"
91         else:
92             self.f = f
93             self.time = [i*(1/self.sample_rate) for i in range(len(self.f))]
94         
95     def make_plots(self):
96         self.sp_f = self.fig.add_subplot(2,1,1, position=[0.075, 0.2, 0.875, 0.6])
97         self.sp_f.set_title(("Amplitude"), fontsize=self.title_font_size, fontweight="bold")
98         self.sp_f.set_xlabel("Time (s)", fontsize=self.label_font_size, fontweight="bold")
99         self.sp_f.set_ylabel("Amplitude (V)", fontsize=self.label_font_size, fontweight="bold")
100         self.plot_f = list()
101
102         maxval = -1e12
103         minval = 1e12
104
105         for hf in self.hfile:
106             # if specified on the command-line, set file pointer
107             hf.seek(self.sizeof_data*self.start, 1)
108
109             self.get_data(hf)
110         
111             # Subplot for real and imaginary parts of signal
112             self.plot_f += plot(self.time, self.f, 'o-')
113             maxval = max(maxval, max(self.f))
114             minval = min(minval, min(self.f))
115
116         self.sp_f.set_ylim([1.5*minval, 1.5*maxval])
117
118         self.leg = self.sp_f.legend(self.plot_f, self.legend_text)
119
120         draw()
121
122     def update_plots(self):
123         maxval = -1e12
124         minval = 1e12
125         for hf,p in zip(self.hfile,self.plot_f):
126             self.get_data(hf)
127             p.set_data([self.time, self.f])
128             maxval = max(maxval, max(self.f))
129             minval = min(minval, min(self.f))
130
131         self.sp_f.set_ylim([1.5*minval, 1.5*maxval])
132         
133         draw()
134         
135     def click(self, event):
136         forward_valid_keys = [" ", "down", "right"]
137         backward_valid_keys = ["up", "left"]
138
139         if(find(event.key, forward_valid_keys)):
140             self.step_forward()
141             
142         elif(find(event.key, backward_valid_keys)):
143             self.step_backward()
144
145     def button_left_click(self, event):
146         self.step_backward()
147
148     def button_right_click(self, event):
149         self.step_forward()
150
151     def step_forward(self):
152         self.update_plots()
153
154     def step_backward(self):
155         for hf in self.hfile:
156             # Step back in file position
157             if(hf.tell() >= 2*self.sizeof_data*self.block_length ):
158                 hf.seek(-2*self.sizeof_data*self.block_length, 1)
159             else:
160                 hf.seek(-hf.tell(),1)
161         self.update_plots()
162         
163
164 def find(item_in, list_search):
165     try:
166         return list_search.index(item_in) != None
167     except ValueError:
168         return False