Added GUI for post-detector offset and gain. Previously these were carried
[debian/gnuradio] / gr-radio-astronomy / src / python / usrp_ra_receiver.py
index fc56a11996b6c210b6ab49f92b57cc1d19537d67..95a9291f03a4ae3870fd08f603f046fdb0b9e83c 100755 (executable)
@@ -6,7 +6,7 @@
 # 
 # GNU Radio is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2, or (at your option)
+# the Free Software Foundation; either version 3, or (at your option)
 # any later version.
 # 
 # GNU Radio is distributed in the hope that it will be useful,
@@ -22,7 +22,7 @@
 
 from gnuradio import gr, gru
 from gnuradio import usrp
-import usrp_dbid
+from usrpm import usrp_dbid
 from gnuradio import eng_notation
 from gnuradio.eng_option import eng_option
 from gnuradio.wxgui import stdgui, ra_fftsink, ra_stripchartsink, ra_waterfallsink, form, slider
@@ -118,6 +118,7 @@ class app_flow_graph(stdgui.gui_flow_graph):
         #  on FFT bins.
         upper_limit = binwidth / 0.10
         self.setitimer = int(upper_limit * 2.00)
+        self.scanning = True
 
         # Calculate the CHIRP values based on Hz/sec
         self.CHIRP_LOWER = 0.10 * self.setitimer
@@ -133,19 +134,21 @@ class app_flow_graph(stdgui.gui_flow_graph):
         # Calculate upper edge
         self.setifreq_upper = options.freq + (self.seti_freq_range/2)
 
-        # We change center frequencies every 20 self.setitimer intervals
-        self.setifreq_timer = self.setitimer * 20
+        # We change center frequencies every 10 self.setitimer intervals
+        self.setifreq_timer = self.setitimer * 10
 
         # Create actual timer
         self.seti_then = time.time()
 
         # The hits recording array
         self.nhits = 10
-        self.hits_array = Numeric.zeros((self.nhits,3), Numeric.Float64)
-
+        self.nhitlines = 3
+        self.hits_array = Numeric.zeros((self.nhits,self.nhitlines), Numeric.Float64)
+        self.hit_intensities = Numeric.zeros((self.nhits,self.nhitlines), Numeric.Float64)
         # Calibration coefficient and offset
         self.calib_coeff = options.calib_coeff
         self.calib_offset = options.calib_offset
+        self.orig_calib_offset = options.calib_offset
 
         self.integ = options.integ
         self.avg_alpha = options.avg
@@ -166,12 +169,12 @@ class app_flow_graph(stdgui.gui_flow_graph):
 
         self.u = usrp.source_c(decim_rate=options.decim)
         self.u.set_mux(usrp.determine_rx_mux_value(self.u, options.rx_subdev_spec))
-        self.cardtype = self.u.daughterboard_id(0)
         # Set initial declination
         self.decln = options.decln
 
         # determine the daughterboard subdevice we're using
         self.subdev = usrp.selected_subdev(self.u, options.rx_subdev_spec)
+        self.cardtype = self.subdev.dbid()
 
         input_rate = self.u.adc_freq() / self.u.decim_rate()
 
@@ -188,14 +191,14 @@ class app_flow_graph(stdgui.gui_flow_graph):
         #
         self.fft_rate = options.fft_rate
 
-        self.fft_size = options.fft_size
+        self.fft_size = int(options.fft_size)
 
         # This buffer is used to remember the most-recent FFT display
         #   values.  Used later by self.write_spectral_data() to write
         #   spectral data to datalogging files, and by the SETI analysis
         #   function.
         #
-        self.fft_outbuf = Numeric.zeros(options.fft_size, Numeric.Float64)
+        self.fft_outbuf = Numeric.zeros(self.fft_size, Numeric.Float64)
 
         #
         # If SETI mode, only look at seti_fft_bandwidth (currently 12.5Khz)
@@ -236,6 +239,9 @@ class app_flow_graph(stdgui.gui_flow_graph):
         self.locality = ephem.Observer()
         self.locality.long = str(options.longitude)
         self.locality.lat = str(options.latitude)
+        # We make notes about Sunset/Sunrise in Continuum log files
+        self.sun = ephem.Sun()
+        self.sunstate = "??"
 
         # Set up stripchart display
         self.stripsize = int(options.stripsize)
@@ -295,17 +301,20 @@ class app_flow_graph(stdgui.gui_flow_graph):
             self.integrator2 = gr.fir_filter_fff (M, tapsM)
             self.integrator3 = gr.single_pole_iir_filter_ff(1.0)
     
+            # The detector
+            self.detector = gr.complex_to_mag_squared()
+
             # Split complex USRP stream into a pair of floats
-            self.splitter = gr.complex_to_float (1);
-    
-            # I squarer (detector)
-            self.multI = gr.multiply_ff();
-    
-            # Q squarer (detector)
-            self.multQ = gr.multiply_ff();
+            #self.splitter = gr.complex_to_float (1);
     
-            # Adding squared I and Q to produce instantaneous signal power
-            self.adder = gr.add_ff();
+#            # I squarer (detector)
+#            self.multI = gr.multiply_ff();
+#    
+#            # Q squarer (detector)
+#            self.multQ = gr.multiply_ff();
+#    
+#            # Adding squared I and Q to produce instantaneous signal power
+#            self.adder = gr.add_ff();
     
             # Signal probe
             self.probe = gr.probe_signal_f();
@@ -313,8 +322,8 @@ class app_flow_graph(stdgui.gui_flow_graph):
             #
             # Continuum calibration stuff
             #
-            self.cal_mult = gr.multiply_const_ff(self.calib_coeff);
-            self.cal_offs = gr.add_const_ff(self.calib_offset);
+            self.cal_mult = gr.multiply_const_ff(self.calib_coeff/100.0);
+            self.cal_offs = gr.add_const_ff(self.calib_offset*4000);
 
         #
         # Start connecting configured modules in the receive chain
@@ -327,37 +336,35 @@ class app_flow_graph(stdgui.gui_flow_graph):
             self.connect(self.u, self.fft_bandpass, self.scope)
 
         if self.setimode == False:
-            #
-            # The head of the continuum chain
-            #
-            self.connect(self.u, self.splitter)
-    
-            # Connect splitter outputs to multipliers
-            # First do I^2
-            self.connect((self.splitter, 0), (self.multI,0))
-            self.connect((self.splitter, 0), (self.multI,1))
-    
-            # Then do Q^2
-            self.connect((self.splitter, 1), (self.multQ,0))
-            self.connect((self.splitter, 1), (self.multQ,1))
+#            #
+#            # The head of the continuum chain
+#            #
+#            self.connect(self.u, self.splitter)
+#    
+#            # Connect splitter outputs to multipliers
+#            # First do I^2
+#            self.connect((self.splitter, 0), (self.multI,0))
+#            self.connect((self.splitter, 0), (self.multI,1))
+#    
+#            # Then do Q^2
+#            self.connect((self.splitter, 1), (self.multQ,0))
+#            self.connect((self.splitter, 1), (self.multQ,1))
+#    
+#            # Then sum the squares
+#            self.connect(self.multI, (self.adder,0))
+#            self.connect(self.multQ, (self.adder,1))
+#    
+#            # Connect adder output to two-stages of FIR integrator
+#            #   followed by a single stage IIR integrator, and
+#            #   the calibrator
+#            self.connect(self.adder, self.integrator1, 
+#               self.integrator2, self.integrator3, self.cal_mult, 
+#               self.cal_offs, self.chart)
+
+            self.connect(self.u, self.detector, 
+                self.integrator1, self.integrator2,
+                self.integrator3, self.cal_mult, self.cal_offs, self.chart)
     
-            # Then sum the squares
-            self.connect(self.multI, (self.adder,0))
-            self.connect(self.multQ, (self.adder,1))
-    
-            # Connect adder output to two-stages of FIR integrator
-            #   followed by a single stage IIR integrator, and
-            #   the calibrator
-            self.connect(self.adder, self.integrator1, 
-               self.integrator2, self.integrator3, self.cal_mult, 
-               self.cal_offs, self.chart)
-    
-            # Connect calibrator to probe
-            # SPECIAL NOTE:  I'm setting the ground work here
-            #   for completely changing the way local_calibrator
-            #   works, including removing some horrible kludges for
-            #   recording data.
-            # But for now, self.probe() will be used to display the
             #  current instantaneous integrated detector value
             self.connect(self.cal_offs, self.probe)
 
@@ -367,8 +374,11 @@ class app_flow_graph(stdgui.gui_flow_graph):
         self.integ = options.integ
         if self.setimode == False:
             self.myform['integration'].set_value(int(options.integ))
+            self.myform['offset'].set_value(options.calib_offset)
+            self.myform['dcgain'].set_value(options.calib_coeff)
         self.myform['average'].set_value(int(options.avg))
 
+
         if self.setimode == False:
             # Make integrator agree with command line
             self.set_integration(int(options.integ))
@@ -414,7 +424,7 @@ class app_flow_graph(stdgui.gui_flow_graph):
         self.myform['dbname'].set_value(self.subdev.name())
 
         # Set analog baseband filtering, if DBS_RX
-        if self.cardtype == usrp_dbid.DBS_RX:
+        if self.cardtype in (usrp_dbid.DBS_RX, usrp_dbid.DBS_RX_REV_2_1):
             lbw = (self.u.adc_freq() / self.u.decim_rate()) / 2
             if lbw < 1.0e6:
                 lbw = 1.0e6
@@ -439,6 +449,7 @@ class app_flow_graph(stdgui.gui_flow_graph):
             self.seti_then = time.time()
             # Zero-out hits array when changing frequency
             self.hits_array[:,:] = 0.0
+            self.hit_intensities[:,:] = -60.0
 
             return self.set_freq(kv['freq'])
 
@@ -472,6 +483,8 @@ class app_flow_graph(stdgui.gui_flow_graph):
         vbox1.Add((4,0), 0, 0)
 
         vbox2 = wx.BoxSizer(wx.VERTICAL)
+        if self.setimode == False:
+            vbox3 = wx.BoxSizer(wx.VERTICAL)
         g = self.subdev.gain_range()
         myform['gain'] = form.slider_field(parent=self.panel, sizer=vbox2, label="RF Gain",
                                            weight=1,
@@ -480,7 +493,18 @@ class app_flow_graph(stdgui.gui_flow_graph):
 
         vbox2.Add((4,0), 0, 0)
         myform['average'] = form.slider_field(parent=self.panel, sizer=vbox2, 
-                    label="Spectral Averaging (FFT frames)", weight=1, min=1, max=2000, callback=self.set_averaging)
+                    label="Spectral Averaging (FFT frames)", weight=1, min=1, max=3000, callback=self.set_averaging)
+
+        # Set up scan control button when in SETI mode
+        if (self.setimode == True):
+               # SETI scanning control
+               buttonbox = wx.BoxSizer(wx.HORIZONTAL)
+               self.scan_control = form.button_with_callback(self.panel,
+                     label="Scan: On ",
+                     callback=self.toggle_scanning)
+       
+               buttonbox.Add(self.scan_control, 0, wx.CENTER)
+               vbox2.Add(buttonbox, 0, wx.CENTER)
 
         vbox2.Add((4,0), 0, 0)
 
@@ -495,10 +519,21 @@ class app_flow_graph(stdgui.gui_flow_graph):
             callback=myform.check_input_and_call(_form_set_decln))
         vbox2.Add((4,0), 0, 0)
 
-        buttonbox = wx.BoxSizer(wx.HORIZONTAL)
-        vbox.Add(buttonbox, 0, wx.CENTER)
+        if self.setimode == False:
+            myform['offset'] = form.slider_field(parent=self.panel, sizer=vbox3,
+                label="Post-Detector Offset", weight=1, min=-500, max=500, 
+                callback=self.set_pd_offset)
+            vbox3.Add((2,0), 0, 0)
+            myform['dcgain'] = form.slider_field(parent=self.panel, sizer=vbox3,
+                label="Post-Detector Gain", weight=1, min=1, max=100, 
+                callback=self.set_pd_gain)
+            vbox3.Add((2,0), 0, 0)
         hbox.Add(vbox1, 0, 0)
