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
9 from usrpm import usrp_dbid
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,
22 usrp_dbid.TV_RX_REV_3,
26 class wfm_rx_graph (gr.flow_graph):
29 gr.flow_graph.__init__(self)
31 parser=OptionParser(option_class=eng_option)
32 parser.add_option("-R", "--rx-subdev-spec", type="subdev", default=None,
33 help="select USRP Rx side A or B (default=A)")
34 parser.add_option("-f", "--freq", type="eng_float", default=100.1e6,
35 help="set frequency to FREQ", metavar="FREQ")
36 parser.add_option("-g", "--gain", type="eng_float", default=None,
37 help="set gain in dB (default is midpoint)")
38 parser.add_option("-O", "--audio-output", type="string", default="",
39 help="pcm device name. E.g., hw:0,0 or surround51 or /dev/dsp")
41 (options, args) = parser.parse_args()
52 self.u = usrp.source_c() # usrp is data source
54 adc_rate = self.u.adc_rate() # 64 MS/s
56 self.u.set_decim_rate(usrp_decim)
57 usrp_rate = adc_rate / usrp_decim # 320 kS/s
59 demod_rate = usrp_rate / chanfilt_decim
61 audio_rate = demod_rate / audio_decimation # 32 kHz
64 if options.rx_subdev_spec is None:
65 options.rx_subdev_spec = pick_subdevice(self.u)
67 self.u.set_mux(usrp.determine_rx_mux_value(self.u, options.rx_subdev_spec))
68 self.subdev = usrp.selected_subdev(self.u, options.rx_subdev_spec)
69 print "Using RX d'board %s" % (self.subdev.side_and_name(),)
72 chan_filt_coeffs = optfir.low_pass (1, # gain
73 usrp_rate, # sampling rate
74 80e3, # passband cutoff
75 115e3, # stopband cutoff
76 0.1, # passband ripple
77 60) # stopband attenuation
78 #print len(chan_filt_coeffs)
79 chan_filt = gr.fir_filter_ccf (chanfilt_decim, chan_filt_coeffs)
81 self.guts = blks.wfm_rcv (self, demod_rate, audio_decimation)
83 self.volume_control = gr.multiply_const_ff(self.vol)
85 # sound card as final sink
86 audio_sink = audio.sink(int(audio_rate),
90 # now wire it all together
91 self.connect (self.u, chan_filt, self.guts, self.volume_control, audio_sink)
94 if options.gain is None:
95 # if no gain was specified, use the mid-point in dB
96 g = self.subdev.gain_range()
97 options.gain = float(g[0]+g[1])/2
99 if abs(options.freq) < 1e6:
104 self.set_gain(options.gain)
106 if not(self.set_freq(options.freq)):
107 self._set_status_msg("Failed to set initial frequency")
109 def set_vol (self, vol):
111 self.volume_control.set_k(self.vol)
112 self.update_status_bar ()
114 def set_freq(self, target_freq):
116 Set the center frequency we're interested in.
118 @param target_freq: frequency in Hz
121 Tuning is a two step process. First we ask the front-end to
122 tune as close to the desired frequency as it can. Then we use
123 the result of that operation and our target_frequency to
124 determine the value for the digital down converter.
126 r = self.u.tune(0, self.subdev, target_freq)
129 self.freq = target_freq
130 self.update_status_bar()
131 self._set_status_msg("OK", 0)
134 self._set_status_msg("Failed", 0)
137 def set_gain(self, gain):
138 self.subdev.set_gain(gain)
140 def update_status_bar (self):
141 msg = "Freq: %s Volume:%f Setting:%s" % (
142 eng_notation.num_to_str(self.freq), self.vol, self.state)
143 self._set_status_msg(msg, 1)
145 def _set_status_msg(self, msg, which=0):
149 if __name__ == '__main__':
153 except KeyboardInterrupt: