Imported Upstream version 3.2.2
[debian/gnuradio] / gnuradio-examples / python / usrp / usrp_wfm_rcv_nogui.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2005,2006,2007 Free Software Foundation, Inc.
4
5 # This file is part of GNU Radio
6
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)
10 # any later version.
11
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.
16
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.
21
22
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
30 import sys
31 import math
32
33 def pick_subdevice(u):
34     """
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.
37
38     @return a subdev_spec
39     """
40     return usrp.pick_subdev(u, (usrp_dbid.TV_RX,
41                                 usrp_dbid.TV_RX_REV_2,
42                                 usrp_dbid.TV_RX_REV_3,
43                                 usrp_dbid.BASIC_RX))
44
45
46 class wfm_rx_block (gr.top_block):
47
48     def __init__(self):
49         gr.top_block.__init__(self)
50
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")
60
61         (options, args) = parser.parse_args()
62         if len(args) != 0:
63             parser.print_help()
64             sys.exit(1)
65         
66         self.vol = .1
67         self.state = "FREQ"
68         self.freq = 0
69
70         # build graph
71         
72         self.u = usrp.source_c()                    # usrp is data source
73
74         adc_rate = self.u.adc_rate()                # 64 MS/s
75         usrp_decim = 200
76         self.u.set_decim_rate(usrp_decim)
77         usrp_rate = adc_rate / usrp_decim           # 320 kS/s
78         chanfilt_decim = 1
79         demod_rate = usrp_rate / chanfilt_decim
80         audio_decimation = 10
81         audio_rate = demod_rate / audio_decimation  # 32 kHz
82
83
84         if options.rx_subdev_spec is None:
85             options.rx_subdev_spec = pick_subdevice(self.u)
86
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(),)
90
91
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)
100
101         self.guts = blks2.wfm_rcv (demod_rate, audio_decimation)
102
103         self.volume_control = gr.multiply_const_ff(self.vol)
104
105         # sound card as final sink
106         audio_sink = audio.sink(int(audio_rate),
107                                 options.audio_output,
108                                 False)  # ok_to_block
109         
110         # now wire it all together
111         self.connect (self.u, chan_filt, self.guts, self.volume_control, audio_sink)
112
113
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
118
119         if abs(options.freq) < 1e6:
120             options.freq *= 1e6
121
122         # set initial values
123
124         self.set_gain(options.gain)
125
126         if not(self.set_freq(options.freq)):
127             self._set_status_msg("Failed to set initial frequency")
128
129     def set_vol (self, vol):
130         self.vol = vol
131         self.volume_control.set_k(self.vol)
132         self.update_status_bar ()
133
134     def set_freq(self, target_freq):
135         """
136         Set the center frequency we're interested in.
137
138         @param target_freq: frequency in Hz
139         @rypte: bool
140
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.
145         """
146         r = self.u.tune(0, self.subdev, target_freq)
147         
148         if r:
149             self.freq = target_freq
150             self.update_status_bar()
151             self._set_status_msg("OK", 0)
152             return True
153
154         self._set_status_msg("Failed", 0)
155         return False
156
157     def set_gain(self, gain):
158         self.subdev.set_gain(gain)
159
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)
164         
165     def _set_status_msg(self, msg, which=0):
166         print msg
167
168     
169 if __name__ == '__main__':
170     tb = wfm_rx_block()
171     try:
172         tb.run()
173     except KeyboardInterrupt:
174         pass