Merged r6271:6278 from jcorgan/t182 into trunk. Implements ticket:182.
[debian/gnuradio] / gnuradio-examples / python / usrp / usrp_wfm_rcv_nogui.py
1 #!/usr/bin/env python
2
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
10 import sys
11 import math
12
13 def pick_subdevice(u):
14     """
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.
17
18     @return a subdev_spec
19     """
20     return usrp.pick_subdev(u, (usrp_dbid.TV_RX,
21                                 usrp_dbid.TV_RX_REV_2,
22                                 usrp_dbid.TV_RX_REV_3,
23                                 usrp_dbid.BASIC_RX))
24
25
26 class wfm_rx_graph (gr.flow_graph):
27
28     def __init__(self):
29         gr.flow_graph.__init__(self)
30
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")
40
41         (options, args) = parser.parse_args()
42         if len(args) != 0:
43             parser.print_help()
44             sys.exit(1)
45         
46         self.vol = .1
47         self.state = "FREQ"
48         self.freq = 0
49
50         # build graph
51         
52         self.u = usrp.source_c()                    # usrp is data source
53
54         adc_rate = self.u.adc_rate()                # 64 MS/s
55         usrp_decim = 200
56         self.u.set_decim_rate(usrp_decim)
57         usrp_rate = adc_rate / usrp_decim           # 320 kS/s
58         chanfilt_decim = 1
59         demod_rate = usrp_rate / chanfilt_decim
60         audio_decimation = 10
61         audio_rate = demod_rate / audio_decimation  # 32 kHz
62
63
64         if options.rx_subdev_spec is None:
65             options.rx_subdev_spec = pick_subdevice(self.u)
66
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(),)
70
71
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)
80
81         self.guts = blks.wfm_rcv (self, demod_rate, audio_decimation)
82
83         self.volume_control = gr.multiply_const_ff(self.vol)
84
85         # sound card as final sink
86         audio_sink = audio.sink(int(audio_rate),
87                                 options.audio_output,
88                                 False)  # ok_to_block
89         
90         # now wire it all together
91         self.connect (self.u, chan_filt, self.guts, self.volume_control, audio_sink)
92
93
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
98
99         if abs(options.freq) < 1e6:
100             options.freq *= 1e6
101
102         # set initial values
103
104         self.set_gain(options.gain)
105
106         if not(self.set_freq(options.freq)):
107             self._set_status_msg("Failed to set initial frequency")
108
109     def set_vol (self, vol):
110         self.vol = vol
111         self.volume_control.set_k(self.vol)
112         self.update_status_bar ()
113
114     def set_freq(self, target_freq):
115         """
116         Set the center frequency we're interested in.
117
118         @param target_freq: frequency in Hz
119         @rypte: bool
120
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.
125         """
126         r = self.u.tune(0, self.subdev, target_freq)
127         
128         if r:
129             self.freq = target_freq
130             self.update_status_bar()
131             self._set_status_msg("OK", 0)
132             return True
133
134         self._set_status_msg("Failed", 0)
135         return False
136
137     def set_gain(self, gain):
138         self.subdev.set_gain(gain)
139
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)
144         
145     def _set_status_msg(self, msg, which=0):
146         print msg
147
148     
149 if __name__ == '__main__':
150     fg = wfm_rx_graph()
151     try:
152         fg.run()
153     except KeyboardInterrupt:
154         pass