3 # Copyright 2004,2005 Free Software Foundation, Inc.
5 # This file is part of GNU Radio
7 # GNU Radio is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2, or (at your option)
12 # GNU Radio is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with GNU Radio; see the file COPYING. If not, write to
19 # the Free Software Foundation, Inc., 51 Franklin Street,
20 # Boston, MA 02110-1301, USA.
23 from gnuradio import gr, gru
24 from gnuradio import usrp
26 from gnuradio import eng_notation
27 from gnuradio.eng_option import eng_option
28 from gnuradio.wxgui import stdgui, ra_fftsink, ra_stripchartsink, waterfallsink, form, slider
29 from optparse import OptionParser
35 from gnuradio.local_calibrator import *
37 class app_flow_graph(stdgui.gui_flow_graph):
38 def __init__(self, frame, panel, vbox, argv):
39 stdgui.gui_flow_graph.__init__(self)
44 parser = OptionParser(option_class=eng_option)
45 parser.add_option("-R", "--rx-subdev-spec", type="subdev", default=(0, 0),
46 help="select USRP Rx side A or B (default=A)")
47 parser.add_option("-d", "--decim", type="int", default=16,
48 help="set fgpa decimation rate to DECIM [default=%default]")
49 parser.add_option("-f", "--freq", type="eng_float", default=None,
50 help="set frequency to FREQ", metavar="FREQ")
51 parser.add_option("-a", "--avg", type="eng_float", default=1.0,
52 help="set spectral averaging alpha")
53 parser.add_option("-i", "--integ", type="eng_float", default=1.0,
54 help="set integration time")
55 parser.add_option("-g", "--gain", type="eng_float", default=None,
56 help="set gain in dB (default is midpoint)")
57 parser.add_option("-l", "--reflevel", type="eng_float", default=30.0,
58 help="Set Total power reference level")
59 parser.add_option("-y", "--division", type="eng_float", default=0.5,
60 help="Set Total power Y division size")
61 parser.add_option("-e", "--longitude", type="eng_float", default=-76.02, help="Set Observer Longitude")
62 parser.add_option("-c", "--latitude", type="eng_float", default=44.85, help="Set Observer Latitude")
63 parser.add_option("-o", "--observing", type="eng_float", default=0.0,
64 help="Set observing frequency")
65 parser.add_option("-x", "--ylabel", default="dB", help="Y axis label")
66 parser.add_option("-C", "--cfunc", default="default", help="Calibration function name")
67 parser.add_option("-z", "--divbase", type="eng_float", default=0.025, help="Y Division increment base")
68 parser.add_option("-v", "--stripsize", type="eng_float", default=2400, help="Size of stripchart, in 2Hz samples")
69 parser.add_option("-F", "--fft_size", type="eng_float", default=1024, help="Size of FFT")
71 parser.add_option("-N", "--decln", type="eng_float", default=999.99, help="Observing declination")
72 parser.add_option("-I", "--interfilt", action="store_true", default=False)
73 parser.add_option("-X", "--prefix", default="./")
74 (options, args) = parser.parse_args()
79 self.show_debug_info = True
83 self.u = usrp.source_c(decim_rate=options.decim)
84 self.u.set_mux(usrp.determine_rx_mux_value(self.u, options.rx_subdev_spec))
85 self.cardtype = self.u.daughterboard_id(0)
86 # Set initial declination
87 self.decln = options.decln
89 # Turn off interference filter by default
90 self.use_interfilt = options.interfilt
92 # determine the daughterboard subdevice we're using
93 self.subdev = usrp.selected_subdev(self.u, options.rx_subdev_spec)
95 input_rate = self.u.adc_freq() / self.u.decim_rate()
97 tpstr="calib_"+options.cfunc+"_total_power"
98 sstr="calib_"+options.cfunc+"_fft"
99 self.tpcfunc=eval(tpstr)
100 self.scfunc=eval(sstr)
103 # Set prefix for data files
105 self.prefix = options.prefix
106 calib_set_prefix(self.prefix)
109 self.scope = ra_fftsink.ra_fft_sink_c (self, panel,
110 fft_size=int(options.fft_size), sample_rate=input_rate,
111 fft_rate=8, title="Spectral",
112 cfunc=self.scfunc, xydfunc=self.xydfunc, interfunc=self.interference)
114 # Set up ephemeris data
115 self.locality = ephem.Observer()
116 self.locality.long = str(options.longitude)
117 self.locality.lat = str(options.latitude)
119 # Set up stripchart display
120 self.stripsize = int(options.stripsize)
121 self.chart = ra_stripchartsink.stripchart_sink_f (self, panel,
122 stripsize=self.stripsize,
124 xlabel="LMST Offset (Seconds)",
125 scaling=1.0, ylabel=options.ylabel,
126 divbase=options.divbase, cfunc=self.tpcfunc)
128 # Set center frequency
129 self.centerfreq = options.freq
131 # Set observing frequency (might be different from actual programmed
133 if options.observing == 0.0:
134 self.observing = options.freq
136 self.observing = options.observing
141 # Produce a default interference map
142 # May not actually get used, unless --interfilt was specified
144 self.intmap = Numeric.zeros(256,Numeric.Complex64)
145 for i in range(0,len(self.intmap)):
146 self.intmap[i] = complex(1.0, 0.0)
148 # We setup the first two integrators to produce a fixed integration
149 # Down to 1Hz, with output at 1 samples/sec
152 # Second stage runs on decimated output of first
155 # Create taps for first integrator
161 # Create taps for second integrator
168 # The 3rd integrator is variable, and user selectable at runtime
169 # This integrator doesn't decimate, but is used to set the
170 # final integration time based on the constant 1Hz input samples
171 # The strip chart is fed at a constant 1Hz rate as a result
175 # Call constructors for receive chains
179 # This is the interference-zapping filter
181 # The GUI is used to set/clear inteference zones in
182 # the filter. The non-interfering zones are set to
186 self.interfilt = gr.fft_filter_ccc(1,self.intmap)
187 tmp = FFT.inverse_fft(self.intmap)
188 self.interfilt.set_taps(tmp)
190 # The three integrators--two FIR filters, and an IIR final filter
191 self.integrator1 = gr.fir_filter_fff (N, tapsN)
192 self.integrator2 = gr.fir_filter_fff (M, tapsM)
193 self.integrator3 = gr.single_pole_iir_filter_ff(1.0)
195 # Split complex USRP stream into a pair of floats
196 self.splitter = gr.complex_to_float (1);
197 self.toshort = gr.float_to_short();
199 # I squarer (detector)
200 self.multI = gr.multiply_ff();
202 # Q squarer (detector)
203 self.multQ = gr.multiply_ff();
205 # Adding squared I and Q to produce instantaneous signal power
206 self.adder = gr.add_ff();
209 # Start connecting configured modules in the receive chain
212 # Connect interference-filtered USRP input to selected scope function
213 if self.use_interfilt == True:
214 self.connect(self.u, self.interfilt, self.scope)
216 # Connect interference-filtered USRP to a complex->float splitter
217 self.connect(self.interfilt, self.splitter)
220 self.connect(self.u, self.scope)
221 self.connect(self.u, self.splitter)
223 # Connect splitter outputs to multipliers
225 self.connect((self.splitter, 0), (self.multI,0))
226 self.connect((self.splitter, 0), (self.multI,1))
229 self.connect((self.splitter, 1), (self.multQ,0))
230 self.connect((self.splitter, 1), (self.multQ,1))
232 # Then sum the squares
233 self.connect(self.multI, (self.adder,0))
234 self.connect(self.multQ, (self.adder,1))
236 # Connect adder output to three-stages of FIR integrator
237 self.connect(self.adder, self.integrator1,
238 self.integrator2, self.integrator3, self.chart)
241 self._build_gui(vbox)
243 # Make GUI agree with command-line
244 self.myform['integration'].set_value(int(options.integ))
245 self.myform['average'].set_value(int(options.avg))
247 # Make integrator agree with command line
248 self.set_integration(int(options.integ))
250 # Make spectral averager agree with command line
251 if options.avg != 1.0:
252 self.scope.set_avg_alpha(float(1.0/options.avg))
253 calib_set_avg_alpha(float(options.avg))
254 self.scope.set_average(True)
258 self.chart.set_y_per_div(options.division)
260 # Set reference(MAX) level
261 self.chart.set_ref_level(options.reflevel)
265 if options.gain is None:
266 # if no gain was specified, use the mid-point in dB
267 g = self.subdev.gain_range()
268 options.gain = float(g[0]+g[1])/2
270 if options.freq is None:
271 # if no freq was specified, use the mid-point
272 r = self.subdev.freq_range()
273 options.freq = float(r[0]+r[1])/2
275 # Set the initial gain control
276 self.set_gain(options.gain)
278 if not(self.set_freq(options.freq)):
279 self._set_status_msg("Failed to set initial frequency")
281 self.set_decln (self.decln)
282 calib_set_decln (self.decln)
284 self.myform['decim'].set_value(self.u.decim_rate())
285 self.myform['fs@usb'].set_value(self.u.adc_freq() / self.u.decim_rate())
286 self.myform['dbname'].set_value(self.subdev.name())
288 # Make sure calibrator knows what our bandwidth is
289 calib_set_bw(self.u.adc_freq() / self.u.decim_rate())
291 # Set analog baseband filtering, if DBS_RX
292 if self.cardtype == usrp_dbid.DBS_RX:
293 self.subdev.set_bw((self.u.adc_freq() / self.u.decim_rate())/2)
295 # Tell calibrator our declination as well
296 calib_set_decln(self.decln)
298 # Start the timer for the LMST display
299 self.lmst_timer.Start(1000)
302 def _set_status_msg(self, msg):
303 self.frame.GetStatusBar().SetStatusText(msg, 0)
305 def _build_gui(self, vbox):
307 def _form_set_freq(kv):
308 return self.set_freq(kv['freq'])
310 def _form_set_decln(kv):
311 return self.set_decln(kv['decln'])
313 # Position the FFT display
314 vbox.Add(self.scope.win, 15, wx.EXPAND)
316 # Position the Total-power stripchart
317 vbox.Add(self.chart.win, 15, wx.EXPAND)
319 # add control area at the bottom
320 self.myform = myform = form.form()
321 hbox = wx.BoxSizer(wx.HORIZONTAL)
322 hbox.Add((7,0), 0, wx.EXPAND)
323 vbox1 = wx.BoxSizer(wx.VERTICAL)
324 myform['freq'] = form.float_field(
325 parent=self.panel, sizer=vbox1, label="Center freq", weight=1,
326 callback=myform.check_input_and_call(_form_set_freq, self._set_status_msg))
328 vbox1.Add((4,0), 0, 0)
330 myform['lmst_high'] = form.static_text_field(
331 parent=self.panel, sizer=vbox1, label="Current LMST", weight=1)
332 vbox1.Add((4,0), 0, 0)
334 myform['spec_data'] = form.static_text_field(
335 parent=self.panel, sizer=vbox1, label="Spectral Cursor", weight=1)
336 vbox1.Add((4,0), 0, 0)
338 vbox2 = wx.BoxSizer(wx.VERTICAL)
339 g = self.subdev.gain_range()
340 myform['gain'] = form.slider_field(parent=self.panel, sizer=vbox2, label="RF Gain",
342 min=int(g[0]), max=int(g[1]),
343 callback=self.set_gain)
345 vbox2.Add((4,0), 0, 0)
346 myform['average'] = form.slider_field(parent=self.panel, sizer=vbox2,
347 label="Spectral Averaging (FFT frames)", weight=1, min=1, max=2000, callback=self.set_averaging)
349 vbox2.Add((4,0), 0, 0)
351 myform['integration'] = form.slider_field(parent=self.panel, sizer=vbox2,
352 label="Continuum Integration Time (sec)", weight=1, min=1, max=180, callback=self.set_integration)
354 vbox2.Add((4,0), 0, 0)
355 myform['decln'] = form.float_field(
356 parent=self.panel, sizer=vbox2, label="Current Declination", weight=1,
357 callback=myform.check_input_and_call(_form_set_decln))
358 vbox2.Add((4,0), 0, 0)
360 buttonbox = wx.BoxSizer(wx.HORIZONTAL)
361 if self.use_interfilt == True:
362 self.doit = form.button_with_callback(self.panel,
363 label="Clear Interference List",
364 callback=self.clear_interferers)
365 if self.use_interfilt == True:
366 buttonbox.Add(self.doit, 0, wx.CENTER)
367 vbox.Add(buttonbox, 0, wx.CENTER)
368 hbox.Add(vbox1, 0, 0)
369 hbox.Add(vbox2, wx.ALIGN_RIGHT, 0)
370 vbox.Add(hbox, 0, wx.EXPAND)
372 self._build_subpanel(vbox)
374 self.lmst_timer = wx.PyTimer(self.lmst_timeout)
378 def _build_subpanel(self, vbox_arg):
379 # build a secondary information panel (sometimes hidden)
381 # FIXME figure out how to have this be a subpanel that is always
382 # created, but has its visibility controlled by foo.Show(True/False)
384 if not(self.show_debug_info):
391 #panel = wx.Panel(self.panel, -1)
392 #vbox = wx.BoxSizer(wx.VERTICAL)
394 hbox = wx.BoxSizer(wx.HORIZONTAL)
396 myform['decim'] = form.static_float_field(
397 parent=panel, sizer=hbox, label="Decim")
400 myform['fs@usb'] = form.static_float_field(
401 parent=panel, sizer=hbox, label="Fs@USB")
404 myform['dbname'] = form.static_text_field(
405 parent=panel, sizer=hbox)
408 myform['baseband'] = form.static_float_field(
409 parent=panel, sizer=hbox, label="Analog BB")
412 myform['ddc'] = form.static_float_field(
413 parent=panel, sizer=hbox, label="DDC")
416 vbox.Add(hbox, 0, wx.EXPAND)
420 def set_freq(self, target_freq):
422 Set the center frequency we're interested in.
424 @param target_freq: frequency in Hz
427 Tuning is a two step process. First we ask the front-end to
428 tune as close to the desired frequency as it can. Then we use
429 the result of that operation and our target_frequency to
430 determine the value for the digital down converter.
433 # Everything except BASIC_RX should support usrp.tune()
435 if not (self.cardtype == usrp_dbid.BASIC_RX):
436 r = usrp.tune(self.u, 0, self.subdev, target_freq)
438 r = self.u.set_rx_freq(0, target_freq)
439 f = self.u.rx_freq(0)
440 if abs(f-target_freq) > 2.0e3:
443 self.myform['freq'].set_value(target_freq) # update displayed value
445 # Make sure calibrator knows our target freq
448 # Remember centerfreq---used for doppler calcs
449 delta = self.centerfreq - target_freq
450 self.centerfreq = target_freq
451 self.observing -= delta
452 self.scope.set_baseband_freq (self.observing)
453 calib_set_freq(self.observing)
455 # Clear interference list
456 self.clear_interferers()
458 self.myform['baseband'].set_value(r.baseband_freq)
459 self.myform['ddc'].set_value(r.dxc_freq)
465 def set_decln(self, dec):
467 self.myform['decln'].set_value(dec) # update displayed value
470 def set_gain(self, gain):
471 self.myform['gain'].set_value(gain) # update displayed value
472 self.subdev.set_gain(gain)
475 # Make sure calibrator knows our gain setting
479 def set_averaging(self, avval):
480 self.myform['average'].set_value(avval)
481 self.scope.set_avg_alpha(1.0/(avval))
482 calib_set_avg_alpha(avval)
483 self.scope.set_average(True)
485 def set_integration(self, integval):
486 self.integrator3.set_taps(1.0/integval)
487 self.myform['integration'].set_value(integval)
490 # Make sure calibrator knows our integration time
492 calib_set_integ(integval)
494 def lmst_timeout(self):
495 self.locality.date = ephem.now()
496 sidtime = self.locality.sidereal_time()
497 self.myform['lmst_high'].set_value(str(ephem.hours(sidtime)))
499 def xydfunc(self,xyv):
500 magn = int(log10(self.observing))
501 if (magn == 6 or magn == 7 or magn == 8):
503 dfreq = xyv[0] * pow(10.0,magn)
504 ratio = self.observing / dfreq
513 s = "%.6f%s\n%.3fdB" % (xyv[0], xhz, xyv[1])
514 s2 = "\n%.3fkm/s" % vs
515 self.myform['spec_data'].set_value(s+s2)
517 def interference(self,x):
518 if self.use_interfilt == False:
520 magn = int(log10(self.observing))
521 dfreq = x * pow(10.0,magn)
522 delta = dfreq - self.observing
523 fincr = self.bw / len(self.intmap)
528 offset = (l) - int((abs(delta)/fincr))
532 if offset >= len(self.intmap) or offset < 0:
533 print "interference offset is invalid--", offset
537 # Zero out the region around the selected interferer
539 self.intmap[offset-2] = complex (0.5, 0.0)
540 self.intmap[offset-1] = complex (0.25, 0.0)
541 self.intmap[offset] = complex (0.0, 0.0)
542 self.intmap[offset+1] = complex(0.25, 0.0)
543 self.intmap[offset+2] = complex(0.5, 0.0)
548 tmp = FFT.inverse_fft(self.intmap)
549 self.interfilt.set_taps(tmp)
551 def clear_interf(self):
552 self.clear_interferers()
554 def clear_interferers(self):
555 for i in range(0,len(self.intmap)):
556 self.intmap[i] = complex(1.0,0.0)
557 tmp = FFT.inverse_fft(self.intmap)
558 if self.use_interfilt == True:
559 self.interfilt.set_taps(tmp)
563 def toggle_cal(self):
564 if (self.calstate == True):
565 self.calstate = False
566 self.u.write_io(0,0,(1<<15))
567 self.calibrator.SetLabel("Calibration Source: Off")
570 self.u.write_io(0,(1<<15),(1<<15))
571 self.calibrator.SetLabel("Calibration Source: On")
573 def toggle_annotation(self):
574 if (self.annotate_state == True):
575 self.annotate_state = False
576 self.annotation.SetLabel("Annotation: Off")
578 self.annotate_state = True
579 self.annotation.SetLabel("Annotation: On")
580 calib_set_interesting(self.annotate_state)
584 app = stdgui.stdapp(app_flow_graph, "RADIO ASTRONOMY SPECTRAL/CONTINUUM RECEIVER: $Revision$", nstatus=1)
587 if __name__ == '__main__':