Fixing up other plotting tools for data read errors.
[debian/gnuradio] / gr-utils / src / python / gr_plot_psd.py
index 669d7b573a4e0b6cfe5e0b0a8c98dadf41c26171..d5bfca389251ba2719ada86d363c8662d0f302c1 100755 (executable)
@@ -1,6 +1,6 @@
 #!/usr/bin/env python
 #
-# Copyright 2007,2008 Free Software Foundation, Inc.
+# Copyright 2007,2008,2011 Free Software Foundation, Inc.
 # 
 # This file is part of GNU Radio
 # 
 # Boston, MA 02110-1301, USA.
 # 
 
-try:
-    import matplotlib
-    matplotlib.use('TkAgg')
-    matplotlib.interactive(True)
-except ImportError:
-    print "Please install Matplotlib to run this script (http://matplotlib.sourceforge.net/)"
-    raise SystemExit, 1
-
 try:
     import scipy
     from scipy import fftpack
@@ -68,8 +60,10 @@ class gr_plot_psd:
         rcParams['xtick.labelsize'] = self.axis_font_size
         rcParams['ytick.labelsize'] = self.axis_font_size
         
-        self.text_file     = figtext(0.10, 0.95, ("File: %s" % filename), weight="heavy", size=self.text_size)
-        self.text_file_pos = figtext(0.10, 0.92, "File Position: ", weight="heavy", size=self.text_size)
+        self.text_file     = figtext(0.10, 0.95, ("File: %s" % filename),
+                                     weight="heavy", size=self.text_size)
+        self.text_file_pos = figtext(0.10, 0.92, "File Position: ",
+                                     weight="heavy", size=self.text_size)
         self.text_block    = figtext(0.35, 0.92, ("Block Size: %d" % self.block_length),
                                      weight="heavy", size=self.text_size)
         self.text_sr       = figtext(0.60, 0.915, ("Sample Rate: %.2f" % self.sample_rate),
@@ -94,13 +88,13 @@ class gr_plot_psd:
     def get_data(self):
         self.position = self.hfile.tell()/self.sizeof_data
         self.text_file_pos.set_text("File Position: %d" % self.position)
-        self.iq = scipy.fromfile(self.hfile, dtype=self.datatype, count=self.block_length)
-        #print "Read in %d items" % len(self.iq)
-        if(len(self.iq) == 0):
+        try:
+            self.iq = scipy.fromfile(self.hfile, dtype=self.datatype, count=self.block_length)
+        except MemoryError:
             print "End of File"
         else:
             tstep = 1.0 / self.sample_rate
-            self.time = [tstep*(self.position + i) for i in xrange(len(self.iq))]
+            self.time = scipy.array([tstep*(self.position + i) for i in xrange(len(self.iq))])
 
             self.iq_psd, self.freq = self.dopsd(self.iq)
             
@@ -160,13 +154,14 @@ class gr_plot_psd:
         imags = self.iq.imag
         self.plot_iq[0].set_data([self.time, reals])
         self.plot_iq[1].set_data([self.time, imags])
-        self.sp_iq.set_xlim(min(self.time), max(self.time))
-        self.sp_iq.set_ylim([1.5*min([min(reals), min(imags)]),
-                             1.5*max([max(reals), max(imags)])])
+        self.sp_iq.set_xlim(self.time.min(), self.time.max())
+        self.sp_iq.set_ylim([1.5*min([reals.min(), imags.min()]),
+                             1.5*max([reals.max(), imags.max()])])
 
     def draw_psd(self):
         self.plot_psd[0].set_data([self.freq, self.iq_psd])
-        self.sp_psd.set_ylim([min(self.iq_psd)-10, max(self.iq_psd)+10])
+        self.sp_psd.set_ylim([self.iq_psd.min()-10, self.iq_psd.max()+10])
+        self.sp_psd.set_xlim([self.freq.min(), self.freq.max()])
 
     def draw_spec(self):
         overlap = self.specfftsize/4
@@ -174,7 +169,7 @@ class gr_plot_psd:
         self.sp_spec.clear()
         self.sp_spec.specgram(self.iq, self.specfftsize, self.sample_rate,
                               window = lambda d: d*winfunc(self.specfftsize),
-                              noverlap = overlap, xextent=[min(self.time), max(self.time)])
+                              noverlap = overlap, xextent=[self.time.min(), self.time.max()])
 
     def update_plots(self):
         self.draw_time()
@@ -187,8 +182,9 @@ class gr_plot_psd:
         draw()
         
     def zoom(self, event):
-        newxlim = self.sp_iq.get_xlim()
-        if(newxlim.all() != self.xlim.all()):
+        newxlim = scipy.array(self.sp_iq.get_xlim())
+        curxlim = scipy.array(self.xlim)
+        if(newxlim.all() != curxlim.all()):
             self.xlim = newxlim
             xmin = max(0, int(ceil(self.sample_rate*(self.xlim[0] - self.position))))
             xmax = min(int(ceil(self.sample_rate*(self.xlim[1] - self.position))), len(self.iq))
@@ -199,8 +195,8 @@ class gr_plot_psd:
             iq_psd, freq = self.dopsd(iq)
             
             self.plot_psd[0].set_data(freq, iq_psd)
-            self.sp_psd.axis([min(freq), max(freq),
-                              min(iq_psd)-10, max(iq_psd)+10])
+            self.sp_psd.axis([freq.min(), freq.max(),
+                              iq_psd.min()-10, iq_psd.max()+10])
 
             draw()