3 # Copyright 2005,2006,2007 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 3, 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, eng_notation, optfir
24 from gnuradio import audio
25 from gnuradio import usrp
26 from gnuradio import blks2
27 from gnuradio.eng_option import eng_option
28 from optparse import OptionParser
29 from usrpm import usrp_dbid
33 def pick_subdevice(u):
35 The user didn't specify a subdevice on the command line.
36 Try for one of these, in order: TV_RX, BASIC_RX, whatever is on side A.
40 return usrp.pick_subdev(u, (usrp_dbid.TV_RX,
41 usrp_dbid.TV_RX_REV_2,
42 usrp_dbid.TV_RX_REV_3,
44 usrp_dbid.TV_RX_REV_2_MIMO,
45 usrp_dbid.TV_RX_REV_3_MIMO,
49 class wfm_rx_block (gr.top_block):
52 gr.top_block.__init__(self)
54 parser=OptionParser(option_class=eng_option)
55 parser.add_option("-R", "--rx-subdev-spec", type="subdev", default=None,
56 help="select USRP Rx side A or B (default=A)")
57 parser.add_option("-f", "--freq", type="eng_float", default=100.1e6,
58 help="set frequency to FREQ", metavar="FREQ")
59 parser.add_option("-g", "--gain", type="eng_float", default=None,
60 help="set gain in dB (default is midpoint)")
61 parser.add_option("-O", "--audio-output", type="string", default="",
62 help="pcm device name. E.g., hw:0,0 or surround51 or /dev/dsp")
64 (options, args) = parser.parse_args()
75 self.u = usrp.source_c() # usrp is data source
77 adc_rate = self.u.adc_rate() # 64 MS/s
79 self.u.set_decim_rate(usrp_decim)
80 usrp_rate = adc_rate / usrp_decim # 320 kS/s
82 demod_rate = usrp_rate / chanfilt_decim
84 audio_rate = demod_rate / audio_decimation # 32 kHz
87 if options.rx_subdev_spec is None:
88 options.rx_subdev_spec = pick_subdevice(self.u)
90 self.u.set_mux(usrp.determine_rx_mux_value(self.u, options.rx_subdev_spec))
91 self.subdev = usrp.selected_subdev(self.u, options.rx_subdev_spec)
92 print "Using RX d'board %s" % (self.subdev.side_and_name(),)
95 chan_filt_coeffs = optfir.low_pass (1, # gain
96 usrp_rate, # sampling rate
97 80e3, # passband cutoff
98 115e3, # stopband cutoff
99 0.1, # passband ripple
100 60) # stopband attenuation
101 #print len(chan_filt_coeffs)
102 chan_filt = gr.fir_filter_ccf (chanfilt_decim, chan_filt_coeffs)
104 self.guts = blks2.wfm_rcv (demod_rate, audio_decimation)
106 self.volume_control = gr.multiply_const_ff(self.vol)
108 # sound card as final sink
109 audio_sink = audio.sink(int(audio_rate),
110 options.audio_output,
113 # now wire it all together
114 self.connect (self.u, chan_filt, self.guts, self.volume_control, audio_sink)
117 if options.gain is None:
118 # if no gain was specified, use the mid-point in dB
119 g = self.subdev.gain_range()
120 options.gain = float(g[0]+g[1])/2
122 if abs(options.freq) < 1e6:
127 self.set_gain(options.gain)
129 if not(self.set_freq(options.freq)):
130 self._set_status_msg("Failed to set initial frequency")
132 def set_vol (self, vol):
134 self.volume_control.set_k(self.vol)
135 self.update_status_bar ()
137 def set_freq(self, target_freq):
139 Set the center frequency we're interested in.
141 @param target_freq: frequency in Hz
144 Tuning is a two step process. First we ask the front-end to
145 tune as close to the desired frequency as it can. Then we use
146 the result of that operation and our target_frequency to
147 determine the value for the digital down converter.
149 r = self.u.tune(0, self.subdev, target_freq)
152 self.freq = target_freq
153 self.update_status_bar()
154 self._set_status_msg("OK", 0)
157 self._set_status_msg("Failed", 0)
160 def set_gain(self, gain):
161 self.subdev.set_gain(gain)
163 def update_status_bar (self):
164 msg = "Freq: %s Volume:%f Setting:%s" % (
165 eng_notation.num_to_str(self.freq), self.vol, self.state)
166 self._set_status_msg(msg, 1)
168 def _set_status_msg(self, msg, which=0):
172 if __name__ == '__main__':
176 except KeyboardInterrupt: