3 from gnuradio import gr, gru, eng_notation, optfir
4 from gnuradio import audio
5 from gnuradio import usrp
6 from gnuradio import blks
7 from gnuradio.eng_option import eng_option
8 from optparse import OptionParser
13 def pick_subdevice(u):
15 The user didn't specify a subdevice on the command line.
16 Try for one of these, in order: TV_RX, BASIC_RX, whatever is on side A.
20 return usrp.pick_subdev(u, (usrp_dbid.TV_RX,
21 usrp_dbid.TV_RX_REV_2,
25 class wfm_rx_graph (gr.flow_graph):
28 gr.flow_graph.__init__(self)
30 parser=OptionParser(option_class=eng_option)
31 parser.add_option("-R", "--rx-subdev-spec", type="subdev", default=None,
32 help="select USRP Rx side A or B (default=A)")
33 parser.add_option("-f", "--freq", type="eng_float", default=100.1e6,
34 help="set frequency to FREQ", metavar="FREQ")
35 parser.add_option("-g", "--gain", type="eng_float", default=None,
36 help="set gain in dB (default is midpoint)")
37 parser.add_option("-O", "--audio-output", type="string", default="",
38 help="pcm device name. E.g., hw:0,0 or surround51 or /dev/dsp")
40 (options, args) = parser.parse_args()
51 self.u = usrp.source_c() # usrp is data source
53 adc_rate = self.u.adc_rate() # 64 MS/s
55 self.u.set_decim_rate(usrp_decim)
56 usrp_rate = adc_rate / usrp_decim # 320 kS/s
58 demod_rate = usrp_rate / chanfilt_decim
60 audio_rate = demod_rate / audio_decimation # 32 kHz
63 if options.rx_subdev_spec is None:
64 options.rx_subdev_spec = pick_subdevice(self.u)
66 self.u.set_mux(usrp.determine_rx_mux_value(self.u, options.rx_subdev_spec))
67 self.subdev = usrp.selected_subdev(self.u, options.rx_subdev_spec)
68 print "Using RX d'board %s" % (self.subdev.side_and_name(),)
71 chan_filt_coeffs = optfir.low_pass (1, # gain
72 usrp_rate, # sampling rate
73 80e3, # passband cutoff
74 115e3, # stopband cutoff
75 0.1, # passband ripple
76 60) # stopband attenuation
77 #print len(chan_filt_coeffs)
78 chan_filt = gr.fir_filter_ccf (chanfilt_decim, chan_filt_coeffs)
80 self.guts = blks.wfm_rcv (self, demod_rate, audio_decimation)
82 self.volume_control = gr.multiply_const_ff(self.vol)
84 # sound card as final sink
85 audio_sink = audio.sink(int(audio_rate),
89 # now wire it all together
90 self.connect (self.u, chan_filt, self.guts, self.volume_control, audio_sink)
93 if options.gain is None:
94 # if no gain was specified, use the mid-point in dB
95 g = self.subdev.gain_range()
96 options.gain = float(g[0]+g[1])/2
98 if abs(options.freq) < 1e6:
103 self.set_gain(options.gain)
105 if not(self.set_freq(options.freq)):
106 self._set_status_msg("Failed to set initial frequency")
108 def set_vol (self, vol):
110 self.volume_control.set_k(self.vol)
111 self.update_status_bar ()
113 def set_freq(self, target_freq):
115 Set the center frequency we're interested in.
117 @param target_freq: frequency in Hz
120 Tuning is a two step process. First we ask the front-end to
121 tune as close to the desired frequency as it can. Then we use
122 the result of that operation and our target_frequency to
123 determine the value for the digital down converter.
125 r = self.u.tune(0, self.subdev, target_freq)
128 self.freq = target_freq
129 self.update_status_bar()
130 self._set_status_msg("OK", 0)
133 self._set_status_msg("Failed", 0)
136 def set_gain(self, gain):
137 self.subdev.set_gain(gain)
139 def update_status_bar (self):
140 msg = "Freq: %s Volume:%f Setting:%s" % (
141 eng_notation.num_to_str(self.freq), self.vol, self.state)
142 self._set_status_msg(msg, 1)
144 def _set_status_msg(self, msg, which=0):
148 if __name__ == '__main__':
152 except KeyboardInterrupt: