#!/usr/bin/env python #import scopesink_mod as scopesink from gnuradio import gr, gru, eng_notation, optfir from gnuradio import audio from gnuradio import usrp from gnuradio import blks from gnuradio.eng_option import eng_option from gnuradio.wxgui import slider, powermate from gnuradio.wxgui import stdgui, form, fftsink from optparse import OptionParser import usrp_dbid import sys import math import wx import wx.lib.evtmgr as em import scopesink_mod as scopesink def pick_subdevice(u): """ The user didn't specify a subdevice on the command line. Try for one of these, in order: TV_RX, BASIC_RX, whatever is on side A. @return a subdev_spec """ return usrp.pick_subdev(u, (usrp_dbid.TV_RX, usrp_dbid.TV_RX_REV_2, usrp_dbid.BASIC_RX)) plot1 = 1 plot2 = 1 plot3 = 1 class am_plasma_rx_graph (stdgui.gui_flow_graph): def __init__(self,frame,panel,vbox,argv): stdgui.gui_flow_graph.__init__ (self,frame,panel,vbox,argv) parser=OptionParser(option_class=eng_option) parser.add_option("-R", "--rx-subdev-spec", type="subdev", default=None, help="select USRP Rx side A or B (default=A)") parser.add_option("-f", "--freq", type="eng_float", default=3e6, help="set frequency to FREQ", metavar="FREQ") parser.add_option("-g", "--gain", type="int", default=10, help="set gain in dB (default is midpoint)") parser.add_option("-O", "--audio-output", type="string", default="", help="pcm device name. E.g., hw:0,0 or surround51 or /dev/dsp") (options, args) = parser.parse_args() if len(args) != 0: parser.print_help() sys.exit(1) self.frame = frame self.panel = panel self.vol = 0 self.state = "FREQ" self.freq = 0 # build graph self.u = usrp.source_c() # usrp is data source adc_rate = self.u.adc_rate() # 64 MS/s usrp_decim = 250 self.u.set_decim_rate(usrp_decim) usrp_rate = adc_rate / usrp_decim # 256 kS/s chanfilt_decim = 16 #chanfilt_decim = 512 demod_rate = usrp_rate / chanfilt_decim # 16 kHz audio_decimation = 1 audio_rate = demod_rate / audio_decimation # 16 kHz if options.rx_subdev_spec is None: options.rx_subdev_spec = pick_subdevice(self.u) # Select USRP channel (0) self.u.set_mux(usrp.determine_rx_mux_value(self.u, options.rx_subdev_spec)) # Tune to the desired IF frequency self.subdev = usrp.selected_subdev(self.u, options.rx_subdev_spec) # Channelize the signal of interest. chan_filt_coeffs = gr.firdes.low_pass (1, # gain usrp_rate, # sampling rate #1000, 6000, # passband cutoff 500, # stopband cutoff gr.firdes.WIN_HANN) self.lpfilter = gr.fir_filter_ccf (chanfilt_decim,chan_filt_coeffs) # Demodulate with classic sqrt (I*I + Q*Q) self.magblock = gr.complex_to_mag() self.volume_control = gr.multiply_const_ff(self.vol) self.gain_correction = gr.divide_ff(math.log10(self.gain)) # Deemphasis. Is this necessary on AM? #TAU = 75e-6 # 75us in US, 50us in EUR #fftaps = [ 1 - math.exp(-1/TAU/usrp_rate), 0] #fbtaps= [ 0 , math.exp(-1/TAU/usrp_rate) ] #self.deemph = gr.iir_filter_ffd(fftaps,fbtaps) # sound card as final sink audio_sink = audio.sink (int (audio_rate), options.audio_output, False) # ok_to_block # now wire it all together self.connect (self.u, self.lpfilter) self.connect (self.lpfilter, self.magblock) self.connect (self.magblock, self.gain_correction) self.connect (self.gain_correction, self.volume_control) self.connect (self.volume_control, (audio_sink, 0)) #self.connect (self.volume_control, audio_filter) #self.connect (audio_filter, (audio_sink, 0)) #self.connect (self.volume_control,self.deemph) #self.connect (self.deemph,audio_filter) #self.connect (audio_filter, (audio_sink, 0)) self._build_gui(vbox, usrp_rate, demod_rate, audio_rate) if options.gain is None: # if no gain was specified, use the mid-point in dB g = self.subdev.gain_range() options.gain = float(g[0]+g[1])/2 if abs(options.freq) < 1e6: options.freq *= 1e6 # set initial values self.set_gain(options.gain) if not(self.set_freq(options.freq)): self._set_status_msg("Failed to set initial frequency") def _set_status_msg(self, msg, which=0): self.frame.GetStatusBar().SetStatusText(msg, which) def _build_gui(self, vbox, usrp_rate, demod_rate, audio_rate): def _form_set_freq(kv): return self.set_freq(kv['freq']) if plot1: self.src_fft = fftsink.fft_sink_c (self, self.panel, title="Raw Spectrum From Sensor", fft_size=1024, sample_rate=usrp_rate, size=(50,200)) self.connect (self.u, self.src_fft) vbox.Add (self.src_fft.win, 1, wx.EXPAND) if plot2: self.post_filt = fftsink.fft_sink_f (self, self.panel, title="AM Demodulated Spectrum", fft_size=1024, sample_rate=demod_rate, size=(50,100)) self.connect (self.magblock,self.post_filt) vbox.Add (self.post_filt.win, 1, wx.EXPAND) if plot3: self.scope = scopesink.scope_sink_f(self, self.panel, title="AM Demodulated Time Series", sample_rate=demod_rate, size=(50,100), t_scale=1.0e-3, v_scale=None, vbox=vbox) self.connect(self.gain_correction, self.scope) vbox.Add (self.scope.win, 1, wx.EXPAND) # control area form at bottom self.myform = myform = form.form() hbox = wx.BoxSizer(wx.HORIZONTAL) hbox.Add((5,0), 0) myform['freq'] = form.float_field( parent=self.panel, sizer=hbox, label="Carrier Freq", weight=1, callback=myform.check_input_and_call(_form_set_freq, self._set_status_msg)) hbox.Add((5,0), 0) myform['freq_slider'] = \ form.quantized_slider_field(parent=self.panel, sizer=hbox, weight=3, range=(.5e6, 3.5e6, 0.001e6), callback=self.set_freq) hbox.Add((5,0), 0) vbox.Add(hbox, 0, wx.EXPAND) hbox = wx.BoxSizer(wx.HORIZONTAL) hbox.Add((5,0), 0) myform['gain'] = \ form.quantized_slider_field(parent=self.panel, sizer=hbox, label="Gain", weight=3, range=self.subdev.gain_range(), callback=self.set_gain) hbox.Add((5,0), 0) vbox.Add(hbox, 0, wx.EXPAND) try: self.knob = powermate.powermate(self.frame) self.rot = 0 powermate.EVT_POWERMATE_ROTATE (self.frame, self.on_rotate) powermate.EVT_POWERMATE_BUTTON (self.frame, self.on_button) except: print "FYI: No Powermate or Contour Knob found" def on_rotate (self, event): self.rot += event.delta if (self.state == "FREQ"): if self.rot >= 3: self.set_freq(self.freq + .001e6) self.rot -= 3 elif self.rot <=-3: self.set_freq(self.freq - .001e6) self.rot += 3 else: step = self.subdev.gain_range()[2] if self.rot >= 3: self.set_gain(self.gain + step) self.rot -= 3 elif self.rot <=-3: self.set_gain(self.gain - step) self.rot += 3 self.update_status_bar () def on_button (self, event): if event.value == 0: # button up return self.rot = 0 if self.state == "FREQ": self.state = "GAIN" else: self.state = "FREQ" self.update_status_bar () def set_freq(self, target_freq): """ Set the center frequency we're interested in. @param target_freq: frequency in Hz @rypte: bool Tuning is a two step process. First we ask the front-end to tune as close to the desired frequency as it can. Then we use the result of that operation and our target_frequency to determine the value for the digital down converter. """ r = usrp.tune(self.u, 0, self.subdev, target_freq) if r: self.freq = target_freq self.myform['freq'].set_value(target_freq) # update displayed value self.myform['freq_slider'].set_value(target_freq) # update displayed value self.update_status_bar() self._set_status_msg("OK", 0) return True self._set_status_msg("Failed", 0) return False def set_gain(self, gain): self.gain=gain self.myform['gain'].set_value(gain) # update displayed value self.subdev.set_gain(gain) self.update_status_bar () def update_status_bar (self): msg = "Gain:%r Setting:%s" % (self.gain, self.state) self._set_status_msg(msg, 1) if plot1: self.src_fft.set_baseband_freq(self.freq) def gain_range(self): return (0, 20, 1) if __name__ == '__main__': app = stdgui.stdapp (am_plasma_rx_graph, "USRP PLASMA AM RX") app.MainLoop ()