-       hbox.Add(vbox2, wx.ALIGN_RIGHT, 0)
+        hbox.Add(vbox2, wx.ALIGN_RIGHT, 0)
+
+        if self.setimode == False:
+            hbox.Add(vbox3, wx.ALIGN_RIGHT, 0)
+
         vbox.Add(hbox, 0, wx.EXPAND)
 
         self._build_subpanel(vbox)
@@ -624,7 +659,7 @@ class app_flow_graph(stdgui.gui_flow_graph):
              x = self.probe.level()
          sidtime = self.locality.sidereal_time()
          # LMST
-         s = str(ephem.hours(sidtime))
+         s = str(ephem.hours(sidtime)) + " " + self.sunstate
          # Continuum detector value
          if self.setimode == False:
              sx = "%7.4f" % x
@@ -645,7 +680,7 @@ class app_flow_graph(stdgui.gui_flow_graph):
          else:
              self.seti_analysis(self.fft_outbuf,sidtime)
              now = time.time()
-             if ((now - self.seti_then) > self.setifreq_timer):
+             if ((self.scanning == True) and ((now - self.seti_then) > self.setifreq_timer)):
                  self.seti_then = now
                  self.setifreq_current = self.setifreq_current + self.fft_input_rate
                  if (self.setifreq_current > self.setifreq_upper):
@@ -654,6 +689,7 @@ class app_flow_graph(stdgui.gui_flow_graph):
                  # Make sure we zero-out the hits array when changing
                  #   frequency.
                  self.hits_array[:,:] = 0.0
+                 self.hit_intensities[:,:] = 0.0
 
     def fft_outfunc(self,data,l):
         self.fft_outbuf=data
@@ -684,11 +720,18 @@ class app_flow_graph(stdgui.gui_flow_graph):
         # If time to write full header info (saves storage this way)
         #
         if (now - self.continuum_then > 20):
+            self.sun.compute(self.locality)
+            enow = ephem.now()
+            sun_insky = "Down"
+            self.sunstate = "Dn"
+            if ((self.sun.rise_time < enow) and (enow < self.sun.set_time)):
+               sun_insky = "Up"
+               self.sunstate = "Up"
             self.continuum_then = now
         
             continuum_file.write(str(ephem.hours(sidtime))+" "+flt+" Dn="+str(inter)+",")
             continuum_file.write("Ti="+str(integ)+",Fc="+str(fc)+",Bw="+str(bw))
-            continuum_file.write(",Ga="+str(ga)+"\n")
+            continuum_file.write(",Ga="+str(ga)+",Sun="+str(sun_insky)+"\n")
         else:
             continuum_file.write(str(ephem.hours(sidtime))+" "+flt+"\n")
     
@@ -740,6 +783,7 @@ class app_flow_graph(stdgui.gui_flow_graph):
         l = len(fftbuf)
         x = 0
         hits = []
+        hit_intensities = []
         if self.seticounter < self.setitimer:
             self.seticounter = self.seticounter + 1
             return
@@ -779,6 +823,7 @@ class app_flow_graph(stdgui.gui_flow_graph):
             #
             if ((fftbuf[i] - avg) > (self.setik * sigma)):
                 hits.append(current_f)
+                hit_intensities.append(fftbuf[i])
             current_f = current_f + f_incr
 
         # DC to nyquist
@@ -789,14 +834,33 @@ class app_flow_graph(stdgui.gui_flow_graph):
             #
             if ((fftbuf[i] - avg) > (self.setik * sigma)):
                 hits.append(current_f)
+                hit_intensities.append(fftbuf[i])
             current_f = current_f + f_incr
 
+        # No hits
         if (len(hits) <= 0):
             return
 
