3 # Copyright 2005,2006,2007,2009 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 gnuradio.wxgui import slider, powermate
29 from gnuradio.wxgui import stdgui2, fftsink2, form
30 from optparse import OptionParser
31 from usrpm import usrp_dbid
36 def pick_subdevice(u):
38 The user didn't specify a subdevice on the command line.
39 Try for one of these, in order: TV_RX, BASIC_RX, whatever is on side A.
43 return usrp.pick_subdev(u, (usrp_dbid.TV_RX,
44 usrp_dbid.TV_RX_REV_2,
45 usrp_dbid.TV_RX_REV_3,
47 usrp_dbid.TV_RX_REV_2_MIMO,
48 usrp_dbid.TV_RX_REV_3_MIMO,
52 class wfm_rx_block (stdgui2.std_top_block):
53 def __init__(self,frame,panel,vbox,argv):
54 stdgui2.std_top_block.__init__ (self,frame,panel,vbox,argv)
56 parser=OptionParser(option_class=eng_option)
57 parser.add_option("-R", "--rx-subdev-spec", type="subdev", default=None,
58 help="select USRP Rx side A or B (default=A)")
59 parser.add_option("-f", "--freq", type="eng_float", default=100.1e6,
60 help="set frequency to FREQ", metavar="FREQ")
61 parser.add_option("-g", "--gain", type="eng_float", default=40,
62 help="set gain in dB (default is midpoint)")
63 parser.add_option("-V", "--volume", type="eng_float", default=None,
64 help="set volume (default is midpoint)")
65 parser.add_option("-O", "--audio-output", type="string", default="",
66 help="pcm device name. E.g., hw:0,0 or surround51 or /dev/dsp")
68 (options, args) = parser.parse_args()
82 self.u = usrp.source_c() # usrp is data source
84 adc_rate = self.u.adc_rate() # 64 MS/s
86 self.u.set_decim_rate(usrp_decim)
87 usrp_rate = adc_rate / usrp_decim # 320 kS/s
89 demod_rate = usrp_rate / chanfilt_decim
91 audio_rate = demod_rate / audio_decimation # 32 kHz
93 if options.rx_subdev_spec is None:
94 options.rx_subdev_spec = pick_subdevice(self.u)
96 self.u.set_mux(usrp.determine_rx_mux_value(self.u, options.rx_subdev_spec))
97 self.subdev = usrp.selected_subdev(self.u, options.rx_subdev_spec)
98 print "Using RX d'board %s" % (self.subdev.side_and_name(),)
99 dbid = self.subdev.dbid()
100 if not (dbid == usrp_dbid.BASIC_RX or
101 dbid == usrp_dbid.TV_RX or
102 dbid == usrp_dbid.TV_RX_REV_2 or
103 dbid == usrp_dbid.TV_RX_REV_3 or
104 dbid == usrp_dbid.TV_RX_MIMO or
105 dbid == usrp_dbid.TV_RX_REV_2_MIMO or
106 dbid == usrp_dbid.TV_RX_REV_3_MIMO
108 print "This daughterboard does not cover the required frequency range"
109 print "for this application. Please use a BasicRX or TVRX daughterboard."
110 raw_input("Press ENTER to continue anyway, or Ctrl-C to exit.")
112 chan_filt_coeffs = optfir.low_pass (1, # gain
113 usrp_rate, # sampling rate
114 80e3, # passband cutoff
115 115e3, # stopband cutoff
116 0.1, # passband ripple
117 60) # stopband attenuation
118 #print len(chan_filt_coeffs)
119 chan_filt = gr.fir_filter_ccf (chanfilt_decim, chan_filt_coeffs)
121 self.guts = blks2.wfm_rcv (demod_rate, audio_decimation)
123 self.volume_control = gr.multiply_const_ff(self.vol)
125 # sound card as final sink
126 audio_sink = audio.sink (int (audio_rate),
127 options.audio_output,
130 # now wire it all together
131 self.connect (self.u, chan_filt, self.guts, self.volume_control, audio_sink)
133 self._build_gui(vbox, usrp_rate, demod_rate, audio_rate)
135 if options.gain is None:
136 # if no gain was specified, use the mid-point in dB
137 g = self.subdev.gain_range()
138 options.gain = float(g[0]+g[1])/2
140 if options.volume is None:
141 g = self.volume_range()
142 options.volume = float(g[0]+g[1])/2
144 if abs(options.freq) < 1e6:
149 self.set_gain(options.gain)
150 self.set_vol(options.volume)
151 if not(self.set_freq(options.freq)):
152 self._set_status_msg("Failed to set initial frequency")
155 def _set_status_msg(self, msg, which=0):
156 self.frame.GetStatusBar().SetStatusText(msg, which)
159 def _build_gui(self, vbox, usrp_rate, demod_rate, audio_rate):
161 def _form_set_freq(kv):
162 return self.set_freq(kv['freq'])
166 self.src_fft = fftsink2.fft_sink_c(self.panel, title="Data from USRP",
167 fft_size=512, sample_rate=usrp_rate,
168 ref_scale=32768.0, ref_level=0, y_divs=12)
169 self.connect (self.u, self.src_fft)
170 vbox.Add (self.src_fft.win, 4, wx.EXPAND)
173 post_filt_fft = fftsink2.fft_sink_f(self.panel, title="Post Demod",
174 fft_size=1024, sample_rate=usrp_rate,
175 y_per_div=10, ref_level=0)
176 self.connect (self.guts.fm_demod, post_filt_fft)
177 vbox.Add (post_filt_fft.win, 4, wx.EXPAND)
180 post_deemph_fft = fftsink2.fft_sink_f(self.panel, title="Post Deemph",
181 fft_size=512, sample_rate=audio_rate,
182 y_per_div=10, ref_level=-20)
183 self.connect (self.guts.deemph, post_deemph_fft)
184 vbox.Add (post_deemph_fft.win, 4, wx.EXPAND)
187 # control area form at bottom
188 self.myform = myform = form.form()
190 hbox = wx.BoxSizer(wx.HORIZONTAL)
192 myform['freq'] = form.float_field(
193 parent=self.panel, sizer=hbox, label="Freq", weight=1,
194 callback=myform.check_input_and_call(_form_set_freq, self._set_status_msg))
197 myform['freq_slider'] = \
198 form.quantized_slider_field(parent=self.panel, sizer=hbox, weight=3,
199 range=(87.9e6, 108.1e6, 0.1e6),
200 callback=self.set_freq)
202 vbox.Add(hbox, 0, wx.EXPAND)
204 hbox = wx.BoxSizer(wx.HORIZONTAL)
208 form.quantized_slider_field(parent=self.panel, sizer=hbox, label="Volume",
209 weight=3, range=self.volume_range(),
210 callback=self.set_vol)
214 form.quantized_slider_field(parent=self.panel, sizer=hbox, label="Gain",
215 weight=3, range=self.subdev.gain_range(),
216 callback=self.set_gain)
218 vbox.Add(hbox, 0, wx.EXPAND)
221 self.knob = powermate.powermate(self.frame)
223 powermate.EVT_POWERMATE_ROTATE (self.frame, self.on_rotate)
224 powermate.EVT_POWERMATE_BUTTON (self.frame, self.on_button)
226 print "FYI: No Powermate or Contour Knob found"
229 def on_rotate (self, event):
230 self.rot += event.delta
231 if (self.state == "FREQ"):
233 self.set_freq(self.freq + .1e6)
236 self.set_freq(self.freq - .1e6)
239 step = self.volume_range()[2]
241 self.set_vol(self.vol + step)
244 self.set_vol(self.vol - step)
247 def on_button (self, event):
248 if event.value == 0: # button up
251 if self.state == "FREQ":
255 self.update_status_bar ()
258 def set_vol (self, vol):
259 g = self.volume_range()
260 self.vol = max(g[0], min(g[1], vol))
261 self.volume_control.set_k(10**(self.vol/10))
262 self.myform['volume'].set_value(self.vol)
263 self.update_status_bar ()
265 def set_freq(self, target_freq):
267 Set the center frequency we're interested in.
269 @param target_freq: frequency in Hz
272 Tuning is a two step process. First we ask the front-end to
273 tune as close to the desired frequency as it can. Then we use
274 the result of that operation and our target_frequency to
275 determine the value for the digital down converter.
277 r = usrp.tune(self.u, 0, self.subdev, target_freq)
280 self.freq = target_freq
281 self.myform['freq'].set_value(target_freq) # update displayed value
282 self.myform['freq_slider'].set_value(target_freq) # update displayed value
283 self.update_status_bar()
284 self._set_status_msg("OK", 0)
287 self._set_status_msg("Failed", 0)
290 def set_gain(self, gain):
291 self.myform['gain'].set_value(gain) # update displayed value
292 self.subdev.set_gain(gain)
294 def update_status_bar (self):
295 msg = "Volume:%r Setting:%s" % (self.vol, self.state)
296 self._set_status_msg(msg, 1)
297 self.src_fft.set_baseband_freq(self.freq)
299 def volume_range(self):
300 return (-20.0, 0.0, 0.5)
303 if __name__ == '__main__':
304 app = stdgui2.stdapp (wfm_rx_block, "USRP WFM RX")