Merge branch 'maint'
[debian/gnuradio] / gr-utils / src / python / gr_plot_psd.py
index 0e3dbecd944f583d126364d76f215f244063d57d..e88995b725c755dfc39255d509e5366a503b6249 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
 # 
@@ -60,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),
@@ -76,7 +78,7 @@ class gr_plot_psd:
         self.button_right = Button(self.button_right_axes, ">")
         self.button_right_callback = self.button_right.on_clicked(self.button_right_click)
 
-        self.xlim = self.sp_iq.get_xlim()
+        self.xlim = scipy.array(self.sp_iq.get_xlim())
 
         self.manager = get_current_fig_manager()
         connect('draw_event', self.zoom)
@@ -86,16 +88,17 @@ 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.time = scipy.array([tstep*(i) for i in xrange(len(self.iq))])
 
             self.iq_psd, self.freq = self.dopsd(self.iq)
-            
+          
     def dopsd(self, iq):
         ''' Need to do this here and plot later so we can do the fftshift '''
         overlap = self.psdfftsize/4
@@ -130,10 +133,10 @@ class gr_plot_psd:
         
         self.plot_iq  = self.sp_iq.plot([], 'bo-') # make plot for reals
         self.plot_iq += self.sp_iq.plot([], 'ro-') # make plot for imags
-        self.draw_time()                           # draw the plot
+        self.draw_time(self.time, self.iq)         # draw the plot
 
         self.plot_psd = self.sp_psd.plot([], 'b')  # make plot for PSD
-        self.draw_psd()                            # draw the plot
+        self.draw_psd(self.freq, self.iq_psd)      # draw the plot
 
 
         if self.dospec:
@@ -143,57 +146,59 @@ class gr_plot_psd:
             self.sp_spec.set_xlabel("Time (s)", fontsize=self.label_font_size, fontweight="bold")
             self.sp_spec.set_ylabel("Frequency (Hz)", fontsize=self.label_font_size, fontweight="bold")
 
-            self.draw_spec()
+            self.draw_spec(self.time, self.iq)
         
         draw()
 
-    def draw_time(self):
-        reals = self.iq.real
-        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)])])
-
-    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])
-
-    def draw_spec(self):
+    def draw_time(self, t, iq):
+        reals = iq.real
+        imags = iq.imag
+        self.plot_iq[0].set_data([t, reals])
+        self.plot_iq[1].set_data([t, imags])
+        self.sp_iq.set_xlim(t.min(), t.max())
+        self.sp_iq.set_ylim([1.5*min([reals.min(), imags.min()]),
+                             1.5*max([reals.max(), imags.max()])])
+
+    def draw_psd(self, f, p):
+        self.plot_psd[0].set_data([f, p])
+        self.sp_psd.set_ylim([p.min()-10, p.max()+10])
+        self.sp_psd.set_xlim([f.min(), f.max()])
+
+    def draw_spec(self, t, s):
         overlap = self.specfftsize/4
         winfunc = scipy.blackman
         self.sp_spec.clear()
-        self.sp_spec.specgram(self.iq, self.specfftsize, self.sample_rate,
+        self.sp_spec.specgram(s, self.specfftsize, self.sample_rate,
                               window = lambda d: d*winfunc(self.specfftsize),
-                              noverlap = overlap, xextent=[min(self.time), max(self.time)])
+                              noverlap = overlap, xextent=[t.min(), t.max()])
 
     def update_plots(self):
-        self.draw_time()
-        self.draw_psd()
+        self.draw_time(self.time, self.iq)
+        self.draw_psd(self.freq, self.iq_psd)
 
         if self.dospec:
-            self.draw_spec()
+            self.draw_spec(self.time, self.iq)
 
-        self.xlim = self.sp_iq.get_xlim() # so zoom doesn't get called
+        self.xlim = scipy.array(self.sp_iq.get_xlim()) # so zoom doesn't get called
+        
         draw()
         
     def zoom(self, event):
         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))
+        if(newxlim[0] != curxlim[0] or newxlim[1] != curxlim[1]):
+            #xmin = max(0, int(ceil(self.sample_rate*(newxlim[0] - self.position))))
+            #xmax = min(int(ceil(self.sample_rate*(newxlim[1] - self.position))), len(self.iq))
+            xmin = max(0, int(ceil(self.sample_rate*(newxlim[0]))))
+            xmax = min(int(ceil(self.sample_rate*(newxlim[1]))), len(self.iq))
 
             iq = self.iq[xmin : xmax]
             time = self.time[xmin : xmax]
 
             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.draw_psd(freq, iq_psd)
+            self.xlim = scipy.array(self.sp_iq.get_xlim())
 
             draw()