+        #
+        # OK, so we have some hits in the FFT buffer
+        #   They'll have a rather substantial gauntlet to run before
+        #   being declared a real "hit"
+        #
+
+        # Weed out buffers with an excessive number of strong signals
         if (len(hits) > self.nhits):
             return
 
+        # Weed out FFT buffers with apparent multiple narrowband signals
+        #   separated significantly in frequency.  This means that a
+        #   single signal spanning multiple bins is OK, but a buffer that
+        #   has multiple, apparently-separate, signals isn't OK.
+        #
+        last = hits[0]
+        for i in range(1,len(hits)):
+            if ((hits[i] - last) > (f_incr*2.0)):
+                return
+            last = hits[i]
 
         #
         # Run through all three hit buffers, computing difference between
@@ -814,15 +878,19 @@ class app_flow_graph(stdgui.gui_flow_graph):
                 self.hitcounter = self.hitcounter + 1
                 break
 
-        if (good_hit):
-            self.write_hits(hits,sidtime)
 
         # Save 'n shuffle hits
-        self.hits_array[:,2] = self.hits_array[:,1]
-        self.hits_array[:,1] = self.hits_array[:,0]
+        for i in range(self.nhitlines,1):
+            self.hits_array[:,i] = self.hits_array[:,i-1]
+            self.hit_intensities[:,i] = self.hit_intensities[:,i-1]
 
         for i in range(0,len(hits)):
             self.hits_array[i,0] = hits[i]
+            self.hit_intensities[i,0] = hit_intensities[i]
+
+        # Finally, write the hits/intensities buffer
+        if (good_hit):
+            self.write_hits(sidtime)
 
         return
 
@@ -838,7 +906,7 @@ class app_flow_graph(stdgui.gui_flow_graph):
         else:
             return (False)
 
-    def write_hits(self,hits,sidtime):
+    def write_hits(self,sidtime):
         # Create localtime structure for producing filename
         foo = time.localtime()
         pfx = self.prefix
@@ -847,7 +915,21 @@ class app_flow_graph(stdgui.gui_flow_graph):
     
         # Open the data file, appending
         hits_file = open (filenamestr+".seti","a")
-        hits_file.write(str(ephem.hours(sidtime))+" "+str(self.decln)+" "+str(hits)+"\n")
+
+        # Write sidtime first
+        hits_file.write(str(ephem.hours(sidtime))+" "+str(self.decln)+" ")
+
+        #
+        # Then write the hits/hit intensities buffers with enough
+        #   "syntax" to allow parsing by external (not yet written!)
+        #   "stuff".
+        #
+        for i in range(0,self.nhitlines):
+            hits_file.write(" ")
+            for j in range(0,self.nhits):
+                hits_file.write(str(self.hits_array[j,i])+":")
+                hits_file.write(str(self.hit_intensities[j,i])+",")
+        hits_file.write("\n")
         hits_file.close()
         return
 
@@ -893,7 +975,29 @@ class app_flow_graph(stdgui.gui_flow_graph):
         else:
           self.annotate_state = True
           self.annotation.SetLabel("Annotation: On")
-        
+    #
+    # Turn scanning on/off
+    # Called-back by "Recording" button
+    #
+    def toggle_scanning(self):
+        # Current scanning?  Flip state
+        if (self.scanning == True):
+          self.scanning = False
+          self.scan_control.SetLabel("Scan: Off")
+        # Not scanning
+        else:
+          self.scanning = True
+          self.scan_control.SetLabel("Scan: On ")
+
+    def set_pd_offset(self,offs):
+         self.myform['offset'].set_value(offs)
+         self.calib_offset=offs
+         self.cal_offs.set_k(offs*4000)
+
+    def set_pd_gain(self,gain):
+         self.myform['dcgain'].set_value(gain)
+         self.cal_mult.set_k(gain*0.01)
+         self.calib_coeff = gain
 
 def main ():
     app = stdgui.stdapp(app_flow_graph, "RADIO ASTRONOMY SPECTRAL/CONTINUUM RECEIVER: $Revision$", nstatus=1)