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,
46 class wfm_rx_block (gr.top_block):
49 gr.top_block.__init__(self)
51 parser=OptionParser(option_class=eng_option)
52 parser.add_option("-R", "--rx-subdev-spec", type="subdev", default=None,
53 help="select USRP Rx side A or B (default=A)")
54 parser.add_option("-f", "--freq", type="eng_float", default=100.1e6,
55 help="set frequency to FREQ", metavar="FREQ")
56 parser.add_option("-g", "--gain", type="eng_float", default=None,
57 help="set gain in dB (default is midpoint)")
58 parser.add_option("-O", "--audio-output", type="string", default="",
59 help="pcm device name. E.g., hw:0,0 or surround51 or /dev/dsp")
61 (options, args) = parser.parse_args()
72 self.u = usrp.source_c() # usrp is data source
74 adc_rate = self.u.adc_rate() # 64 MS/s
76 self.u.set_decim_rate(usrp_decim)
77 usrp_rate = adc_rate / usrp_decim # 320 kS/s
79 demod_rate = usrp_rate / chanfilt_decim
81 audio_rate = demod_rate / audio_decimation # 32 kHz
84 if options.rx_subdev_spec is None:
85 options.rx_subdev_spec = pick_subdevice(self.u)
87 self.u.set_mux(usrp.determine_rx_mux_value(self.u, options.rx_subdev_spec))
88 self.subdev = usrp.selected_subdev(self.u, options.rx_subdev_spec)
89 print "Using RX d'board %s" % (self.subdev.side_and_name(),)
92 chan_filt_coeffs = optfir.low_pass (1, # gain
93 usrp_rate, # sampling rate
94 80e3, # passband cutoff
95 115e3, # stopband cutoff
96 0.1, # passband ripple
97 60) # stopband attenuation
98 #print len(chan_filt_coeffs)
99 chan_filt = gr.fir_filter_ccf (chanfilt_decim, chan_filt_coeffs)
101 self.guts = blks2.wfm_rcv (demod_rate, audio_decimation)
103 self.volume_control = gr.multiply_const_ff(self.vol)
105 # sound card as final sink
106 audio_sink = audio.sink(int(audio_rate),
107 options.audio_output,
110 # now wire it all together
111 self.connect (self.u, chan_filt, self.guts, self.volume_control, audio_sink)
114 if options.gain is None:
115 # if no gain was specified, use the mid-point in dB
116 g = self.subdev.gain_range()
117 options.gain = float(g[0]+g[1])/2
119 if abs(options.freq) < 1e6:
124 self.set_gain(options.gain)
126 if not(self.set_freq(options.freq)):
127 self._set_status_msg("Failed to set initial frequency")
129 def set_vol (self, vol):
131 self.volume_control.set_k(self.vol)
132 self.update_status_bar ()
134 def set_freq(self, target_freq):
136 Set the center frequency we're interested in.
138 @param target_freq: frequency in Hz
141 Tuning is a two step process. First we ask the front-end to
142 tune as close to the desired frequency as it can. Then we use
143 the result of that operation and our target_frequency to
144 determine the value for the digital down converter.
146 r = self.u.tune(0, self.subdev, target_freq)
149 self.freq = target_freq
150 self.update_status_bar()
151 self._set_status_msg("OK", 0)
154 self._set_status_msg("Failed", 0)
157 def set_gain(self, gain):
158 self.subdev.set_gain(gain)
160 def update_status_bar (self):
161 msg = "Freq: %s Volume:%f Setting:%s" % (
162 eng_notation.num_to_str(self.freq), self.vol, self.state)
163 self._set_status_msg(msg, 1)
165 def _set_status_msg(self, msg, which=0):
169 if __name__ == '__main__':
173 except KeyboardInterrupt: